PTdecode/CImg-1.3.0/examples/tron.cpp

Fri, 25 Sep 2009 10:50:44 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Fri, 25 Sep 2009 10:50:44 +0100
changeset 21
629637abfe1f
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

added dots-per-inch to status readback

     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.sourceforge.net )
     9  #
    10  #  Copyright   : David Tschumperle
    11  #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    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 */
    44 #include "CImg.h"
    45 using namespace cimg_library;
    46 // The lines below are necessary when using a non-standard compiler as visualcpp6.
    47 #ifdef cimg_use_visualcpp6
    48 #define std
    49 #endif
    51 // Start main procedure
    52 //----------------------
    53 int main(int argc, char **argv) {
    55   // Print usage, help and retrieve command line options
    56   //-----------------------------------------------------
    57   cimg_usage("A very simple Tron game, using the CImg Library");
    58   cimg_help("--- Quick help ----------------------------\n"
    59             " Player 1 (blue) :\n"
    60             " Use keys 'Z' (up), 'S' (down), 'Q' (left)\n"
    61             "     and 'D' (right) to control your player.\n"
    62             "     Right 'CONTROL' key enables turbospeed\n"
    63             " Player 2 (red) : \n"
    64             "     Use arrow keys to control your player.\n"
    65             "     'TAB' key enables turbospeed.\n"
    66             "-------------------------------------------");
    68   const char *geom      = cimg_option("-g","300x300","Size of the game board");
    69   const int delay       = cimg_option("-s",10,"Game speed (lower value means faster)");
    70   const bool twoplayers = !cimg_option("-1",false,"One player only");
    71   const int zoom        = cimg_option("-z",1,"Zoom factor");
    72   const bool full       = cimg_option("-f",false,"Fullscreen mode");
    73   unsigned int W = 400, H = 400;
    74   std::sscanf(geom,"%u%*c%u",&W,&H);
    76   // Define game colors and variables
    77   //----------------------------------
    78   const unsigned char green[] = { 64,255,32 }, blue[] = { 128,200,255}, red[] = { 255,0,0 }, white[] = { 255,255,255 };
    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;
    80   bool start_round = true, turbo1 = false, turbo2 = false;
    82   // Create background image
    83   //--------------------------
    84   CImg<unsigned char> background, img;
    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);
    87   // Open display window
    88   //---------------------
    89   CImgDisplay disp(background,"* CImg-Tron *");
    90   if (zoom>1) disp.resize(-100*zoom,-100*zoom);
    91   if (full) disp.toggle_fullscreen().display(background);
    93   // Start main game loop
    94   //----------------------
    95   while (!disp.is_closed && !disp.is_keyESC) {
    97     // Init new game round if necessary
    98     //----------------------------------
    99     if (start_round) {
   101       // Init game variables
   102       round_over = 0;
   103       ix1=-1; iy1=-1; x1 = 10;   y1 = 10;   u1 = 1;  v1 = 0; turbo1 = false;
   104       ix2=-1; iy2=-1; x2 = W-11; y2 = H-11; u2 = -1; v2 = 0; turbo2 = false;
   105       img = background;
   106       start_round = false;
   108       // Display a simple pre-round page
   109       CImg<unsigned char> logo, pressakey;
   110       logo.draw_text(0,0," CImg-Tron ",green,0,1,32);
   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)).
   112         draw_text(W/2-70,H/2+10,"Blue ( %u )",blue,0,1,16,score1).
   113         draw_text(W/2+10,H/2+10,"Red ( %u )",red,0,1,16,score2);
   114       pressakey.draw_text(0,0,"* Press a key to start round *",white);
   115       for (float i=0; i<1; i+=0.05f) ((+tmp)*=i).display(disp.wait(20));
   116       disp.flush();
   117       while (!disp.is_key() && !disp.is_closed) {
   118         disp.display(tmp).wait(200).display((+tmp).draw_image(W/2-90,H/2+50,pressakey,pressakey,1,255)).wait(400);
   119         if (disp.is_resized) disp.resize(disp);
   120       }
   121       if (disp.is_keyESC) disp.flush();
   122     }
   124     // Test collision between players and borders
   125     if (x1<0 || x1>=img.dimx() || y1<0 || y1>=img.dimy() ||
   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.dimx() || y2<0 || y2>=img.dimy() ||
   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     }
   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);
   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     }
   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; }
   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     }
   186     // Wait a small amount of time
   187     disp.wait(delay);
   188   }
   189   return 0;
   190 }