PTdecode/CImg-1.3.0/examples/odykill.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/odykill.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : odykill.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : Simple shoot-em-up game featuring the Robotvis/Odyssee Team !
    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/odykill.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 +int main(int argc,char **argv) {
    1.61 +
    1.62 +  // Create game graphics
    1.63 +  CImg<unsigned char> graphics[21] = {
    1.64 +    CImg<unsigned char>(data_tomato,100,100,1,3,false),
    1.65 +    CImg<unsigned char>(data_heart,100,100,1,3,false),
    1.66 +    CImg<unsigned char>(data_dynamite,100,100,1,3,false),
    1.67 +    CImg<unsigned char>(data_brain,100,100,1,3,false),
    1.68 +    CImg<unsigned char>(data_cdrom,100,100,1,3,false),
    1.69 +    CImg<unsigned char>(data_enemy,113,150,1,3,false),
    1.70 +    CImg<unsigned char>(data_enemy2,116,155,1,3,false),
    1.71 +    CImg<unsigned char>(data_enemy3,104,134,1,3,false),
    1.72 +    CImg<unsigned char>(data_enemy4,141,151,1,3,false),
    1.73 +    CImg<unsigned char>(data_enemy5,140,152,1,3,false),
    1.74 +    CImg<unsigned char>(data_enemy6,131,156,1,3,false),
    1.75 +    CImg<unsigned char>(data_enemy7,114,125,1,3,false),
    1.76 +    CImg<unsigned char>(data_enemy8,97,125,1,3,false),
    1.77 +    CImg<unsigned char>(data_enemy9,143,134,1,3,false),
    1.78 +    CImg<unsigned char>(data_enemy10,158,214,1,3,false),
    1.79 +    CImg<unsigned char>(data_enemy11,131,168,1,3,false),
    1.80 +    CImg<unsigned char>(data_enemy12,114,138,1,3,false),
    1.81 +    CImg<unsigned char>(data_enemy13,144,144,1,3,false),
    1.82 +    CImg<unsigned char>(data_enemy14,132,153,1,3,false),
    1.83 +    CImg<unsigned char>(data_enemy15,152,151,1,3,false),
    1.84 +    CImg<unsigned char>(data_enemy16,139,185,1,3,false),
    1.85 +  };
    1.86 +  CImg<> masks[21];
    1.87 +  const unsigned char black[] = { 0,0,0 }, white[] = { 255,255,255 };
    1.88 +
    1.89 +  // Display weapon selection menu
    1.90 +  CImg<unsigned char> back0(640,480,1,3), title(data_title,294,94,1,3,true), choose(data_choose,524,49,1,3,true);
    1.91 +  back0.fill(0).draw_image(back0.dimx()/2-title.dimx()/2,30,title).draw_image(back0.dimx()/2-choose.dimx()/2,150,choose);
    1.92 +  CImgDisplay disp(back0,"OdyKill");
    1.93 +  int weapon=-1;
    1.94 +
    1.95 +  while (!disp.is_closed && !disp.button) {
    1.96 +    weapon = -1;
    1.97 +    for (int k=0; k<5; k++) {
    1.98 +      const int mx = disp.mouse_x, my = disp.mouse_y;
    1.99 +      if (!((mx-40)/110==k && my>250 && my<350)) back0.draw_image(40+k*110,250,graphics[k]/2.0);
   1.100 +      else back0.draw_image(40+k*110,250,graphics[weapon=k]);
   1.101 +    }
   1.102 +    CImg<unsigned char> tmp = CImg<unsigned char>().draw_text(0,0,
   1.103 +                                                              weapon==0?" Tomato   ":
   1.104 +                                                              weapon==1?"  Heart   ":
   1.105 +                                                              weapon==2?" Dynamite ":
   1.106 +                                                              weapon==3?"  Brain   ":
   1.107 +                                                              weapon==4?"  CD-Rom  ":
   1.108 +                                                              "          ",white,black,1,32).resize(-100,-100,1,1),
   1.109 +      tmp2 = tmp.get_blur(6).normalize(0,255).draw_image(tmp,0.5f);
   1.110 +    { cimg_forV(back0,k) back0.draw_image(250,390,0,k,tmp2); }
   1.111 +
   1.112 +    disp.resize(disp).display(back0).wait();
   1.113 +    if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.toggle_fullscreen();
   1.114 +    if (disp.is_closed || disp.is_keyQ || disp.is_keyESC) std::exit(0);
   1.115 +  }
   1.116 +  disp.hide_mouse();
   1.117 +
   1.118 +  /*---------------------------------
   1.119 +
   1.120 +  Go !
   1.121 +
   1.122 +  --------------------------------*/
   1.123 +
   1.124 +  const CImg<unsigned char>
   1.125 +    background = CImg<unsigned char>(100,100,1,3,0).noise(100,2).draw_plasma(0,0,99,99).
   1.126 +    resize(back0.dimx(),back0.dimy(),1,3,5)/2.5;
   1.127 +  { for (unsigned int k=0; k<21; k++) {
   1.128 +    CImg<> tmp = graphics[k].resize(k<5?32:164,k<5?32:164,1,3);
   1.129 +    cimg_forXY(tmp,x,y) tmp(x,y)  = (tmp(x,y,0)==255 && tmp(x,y,1)==255 && tmp(x,y,2)==255)?0.0f:1.0f;
   1.130 +    masks[k]=tmp.get_shared_channel(0);
   1.131 +    graphics[k].resize(k<5?32:164,k<5?32:164,1,3,5);
   1.132 +  }}
   1.133 +
   1.134 +  CImg<unsigned char> canvas(background);
   1.135 +  int n = 5+((int)(200*cimg::rand())%16);
   1.136 +  CImg<unsigned char> tomato = graphics[weapon], enemy = graphics[n];
   1.137 +  CImg<> m_tomato = masks[weapon], m_enemy = masks[n];
   1.138 +
   1.139 +  double angle=0;
   1.140 +  int tomato_x=0,tomato_y=0,shooted=0;
   1.141 +  double enemy_x=-1000, enemy_y=-1000, enemy_z=-1000, tomato_z = 0, vx = 0, vy = 0, vz = 0, va = 0;
   1.142 +  double speed = cimg_option("-speed",5.0,"Speed");
   1.143 +  int timeleft = 2000, score = 0;
   1.144 +  CImg<unsigned char> r_enemy;
   1.145 +
   1.146 +  // Main loop
   1.147 +  while(timeleft && !disp.is_closed && !disp.is_keyESC && !disp.is_keyQ) {
   1.148 +    timeleft--;
   1.149 +    const int mx = disp.mouse_x*back0.dimx()/disp.dimx(), my = disp.mouse_y*back0.dimy()/disp.dimy();
   1.150 +
   1.151 +    // Handle object motion
   1.152 +    if (tomato_z>0) {
   1.153 +      tomato_z+=0.07; tomato_y -= (int)(20*std::cos(cimg::valuePI/7 + tomato_z*cimg::valuePI));
   1.154 +      if (tomato_z>=1) { tomato_z=0; tomato_x = mx; tomato_y = my; }
   1.155 +    }
   1.156 +    if (!shooted) {
   1.157 +      enemy_x +=vx;
   1.158 +      enemy_y +=vy;
   1.159 +      enemy_z +=vz;
   1.160 +    }
   1.161 +    else {
   1.162 +      va = 10;
   1.163 +      enemy_y += vy;
   1.164 +      vy += 2;
   1.165 +      tomato_z = 0;
   1.166 +      if (enemy_y>5*canvas.dimy()/4) {
   1.167 +        shooted = 0;
   1.168 +        int n = 5 + ((int)(200*cimg::rand())%16);
   1.169 +        enemy = graphics[n];
   1.170 +        m_enemy = masks[n];
   1.171 +        enemy_x=cimg::crand()*1e8; enemy_y=cimg::crand()*1e8; enemy_z=cimg::crand()*1e8;
   1.172 +        va = angle = 0;
   1.173 +      }
   1.174 +    }
   1.175 +
   1.176 +    if (enemy_x<0) { enemy_x=0; vx = speed*cimg::crand(); }
   1.177 +    if (enemy_x>canvas.dimx()) { enemy_x=canvas.dimx(); vx = speed*cimg::crand(); }
   1.178 +    if (enemy_y<0) { enemy_y=0; vy = speed*cimg::crand(); }
   1.179 +    if (!shooted && enemy_y>canvas.dimy()) { enemy_y=canvas.dimy(); vy = speed*cimg::crand(); }
   1.180 +    if (enemy_z<0.1) { enemy_z = 0.1; vz = speed*0.01*cimg::crand(); }
   1.181 +    if (enemy_z>0.7) { enemy_z = 0.7; vz = speed*0.01*cimg::crand(); }
   1.182 +    angle+=va;
   1.183 +
   1.184 +    // Handle mouse interaction
   1.185 +    if (!disp.button) {
   1.186 +      if (tomato_z==0) {
   1.187 +        tomato_x = mx; tomato_y = my;
   1.188 +      }
   1.189 +    } else tomato_z +=0.0001;
   1.190 +
   1.191 +    // Detect shooting
   1.192 +    if (cimg::abs(tomato_z-enemy_z)<0.1) {
   1.193 +      if (tomato_x>enemy_x-r_enemy.dimx()/2 && tomato_x<enemy_x+r_enemy.dimx()/2 &&
   1.194 +      tomato_y>enemy_y-r_enemy.dimy()/2 && tomato_y<enemy_y+r_enemy.dimy()/2) {
   1.195 +        score++;
   1.196 +        shooted = 1;
   1.197 +      }
   1.198 +    }
   1.199 +
   1.200 +    // Draw into canvas
   1.201 +    canvas = background;
   1.202 +    r_enemy = enemy.get_resize((int)(8+enemy.dimx()*(1-enemy_z)),(int)(8+enemy.dimy()*(1-enemy_z)),-100,-100);
   1.203 +    CImg<> rm_enemy = m_enemy.get_resize(r_enemy.dimx(),r_enemy.dimy());
   1.204 +    CImg<unsigned char> r_tomato  = tomato.get_resize((int)(8+tomato.dimx()*(1-tomato_z)),(int)(8+tomato.dimy()*(1-tomato_z)),-100,-100);
   1.205 +    CImg<> rm_tomato = m_tomato.get_resize(r_tomato.dimx(),r_tomato.dimy());
   1.206 +
   1.207 +    if (angle!=0) { r_enemy.rotate((float)angle,0,0); rm_enemy.rotate((float)angle,0,0); cimg_forXY(r_enemy,x,y) r_enemy(x,y,0) = (r_enemy(x,y,0)+255)/2; }
   1.208 +    r_enemy*=(1-(enemy_z-0.1)/1.6);
   1.209 +    r_tomato*=(1-tomato_z/1.6);
   1.210 +    rm_enemy*=(1-(enemy_z-0.1)/1.6);
   1.211 +
   1.212 +    if (enemy_z>tomato_z) {
   1.213 +      canvas.draw_image((int)(enemy_x - r_enemy.dimx()/2),
   1.214 +                        (int)(enemy_y - r_enemy.dimy()/2),
   1.215 +                        r_enemy,rm_enemy);
   1.216 +      if (tomato_x>=0) canvas.draw_image(tomato_x - r_tomato.dimx()/2,
   1.217 +                                         tomato_y - r_tomato.dimy()/2,
   1.218 +                                         r_tomato,rm_tomato);
   1.219 +    }
   1.220 +    else {
   1.221 +      if (tomato_x>=0) canvas.draw_image(tomato_x - r_tomato.dimx()/2,
   1.222 +                                         tomato_y - r_tomato.dimy()/2,
   1.223 +                                         r_tomato,rm_tomato);
   1.224 +      canvas.draw_image((int)(enemy_x - r_enemy.dimx()/2),
   1.225 +                        (int)(enemy_y - r_enemy.dimy()/2),
   1.226 +                        r_enemy,rm_enemy);
   1.227 +    }
   1.228 +    canvas.draw_text(1,1," Time left %d, Score = %d",white,0,0.5f,24,timeleft,score);
   1.229 +    disp.resize(disp).display(canvas).wait(25);
   1.230 +    if (disp.is_key(cimg::keyCTRLLEFT,cimg::keyF,true)) disp.toggle_fullscreen();
   1.231 +  }
   1.232 +
   1.233 +  std::fprintf(stderr,"\n\n YOUR SCORE : %d\n\n\n",score);
   1.234 +
   1.235 +  return 0;
   1.236 +}