]> git.jsancho.org Git - lugaru.git/blob - Source/Sprite.cpp
f40533155c5c0e44638970af79f3a4dd8fd976f5
[lugaru.git] / Source / Sprite.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 #include "Sprite.h"
23 #include "Person.h"
24 #include "Game.h"
25 extern XYZ viewer;
26 extern float viewdistance;
27 extern float fadestart;
28 extern int environment;
29 extern float texscale;
30 extern Light light;
31 extern float multiplier;
32 extern float gravity;
33 extern Terrain terrain;
34 extern Objects objects;
35 extern int detail;
36 extern XYZ viewerfacing;
37 extern int bloodtoggle;
38 extern XYZ windvector;
39
40 // init statics
41 Texture Sprite::cloudtexture;
42 Texture Sprite::cloudimpacttexture;
43 Texture Sprite::bloodtexture;
44 Texture Sprite::flametexture;
45 Texture Sprite::bloodflametexture;
46 Texture Sprite::smoketexture;
47 Texture Sprite::snowflaketexture;
48 Texture Sprite::shinetexture;
49 Texture Sprite::splintertexture;
50 Texture Sprite::leaftexture;
51 Texture Sprite::toothtexture;
52
53 float Sprite::checkdelay = 0;
54
55 vector<Sprite*> Sprite::sprites = vector<Sprite*>();
56
57 //Functions
58 void Sprite::Draw()
59 {
60     int i, j, k;
61     static float M[16];
62     static XYZ point;
63     static float distancemult;
64     static int lasttype;
65     static int lastspecial;
66     static int whichpatchx, whichpatchz;
67     static XYZ start, end, colpoint;
68     static bool check;
69     static bool blend;
70     static float tempmult;
71     static XYZ difference;
72     static float lightcolor[3];
73     static float viewdistsquared = viewdistance * viewdistance;
74     static XYZ tempviewer;
75
76     tempviewer = viewer + viewerfacing * 6;
77     check = 0;
78
79     lightcolor[0] = light.color[0] * .5 + light.ambient[0];
80     lightcolor[1] = light.color[1] * .5 + light.ambient[1];
81     lightcolor[2] = light.color[2] * .5 + light.ambient[2];
82
83     checkdelay -= multiplier * 10;
84
85     if (checkdelay <= 0) {
86         check = 1;
87         checkdelay = 1;
88     }
89
90     lasttype = -1;
91     lastspecial = -1;
92     glEnable(GL_BLEND);
93     glDisable(GL_LIGHTING);
94     glDisable(GL_CULL_FACE);
95     glEnable(GL_TEXTURE_2D);
96     blend = 1;
97     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98     glDepthMask(0);
99     glAlphaFunc(GL_GREATER, 0.0001);
100     for (i = 0; i < sprites.size(); i++) {
101         if (lasttype != sprites[i]->type) {
102             switch (sprites[i]->type) {
103             case cloudsprite:
104                 cloudtexture.bind();
105                 if (!blend) {
106                     blend = 1;
107                     glAlphaFunc(GL_GREATER, 0.0001);
108                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
109                 }
110                 break;
111             case cloudimpactsprite:
112                 cloudimpacttexture.bind();
113                 if (!blend) {
114                     blend = 1;
115                     glAlphaFunc(GL_GREATER, 0.0001);
116                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117                 }
118                 break;
119             case breathsprite:
120                 cloudimpacttexture.bind();
121                 if (!blend) {
122                     blend = 1;
123                     glAlphaFunc(GL_GREATER, 0.0001);
124                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125                 }
126                 break;
127             case smoketype:
128                 smoketexture.bind();
129                 if (!blend) {
130                     blend = 1;
131                     glAlphaFunc(GL_GREATER, 0.0001);
132                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
133                 }
134                 break;
135             case bloodsprite:
136                 bloodtexture.bind();
137                 if (!blend) {
138                     blend = 1;
139                     glAlphaFunc(GL_GREATER, 0.0001);
140                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141                 }
142                 break;
143             case splintersprite :
144                 if (lastspecial != sprites[i]->special) {
145                     if (sprites[i]->special == 0)
146                         splintertexture.bind();
147                     if (sprites[i]->special == 1)
148                         leaftexture.bind();
149                     if (sprites[i]->special == 2)
150                         snowflaketexture.bind();
151                     if (sprites[i]->special == 3)
152                         toothtexture.bind();
153                     if (!blend) {
154                         blend = 1;
155                         glAlphaFunc(GL_GREATER, 0.0001);
156                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
157                     }
158                 }
159                 break;
160             case snowsprite:
161                 snowflaketexture.bind();
162                 if (!blend) {
163                     blend = 1;
164                     glAlphaFunc(GL_GREATER, 0.0001);
165                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
166                 }
167                 break;
168             case weaponshinesprite:
169                 shinetexture.bind();
170                 if (blend) {
171                     blend = 0;
172                     glAlphaFunc(GL_GREATER, 0.001);
173                     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
174                 }
175                 break;
176             case flamesprite:
177             case weaponflamesprite:
178                 flametexture.bind();
179                 if (blend || lasttype == bloodflamesprite) {
180                     blend = 0;
181                     glAlphaFunc(GL_GREATER, 0.3);
182                     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
183                 }
184                 break;
185             case bloodflamesprite:
186                 bloodflametexture.bind();
187                 if (blend) {
188                     blend = 0;
189                     glAlphaFunc(GL_GREATER, 0.3);
190                     glBlendFunc(GL_ONE, GL_ZERO);
191                 }
192                 break;
193             }
194         }
195         if (sprites[i]->type == snowsprite)
196             distancemult = (144 - (distsq(&tempviewer, &sprites[i]->position) - (144 * fadestart)) * (1 / (1 - fadestart))) / 144;
197         else
198             distancemult = (viewdistsquared - (distsq(&viewer, &sprites[i]->position) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
199         if (sprites[i]->type == flamesprite) {
200             if (distancemult >= 1)
201                 glColor4f(sprites[i]->color[0], sprites[i]->color[1], sprites[i]->color[2], sprites[i]->opacity);
202             else
203                 glColor4f(sprites[i]->color[0], sprites[i]->color[1], sprites[i]->color[2], sprites[i]->opacity * distancemult);
204         } else {
205             if (distancemult >= 1)
206                 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity);
207             else
208                 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * distancemult);
209         }
210         lasttype = sprites[i]->type;
211         lastspecial = sprites[i]->special;
212         glMatrixMode(GL_MODELVIEW);
213         glPushMatrix();
214         glTranslatef(sprites[i]->position.x, sprites[i]->position.y, sprites[i]->position.z);
215         if ((sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite)) {
216             difference = viewer - sprites[i]->position;
217             Normalise(&difference);
218             glTranslatef(difference.x * sprites[i]->size / 4, difference.y * sprites[i]->size / 4, difference.z * sprites[i]->size / 4);
219         }
220         if (sprites[i]->type == snowsprite) {
221             glRotatef(sprites[i]->rotation * .2, 0, .3, 1);
222             glTranslatef(1, 0, 0);
223         }
224         glGetFloatv(GL_MODELVIEW_MATRIX, M);
225         point.x = M[12];
226         point.y = M[13];
227         point.z = M[14];
228         glLoadIdentity();
229         glTranslatef(point.x, point.y, point.z);
230
231         glRotatef(sprites[i]->rotation, 0, 0, 1);
232
233         if ((sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == bloodflamesprite)) {
234             if (sprites[i]->alivetime < .14)
235                 glScalef(sprites[i]->alivetime / .14, sprites[i]->alivetime / .14, sprites[i]->alivetime / .14);
236         }
237         if (sprites[i]->type == smoketype || sprites[i]->type == snowsprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == breathsprite) {
238             if (sprites[i]->alivetime < .3) {
239                 if (distancemult >= 1)
240                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * sprites[i]->alivetime / .3);
241                 if (distancemult < 1)
242                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * distancemult * sprites[i]->alivetime / .3);
243             }
244         }
245         if (sprites[i]->type == splintersprite && sprites[i]->special > 0 && sprites[i]->special != 3) {
246             if (sprites[i]->alivetime < .2) {
247                 if (distancemult >= 1)
248                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->alivetime / .2);
249                 else
250                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], distancemult * sprites[i]->alivetime / .2);
251             } else {
252                 if (distancemult >= 1)
253                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
254                 else
255                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
256             }
257         }
258         if (sprites[i]->type == splintersprite && (sprites[i]->special == 0 || sprites[i]->special == 3)) {
259             if (distancemult >= 1)
260                 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
261             else
262                 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
263         }
264
265         glBegin(GL_TRIANGLES);
266         glTexCoord2f(1.0f, 1.0f);
267         glVertex3f( .5 * sprites[i]->size, .5 * sprites[i]->size, 0.0f);
268         glTexCoord2f(0.0f, 1.0f);
269         glVertex3f(-.5 * sprites[i]->size, .5 * sprites[i]->size, 0.0f);
270         glTexCoord2f(1.0f, 0.0f);
271         glVertex3f( .5 * sprites[i]->size, -.5 * sprites[i]->size, 0.0f);
272         glTexCoord2f(0.0f, 0.0f);
273         glVertex3f(-.5 * sprites[i]->size, -.5 * sprites[i]->size, 0.0f);
274         glTexCoord2f(1.0f, 0.0f);
275         glVertex3f( .5 * sprites[i]->size, -.5 * sprites[i]->size, 0.0f);
276         glTexCoord2f(0.0f, 1.0f);
277         glVertex3f(-.5 * sprites[i]->size, .5 * sprites[i]->size, 0.0f);
278         glEnd();
279         glPopMatrix();
280     }
281     tempmult = multiplier;
282     for (i = sprites.size() - 1; i >= 0; i--) {
283         multiplier = tempmult;
284         if (sprites[i]->type != snowsprite) {
285             sprites[i]->position += sprites[i]->velocity * multiplier;
286             sprites[i]->velocity += windvector * multiplier;
287         }
288         if (sprites[i]->type == flamesprite || sprites[i]->type == smoketype)
289             sprites[i]->position += windvector * multiplier / 2;
290         if ((sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == bloodflamesprite))
291             multiplier *= sprites[i]->speed * .7;
292         sprites[i]->alivetime += multiplier;
293
294         if (sprites[i]->type == cloudsprite || sprites[i]->type == cloudimpactsprite) {
295             sprites[i]->opacity -= multiplier / 2;
296             sprites[i]->size += multiplier / 2;
297             sprites[i]->velocity.y += gravity * multiplier * .25;
298         }
299         if (sprites[i]->type == breathsprite) {
300             sprites[i]->opacity -= multiplier / 2;
301             sprites[i]->size += multiplier / 2;
302             if (findLength(&sprites[i]->velocity) <= multiplier) {
303                 sprites[i]->velocity = 0;
304             } else {
305                 XYZ slowdown;
306                 slowdown = sprites[i]->velocity * -1;
307                 Normalise(&slowdown);
308                 slowdown *= multiplier;
309                 sprites[i]->velocity += slowdown;
310             }
311         }
312         if (sprites[i]->type == snowsprite) {
313             sprites[i]->size -= multiplier / 120;
314             sprites[i]->rotation += multiplier * 360;
315             sprites[i]->position.y -= multiplier;
316             sprites[i]->position += windvector * multiplier;
317             if (sprites[i]->position.y < tempviewer.y - 6) sprites[i]->position.y += 12;
318             if (sprites[i]->position.y > tempviewer.y + 6) sprites[i]->position.y -= 12;
319             if (sprites[i]->position.z < tempviewer.z - 6) sprites[i]->position.z += 12;
320             if (sprites[i]->position.z > tempviewer.z + 6) sprites[i]->position.z -= 12;
321             if (sprites[i]->position.x < tempviewer.x - 6) sprites[i]->position.x += 12;
322             if (sprites[i]->position.x > tempviewer.x + 6) sprites[i]->position.x -= 12;
323         }
324         if (sprites[i]->type == bloodsprite) {
325             bool spritehit = 0;
326             sprites[i]->rotation += multiplier * 100;
327             sprites[i]->velocity.y += gravity * multiplier;
328             if (check) {
329                 XYZ where, startpoint, endpoint, movepoint, footpoint;
330                 float rotationpoint;
331                 int whichtri;
332
333                 for (j = 0; j < numplayers; j++) {
334                     if (!spritehit && player[j].dead && sprites[i]->alivetime > .1) {
335                         where = sprites[i]->oldposition;
336                         where -= player[j].coords;
337                         if (!player[j].skeleton.free)
338                             where = DoRotation(where, 0, -player[j].yaw, 0);
339                         startpoint = where;
340                         where = sprites[i]->position;
341                         where -= player[j].coords;
342                         if (!player[j].skeleton.free)
343                             where = DoRotation(where, 0, -player[j].yaw, 0);
344                         endpoint = where;
345
346                         movepoint = 0;
347                         rotationpoint = 0;
348                         whichtri = player[j].skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &footpoint, &movepoint, &rotationpoint);
349                         if (whichtri != -1) {
350                             spritehit = 1;
351                             player[j].DoBloodBigWhere(0, 160, sprites[i]->oldposition);
352                             DeleteSprite(i);
353                         }
354                     }
355                 }
356
357                 whichpatchx = sprites[i]->position.x / (terrain.size / subdivision * terrain.scale);
358                 whichpatchz = sprites[i]->position.z / (terrain.size / subdivision * terrain.scale);
359                 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision)
360                     if (terrain.patchobjectnum[whichpatchx][whichpatchz]) {
361                         if (!spritehit)
362                             for (j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) {
363                                 k = terrain.patchobjects[whichpatchx][whichpatchz][j];
364                                 start = sprites[i]->oldposition;
365                                 end = sprites[i]->position;
366                                 if (!spritehit)
367                                     if (objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]) != -1) {
368                                         if (detail == 2 || (detail == 1 && abs(Random() % 4) == 0) || (detail == 0 && abs(Random() % 8) == 0))
369                                             objects.model[k].MakeDecal(blooddecalfast, DoRotation(colpoint - objects.position[k], 0, -objects.yaw[k], 0), sprites[i]->size * 1.6/*+abs((float)(Random()%100))/2400*/, .5, Random() % 360);
370                                         DeleteSprite(i);
371                                         spritehit = 1;
372                                     }
373                             }
374                     }
375                 if (!spritehit)
376                     if (sprites[i]->position.y < terrain.getHeight(sprites[i]->position.x, sprites[i]->position.z)) {
377                         terrain.MakeDecal(blooddecalfast, sprites[i]->position, sprites[i]->size * 1.6/*+abs((float)(Random()%100))/2400*/, .6, Random() % 360);
378                         DeleteSprite(i);
379                     }
380             }
381         }
382         if (sprites[i]->type == splintersprite) {
383             sprites[i]->rotation += sprites[i]->rotatespeed * multiplier;
384             sprites[i]->opacity -= multiplier / 2;
385             if (sprites[i]->special == 0 || sprites[i]->special == 2 || sprites[i]->special == 3)
386                 sprites[i]->velocity.y += gravity * multiplier;
387             if (sprites[i]->special == 1)
388                 sprites[i]->velocity.y += gravity * multiplier * .5;
389         }
390         if (sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == bloodflamesprite) {
391             sprites[i]->rotation += multiplier * sprites[i]->rotatespeed;
392             sprites[i]->opacity -= multiplier * 5 / 4;
393             if (sprites[i]->type != weaponshinesprite && sprites[i]->type != bloodflamesprite)
394                 if (sprites[i]->opacity < .5 && sprites[i]->opacity + multiplier * 5 / 4 >= .5 && (abs(Random() % 4) == 0 || (sprites[i]->initialsize > 2 && Random() % 2 == 0)))
395                     MakeSprite(smoketype, sprites[i]->position, sprites[i]->velocity, .9, .9, .6, sprites[i]->size * 1.2, .4);
396             if (sprites[i]->alivetime > .14 && (sprites[i]->type == flamesprite)) {
397                 sprites[i]->velocity = 0;
398                 sprites[i]->velocity.y = 1.5;
399             }
400         }
401         if (sprites[i]->type == smoketype) {
402             sprites[i]->opacity -= multiplier / 3 / sprites[i]->initialsize;
403             sprites[i]->color[0] -= multiplier;
404             sprites[i]->color[1] -= multiplier;
405             sprites[i]->color[2] -= multiplier;
406             if (sprites[i]->color[0] < .6)
407                 sprites[i]->color[0] = .6;
408             if (sprites[i]->color[1] < .6)
409                 sprites[i]->color[1] = .6;
410             if (sprites[i]->color[2] < .6)
411                 sprites[i]->color[2] = .6;
412             sprites[i]->size += multiplier;
413             sprites[i]->velocity = 0;
414             sprites[i]->velocity.y = 1.5;
415             sprites[i]->rotation += multiplier * sprites[i]->rotatespeed / 5;
416         }
417         if (sprites[i]->opacity <= 0 || sprites[i]->size <= 0)
418             DeleteSprite(i);
419     }
420     if (check)
421         for (i = sprites.size() - 1; i >= 0; i--) {
422             sprites[i]->oldposition = sprites[i]->position;
423         }
424     glAlphaFunc(GL_GREATER, 0.0001);
425     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
426 }
427
428 void Sprite::DeleteSprite(int i)
429 {
430     sprites.erase(sprites.begin() + i);
431 }
432
433 void Sprite::MakeSprite(int atype, XYZ where, XYZ avelocity, float red, float green, float blue, float asize, float aopacity)
434 {
435     if (sprites.size() < max_sprites - 1) {
436         sprites.push_back(new Sprite());
437         if ((atype != bloodsprite && atype != bloodflamesprite) || bloodtoggle) {
438             sprites.back()->special = 0;
439             sprites.back()->type = atype;
440             sprites.back()->position = where;
441             sprites.back()->oldposition = where;
442             sprites.back()->velocity = avelocity;
443             sprites.back()->alivetime = 0;
444             sprites.back()->opacity = aopacity;
445             sprites.back()->size = asize;
446             sprites.back()->initialsize = asize;
447             sprites.back()->color[0] = red;
448             sprites.back()->color[1] = green;
449             sprites.back()->color[2] = blue;
450             sprites.back()->rotatespeed = abs(Random() % 720) - 360;
451             sprites.back()->speed = float(abs(Random() % 100)) / 200 + 1.5;
452         }
453     }
454 }
455
456 Sprite::Sprite()
457 {
458     oldposition = 0;
459     position = 0;
460     velocity = 0;
461     size = 0;
462     initialsize = 0;
463     type = 0;
464     special = 0;
465     memset(color, 0, sizeof(color));
466     opacity = 0;
467     rotation = 0;
468     alivetime = 0;
469     speed = 0;
470     rotatespeed = 0;
471 }
472
473 void Sprite::clearTextures()
474 {
475     toothtexture.destroy();
476     cloudtexture.destroy();
477     cloudimpacttexture.destroy();
478     bloodtexture.destroy();
479     flametexture.destroy();
480     bloodflametexture.destroy();
481     smoketexture.destroy();
482     snowflaketexture.destroy();
483     shinetexture.destroy();
484     splintertexture.destroy();
485     leaftexture.destroy();
486 }
487