blob: cfa35170f5242886096dde01668930bd9bd274a3 [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001/*
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.eu )
9 #
10 # Copyright : David Tschumperle
11 # ( http://tschumperle.users.greyc.fr/ )
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*/
43
44#include "img/odykill.h"
45#include "CImg.h"
46using namespace cimg_library;
47
48// Main procedure
49//----------------
50int main(int argc,char **argv) {
51
52 // Create game graphics
53 CImg<unsigned char> graphics[21] = {
54 CImg<unsigned char>(data_tomato,100,100,1,3,false),
55 CImg<unsigned char>(data_heart,100,100,1,3,false),
56 CImg<unsigned char>(data_dynamite,100,100,1,3,false),
57 CImg<unsigned char>(data_brain,100,100,1,3,false),
58 CImg<unsigned char>(data_cdrom,100,100,1,3,false),
59 CImg<unsigned char>(data_enemy,113,150,1,3,false),
60 CImg<unsigned char>(data_enemy2,116,155,1,3,false),
61 CImg<unsigned char>(data_enemy3,104,134,1,3,false),
62 CImg<unsigned char>(data_enemy4,141,151,1,3,false),
63 CImg<unsigned char>(data_enemy5,140,152,1,3,false),
64 CImg<unsigned char>(data_enemy6,131,156,1,3,false),
65 CImg<unsigned char>(data_enemy7,114,125,1,3,false),
66 CImg<unsigned char>(data_enemy8,97,125,1,3,false),
67 CImg<unsigned char>(data_enemy9,143,134,1,3,false),
68 CImg<unsigned char>(data_enemy10,158,214,1,3,false),
69 CImg<unsigned char>(data_enemy11,131,168,1,3,false),
70 CImg<unsigned char>(data_enemy12,114,138,1,3,false),
71 CImg<unsigned char>(data_enemy13,144,144,1,3,false),
72 CImg<unsigned char>(data_enemy14,132,153,1,3,false),
73 CImg<unsigned char>(data_enemy15,152,151,1,3,false),
74 CImg<unsigned char>(data_enemy16,139,185,1,3,false),
75 };
76 CImg<> masks[21];
77 const unsigned char black[] = { 0,0,0 }, white[] = { 255,255,255 };
78
79 // Display weapon selection menu
80 CImg<unsigned char> back0(640,480,1,3), title(data_title,294,94,1,3,true), choose(data_choose,524,49,1,3,true);
81 back0.fill(0).draw_image(back0.width()/2 - title.width()/2,30,title).
82 draw_image(back0.width()/2 - choose.width()/2,150,choose);
83 CImgDisplay disp(back0,"OdyKill");
84 int weapon=-1;
85
86 while (!disp.is_closed() && !disp.button()) {
87 weapon = -1;
88 for (int k=0; k<5; k++) {
89 const int mx = disp.mouse_x(), my = disp.mouse_y();
90 if (!((mx - 40)/110==k && my>250 && my<350)) back0.draw_image(40 + k*110,250,graphics[k]/2.0);
91 else back0.draw_image(40 + k*110,250,graphics[weapon=k]);
92 }
93 CImg<unsigned char> tmp = CImg<unsigned char>().draw_text(0,0,
94 weapon==0?" Tomato ":
95 weapon==1?" Heart ":
96 weapon==2?" Dynamite ":
97 weapon==3?" Brain ":
98 weapon==4?" CD-Rom ":
99 " ",white,black,1,32).resize(-100,-100,1,1),
100 tmp2 = tmp.get_blur(6).normalize(0,255).draw_image(tmp,0.5f);
101 cimg_forC(back0,k) back0.draw_image(250,390,0,k,tmp2);
102
103 disp.resize(disp).display(back0).wait();
104 if (disp.is_keyCTRLLEFT() && disp.is_keyF()) disp.toggle_fullscreen();
105 if (disp.is_closed() || disp.is_keyQ() || disp.is_keyESC()) std::exit(0);
106 }
107 disp.hide_mouse();
108
109 /*---------------------------------
110
111 Go !
112
113 --------------------------------*/
114
115 const CImg<unsigned char>
116 background = CImg<unsigned char>(100,100,1,3,0).noise(100,2).draw_plasma().
117 resize(back0.width(),back0.height(),1,3,5)/2.5;
118 for (unsigned int k = 0; k<21; k++) {
119 CImg<> tmp = graphics[k].resize(k<5?32:164,k<5?32:164,1,3);
120 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;
121 masks[k] = tmp.get_channel(0);
122 graphics[k].resize(k<5?32:164,k<5?32:164,1,3,5);
123 }
124
125 CImg<unsigned char> canvas(background);
126 int n = 5 + ((int)(200*cimg::rand())%16);
127 CImg<unsigned char> tomato = graphics[weapon], enemy = graphics[n];
128 CImg<> m_tomato = masks[weapon], m_enemy = masks[n];
129
130 double angle = 0;
131 int tomato_x = 0,tomato_y = 0,shooted = 0;
132 double enemy_x = -1000, enemy_y = -1000, enemy_z = -1000, tomato_z = 0, vx = 0, vy = 0, vz = 0, va = 0;
133 double speed = cimg_option("-speed",5.0,"Speed");
134 int timeleft = 2000, score = 0;
135 CImg<unsigned char> r_enemy;
136
137 // Main loop
138 while (timeleft && !disp.is_closed() && !disp.is_keyESC() && !disp.is_keyQ()) {
139 --timeleft;
140 const int mx = disp.mouse_x()*back0.width()/disp.width(), my = disp.mouse_y()*back0.height()/disp.height();
141
142 // Handle object motion
143 if (tomato_z>0) {
144 tomato_z+=0.07; tomato_y -= (int)(20*std::cos(cimg::PI/7 + tomato_z*cimg::PI));
145 if (tomato_z>=1) { tomato_z=0; tomato_x = mx; tomato_y = my; }
146 }
147 if (!shooted) { enemy_x +=vx; enemy_y +=vy; enemy_z +=vz; }
148 else {
149 va = 10;
150 enemy_y += vy;
151 vy += 2;
152 tomato_z = 0;
153 if (enemy_y>5*canvas.height()/4) {
154 shooted = 0;
155 int n = 5 + ((int)(200*cimg::rand())%16);
156 enemy = graphics[n];
157 m_enemy = masks[n];
158 enemy_x=cimg::rand(-1,1)*1e8; enemy_y=cimg::rand(-1,1)*1e8; enemy_z=cimg::rand(-1,1)*1e8;
159 va = angle = 0;
160 }
161 }
162
163 if (enemy_x<0) { enemy_x=0; vx = speed*cimg::rand(-1,1); }
164 if (enemy_x>canvas.width()) { enemy_x=canvas.width(); vx = speed*cimg::rand(-1,1); }
165 if (enemy_y<0) { enemy_y=0; vy = speed*cimg::rand(-1,1); }
166 if (!shooted && enemy_y>canvas.height()) { enemy_y=canvas.height(); vy = speed*cimg::rand(-1,1); }
167 if (enemy_z<0.1) { enemy_z = 0.1; vz = speed*0.01*cimg::rand(-1,1); }
168 if (enemy_z>0.7) { enemy_z = 0.7; vz = speed*0.01*cimg::rand(-1,1); }
169 angle+=va;
170
171 // Handle mouse interaction
172 if (!disp.button()) {
173 if (tomato_z==0) {
174 tomato_x = mx; tomato_y = my;
175 }
176 } else tomato_z +=0.0001;
177
178 // Detect shooting
179 if (cimg::abs(tomato_z - enemy_z)<0.1) {
180 if (tomato_x>enemy_x - r_enemy.width()/2 && tomato_x<enemy_x + r_enemy.width()/2 &&
181 tomato_y>enemy_y - r_enemy.height()/2 && tomato_y<enemy_y + r_enemy.height()/2) {
182 score++;
183 shooted = 1;
184 }
185 }
186
187 // Draw into canvas
188 canvas = background;
189 r_enemy = enemy.get_resize((int)(8 + enemy.width()*(1 - enemy_z)),
190 (int)(8 + enemy.height()*(1 - enemy_z)),-100,-100);
191 CImg<> rm_enemy = m_enemy.get_resize(r_enemy.width(),r_enemy.height());
192 CImg<unsigned char> r_tomato = tomato.get_resize((int)(8 + tomato.width()*(1 - tomato_z)),
193 (int)(8 + tomato.height()*(1 - tomato_z)),-100,-100);
194 CImg<> rm_tomato = m_tomato.get_resize(r_tomato.width(),r_tomato.height());
195
196 if (angle!=0) {
197 r_enemy.rotate((float)angle,0,0);
198 rm_enemy.rotate((float)angle,0,0);
199 cimg_forXY(r_enemy,x,y) r_enemy(x,y,0) = (r_enemy(x,y,0) + 255)/2;
200 }
201 r_enemy*=(1 - (enemy_z - 0.1)/1.6);
202 r_tomato*=(1 - tomato_z/1.6);
203 rm_enemy*=(1 - (enemy_z - 0.1)/1.6);
204
205 if (enemy_z>tomato_z) {
206 canvas.draw_image((int)(enemy_x - r_enemy.width()/2),
207 (int)(enemy_y - r_enemy.height()/2),
208 r_enemy,rm_enemy);
209 if (tomato_x>=0) canvas.draw_image(tomato_x - r_tomato.width()/2,
210 tomato_y - r_tomato.height()/2,
211 r_tomato,rm_tomato);
212 }
213 else {
214 if (tomato_x>=0) canvas.draw_image(tomato_x - r_tomato.width()/2,
215 tomato_y - r_tomato.height()/2,
216 r_tomato,rm_tomato);
217 canvas.draw_image((int)(enemy_x - r_enemy.width()/2),
218 (int)(enemy_y - r_enemy.height()/2),
219 r_enemy,rm_enemy);
220 }
221 canvas.draw_text(1,1," Time left %d, Score = %d",white,0,0.5f,24,timeleft,score);
222 disp.resize(disp).display(canvas).wait(25);
223 if (disp.is_keyCTRLLEFT() && disp.is_keyF()) disp.toggle_fullscreen();
224 }
225
226 std::fprintf(stderr,"\n\n YOUR SCORE : %d\n\n\n",score);
227
228 return 0;
229}