PTdecode/CImg-1.3.0/examples/odykill.cpp

Wed, 05 Aug 2009 17:10:56 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 05 Aug 2009 17:10:56 +0100
changeset 17
cf9d239ac1c9
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

add README

     1 /*
     2  #
     3  #  File        : odykill.cpp
     4  #                ( C++ source file )
     5  #
     6  #  Description : Simple shoot-em-up game featuring the Robotvis/Odyssee Team !
     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 "img/odykill.h"
    45 #include "CImg.h"
    46 using namespace cimg_library;
    48 // The lines below are necessary when using a non-standard compiler as visualcpp6.
    49 #ifdef cimg_use_visualcpp6
    50 #define std
    51 #endif
    52 #ifdef min
    53 #undef min
    54 #undef max
    55 #endif
    57 int main(int argc,char **argv) {
    59   // Create game graphics
    60   CImg<unsigned char> graphics[21] = {
    61     CImg<unsigned char>(data_tomato,100,100,1,3,false),
    62     CImg<unsigned char>(data_heart,100,100,1,3,false),
    63     CImg<unsigned char>(data_dynamite,100,100,1,3,false),
    64     CImg<unsigned char>(data_brain,100,100,1,3,false),
    65     CImg<unsigned char>(data_cdrom,100,100,1,3,false),
    66     CImg<unsigned char>(data_enemy,113,150,1,3,false),
    67     CImg<unsigned char>(data_enemy2,116,155,1,3,false),
    68     CImg<unsigned char>(data_enemy3,104,134,1,3,false),
    69     CImg<unsigned char>(data_enemy4,141,151,1,3,false),
    70     CImg<unsigned char>(data_enemy5,140,152,1,3,false),
    71     CImg<unsigned char>(data_enemy6,131,156,1,3,false),
    72     CImg<unsigned char>(data_enemy7,114,125,1,3,false),
    73     CImg<unsigned char>(data_enemy8,97,125,1,3,false),
    74     CImg<unsigned char>(data_enemy9,143,134,1,3,false),
    75     CImg<unsigned char>(data_enemy10,158,214,1,3,false),
    76     CImg<unsigned char>(data_enemy11,131,168,1,3,false),
    77     CImg<unsigned char>(data_enemy12,114,138,1,3,false),
    78     CImg<unsigned char>(data_enemy13,144,144,1,3,false),
    79     CImg<unsigned char>(data_enemy14,132,153,1,3,false),
    80     CImg<unsigned char>(data_enemy15,152,151,1,3,false),
    81     CImg<unsigned char>(data_enemy16,139,185,1,3,false),
    82   };
    83   CImg<> masks[21];
    84   const unsigned char black[] = { 0,0,0 }, white[] = { 255,255,255 };
    86   // Display weapon selection menu
    87   CImg<unsigned char> back0(640,480,1,3), title(data_title,294,94,1,3,true), choose(data_choose,524,49,1,3,true);
    88   back0.fill(0).draw_image(back0.dimx()/2-title.dimx()/2,30,title).draw_image(back0.dimx()/2-choose.dimx()/2,150,choose);
    89   CImgDisplay disp(back0,"OdyKill");
    90   int weapon=-1;
    92   while (!disp.is_closed && !disp.button) {
    93     weapon = -1;
    94     for (int k=0; k<5; k++) {
    95       const int mx = disp.mouse_x, my = disp.mouse_y;
    96       if (!((mx-40)/110==k && my>250 && my<350)) back0.draw_image(40+k*110,250,graphics[k]/2.0);
    97       else back0.draw_image(40+k*110,250,graphics[weapon=k]);
    98     }
    99     CImg<unsigned char> tmp = CImg<unsigned char>().draw_text(0,0,
   100                                                               weapon==0?" Tomato   ":
   101                                                               weapon==1?"  Heart   ":
   102                                                               weapon==2?" Dynamite ":
   103                                                               weapon==3?"  Brain   ":
   104                                                               weapon==4?"  CD-Rom  ":
   105                                                               "          ",white,black,1,32).resize(-100,-100,1,1),
   106       tmp2 = tmp.get_blur(6).normalize(0,255).draw_image(tmp,0.5f);
   107     { cimg_forV(back0,k) back0.draw_image(250,390,0,k,tmp2); }
   109     disp.resize(disp).display(back0).wait();
   110     if (disp.is_keyCTRLLEFT && disp.key==cimg::keyF) disp.toggle_fullscreen();
   111     if (disp.is_closed || disp.is_keyQ || disp.is_keyESC) std::exit(0);
   112   }
   113   disp.hide_mouse();
   115   /*---------------------------------
   117   Go !
   119   --------------------------------*/
   121   const CImg<unsigned char>
   122     background = CImg<unsigned char>(100,100,1,3,0).noise(100,2).draw_plasma(0,0,99,99).
   123     resize(back0.dimx(),back0.dimy(),1,3,5)/2.5;
   124   { for (unsigned int k=0; k<21; k++) {
   125     CImg<> tmp = graphics[k].resize(k<5?32:164,k<5?32:164,1,3);
   126     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;
   127     masks[k]=tmp.get_shared_channel(0);
   128     graphics[k].resize(k<5?32:164,k<5?32:164,1,3,5);
   129   }}
   131   CImg<unsigned char> canvas(background);
   132   int n = 5+((int)(200*cimg::rand())%16);
   133   CImg<unsigned char> tomato = graphics[weapon], enemy = graphics[n];
   134   CImg<> m_tomato = masks[weapon], m_enemy = masks[n];
   136   double angle=0;
   137   int tomato_x=0,tomato_y=0,shooted=0;
   138   double enemy_x=-1000, enemy_y=-1000, enemy_z=-1000, tomato_z = 0, vx = 0, vy = 0, vz = 0, va = 0;
   139   double speed = cimg_option("-speed",5.0,"Speed");
   140   int timeleft = 2000, score = 0;
   141   CImg<unsigned char> r_enemy;
   143   // Main loop
   144   while(timeleft && !disp.is_closed && !disp.is_keyESC && !disp.is_keyQ) {
   145     timeleft--;
   146     const int mx = disp.mouse_x*back0.dimx()/disp.dimx(), my = disp.mouse_y*back0.dimy()/disp.dimy();
   148     // Handle object motion
   149     if (tomato_z>0) {
   150       tomato_z+=0.07; tomato_y -= (int)(20*std::cos(cimg::valuePI/7 + tomato_z*cimg::valuePI));
   151       if (tomato_z>=1) { tomato_z=0; tomato_x = mx; tomato_y = my; }
   152     }
   153     if (!shooted) {
   154       enemy_x +=vx;
   155       enemy_y +=vy;
   156       enemy_z +=vz;
   157     }
   158     else {
   159       va = 10;
   160       enemy_y += vy;
   161       vy += 2;
   162       tomato_z = 0;
   163       if (enemy_y>5*canvas.dimy()/4) {
   164         shooted = 0;
   165         int n = 5 + ((int)(200*cimg::rand())%16);
   166         enemy = graphics[n];
   167         m_enemy = masks[n];
   168         enemy_x=cimg::crand()*1e8; enemy_y=cimg::crand()*1e8; enemy_z=cimg::crand()*1e8;
   169         va = angle = 0;
   170       }
   171     }
   173     if (enemy_x<0) { enemy_x=0; vx = speed*cimg::crand(); }
   174     if (enemy_x>canvas.dimx()) { enemy_x=canvas.dimx(); vx = speed*cimg::crand(); }
   175     if (enemy_y<0) { enemy_y=0; vy = speed*cimg::crand(); }
   176     if (!shooted && enemy_y>canvas.dimy()) { enemy_y=canvas.dimy(); vy = speed*cimg::crand(); }
   177     if (enemy_z<0.1) { enemy_z = 0.1; vz = speed*0.01*cimg::crand(); }
   178     if (enemy_z>0.7) { enemy_z = 0.7; vz = speed*0.01*cimg::crand(); }
   179     angle+=va;
   181     // Handle mouse interaction
   182     if (!disp.button) {
   183       if (tomato_z==0) {
   184         tomato_x = mx; tomato_y = my;
   185       }
   186     } else tomato_z +=0.0001;
   188     // Detect shooting
   189     if (cimg::abs(tomato_z-enemy_z)<0.1) {
   190       if (tomato_x>enemy_x-r_enemy.dimx()/2 && tomato_x<enemy_x+r_enemy.dimx()/2 &&
   191       tomato_y>enemy_y-r_enemy.dimy()/2 && tomato_y<enemy_y+r_enemy.dimy()/2) {
   192         score++;
   193         shooted = 1;
   194       }
   195     }
   197     // Draw into canvas
   198     canvas = background;
   199     r_enemy = enemy.get_resize((int)(8+enemy.dimx()*(1-enemy_z)),(int)(8+enemy.dimy()*(1-enemy_z)),-100,-100);
   200     CImg<> rm_enemy = m_enemy.get_resize(r_enemy.dimx(),r_enemy.dimy());
   201     CImg<unsigned char> r_tomato  = tomato.get_resize((int)(8+tomato.dimx()*(1-tomato_z)),(int)(8+tomato.dimy()*(1-tomato_z)),-100,-100);
   202     CImg<> rm_tomato = m_tomato.get_resize(r_tomato.dimx(),r_tomato.dimy());
   204     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; }
   205     r_enemy*=(1-(enemy_z-0.1)/1.6);
   206     r_tomato*=(1-tomato_z/1.6);
   207     rm_enemy*=(1-(enemy_z-0.1)/1.6);
   209     if (enemy_z>tomato_z) {
   210       canvas.draw_image((int)(enemy_x - r_enemy.dimx()/2),
   211                         (int)(enemy_y - r_enemy.dimy()/2),
   212                         r_enemy,rm_enemy);
   213       if (tomato_x>=0) canvas.draw_image(tomato_x - r_tomato.dimx()/2,
   214                                          tomato_y - r_tomato.dimy()/2,
   215                                          r_tomato,rm_tomato);
   216     }
   217     else {
   218       if (tomato_x>=0) canvas.draw_image(tomato_x - r_tomato.dimx()/2,
   219                                          tomato_y - r_tomato.dimy()/2,
   220                                          r_tomato,rm_tomato);
   221       canvas.draw_image((int)(enemy_x - r_enemy.dimx()/2),
   222                         (int)(enemy_y - r_enemy.dimy()/2),
   223                         r_enemy,rm_enemy);
   224     }
   225     canvas.draw_text(1,1," Time left %d, Score = %d",white,0,0.5f,24,timeleft,score);
   226     disp.resize(disp).display(canvas).wait(25);
   227     if (disp.is_key(cimg::keyCTRLLEFT,cimg::keyF,true)) disp.toggle_fullscreen();
   228   }
   230   std::fprintf(stderr,"\n\n YOUR SCORE : %d\n\n\n",score);
   232   return 0;
   233 }