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