corrected CMake build
[nespa.git] / src / e3d_sprite.cpp
blob1ba5ca4fbd4b2b79ecdaa2f7e359d5f5e46fcc00
1 #ifdef WIN32
2 #define WIN32_LEAN_AND_MEAN
3 #include <windows.h>
4 #endif
5 #if defined(__APPLE__) && defined(__MACH__)
6 #include <OpenGL/gl.h> // Header File For The OpenGL32 Library
7 #include <OpenGL/glu.h> // Header File For The GLu32 Library
8 #else
9 #include <GL/gl.h> // Header File For The OpenGL32 Library
10 #include <GL/glu.h> // Header File For The GLu32 Library
11 #endif
12 #include "SDL/SDL.h"
13 #include "SDL/SDL_image.h"
14 #include "SDL/SDL_timer.h"
15 #include "SDL/SDL_mixer.h"
17 #include "e3d_engine.h"
19 // Desde nespa.cpp; Callbacks
20 extern void callback_Checkpoint_onChange(int new_checkpoint);
21 extern void callback_Checkpoint_done(int checkpoint, int points_earned);
24 string e3D_Sprite::spritelist[MAX_SPRITE_LIMIT];
25 GLuint e3D_Sprite::maxsprite;
26 GLuint e3D_Sprite::sprite_shadow;
27 e3D_Checkpoint e3D_Checkpoint::*checkpointlist[128];
28 int e3D_Checkpoint::maxcheckpoint=0;
29 int e3D_Checkpoint::currentpoint=0;
32 e3D_Sprite::e3D_Sprite(e3D_Object *parent, const char *sprite) : e3D_Object(parent)
34 // Ship.LoadObj(fileobj,size);
35 // cout << "Create ship:" << fileobj << endl;
36 //matrixidentity(position);
37 //matrixidentity(inverse);
38 //matrixidentity(inverse_orientation);
39 GLuint i;
40 nsprite=0;
41 for (i=1;i<=this->maxsprite && i<MAX_SPRITE_LIMIT;i++)
43 if (this->spritelist[i]==sprite)
45 nsprite=i;
46 // cout << "Located sprite " << sprite << " onto " << nsprite << endl;
49 if (!nsprite) LoadSprite(sprite);
50 angle=0;
51 shadowed=0;
52 R=G=B=A=1;
54 init_physics();
57 e3D_Sprite::e3D_Sprite(e3D_Object *parent, const char *sprite, double size, double density) : e3D_Object(parent)
59 GLuint i;
60 nsprite=0;
61 shadowed=0;
62 for (i=1;i<=this->maxsprite && i<MAX_SPRITE_LIMIT;i++)
64 if (this->spritelist[i]==sprite)
66 nsprite=i;
69 if (!nsprite) LoadSprite(sprite);
70 angle=0;
71 R=G=B=A=1;
72 init_physics(size,density);
75 void e3D_Sprite::LoadShadow(const char *spritefile)
77 SDL_Surface *pixsprite;
78 pixsprite = IMG_Load(spritefile);
79 if (!pixsprite) {
80 cout << "Can't load sprite " << spritefile << " onto " << nsprite << endl;
81 return;}
83 // Create MipMapped Texture
84 GLuint texture;
85 glGenTextures(1, &texture);
86 if (texture)
88 glBindTexture(GL_TEXTURE_2D, texture);
89 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
90 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
91 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
92 pixsprite->w, pixsprite->h, GL_RGBA, GL_UNSIGNED_BYTE,
93 pixsprite->pixels);
96 sprite_shadow=texture;
97 spritelist[sprite_shadow]=spritefile;
98 if (maxsprite<sprite_shadow) maxsprite=sprite_shadow;
100 cout << "Loaded sprite " << spritefile << " onto " << texture << endl;
105 void e3D_Sprite::LoadSprite(const char *spritefile)
107 if (!this->sprite_shadow)
109 LoadShadow("images/shadow1.png");
111 SDL_Surface *pixsprite;
112 pixsprite = IMG_Load(spritefile);
113 if (!pixsprite) {
114 cout << "Can't load sprite " << spritefile << " onto " << nsprite << endl;
115 return;}
117 // Create MipMapped Texture
118 GLuint texture;
119 glGenTextures(1, &texture);
120 if (texture)
122 glBindTexture(GL_TEXTURE_2D, texture);
123 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
124 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
125 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
126 pixsprite->w, pixsprite->h, GL_RGBA, GL_UNSIGNED_BYTE,
127 pixsprite->pixels);
130 nsprite=texture;
131 spritelist[nsprite]=spritefile;
132 if (maxsprite<nsprite) maxsprite=nsprite;
133 if (__E3D_ENGINE_DEBUG) cout << "Loaded sprite " << spritefile << " onto " << nsprite << endl;
137 void e3D_Sprite::Draw(int pass=0)
140 glDisable(GL_LIGHTING);
141 glEnable(GL_BLEND);
142 //glDepthMask(GL_FALSE);
143 // glDepthMask(GL_TRUE);
145 glPushMatrix();
147 matriz M;
148 glGetDoublev(GL_MODELVIEW_MATRIX,M);
149 matrixresetrot(M);
150 glLoadMatrixd(M);
151 glRotated(angle,0,0,1);
152 if (shadowed)
154 glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,GL_SRC_COLOR); // Sustractivo
155 glBindTexture(GL_TEXTURE_2D, sprite_shadow);
156 glColor4f(1.0f,1.0f,1.0f,1.0f);
157 glBegin(GL_QUADS);
158 glNormal3f( 0.0f, 0.0f, -1.0f);
159 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
160 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
161 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
162 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
163 glEnd();
166 glBindTexture(GL_TEXTURE_2D, nsprite);
167 glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Aditivo
168 //Render->LoadCameraRot(Render->Camera);
169 glColor4f(R,G,B,A);
170 glBegin(GL_QUADS);
171 glNormal3f( 0.0f, 0.0f, -1.0f);
172 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
173 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
174 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
175 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
176 glEnd();
179 glPopMatrix();
183 void e3D_Shoot::Draw(int pass=0)
185 if (life<0) return;
186 ApplyGravityForces(Render->OG_Planets);
187 if (scale_x<max_size)
189 scale_x*=1+0.02/scale_x;
190 scale_y*=1+0.02/scale_y;
191 scale_z*=1+0.02/scale_z;
192 dBodyAddRelForce(body,0,0,-speed*(max_size-scale_x));
194 life-=1.0/60.0;
195 if (life<0)
197 AutoDeleteAfterDraw=1;
199 else if (life<3)
201 scale_x=max_size*(life)/3.0;
202 scale_y=max_size*(life)/3.0;
203 scale_z=max_size*(life)/3.0;
205 glBindTexture(GL_TEXTURE_2D, nsprite);
206 glDisable(GL_LIGHTING);
207 glEnable(GL_BLEND);
208 glDepthMask(GL_FALSE);
209 glDisable(GL_ALPHA_TEST);
211 glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Aditivo
213 glPushMatrix();
215 matriz M;
216 glGetDoublev(GL_MODELVIEW_MATRIX,M);
217 matrixresetrot(M);
218 glLoadMatrixd(M);
219 glRotated(angle,0,0,1);
220 //Render->LoadCameraRot(Render->Camera);
221 //glColor4f(1.0f,1.0f,1.0f,1.0f);
222 glColor4f(R,G,B,A);
223 glBegin(GL_QUADS);
224 glNormal3f( 0.0f, 0.0f, -1.0f);
225 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
226 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
227 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
228 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
229 glEnd();
230 glPopMatrix();
234 void e3D_Checkpoint::Draw(int pass)
236 e3D_Sprite::Draw(pass);
237 float d=1+sin(Render->App->total_time/120.0)/15.0;
238 if (cpoint!=currentpoint) d=1+sin(Render->App->total_time/240.0)/15.0;
239 glScaled(d,d,d);
240 e3D_Sprite::Draw(pass);
242 double Ax,Ay,Az;
243 double Bx,By,Bz;
244 Parent->GetXYZ(&Ax,&Ay,&Az);
245 Render->Ship1->GetXYZ(&Bx,&By,&Bz);
247 double ABx=Bx-Ax,ABy=By-Ay,ABz=Bz-Az;
248 double Rxy=sqrt(ABx*ABx+ABy*ABy);
249 double R2=Rxy*Rxy+ABz*ABz;
250 double R=sqrt(R2);
251 if (cpoint!=currentpoint && cpoint!=currentpoint-1)
253 this->A=(this->A*800+0)/801.0;
254 this->R=(this->R*30+1)/31.0;
255 this->G=(this->G*60+1)/61.0;
256 this->B=(this->B*120+1)/121.0;
258 else
260 this->A=(this->A*300+1)/301.0;
261 if (cpoint==currentpoint)
263 if (cpoint==0)
265 this->R=(this->R*100+0)/101.0;
266 this->G=(this->G*100+1)/101.0;
267 this->B=(this->B*100+0.5)/101.0;
269 else
271 this->R=(this->R*100+1)/101.0;
272 this->G=(this->G*100+2/scale_x)/101.0;
273 this->B=(this->B*100+1/scale_x)/101.0;
276 else
278 this->R=(this->R*100+0)/101.0;
279 this->G=(this->G*30+.2)/31.0;
280 this->B=(this->B*100+1)/101.0;
283 if (this->R>1) this->R=1;
284 if (this->G>1) this->G=1;
285 if (this->B>1) this->B=1;
286 if (this->A>1) this->A=1;
287 R-=8.0;
288 if (R<0.1) R=0.1;
289 if (R/planet_size<scale_x) {
290 if (cpoint==currentpoint)
292 currentpoint--;
293 callback_Checkpoint_onChange(currentpoint);
295 if (cpoint>currentpoint)
297 int score=sqrt(planet_size)*50.0*(scale_x-R/planet_size)/(R/planet_size);
298 total_score+=score;
299 Render->Ship1->score+=score;
300 if (Render->Ship1->used_fuel<0) Render->Ship1->used_fuel=0;
301 scale_x=scale_y=scale_z=R/planet_size;
303 } else if (R/planet_size>scale_x*3 && scale_x<2 && cpoint>currentpoint) {
305 if (total_score)
307 callback_Checkpoint_done(cpoint, total_score);
308 total_score=0;
310 scale_x+=0.001;
311 scale_y+=0.001;
312 scale_z+=0.001;
316 e3D_Checkpoint::e3D_Checkpoint(e3D_Object *parent) : e3D_Sprite(parent,"images/escudos.png")
318 R=1.0;
319 G=0.2;
320 B=0.0;
321 A=0.0;
322 total_score=0;
323 planet_size=Parent->scale_x;
324 float max_size=sqrt(planet_size)*1;
325 Scale(max_size,max_size,max_size);
326 //checkpointlist[maxcheckpoint]=this;
327 cpoint=currentpoint=maxcheckpoint;
328 maxcheckpoint++;