Debian Codegraph @

particles.c

raw symbols SCIP
1 #include <assert.h>
2 #include <lua.h>
3 #include <lualib.h>
4 #include <lauxlib.h>
5
6 #ifdef __APPLE__
7 #include "OpenGL/gl.h"
8 #include "OpenGL/glu.h"
9 #else
10 #include "GL/gl.h"
11 #include "GL/glu.h"
12 #endif
13
14 #define emitter_udata "particles.emitter"
15 #define PARTICLE_COUNT 2000
16
17 typedef struct {
18 int life;
19 float x;
20 float y;
21 float xvel;
22 float yvel;
23 float scale;
24 } particle_t;
25
26 typedef struct {
27 int texture;
28 float width;
29 float height;
30 int life;
31 float damping;
32 float delta_scale;
33 int next;
34 particle_t particles[PARTICLE_COUNT];
35 } emitter_t;
36
37 static inline void draw_particle(emitter_t *emitter, particle_t *particle)
38 {
39 if(particle->life)
40 {
41 float dx = emitter->width/2 * particle->scale;
42 float dy = emitter->height/2 * particle->scale;
43 glColor4f(1, 1, 1, (float)particle->life / emitter->life);
44 glTexCoord2d(0, 1);
45 glVertex2f(particle->x - dx, particle->y - dy);
46 glTexCoord2d(1, 1);
47 glVertex2f(particle->x + dx, particle->y - dy);
48 glTexCoord2d(1, 0);
49 glVertex2f(particle->x + dx, particle->y + dy);
50 glTexCoord2d(0, 0);
51 glVertex2f(particle->x - dx, particle->y + dy);
52 }
53 }
54
55 static int emitter__draw(lua_State *L)
56 {
57 emitter_t *emitter = luaL_checkudata(L, 1, "particles.emitter");
58 int i;
59
60 glEnable(GL_TEXTURE_2D);
61 glBindTexture(GL_TEXTURE_2D, emitter->texture);
62 glBegin(GL_QUADS);
63 for(i = emitter->next; i != PARTICLE_COUNT; i++)
64 draw_particle(emitter, &emitter->particles[i]);
65 for(i = 0; i != emitter->next; i++)
66 draw_particle(emitter, &emitter->particles[i]);
67 glEnd();
68 glBindTexture(GL_TEXTURE_2D, 0);
69 glDisable(GL_TEXTURE_2D);
70
71 glColor3f(1, 1, 1);
72
73 return 0;
74 }
75
76 static int emitter__update(lua_State *L)
77 {
78 emitter_t *emitter = luaL_checkudata(L, 1, emitter_udata);
79
80 particle_t *particle;
81 for(particle = emitter->particles;
82 particle != emitter->particles + PARTICLE_COUNT;
83 particle++)
84 {
85 if(particle->life)
86 {
87 particle->life--;
88 particle->x += particle->xvel;
89 particle->y += particle->yvel;
90 particle->xvel *= emitter->damping;
91 particle->yvel *= emitter->damping;
92 particle->scale += emitter->delta_scale;
93 }
94 }
95
96 return 0;
97 }
98
99 static int emitter__add_particle(lua_State *L)
100 {
101 emitter_t *emitter = luaL_checkudata(L, 1, emitter_udata);
102 float x = (float)luaL_checknumber(L, 2);
103 float y = (float)luaL_checknumber(L, 3);
104 float xvel = (float)luaL_checknumber(L, 4);
105 float yvel = (float)luaL_checknumber(L, 5);
106
107 // grab a particle slot from the emitter
108 particle_t *particle = &emitter->particles[emitter->next];
109 emitter->next = (emitter->next + 1) % PARTICLE_COUNT;
110
111 // initialize it
112 particle->life = emitter->life;
113 particle->x = x;
114 particle->y = y;
115 particle->xvel = xvel;
116 particle->yvel = yvel;
117 particle->scale = 1;
118
119 return 0;
120 }
121
122 static int particles__make_emitter(lua_State *L)
123 {
124 float width = (float)luaL_checknumber(L, 1);
125 float height = (float)luaL_checknumber(L, 2);
126 int texture = luaL_checkint(L, 3);
127 int life = luaL_checkint(L, 4);
128 float damping = luaL_checknumber(L, 5);
129 float delta_scale = luaL_checknumber(L, 6);
130
131 emitter_t *emitter = lua_newuserdata(L, sizeof(emitter_t));
132 luaL_getmetatable(L, emitter_udata);
133 lua_setmetatable(L, -2);
134
135 emitter->width = width;
136 emitter->height = height;
137 emitter->texture = texture;
138 emitter->life = life;
139 emitter->damping = damping;
140 emitter->delta_scale = delta_scale;
141
142 emitter->next = 0;
143 int i;
144 for(i = 0; i < PARTICLE_COUNT; i++)
145 {
146 emitter->particles[i].life = 0;
147 }
148
149 return 1;
150 }
151
152 static const luaL_reg emitter_lib[] =
153 {
154 {"draw", emitter__draw},
155 {"update", emitter__update},
156 {"add_particle", emitter__add_particle},
157 {NULL, NULL}
158 };
159
160 static const luaL_Reg particles_lib[] =
161 {
162 {"make_emitter", particles__make_emitter},
163 {NULL, NULL}
164 };
165
166 int luaopen_particles(lua_State *L)
167 {
168 luaL_newmetatable(L, "particles.emitter");
169 lua_pushvalue(L, -1);
170 lua_setfield(L, -2, "__index");
171 luaL_register(L, NULL, emitter_lib);
172
173 lua_newtable(L);
174 luaL_register(L, NULL, particles_lib);
175 return 1;
176 }
177