1 /*
2  #
3  #  File        : jawbreaker.cpp
4  #                ( C++ source file )
5  #
6  #  Description : A funny game featuring small colored balls.
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 #undef min
47 #undef max
48 
49 // Main procedure
50 //----------------
main(int argc,char ** argv)51 int main(int argc, char **argv) {
52 
53   // Display help (if option '-h' or '--help' specified) and retrieve program arguments
54   cimg_usage("A small and funny game featuring colored balls.\n             (by David Tschumperlé).");
55   const char *score_file = cimg_option("-s",(char*)0,"Specify score file to use (0=default file).");
56   cimg_help("\n"
57             "** Quick Help *********************************************************\n\n"
58             "Goal : Delete the board by clicking on groups of adjacent colored balls\n"
59             "       (a group is made of at least two balls with the same color).\n"
60 	    "       Suppressing large sets gives higher scores.\n\n"
61             "In-game keys : - BACKSPACE or SPACE = Undo last move\n"
62 	    "               - CTRL+F = Toggle fullscreen mode\n"
63             "               - ESC   = Quit application\n"
64             "               - Q     = End current game\n\n"
65             "*********************************************************************");
66 
67   // Load score file if available
68   CImgList<unsigned int> score_history;
69   char filename_history[1024];
70   std::sprintf(filename_history,"%s%s",score_file?"":cimg::temporary_path(),score_file?score_file:"/jawbreaker.score");
71   std::FILE *file = std::fopen(filename_history,"r");
72   if (file) { std::fclose(file); score_history = CImg<unsigned int>::get_load_dlm(filename_history)<'y'; }
73 
74   // Create ball graphics
75   const unsigned int W = 12, H = 14, Wi = (W<<5), Hi = (H<<5);
76   unsigned int score = 0, previous_score = 0, shape_score = 0,
77     best_score = score_history?score_history.max():0U;
78 
79   const CImg<> colors(3,7,1,1, 255,255,255, 205,0,230, 0,235,0, 235,255,0, 235,0,0, 0,128,255, 450,350,300);
80   const unsigned char
81     white[] = { 255,255,255 }, orange[] = { 255,128,64 }, yellow[] = { 255,255,64 }, red[] = { 255,64,64 }, six = 6;
82   CImgList<> balls0(7,32,32,1,3,0);
83   cimglist_for(balls0,l) if (l) {
84     balls0[l].draw_circle(16,16,14,colors.data(0,l));
85     cimg_forXYC(balls0[l],x,y,k) if (balls0(l,x,y,k)) (balls0(l,x,y,k)*=(32 - x + y)/60.0f)+=20;
86     balls0[l].draw_circle(16,16,14,colors.data(0,l),0.5f,~0U).
87       draw_circle(20,10,5,colors.data(),0.2f).draw_circle(22,8,2,colors.data(),0.4f).cut(0,255);
88   }
89 
90   // Create background graphics
91   CImgList<unsigned char> balls(balls0);
92   CImg<unsigned char>
93     mask =  balls[1].get_cut(0,1).channel(0).dilate(3),
94     background = CImg<unsigned char>(Wi,Hi,1,3,0).
95     noise(255,1).blur(6,20,0,true).equalize(100,0,255).blur(2,4,0,true);
96   background.get_shared_channel(0)/=4; background.get_shared_channel(1)/=8; background.get_shared_channel(2)/=2;
97 
98   // Begin user-interaction loop.
99   CImg<unsigned char> board, previous_board, selected_board, shape, img(background);
100   CImgDisplay disp(img.width(),img.height(),"Jawbreaker",0);
101   bool redraw = true, gameover = false, title = true;
102 
103   for (float opac = 0.0f; !disp.is_closed(); ) {
104 
105     // Init board
106     if (!board) {
107       (++((board.assign(W,H,1,1,5).noise(5,1))%=5)).get_shared_row(0).fill(0);
108       opac = (float)(score = previous_score = shape_score = 0);
109       gameover = false; redraw = title = true;
110       previous_board = board;
111     }
112 
113     // Draw graphical board
114     if (redraw) {
115       (img=background).draw_text(2,2,"Score : %u",yellow,0,0.7f,24,score).
116         draw_text(Wi - 90,2,"Best : %u",orange,0,0.9f,17,best_score);
117 
118       if (selected_board) {
119         cimg_forXY(selected_board,x,y) if (selected_board(x,y))
120           img.draw_image(x<<5,y<<5,balls[selected_board(x,y)],mask);
121       } else cimg_forXY(board,x,y) if (board(x,y)) img.draw_image(x<<5,y<<5,balls[board(x,y)],mask);
122 
123       if (title) {
124         CImg<unsigned char> text1, text2;
125         text1.draw_text(0,0,"- Jawbreaker -",white,0,1,48).resize(-100,-100,1,3);
126         text2.draw_text(0,0,"Press button to start",yellow,0,1,24).resize(-100,-100,1,3);
127         (img/=2).draw_image((Wi - text1.width())/2,
128                             (Hi - text1.height())/2,
129                             text1,text1.get_dilate(7),1,255).
130           draw_image((Wi - text2.width())/2,
131                      (Hi + text1.height() + 10)/2,
132                      text2,text2.get_dilate(5),0.7f,255);
133 	for (float i = 1; i<10 && !disp.is_keyESC(); i+=0.25)
134 	  disp.display(img.get_crop((int)(Wi*(0.5f - i*i/200.0f)),(int)(Hi*(0.5f - i*i*i*i/20000.0f)),
135 				    (int)(Wi*(0.5f + i*i/200.0f)),(int)(Hi*(0.5f + i*i*i*i/20000.0f)))).wait(20);
136       }
137     }
138     if ((opac-=0.06f)>0) disp.display((+img).draw_text(disp.mouse_x() - 8,disp.mouse_y() - 80 + (int)(60*opac),"+%u",
139                                                        white,0,(float)std::sqrt(opac),32,shape_score)).wait(20);
140     else { if (redraw) { disp.display(img); redraw = false; } else disp.wait(); }
141 
142     // Handle key and window events
143     if (disp.is_resized()) disp.resize(disp);
144     if (disp.is_keyBACKSPACE() || disp.is_keySPACE()) {
145       board = previous_board; score = previous_score; selected_board.assign(); redraw = true; disp.set_key();
146     }
147     if (disp.is_keyQ()) { gameover = true; disp.set_key(); }
148     if (disp.is_keyESC()) disp.close();
149     if (disp.is_keyCTRLLEFT() && disp.is_keyF()) disp.toggle_fullscreen().display(img);
150 
151     // Handle ball selection and removal
152     const int x = disp.mouse_x()*board.width()/disp.width(), y = disp.mouse_y()*board.height()/disp.height();
153     if (disp.button()&1 && x>=0 && y>=0) {
154       if (title) { title = false; redraw = true; } else {
155         if (!board(x,y)) { selected_board.assign(); redraw = true; }
156         else {
157           if (!selected_board || selected_board(x,y)!=6) {
158             (selected_board=board).draw_fill(x,y,0,&six,1,shape);
159             if ((shape_score=(unsigned int)shape.sum())<2) selected_board.assign();
160             else { shape_score-=1; shape_score*=shape_score; opac = 1.0f; redraw = true; }
161           } else {
162             selected_board.assign();
163             previous_board = board;
164             previous_score = score;
165             score += shape_score;
166             board&=--shape;
167             redraw = true;
168 
169             // Handle board modification due to ball removal
170             for (int pmax = board.width(), p = 0; p<pmax; ++p) {
171               for (int q = board.height() - 1, qs = q; q>=0; --q) {
172                 while (qs>=0 && !board(p,qs)) --qs;
173                 board(p,q) = (qs>=0?board(p,qs--):0);
174               }
175               if (!board(p,board.height() - 1)) {
176                 board.draw_image(p,board.get_crop(p,0,board.width() - 1,board.height() - 1).shift(-1));
177                 if (p<pmax) { p--; pmax--; }
178               }
179             }
180 
181             // Test possible end of the game
182             gameover = true;
183             cimg_forXY(board,p,q)
184               if (board(p,q) && ((q && board(p,q)==board(p,q - 1)) || (p && board(p,q)==board(p - 1,q))))
185                 gameover = false;
186           }
187         }
188       }
189       disp.set_button();
190     }
191 
192     // If game is over...
193     if (gameover && opac<=0) {
194       CImg<unsigned char> text1, text2, text3, text4, text5, text6;
195       text1.draw_text(0,0,"Game Over !",white,0,1,48).resize(-100,-100,1,3);
196       const unsigned int remaining_balls = (unsigned int)board.get_cut(0,1).sum();
197       if (remaining_balls<8) {
198         const unsigned int bonus = (22 - 2*remaining_balls)*10;
199         score += bonus;
200         text2.draw_text(0,0,"Jawbreaker Bonus : +%u",white,0,1,24,bonus);
201       }
202       score_history.insert(CImg<unsigned int>::vector(score));
203       text3.draw_text(0,0,"Final score : %u",yellow,0,1,24,score).resize(-100,-100,1,3);
204       text4.draw_text(0,0,score>best_score?"** New record ! **":"Best score : %u",
205                       orange,0,1,24,score>best_score?score:best_score).resize(-100,-100,1,3);
206       text5.draw_text(0,0,"Average score : %u",red,0,1,24,
207                       score_history?(unsigned int)(score_history>'x').mean():0U).resize(-100,-100,1,3);
208       text6.draw_text(0,0,"Games played : %u",red,0,1,24,score_history.size()).resize(-100,-100,1,3);
209       if (score>best_score) best_score = score;
210 
211       unsigned int yt = (Hi - text1.height())/2 - 20;
212       (img/=2).draw_image((Wi - text1.width())/2,yt,text1,text1.get_dilate(7),1,255); yt+=80;
213       if (text2) { img.draw_image((Wi - text2.width())/2,yt,text2,text2.get_dilate(5),1,255); yt+=25; }
214       img.draw_image((Wi - text3.width())/2,yt,text3,text3.get_dilate(5),1,255).
215         draw_image((Wi - text4.width())/2,yt + 25,text4,text4.get_dilate(5),1,255).
216         draw_image((Wi - text5.width())/2,yt + 50,text5,text5.get_dilate(5),1,255).
217         draw_image((Wi - text6.width())/2,yt + 75,text6,text6.get_dilate(5),1,255).display(disp);
218       for (disp.flush(); !disp.is_closed() && !disp.key() && !disp.button(); disp.wait())
219         if (disp.is_resized()) disp.resize(disp);
220       disp.flush();
221       board.assign();
222       for (float i = 10; i>0 && !disp.is_keyESC(); i-=0.25)
223 	disp.display(img.get_crop((int)(Wi*(0.5f - i*i*i*i/20000.0f)),(int)(Hi*(0.5f - i*i/200.0f)),
224 				  (int)(Wi*(0.5f + i*i*i*i/20000.0f)),(int)(Hi*(0.5f + i*i/200.0f)))).wait(20);
225     }
226   }
227 
228   // Save score history if possible, and exit.
229   if (score_history) {
230     file = std::fopen(filename_history,"w");
231     if (file) { std::fclose(file); (score_history>'y').save_dlm(filename_history); }
232   }
233 
234   return 0;
235 }
236