added newmap function to render modules (so explosions and other things are reset...
[divverent/darkplaces.git] / cl_effects.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22
23 #define MAX_EFFECTS 256
24
25 typedef struct effect_s
26 {
27         int active;
28         vec3_t origin;
29         float starttime;
30         float framerate;
31         int modelindex;
32         int startframe;
33         int endframe;
34         // these are for interpolation
35         int frame;
36         double frame1start;
37         double frame2start;
38 }
39 effect_t;
40
41 effect_t effect[MAX_EFFECTS];
42
43 cvar_t r_draweffects = {"r_draweffects", "1"};
44
45 void r_effects_start()
46 {
47         memset(effect, 0, sizeof(effect));
48 }
49
50 void r_effects_shutdown()
51 {
52 }
53
54 void r_effects_newmap()
55 {
56         memset(effect, 0, sizeof(effect));
57 }
58
59 void CL_Effects_Init()
60 {
61         Cvar_RegisterVariable(&r_draweffects);
62
63         R_RegisterModule("R_Effects", r_effects_start, r_effects_shutdown, r_effects_newmap);
64 }
65
66 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
67 {
68         int i;
69         effect_t *e;
70         if (!modelindex) // sanity check
71                 return;
72         for (i = 0, e = effect;i < MAX_EFFECTS;i++, e++)
73         {
74                 if (e->active)
75                         continue;
76                 e->active = true;
77                 VectorCopy(org, e->origin);
78                 e->modelindex = modelindex;
79                 e->starttime = cl.time;
80                 e->startframe = startframe;
81                 e->endframe = startframe + framecount;
82                 e->framerate = framerate;
83
84                 e->frame = 0;
85                 e->frame1start = cl.time;
86                 e->frame2start = cl.time;
87                 break;
88         }
89 }
90
91 void CL_DoEffects()
92 {
93         int i, intframe;
94         effect_t *e;
95         entity_t *vis;
96         float frame;
97
98         for (i = 0, e = effect;i < MAX_EFFECTS;i++, e++)
99         {
100                 if (e->active)
101                 {
102                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
103                         intframe = frame;
104                         if (intframe < 0 || intframe >= e->endframe)
105                         {
106                                 e->active = false;
107                                 memset(e, 0, sizeof(*e));
108                                 continue;
109                         }
110
111                         if (intframe != e->frame)
112                         {
113                                 e->frame = intframe;
114                                 e->frame1start = e->frame2start;
115                                 e->frame2start = cl.time;
116                         }
117
118                         vis = CL_NewTempEntity();
119                         if (!vis)
120                                 continue;
121                         VectorCopy(e->origin, vis->origin);
122                         vis->lerp_model = vis->model = cl.model_precache[e->modelindex];
123                         vis->frame1 = e->frame;
124                         vis->frame2 = e->frame + 1;
125                         if (vis->frame2 >= e->endframe)
126                                 vis->frame2 = -1; // disappear
127                         vis->frame = vis->frame2;
128                         vis->framelerp = frame - vis->frame1;
129                         vis->frame1start = e->frame1start;
130                         vis->frame2start = e->frame2start;
131                         vis->lerp_starttime = -1;
132                         vis->colormap = -1; // no special coloring
133                         vis->scale = 1;
134                         vis->alpha = 1;
135                         vis->colormod[0] = vis->colormod[1] = vis->colormod[2] = 1;
136                 }
137         }
138 }