Wed, 05 Aug 2009 17:10:56 +0100
add README
philpem@5 | 1 | /* |
philpem@5 | 2 | # |
philpem@5 | 3 | # File : tron.cpp |
philpem@5 | 4 | # ( C++ source file ) |
philpem@5 | 5 | # |
philpem@5 | 6 | # Description : A clone of the famous (and very simple) Tron game. |
philpem@5 | 7 | # This file is a part of the CImg Library project. |
philpem@5 | 8 | # ( http://cimg.sourceforge.net ) |
philpem@5 | 9 | # |
philpem@5 | 10 | # Copyright : David Tschumperle |
philpem@5 | 11 | # ( http://www.greyc.ensicaen.fr/~dtschump/ ) |
philpem@5 | 12 | # |
philpem@5 | 13 | # License : CeCILL v2.0 |
philpem@5 | 14 | # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) |
philpem@5 | 15 | # |
philpem@5 | 16 | # This software is governed by the CeCILL license under French law and |
philpem@5 | 17 | # abiding by the rules of distribution of free software. You can use, |
philpem@5 | 18 | # modify and/ or redistribute the software under the terms of the CeCILL |
philpem@5 | 19 | # license as circulated by CEA, CNRS and INRIA at the following URL |
philpem@5 | 20 | # "http://www.cecill.info". |
philpem@5 | 21 | # |
philpem@5 | 22 | # As a counterpart to the access to the source code and rights to copy, |
philpem@5 | 23 | # modify and redistribute granted by the license, users are provided only |
philpem@5 | 24 | # with a limited warranty and the software's author, the holder of the |
philpem@5 | 25 | # economic rights, and the successive licensors have only limited |
philpem@5 | 26 | # liability. |
philpem@5 | 27 | # |
philpem@5 | 28 | # In this respect, the user's attention is drawn to the risks associated |
philpem@5 | 29 | # with loading, using, modifying and/or developing or reproducing the |
philpem@5 | 30 | # software by the user in light of its specific status of free software, |
philpem@5 | 31 | # that may mean that it is complicated to manipulate, and that also |
philpem@5 | 32 | # therefore means that it is reserved for developers and experienced |
philpem@5 | 33 | # professionals having in-depth computer knowledge. Users are therefore |
philpem@5 | 34 | # encouraged to load and test the software's suitability as regards their |
philpem@5 | 35 | # requirements in conditions enabling the security of their systems and/or |
philpem@5 | 36 | # data to be ensured and, more generally, to use and operate it in the |
philpem@5 | 37 | # same conditions as regards security. |
philpem@5 | 38 | # |
philpem@5 | 39 | # The fact that you are presently reading this means that you have had |
philpem@5 | 40 | # knowledge of the CeCILL license and that you accept its terms. |
philpem@5 | 41 | # |
philpem@5 | 42 | */ |
philpem@5 | 43 | |
philpem@5 | 44 | #include "CImg.h" |
philpem@5 | 45 | using namespace cimg_library; |
philpem@5 | 46 | // The lines below are necessary when using a non-standard compiler as visualcpp6. |
philpem@5 | 47 | #ifdef cimg_use_visualcpp6 |
philpem@5 | 48 | #define std |
philpem@5 | 49 | #endif |
philpem@5 | 50 | |
philpem@5 | 51 | // Start main procedure |
philpem@5 | 52 | //---------------------- |
philpem@5 | 53 | int main(int argc, char **argv) { |
philpem@5 | 54 | |
philpem@5 | 55 | // Print usage, help and retrieve command line options |
philpem@5 | 56 | //----------------------------------------------------- |
philpem@5 | 57 | cimg_usage("A very simple Tron game, using the CImg Library"); |
philpem@5 | 58 | cimg_help("--- Quick help ----------------------------\n" |
philpem@5 | 59 | " Player 1 (blue) :\n" |
philpem@5 | 60 | " Use keys 'Z' (up), 'S' (down), 'Q' (left)\n" |
philpem@5 | 61 | " and 'D' (right) to control your player.\n" |
philpem@5 | 62 | " Right 'CONTROL' key enables turbospeed\n" |
philpem@5 | 63 | " Player 2 (red) : \n" |
philpem@5 | 64 | " Use arrow keys to control your player.\n" |
philpem@5 | 65 | " 'TAB' key enables turbospeed.\n" |
philpem@5 | 66 | "-------------------------------------------"); |
philpem@5 | 67 | |
philpem@5 | 68 | const char *geom = cimg_option("-g","300x300","Size of the game board"); |
philpem@5 | 69 | const int delay = cimg_option("-s",10,"Game speed (lower value means faster)"); |
philpem@5 | 70 | const bool twoplayers = !cimg_option("-1",false,"One player only"); |
philpem@5 | 71 | const int zoom = cimg_option("-z",1,"Zoom factor"); |
philpem@5 | 72 | const bool full = cimg_option("-f",false,"Fullscreen mode"); |
philpem@5 | 73 | unsigned int W = 400, H = 400; |
philpem@5 | 74 | std::sscanf(geom,"%u%*c%u",&W,&H); |
philpem@5 | 75 | |
philpem@5 | 76 | // Define game colors and variables |
philpem@5 | 77 | //---------------------------------- |
philpem@5 | 78 | const unsigned char green[] = { 64,255,32 }, blue[] = { 128,200,255}, red[] = { 255,0,0 }, white[] = { 255,255,255 }; |
philpem@5 | 79 | int score1=0, score2=0, round_over=0, ix1=-1, iy1=-1, x1=0, y1=0, u1=0, v1=0, ix2=-1, iy2=-1, x2=0, y2=0, u2=0, v2=0; |
philpem@5 | 80 | bool start_round = true, turbo1 = false, turbo2 = false; |
philpem@5 | 81 | |
philpem@5 | 82 | // Create background image |
philpem@5 | 83 | //-------------------------- |
philpem@5 | 84 | CImg<unsigned char> background, img; |
philpem@5 | 85 | background.assign(64,64,1,3,0).noise(60).draw_plasma().resize(W,H).blur(2).draw_rectangle(0,0,W-1,H-1,white,1.0f,~0U); |
philpem@5 | 86 | |
philpem@5 | 87 | // Open display window |
philpem@5 | 88 | //--------------------- |
philpem@5 | 89 | CImgDisplay disp(background,"* CImg-Tron *"); |
philpem@5 | 90 | if (zoom>1) disp.resize(-100*zoom,-100*zoom); |
philpem@5 | 91 | if (full) disp.toggle_fullscreen().display(background); |
philpem@5 | 92 | |
philpem@5 | 93 | // Start main game loop |
philpem@5 | 94 | //---------------------- |
philpem@5 | 95 | while (!disp.is_closed && !disp.is_keyESC) { |
philpem@5 | 96 | |
philpem@5 | 97 | // Init new game round if necessary |
philpem@5 | 98 | //---------------------------------- |
philpem@5 | 99 | if (start_round) { |
philpem@5 | 100 | |
philpem@5 | 101 | // Init game variables |
philpem@5 | 102 | round_over = 0; |
philpem@5 | 103 | ix1=-1; iy1=-1; x1 = 10; y1 = 10; u1 = 1; v1 = 0; turbo1 = false; |
philpem@5 | 104 | ix2=-1; iy2=-1; x2 = W-11; y2 = H-11; u2 = -1; v2 = 0; turbo2 = false; |
philpem@5 | 105 | img = background; |
philpem@5 | 106 | start_round = false; |
philpem@5 | 107 | |
philpem@5 | 108 | // Display a simple pre-round page |
philpem@5 | 109 | CImg<unsigned char> logo, pressakey; |
philpem@5 | 110 | logo.draw_text(0,0," CImg-Tron ",green,0,1,32); |
philpem@5 | 111 | CImg<unsigned char> tmp = (+background).draw_image((W-logo.dimx())/2,(H-logo.dimy())/2-20,logo,logo.get_channel(0).dilate(6).normalize(0,1)). |
philpem@5 | 112 | draw_text(W/2-70,H/2+10,"Blue ( %u )",blue,0,1,16,score1). |
philpem@5 | 113 | draw_text(W/2+10,H/2+10,"Red ( %u )",red,0,1,16,score2); |
philpem@5 | 114 | pressakey.draw_text(0,0,"* Press a key to start round *",white); |
philpem@5 | 115 | for (float i=0; i<1; i+=0.05f) ((+tmp)*=i).display(disp.wait(20)); |
philpem@5 | 116 | disp.flush(); |
philpem@5 | 117 | while (!disp.is_key() && !disp.is_closed) { |
philpem@5 | 118 | disp.display(tmp).wait(200).display((+tmp).draw_image(W/2-90,H/2+50,pressakey,pressakey,1,255)).wait(400); |
philpem@5 | 119 | if (disp.is_resized) disp.resize(disp); |
philpem@5 | 120 | } |
philpem@5 | 121 | if (disp.is_keyESC) disp.flush(); |
philpem@5 | 122 | } |
philpem@5 | 123 | |
philpem@5 | 124 | // Test collision between players and borders |
philpem@5 | 125 | if (x1<0 || x1>=img.dimx() || y1<0 || y1>=img.dimy() || |
philpem@5 | 126 | img(x1,y1,0)!=background(x1,y1,0) || |
philpem@5 | 127 | img(x1,y1,1)!=background(x1,y1,1) || |
philpem@5 | 128 | img(x1,y1,2)!=background(x1,y1,2) || |
philpem@5 | 129 | ((ix1>=0 || iy1>=0) && (img(ix1,iy1,0)!=background(ix1,iy1,0) || // Collision test for turbo mode |
philpem@5 | 130 | img(ix1,iy1,1)!=background(ix1,iy1,1) || |
philpem@5 | 131 | img(ix1,iy1,2)!=background(ix1,iy1,2)))) { round_over=1; score2++; } |
philpem@5 | 132 | if (twoplayers) { |
philpem@5 | 133 | if (x2<0 || x2>=img.dimx() || y2<0 || y2>=img.dimy() || |
philpem@5 | 134 | img(x2,y2,0)!=background(x2,y2,0) || |
philpem@5 | 135 | img(x2,y2,1)!=background(x2,y2,1) || |
philpem@5 | 136 | img(x2,y2,2)!=background(x2,y2,2) || |
philpem@5 | 137 | ((ix2>=0 || iy2>=0) && (img(ix2,iy2,0)!=background(ix2,iy2,0) || // Collision test for turbo mode |
philpem@5 | 138 | img(ix2,iy2,1)!=background(ix2,iy2,1) || |
philpem@5 | 139 | img(ix2,iy2,2)!=background(ix2,iy2,2)))) { round_over=2; score1++; } |
philpem@5 | 140 | } |
philpem@5 | 141 | |
philpem@5 | 142 | // Draw new players positions |
philpem@5 | 143 | img.draw_point(x1,y1,blue); |
philpem@5 | 144 | if (ix1>=0 && iy1>=0) img.draw_point(ix1,iy1,blue); |
philpem@5 | 145 | if (twoplayers) { |
philpem@5 | 146 | img.draw_point(x2,y2,red); |
philpem@5 | 147 | if (ix2>=0 && iy2>=0) img.draw_point(ix2,iy2,red); |
philpem@5 | 148 | } |
philpem@5 | 149 | if (disp.is_resized) disp.resize(disp); |
philpem@5 | 150 | img.display(disp); |
philpem@5 | 151 | |
philpem@5 | 152 | // Update players positions |
philpem@5 | 153 | x1+=u1; y1+=v1; |
philpem@5 | 154 | if (turbo1) { ix1 = x1; iy1 = y1; x1+=u1; y1+=v1; } else { ix1 = iy1 = -1; } |
philpem@5 | 155 | if (twoplayers) { |
philpem@5 | 156 | x2+=u2; y2+=v2; |
philpem@5 | 157 | if (turbo2) { ix2 = x2; iy2 = y2; x2+=u2; y2+=v2; } else { ix2 = iy2 = -1; } |
philpem@5 | 158 | } |
philpem@5 | 159 | |
philpem@5 | 160 | // Test keyboard events |
philpem@5 | 161 | int nu1 = u1, nv1 = v1, nu2 = u2, nv2 = v2; |
philpem@5 | 162 | if (disp.is_keyARROWLEFT) { nu1 = -1; nv1 = 0; } |
philpem@5 | 163 | if (disp.is_keyARROWRIGHT) { nu1 = 1; nv1 = 0; } |
philpem@5 | 164 | if (disp.is_keyARROWUP) { nu1 = 0; nv1 = -1; } |
philpem@5 | 165 | if (disp.is_keyARROWDOWN) { nu1 = 0; nv1 = 1; } |
philpem@5 | 166 | turbo1 = disp.is_keyCTRLRIGHT; |
philpem@5 | 167 | if (twoplayers) { |
philpem@5 | 168 | if (disp.is_keyQ) { nu2 = -1; nv2 = 0; } |
philpem@5 | 169 | if (disp.is_keyD) { nu2 = 1; nv2 = 0; } |
philpem@5 | 170 | if (disp.is_keyZ) { nu2 = 0; nv2 = -1; } |
philpem@5 | 171 | if (disp.is_keyS) { nu2 = 0; nv2 = 1; } |
philpem@5 | 172 | turbo2 = disp.is_keyTAB; |
philpem@5 | 173 | } |
philpem@5 | 174 | if (nu1!=-u1 && nv1!=-v1) { u1 = nu1; v1 = nv1; } |
philpem@5 | 175 | if (nu2!=-u2 && nv2!=-v2) { u2 = nu2; v2 = nv2; } |
philpem@5 | 176 | |
philpem@5 | 177 | // Check if round is over. |
philpem@5 | 178 | if (round_over) { |
philpem@5 | 179 | const int xc = round_over==1?x1:x2, yc = round_over==1?y1:y2; |
philpem@5 | 180 | for (int r=0; r<50; r+=3) img.draw_circle(xc,yc,r,round_over==1?blue:red,r/300.0f).display(disp.wait(20)); |
philpem@5 | 181 | for (int rr=0; rr<50; rr+=3) |
philpem@5 | 182 | ((+img)*=(50-rr)/50.0f).draw_circle(xc,yc,(50+rr),round_over==1?blue:red,1/6.0f).display(disp.wait(20)); |
philpem@5 | 183 | start_round = true; |
philpem@5 | 184 | } |
philpem@5 | 185 | |
philpem@5 | 186 | // Wait a small amount of time |
philpem@5 | 187 | disp.wait(delay); |
philpem@5 | 188 | } |
philpem@5 | 189 | return 0; |
philpem@5 | 190 | } |