Wed, 05 Aug 2009 17:10:56 +0100
add README
philpem@5 | 1 | /* |
philpem@5 | 2 | # |
philpem@5 | 3 | # File : tetris.cpp |
philpem@5 | 4 | # ( C++ source file ) |
philpem@5 | 5 | # |
philpem@5 | 6 | # Description : A CImg version of the famous Tetris 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 "img/tetris.h" |
philpem@5 | 45 | #include "CImg.h" |
philpem@5 | 46 | using namespace cimg_library; |
philpem@5 | 47 | |
philpem@5 | 48 | // The lines below are necessary when using a non-standard compiler as visualcpp6. |
philpem@5 | 49 | #ifdef cimg_use_visualcpp6 |
philpem@5 | 50 | #define std |
philpem@5 | 51 | #endif |
philpem@5 | 52 | #ifdef min |
philpem@5 | 53 | #undef min |
philpem@5 | 54 | #undef max |
philpem@5 | 55 | #endif |
philpem@5 | 56 | |
philpem@5 | 57 | // Begin the main procedure |
philpem@5 | 58 | //------------------------- |
philpem@5 | 59 | int main(int argc,char **argv) { |
philpem@5 | 60 | |
philpem@5 | 61 | // Read command line argument (if any) |
philpem@5 | 62 | cimg_usage("An implementation of the well known 'Tetris' game with CImg."); |
philpem@5 | 63 | unsigned int |
philpem@5 | 64 | blocdim = cimg_option("-blocdim",18,"Sprite bloc size"), |
philpem@5 | 65 | speed = cimg_option("-speed",20,"Initial speed"), |
philpem@5 | 66 | level = cimg_option("-level",0,"Level"); |
philpem@5 | 67 | const char *geometry = cimg_option("-g","12x20","Size of the board"); |
philpem@5 | 68 | unsigned int bwidth = 12,bheight = 20; |
philpem@5 | 69 | std::sscanf(geometry,"%u%*c%u",&bwidth,&bheight); |
philpem@5 | 70 | |
philpem@5 | 71 | const CImg<unsigned char> dlogo = CImg<unsigned char>(data_logo,128,96,1,3,true); |
philpem@5 | 72 | if (cimg::dialog("CImg Tetris", |
philpem@5 | 73 | "Welcome to the CImg version of Tetris.\n" |
philpem@5 | 74 | "( by David Tschumperle )\n\n" |
philpem@5 | 75 | "Press 'Start' when you are ready to play !","Start","Quit",0,0,0,0,dlogo,true)) std::exit(0); |
philpem@5 | 76 | |
philpem@5 | 77 | // Create sprite, background graphics and initial board data |
philpem@5 | 78 | const CImgList<unsigned char> pieces = CImgList<unsigned char>(). |
philpem@5 | 79 | insert(CImg<unsigned char>(3,2).fill(1,1,1,0,0,1)). |
philpem@5 | 80 | insert(CImg<unsigned char>(3,2).fill(2,2,2,2,0,0)). |
philpem@5 | 81 | insert(CImg<unsigned char>(2,2).fill(3,3,3,3)). |
philpem@5 | 82 | insert(CImg<unsigned char>(4,1).fill(4,4,4,4)). |
philpem@5 | 83 | insert(CImg<unsigned char>(3,2).fill(5,5,0,0,5,5)). |
philpem@5 | 84 | insert(CImg<unsigned char>(3,2).fill(0,6,6,6,6,0)). |
philpem@5 | 85 | insert(CImg<unsigned char>(3,3).fill(0,7,0,7,7,7,0,7,0)). |
philpem@5 | 86 | insert(CImg<unsigned char>(2,1).fill(8,8)). |
philpem@5 | 87 | insert(CImg<unsigned char>(3,2).fill(9,9,9,0,9,0)). |
philpem@5 | 88 | insert(CImg<unsigned char>(2,2).fill(10,10,0,10)). |
philpem@5 | 89 | insert(CImg<unsigned char>(3,1).fill(11,11,11)); |
philpem@5 | 90 | |
philpem@5 | 91 | CImg<unsigned char> board(bwidth,bheight,1,1,0), background(board.dimx()*blocdim,board.dimy()*blocdim,1,3,0); |
philpem@5 | 92 | (background.noise(30).draw_plasma().noise(30).deriche(5,0,'y').translate(0,background.dimy()/2,0,0,2).deriche(5,0,'y'))/=1.5f; |
philpem@5 | 93 | if (level) (board.get_shared_lines(board.dimy()-level,board.dimy()-1,0,0).noise(100))%=pieces.size+1; |
philpem@5 | 94 | |
philpem@5 | 95 | // Create a set of small gradient-colored blocs used to draw the pieces. |
philpem@5 | 96 | CImgList<unsigned char> blocs(pieces.size,blocdim,blocdim,1,3); |
philpem@5 | 97 | cimglist_for(blocs,l) { |
philpem@5 | 98 | CImg<unsigned char> color = CImg<unsigned char>(3,1,1,1,128).noise(127,1).cut(120,255); |
philpem@5 | 99 | float val; |
philpem@5 | 100 | cimg_forXYV(blocs[l],x,y,k) blocs[l](x,y,k) = (unsigned char)((val=(color[k]*0.7f*(x+y+5)/blocdim))>255?255:val); |
philpem@5 | 101 | blocs[l].draw_line(0,0,0,blocdim-1,color>>1).draw_line(0,blocdim-1,blocdim-1,blocdim-1,color>>1); |
philpem@5 | 102 | color = (CImg<unsigned int>(color)*=2).cut(0,255); |
philpem@5 | 103 | blocs[l].draw_line(0,0,(int)blocdim-1,0,color).draw_line(blocdim-1,0,blocdim-1,blocdim-1,color); |
philpem@5 | 104 | } |
philpem@5 | 105 | |
philpem@5 | 106 | // Initialize window display and enter the main event loop |
philpem@5 | 107 | CImgDisplay disp(background,"CImg Tetris",0,false,true); |
philpem@5 | 108 | disp.move((CImgDisplay::screen_dimx()-disp.dimx())/2, |
philpem@5 | 109 | (CImgDisplay::screen_dimy()-disp.dimy())/2).hide_mouse(); |
philpem@5 | 110 | const unsigned char white[3]={ 255, 255, 255 }; |
philpem@5 | 111 | CImg<unsigned char> visu, nboard, piece, next, next_mask; |
philpem@5 | 112 | int cx=-1,cy=-1,cn=-1,nn=rand()%pieces.size,time=0, score=0; |
philpem@5 | 113 | bool gameover = false, pause = false; |
philpem@5 | 114 | |
philpem@5 | 115 | while (!gameover && !disp.is_closed && !disp.is_keyESC && !disp.is_keyQ) { |
philpem@5 | 116 | |
philpem@5 | 117 | if (!pause) { |
philpem@5 | 118 | |
philpem@5 | 119 | // Draw the board on the display window. |
philpem@5 | 120 | nboard = board; visu = background; |
philpem@5 | 121 | if (cx>=0 && cy>=0) cimg_forXY(piece,x,y) if (piece(x,y)) nboard(cx-piece.dimx()/2+x,cy-piece.dimy()/2+y)=piece(x,y); |
philpem@5 | 122 | cimg_forXY(board,xx,yy) if (nboard(xx,yy)) visu.draw_image(xx*blocdim,yy*blocdim,blocs[nboard(xx,yy)-1]); |
philpem@5 | 123 | visu.draw_text(5,5,"Lines : %d",white,0,1,11,score,nn).draw_text(visu.dimx()-75,5,"Next :",white,0,1,11); |
philpem@5 | 124 | if (next.data) visu.draw_image(visu.dimx()-next.dimx()-2,10-next.dimy()/2,next,next_mask).display(disp.wait(20)); |
philpem@5 | 125 | |
philpem@5 | 126 | if (cn<0) { |
philpem@5 | 127 | |
philpem@5 | 128 | // Introduce a new piece on the board (if necessary) and create representation of the next piece |
philpem@5 | 129 | board = nboard; |
philpem@5 | 130 | piece = pieces[cn=nn]; |
philpem@5 | 131 | nn = rand()%pieces.size; |
philpem@5 | 132 | cx = board.dimx()/2; |
philpem@5 | 133 | cy = piece.dimy()/2; |
philpem@5 | 134 | next = CImg<unsigned char>(pieces[nn].dimx()*blocdim,pieces[nn].dimy()*blocdim,1,3,0); |
philpem@5 | 135 | cimg_forXY(pieces[nn],xi,yi) if (pieces[nn](xi,yi)) next.draw_image(xi*blocdim,yi*blocdim,blocs[pieces[nn](xi,yi)-1]); |
philpem@5 | 136 | next_mask = next.resize(-50,-50).get_pointwise_norm().threshold(0); |
philpem@5 | 137 | |
philpem@5 | 138 | // Detect tetris lines and do line removal animation if found. |
philpem@5 | 139 | cimg_forY(board,yyy) { |
philpem@5 | 140 | int Y = yyy*blocdim, line = 1; |
philpem@5 | 141 | cimg_forX(board,xxx) if (!board(xxx,yyy)) line=0; |
philpem@5 | 142 | if (line) { |
philpem@5 | 143 | board.draw_image(0,1,board.get_crop(0,0,board.dimx()-1,yyy-1)); |
philpem@5 | 144 | if (!((++score)%1) && speed>1) --speed; |
philpem@5 | 145 | for (float alpha=0; alpha<=1; alpha+=0.07f) |
philpem@5 | 146 | CImg<unsigned char>(visu).draw_image(0,Y,background.get_crop(0,Y,visu.dimx()-1,Y+blocdim-1),alpha).display(disp.wait(20)); |
philpem@5 | 147 | visu.draw_image(0,Y,background.get_crop(0,Y,visu.dimx()-1,Y+blocdim-1)); |
philpem@5 | 148 | } |
philpem@5 | 149 | } |
philpem@5 | 150 | } |
philpem@5 | 151 | |
philpem@5 | 152 | // Handle motion & collisions |
philpem@5 | 153 | const int ox=cx, oy=cy; |
philpem@5 | 154 | bool rotated = false, collision; |
philpem@5 | 155 | switch (disp.key) { |
philpem@5 | 156 | case cimg::keyP: pause = true; break; |
philpem@5 | 157 | case cimg::keyARROWUP: piece.rotate(90); rotated = true; disp.key = 0; break; |
philpem@5 | 158 | case cimg::keyARROWLEFT: cx--; disp.key = 0; break; |
philpem@5 | 159 | case cimg::keyARROWRIGHT: cx++; disp.key = 0; break; |
philpem@5 | 160 | } |
philpem@5 | 161 | if (cx-piece.dimx()/2<0) cx=piece.dimx()/2; |
philpem@5 | 162 | if (cy-piece.dimy()/2<0) cy=piece.dimy()/2; |
philpem@5 | 163 | if (cx+(piece.dimx()-1)/2>=board.dimx()) cx = board.dimx()-1-(piece.dimx()-1)/2; |
philpem@5 | 164 | |
philpem@5 | 165 | // Detect collision along the X axis |
philpem@5 | 166 | collision = false; cimg_forXY(piece,i,j) if (piece(i,j) && board(cx-piece.dimx()/2+i,cy-piece.dimy()/2+j)) collision = true; |
philpem@5 | 167 | if (collision) { cx=ox; if (rotated) piece.rotate(-90); } |
philpem@5 | 168 | |
philpem@5 | 169 | if (disp.key==cimg::keyARROWDOWN || !((++time)%speed)) { cy++; disp.key=0; } |
philpem@5 | 170 | // Detect collisiong along the Y axis |
philpem@5 | 171 | collision = false; cimg_forXY(piece,ii,jj) if (piece(ii,jj) && board(cx-piece.dimx()/2+ii,cy-piece.dimy()/2+jj)) collision = true; |
philpem@5 | 172 | if (collision || cy+(piece.dimy()-1)/2>=board.dimy()) { cy = oy; cn=-1; } |
philpem@5 | 173 | if (collision && cy==piece.dimy()/2) gameover=true; |
philpem@5 | 174 | } else { |
philpem@5 | 175 | |
philpem@5 | 176 | // If game is paused (key 'P'), do a little text animation |
philpem@5 | 177 | float A = 0, B = 0; |
philpem@5 | 178 | CImg<float> pauselogo = CImg<unsigned char>().draw_text(0,0,"Game Paused\nPress a key",white); |
philpem@5 | 179 | disp.key = 0; while (!disp.is_closed && !disp.key) { |
philpem@5 | 180 | const CImg<float> pauserotated = pauselogo.get_rotate((float)(30*std::sin(A)),0,1). |
philpem@5 | 181 | resize((int)(-150-80*std::sin(B)),(int)(-150-80*std::sin(B))); |
philpem@5 | 182 | A+=0.08f; B+=0.043f; |
philpem@5 | 183 | CImg<unsigned char>(background). |
philpem@5 | 184 | draw_image((background.dimx()-pauserotated.dimx())/2, |
philpem@5 | 185 | (background.dimy()-pauserotated.dimy())/2, |
philpem@5 | 186 | pauserotated.get_resize(-100,-100,1,3,2),pauserotated,1,255).display(disp.wait(20)); |
philpem@5 | 187 | if (disp.is_resized) disp.resize(); |
philpem@5 | 188 | } |
philpem@5 | 189 | disp.key = 0; |
philpem@5 | 190 | pause = false; |
philpem@5 | 191 | } |
philpem@5 | 192 | background.translate(0,20/speed,0,0,2); |
philpem@5 | 193 | if (disp.is_resized) disp.resize(); |
philpem@5 | 194 | } |
philpem@5 | 195 | |
philpem@5 | 196 | // End of game reached, display the score and do a 'game over' animation |
philpem@5 | 197 | cimg_forXYV(visu,x,y,k) if (x%2 || y%2) visu(x,y,k) = 0; |
philpem@5 | 198 | visu.display(disp); |
philpem@5 | 199 | char tmp[1024]; |
philpem@5 | 200 | std::sprintf(tmp,"Game Over !\n\nYour score : %d",score); |
philpem@5 | 201 | cimg::dialog("CImg Tetris",tmp,"Quit"); |
philpem@5 | 202 | return 0; |
philpem@5 | 203 | } |