1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/PTdecode/CImg-1.3.0/examples/tetris.cpp Mon Aug 03 14:09:20 2009 +0100 1.3 @@ -0,0 +1,203 @@ 1.4 +/* 1.5 + # 1.6 + # File : tetris.cpp 1.7 + # ( C++ source file ) 1.8 + # 1.9 + # Description : A CImg version of the famous Tetris game. 1.10 + # This file is a part of the CImg Library project. 1.11 + # ( http://cimg.sourceforge.net ) 1.12 + # 1.13 + # Copyright : David Tschumperle 1.14 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.15 + # 1.16 + # License : CeCILL v2.0 1.17 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.18 + # 1.19 + # This software is governed by the CeCILL license under French law and 1.20 + # abiding by the rules of distribution of free software. You can use, 1.21 + # modify and/ or redistribute the software under the terms of the CeCILL 1.22 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.23 + # "http://www.cecill.info". 1.24 + # 1.25 + # As a counterpart to the access to the source code and rights to copy, 1.26 + # modify and redistribute granted by the license, users are provided only 1.27 + # with a limited warranty and the software's author, the holder of the 1.28 + # economic rights, and the successive licensors have only limited 1.29 + # liability. 1.30 + # 1.31 + # In this respect, the user's attention is drawn to the risks associated 1.32 + # with loading, using, modifying and/or developing or reproducing the 1.33 + # software by the user in light of its specific status of free software, 1.34 + # that may mean that it is complicated to manipulate, and that also 1.35 + # therefore means that it is reserved for developers and experienced 1.36 + # professionals having in-depth computer knowledge. Users are therefore 1.37 + # encouraged to load and test the software's suitability as regards their 1.38 + # requirements in conditions enabling the security of their systems and/or 1.39 + # data to be ensured and, more generally, to use and operate it in the 1.40 + # same conditions as regards security. 1.41 + # 1.42 + # The fact that you are presently reading this means that you have had 1.43 + # knowledge of the CeCILL license and that you accept its terms. 1.44 + # 1.45 +*/ 1.46 + 1.47 +#include "img/tetris.h" 1.48 +#include "CImg.h" 1.49 +using namespace cimg_library; 1.50 + 1.51 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.52 +#ifdef cimg_use_visualcpp6 1.53 +#define std 1.54 +#endif 1.55 +#ifdef min 1.56 +#undef min 1.57 +#undef max 1.58 +#endif 1.59 + 1.60 +// Begin the main procedure 1.61 +//------------------------- 1.62 +int main(int argc,char **argv) { 1.63 + 1.64 + // Read command line argument (if any) 1.65 + cimg_usage("An implementation of the well known 'Tetris' game with CImg."); 1.66 + unsigned int 1.67 + blocdim = cimg_option("-blocdim",18,"Sprite bloc size"), 1.68 + speed = cimg_option("-speed",20,"Initial speed"), 1.69 + level = cimg_option("-level",0,"Level"); 1.70 + const char *geometry = cimg_option("-g","12x20","Size of the board"); 1.71 + unsigned int bwidth = 12,bheight = 20; 1.72 + std::sscanf(geometry,"%u%*c%u",&bwidth,&bheight); 1.73 + 1.74 + const CImg<unsigned char> dlogo = CImg<unsigned char>(data_logo,128,96,1,3,true); 1.75 + if (cimg::dialog("CImg Tetris", 1.76 + "Welcome to the CImg version of Tetris.\n" 1.77 + "( by David Tschumperle )\n\n" 1.78 + "Press 'Start' when you are ready to play !","Start","Quit",0,0,0,0,dlogo,true)) std::exit(0); 1.79 + 1.80 + // Create sprite, background graphics and initial board data 1.81 + const CImgList<unsigned char> pieces = CImgList<unsigned char>(). 1.82 + insert(CImg<unsigned char>(3,2).fill(1,1,1,0,0,1)). 1.83 + insert(CImg<unsigned char>(3,2).fill(2,2,2,2,0,0)). 1.84 + insert(CImg<unsigned char>(2,2).fill(3,3,3,3)). 1.85 + insert(CImg<unsigned char>(4,1).fill(4,4,4,4)). 1.86 + insert(CImg<unsigned char>(3,2).fill(5,5,0,0,5,5)). 1.87 + insert(CImg<unsigned char>(3,2).fill(0,6,6,6,6,0)). 1.88 + insert(CImg<unsigned char>(3,3).fill(0,7,0,7,7,7,0,7,0)). 1.89 + insert(CImg<unsigned char>(2,1).fill(8,8)). 1.90 + insert(CImg<unsigned char>(3,2).fill(9,9,9,0,9,0)). 1.91 + insert(CImg<unsigned char>(2,2).fill(10,10,0,10)). 1.92 + insert(CImg<unsigned char>(3,1).fill(11,11,11)); 1.93 + 1.94 + CImg<unsigned char> board(bwidth,bheight,1,1,0), background(board.dimx()*blocdim,board.dimy()*blocdim,1,3,0); 1.95 + (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; 1.96 + if (level) (board.get_shared_lines(board.dimy()-level,board.dimy()-1,0,0).noise(100))%=pieces.size+1; 1.97 + 1.98 + // Create a set of small gradient-colored blocs used to draw the pieces. 1.99 + CImgList<unsigned char> blocs(pieces.size,blocdim,blocdim,1,3); 1.100 + cimglist_for(blocs,l) { 1.101 + CImg<unsigned char> color = CImg<unsigned char>(3,1,1,1,128).noise(127,1).cut(120,255); 1.102 + float val; 1.103 + 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); 1.104 + blocs[l].draw_line(0,0,0,blocdim-1,color>>1).draw_line(0,blocdim-1,blocdim-1,blocdim-1,color>>1); 1.105 + color = (CImg<unsigned int>(color)*=2).cut(0,255); 1.106 + blocs[l].draw_line(0,0,(int)blocdim-1,0,color).draw_line(blocdim-1,0,blocdim-1,blocdim-1,color); 1.107 + } 1.108 + 1.109 + // Initialize window display and enter the main event loop 1.110 + CImgDisplay disp(background,"CImg Tetris",0,false,true); 1.111 + disp.move((CImgDisplay::screen_dimx()-disp.dimx())/2, 1.112 + (CImgDisplay::screen_dimy()-disp.dimy())/2).hide_mouse(); 1.113 + const unsigned char white[3]={ 255, 255, 255 }; 1.114 + CImg<unsigned char> visu, nboard, piece, next, next_mask; 1.115 + int cx=-1,cy=-1,cn=-1,nn=rand()%pieces.size,time=0, score=0; 1.116 + bool gameover = false, pause = false; 1.117 + 1.118 + while (!gameover && !disp.is_closed && !disp.is_keyESC && !disp.is_keyQ) { 1.119 + 1.120 + if (!pause) { 1.121 + 1.122 + // Draw the board on the display window. 1.123 + nboard = board; visu = background; 1.124 + 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); 1.125 + cimg_forXY(board,xx,yy) if (nboard(xx,yy)) visu.draw_image(xx*blocdim,yy*blocdim,blocs[nboard(xx,yy)-1]); 1.126 + visu.draw_text(5,5,"Lines : %d",white,0,1,11,score,nn).draw_text(visu.dimx()-75,5,"Next :",white,0,1,11); 1.127 + if (next.data) visu.draw_image(visu.dimx()-next.dimx()-2,10-next.dimy()/2,next,next_mask).display(disp.wait(20)); 1.128 + 1.129 + if (cn<0) { 1.130 + 1.131 + // Introduce a new piece on the board (if necessary) and create representation of the next piece 1.132 + board = nboard; 1.133 + piece = pieces[cn=nn]; 1.134 + nn = rand()%pieces.size; 1.135 + cx = board.dimx()/2; 1.136 + cy = piece.dimy()/2; 1.137 + next = CImg<unsigned char>(pieces[nn].dimx()*blocdim,pieces[nn].dimy()*blocdim,1,3,0); 1.138 + cimg_forXY(pieces[nn],xi,yi) if (pieces[nn](xi,yi)) next.draw_image(xi*blocdim,yi*blocdim,blocs[pieces[nn](xi,yi)-1]); 1.139 + next_mask = next.resize(-50,-50).get_pointwise_norm().threshold(0); 1.140 + 1.141 + // Detect tetris lines and do line removal animation if found. 1.142 + cimg_forY(board,yyy) { 1.143 + int Y = yyy*blocdim, line = 1; 1.144 + cimg_forX(board,xxx) if (!board(xxx,yyy)) line=0; 1.145 + if (line) { 1.146 + board.draw_image(0,1,board.get_crop(0,0,board.dimx()-1,yyy-1)); 1.147 + if (!((++score)%1) && speed>1) --speed; 1.148 + for (float alpha=0; alpha<=1; alpha+=0.07f) 1.149 + CImg<unsigned char>(visu).draw_image(0,Y,background.get_crop(0,Y,visu.dimx()-1,Y+blocdim-1),alpha).display(disp.wait(20)); 1.150 + visu.draw_image(0,Y,background.get_crop(0,Y,visu.dimx()-1,Y+blocdim-1)); 1.151 + } 1.152 + } 1.153 + } 1.154 + 1.155 + // Handle motion & collisions 1.156 + const int ox=cx, oy=cy; 1.157 + bool rotated = false, collision; 1.158 + switch (disp.key) { 1.159 + case cimg::keyP: pause = true; break; 1.160 + case cimg::keyARROWUP: piece.rotate(90); rotated = true; disp.key = 0; break; 1.161 + case cimg::keyARROWLEFT: cx--; disp.key = 0; break; 1.162 + case cimg::keyARROWRIGHT: cx++; disp.key = 0; break; 1.163 + } 1.164 + if (cx-piece.dimx()/2<0) cx=piece.dimx()/2; 1.165 + if (cy-piece.dimy()/2<0) cy=piece.dimy()/2; 1.166 + if (cx+(piece.dimx()-1)/2>=board.dimx()) cx = board.dimx()-1-(piece.dimx()-1)/2; 1.167 + 1.168 + // Detect collision along the X axis 1.169 + collision = false; cimg_forXY(piece,i,j) if (piece(i,j) && board(cx-piece.dimx()/2+i,cy-piece.dimy()/2+j)) collision = true; 1.170 + if (collision) { cx=ox; if (rotated) piece.rotate(-90); } 1.171 + 1.172 + if (disp.key==cimg::keyARROWDOWN || !((++time)%speed)) { cy++; disp.key=0; } 1.173 + // Detect collisiong along the Y axis 1.174 + collision = false; cimg_forXY(piece,ii,jj) if (piece(ii,jj) && board(cx-piece.dimx()/2+ii,cy-piece.dimy()/2+jj)) collision = true; 1.175 + if (collision || cy+(piece.dimy()-1)/2>=board.dimy()) { cy = oy; cn=-1; } 1.176 + if (collision && cy==piece.dimy()/2) gameover=true; 1.177 + } else { 1.178 + 1.179 + // If game is paused (key 'P'), do a little text animation 1.180 + float A = 0, B = 0; 1.181 + CImg<float> pauselogo = CImg<unsigned char>().draw_text(0,0,"Game Paused\nPress a key",white); 1.182 + disp.key = 0; while (!disp.is_closed && !disp.key) { 1.183 + const CImg<float> pauserotated = pauselogo.get_rotate((float)(30*std::sin(A)),0,1). 1.184 + resize((int)(-150-80*std::sin(B)),(int)(-150-80*std::sin(B))); 1.185 + A+=0.08f; B+=0.043f; 1.186 + CImg<unsigned char>(background). 1.187 + draw_image((background.dimx()-pauserotated.dimx())/2, 1.188 + (background.dimy()-pauserotated.dimy())/2, 1.189 + pauserotated.get_resize(-100,-100,1,3,2),pauserotated,1,255).display(disp.wait(20)); 1.190 + if (disp.is_resized) disp.resize(); 1.191 + } 1.192 + disp.key = 0; 1.193 + pause = false; 1.194 + } 1.195 + background.translate(0,20/speed,0,0,2); 1.196 + if (disp.is_resized) disp.resize(); 1.197 + } 1.198 + 1.199 + // End of game reached, display the score and do a 'game over' animation 1.200 + cimg_forXYV(visu,x,y,k) if (x%2 || y%2) visu(x,y,k) = 0; 1.201 + visu.display(disp); 1.202 + char tmp[1024]; 1.203 + std::sprintf(tmp,"Game Over !\n\nYour score : %d",score); 1.204 + cimg::dialog("CImg Tetris",tmp,"Quit"); 1.205 + return 0; 1.206 +}