PTdecode/CImg-1.3.0/examples/tron.cpp

changeset 5
1204ebf9340d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/PTdecode/CImg-1.3.0/examples/tron.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,190 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : tron.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : A clone of the famous (and very simple) Tron 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 "CImg.h"
    1.48 +using namespace cimg_library;
    1.49 +// The lines below are necessary when using a non-standard compiler as visualcpp6.
    1.50 +#ifdef cimg_use_visualcpp6
    1.51 +#define std
    1.52 +#endif
    1.53 +
    1.54 +// Start main procedure
    1.55 +//----------------------
    1.56 +int main(int argc, char **argv) {
    1.57 +
    1.58 +  // Print usage, help and retrieve command line options
    1.59 +  //-----------------------------------------------------
    1.60 +  cimg_usage("A very simple Tron game, using the CImg Library");
    1.61 +  cimg_help("--- Quick help ----------------------------\n"
    1.62 +            " Player 1 (blue) :\n"
    1.63 +            " Use keys 'Z' (up), 'S' (down), 'Q' (left)\n"
    1.64 +            "     and 'D' (right) to control your player.\n"
    1.65 +            "     Right 'CONTROL' key enables turbospeed\n"
    1.66 +            " Player 2 (red) : \n"
    1.67 +            "     Use arrow keys to control your player.\n"
    1.68 +            "     'TAB' key enables turbospeed.\n"
    1.69 +            "-------------------------------------------");
    1.70 +
    1.71 +  const char *geom      = cimg_option("-g","300x300","Size of the game board");
    1.72 +  const int delay       = cimg_option("-s",10,"Game speed (lower value means faster)");
    1.73 +  const bool twoplayers = !cimg_option("-1",false,"One player only");
    1.74 +  const int zoom        = cimg_option("-z",1,"Zoom factor");
    1.75 +  const bool full       = cimg_option("-f",false,"Fullscreen mode");
    1.76 +  unsigned int W = 400, H = 400;
    1.77 +  std::sscanf(geom,"%u%*c%u",&W,&H);
    1.78 +
    1.79 +  // Define game colors and variables
    1.80 +  //----------------------------------
    1.81 +  const unsigned char green[] = { 64,255,32 }, blue[] = { 128,200,255}, red[] = { 255,0,0 }, white[] = { 255,255,255 };
    1.82 +  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;
    1.83 +  bool start_round = true, turbo1 = false, turbo2 = false;
    1.84 +
    1.85 +  // Create background image
    1.86 +  //--------------------------
    1.87 +  CImg<unsigned char> background, img;
    1.88 +  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);
    1.89 +
    1.90 +  // Open display window
    1.91 +  //---------------------
    1.92 +  CImgDisplay disp(background,"* CImg-Tron *");
    1.93 +  if (zoom>1) disp.resize(-100*zoom,-100*zoom);
    1.94 +  if (full) disp.toggle_fullscreen().display(background);
    1.95 +
    1.96 +  // Start main game loop
    1.97 +  //----------------------
    1.98 +  while (!disp.is_closed && !disp.is_keyESC) {
    1.99 +
   1.100 +    // Init new game round if necessary
   1.101 +    //----------------------------------
   1.102 +    if (start_round) {
   1.103 +
   1.104 +      // Init game variables
   1.105 +      round_over = 0;
   1.106 +      ix1=-1; iy1=-1; x1 = 10;   y1 = 10;   u1 = 1;  v1 = 0; turbo1 = false;
   1.107 +      ix2=-1; iy2=-1; x2 = W-11; y2 = H-11; u2 = -1; v2 = 0; turbo2 = false;
   1.108 +      img = background;
   1.109 +      start_round = false;
   1.110 +
   1.111 +      // Display a simple pre-round page
   1.112 +      CImg<unsigned char> logo, pressakey;
   1.113 +      logo.draw_text(0,0," CImg-Tron ",green,0,1,32);
   1.114 +      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)).
   1.115 +        draw_text(W/2-70,H/2+10,"Blue ( %u )",blue,0,1,16,score1).
   1.116 +        draw_text(W/2+10,H/2+10,"Red ( %u )",red,0,1,16,score2);
   1.117 +      pressakey.draw_text(0,0,"* Press a key to start round *",white);
   1.118 +      for (float i=0; i<1; i+=0.05f) ((+tmp)*=i).display(disp.wait(20));
   1.119 +      disp.flush();
   1.120 +      while (!disp.is_key() && !disp.is_closed) {
   1.121 +        disp.display(tmp).wait(200).display((+tmp).draw_image(W/2-90,H/2+50,pressakey,pressakey,1,255)).wait(400);
   1.122 +        if (disp.is_resized) disp.resize(disp);
   1.123 +      }
   1.124 +      if (disp.is_keyESC) disp.flush();
   1.125 +    }
   1.126 +
   1.127 +    // Test collision between players and borders
   1.128 +    if (x1<0 || x1>=img.dimx() || y1<0 || y1>=img.dimy() ||
   1.129 +        img(x1,y1,0)!=background(x1,y1,0) ||
   1.130 +        img(x1,y1,1)!=background(x1,y1,1) ||
   1.131 +        img(x1,y1,2)!=background(x1,y1,2) ||
   1.132 +        ((ix1>=0 || iy1>=0) && (img(ix1,iy1,0)!=background(ix1,iy1,0) ||  // Collision test for turbo mode
   1.133 +                                img(ix1,iy1,1)!=background(ix1,iy1,1) ||
   1.134 +                                img(ix1,iy1,2)!=background(ix1,iy1,2)))) { round_over=1; score2++; }
   1.135 +    if (twoplayers) {
   1.136 +      if (x2<0 || x2>=img.dimx() || y2<0 || y2>=img.dimy() ||
   1.137 +          img(x2,y2,0)!=background(x2,y2,0) ||
   1.138 +          img(x2,y2,1)!=background(x2,y2,1) ||
   1.139 +          img(x2,y2,2)!=background(x2,y2,2) ||
   1.140 +          ((ix2>=0 || iy2>=0) && (img(ix2,iy2,0)!=background(ix2,iy2,0) ||  // Collision test for turbo mode
   1.141 +                                  img(ix2,iy2,1)!=background(ix2,iy2,1) ||
   1.142 +                                  img(ix2,iy2,2)!=background(ix2,iy2,2)))) { round_over=2; score1++; }
   1.143 +    }
   1.144 +
   1.145 +    // Draw new players positions
   1.146 +    img.draw_point(x1,y1,blue);
   1.147 +    if (ix1>=0 && iy1>=0) img.draw_point(ix1,iy1,blue);
   1.148 +    if (twoplayers) {
   1.149 +      img.draw_point(x2,y2,red);
   1.150 +      if (ix2>=0 && iy2>=0) img.draw_point(ix2,iy2,red);
   1.151 +    }
   1.152 +    if (disp.is_resized) disp.resize(disp);
   1.153 +    img.display(disp);
   1.154 +
   1.155 +    // Update players positions
   1.156 +    x1+=u1; y1+=v1;
   1.157 +    if (turbo1) { ix1 = x1; iy1 = y1; x1+=u1; y1+=v1; } else { ix1 = iy1 = -1; }
   1.158 +    if (twoplayers) {
   1.159 +      x2+=u2; y2+=v2;
   1.160 +      if (turbo2) { ix2 = x2; iy2 = y2; x2+=u2; y2+=v2; } else { ix2 = iy2 = -1; }
   1.161 +    }
   1.162 +
   1.163 +    // Test keyboard events
   1.164 +    int nu1 = u1, nv1 = v1, nu2 = u2, nv2 = v2;
   1.165 +    if (disp.is_keyARROWLEFT)  { nu1 = -1; nv1 = 0; }
   1.166 +    if (disp.is_keyARROWRIGHT) { nu1 = 1; nv1 = 0; }
   1.167 +    if (disp.is_keyARROWUP)    { nu1 = 0; nv1 = -1; }
   1.168 +    if (disp.is_keyARROWDOWN)  { nu1 = 0; nv1 = 1; }
   1.169 +    turbo1 = disp.is_keyCTRLRIGHT;
   1.170 +    if (twoplayers) {
   1.171 +      if (disp.is_keyQ) { nu2 = -1; nv2 = 0; }
   1.172 +      if (disp.is_keyD) { nu2 = 1; nv2 = 0; }
   1.173 +      if (disp.is_keyZ) { nu2 = 0; nv2 = -1; }
   1.174 +      if (disp.is_keyS) { nu2 = 0; nv2 = 1; }
   1.175 +      turbo2 = disp.is_keyTAB;
   1.176 +    }
   1.177 +    if (nu1!=-u1 && nv1!=-v1) { u1 = nu1; v1 = nv1; }
   1.178 +    if (nu2!=-u2 && nv2!=-v2) { u2 = nu2; v2 = nv2; }
   1.179 +
   1.180 +    // Check if round is over.
   1.181 +    if (round_over) {
   1.182 +      const int xc = round_over==1?x1:x2, yc = round_over==1?y1:y2;
   1.183 +      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));
   1.184 +      for (int rr=0; rr<50; rr+=3)
   1.185 +        ((+img)*=(50-rr)/50.0f).draw_circle(xc,yc,(50+rr),round_over==1?blue:red,1/6.0f).display(disp.wait(20));
   1.186 +      start_round = true;
   1.187 +    }
   1.188 +
   1.189 +    // Wait a small amount of time
   1.190 +    disp.wait(delay);
   1.191 +  }
   1.192 +  return 0;
   1.193 +}