-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.c
210 lines (191 loc) · 5.56 KB
/
particle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define PARTICLE_COUNT 1000
#define BASE_LIFE 1000
#define BASE_SIZE 16.0
struct Particle {
float x;
float y;
float vx;
float vy;
float ax;
float ay;
//Time(ms) until destruction
int life;
} particle;
void quit(int exit_code) {
SDL_Quit();
exit(exit_code);
}
void init() {
//Check SDL Version
SDL_version compiled;
SDL_version linked;
SDL_VERSION(&compiled);
SDL_GetVersion(&linked);
if (compiled.major != linked.major) {
fprintf(stderr, "SDL version mismatch! Found version %d, require version %d", linked.major, compiled.major);
quit(EXIT_FAILURE);
}
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER)) {
fprintf(stderr, "Initialisation Error: %s", SDL_GetError());
quit(EXIT_FAILURE);
}
//Hints
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
}
int particle_draw(struct Particle particle, SDL_Texture *tex, SDL_Renderer *renderer) {
int life = particle.life;
if(life > 0){
float size = 8+(life < 0.1*BASE_LIFE ? life*BASE_SIZE/(0.1*BASE_LIFE): BASE_SIZE);
SDL_RenderCopy(renderer, tex, NULL, &(SDL_Rect){particle.x - size/2, particle.y - size/2, size, size});
return 1;
} else {
return 0;
}
}
int draw2(struct Particle particle, SDL_Texture *tex, SDL_Renderer *renderer) {
int life = particle.life;
if(life > 0) {
int r, g, b;
r = life > BASE_LIFE? 255 : life*(255/BASE_LIFE);
if(life < 0.25*BASE_LIFE) {
r = life*255/(0.25*BASE_LIFE);
} else {
r = 255;
}
if(life < 0.25*BASE_LIFE) {
g = 0;
} else if(life < 0.75*BASE_LIFE) {
g = -128 + life*512.0/BASE_LIFE;
} else {
g = 255;
}
if(life < 0.5*BASE_LIFE) {
b = 0;
} else if(life < BASE_LIFE) {
b = -255 + life*512.0/BASE_LIFE;
} else {
b = 255;
}
float size = life < 0.1*BASE_LIFE ? life*BASE_SIZE/(0.1*BASE_LIFE): BASE_SIZE;
SDL_SetTextureColorMod(tex, r, g, b);
SDL_RenderCopy(renderer, tex, NULL, &(SDL_Rect){particle.x - size/2, particle.y - size/2, size, size});
return 1;
} else {
return 0;
}
}
//Emits particles at random angles
//ppf is the emission rate, in particles per frame
struct Particle particle_emit_circle(struct Particle particle, int x, int y, float ppf) {
if(particle.life <= 0 && ppf > 1){
ppf--;
particle.life = BASE_LIFE + rand() * 500.0 / RAND_MAX;
particle.x = x;
particle.y = y;
float theta = rand() * 2*M_PI / RAND_MAX;
float v = rand() * 30.0 / RAND_MAX;
particle.vx = v * cos(theta);
particle.vy = v * sin(theta);
}
return particle;
}
//Emits particles at random bounded angles
//particle: particle to emit
//x, y: point of particle generation
//vrange: difference between maximum and minimum magnitude of velocity
//vmin: minimum magnitude of velocity
//angle: difference between maximum and minimum angle to emit
//angle_offset: minimum angle
//ppf: particles per frame
struct Particle particle_emit_sector(struct Particle particle, float x, float y, float vrange, float vmin, float angle, float angle_offset, float ppf) {
if(particle.life <= 0 && ppf > 1){
ppf--;
particle.life = BASE_LIFE + rand() * 500.0 / RAND_MAX;
particle.x = x;
particle.y = y;
float theta = rand() * angle / (float)RAND_MAX + angle_offset;
float v = rand() * vrange / (float)RAND_MAX + vmin;
particle.vx = v * cos(theta);
particle.vy = v * sin(theta);
}
return particle;
}
//basic physics tick for particles
struct Particle particle_tick(struct Particle particle, int dt) {
particle.life -= dt;
//Accelerate
particle.vy += -0.1*dt;
//This keeps the vx unit in pixels/second
particle.x += (particle.vx*dt/1000.0);
particle.y += (particle.vy*dt/1000.0);
return particle;
}
int
main(int argc, char *argv[]) {
init();
int x = WINDOW_WIDTH/2;
int y = WINDOW_HEIGHT/2;
//Setup Rendering System
SDL_Window* window;
SDL_Renderer* renderer;
if(SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
fprintf(stderr, "Window creation failed: %s", SDL_GetError());
quit(EXIT_FAILURE);
}
//Load Initial Sprites
SDL_Texture* particle_sprite = IMG_LoadTexture(renderer, "particle.png");
SDL_Texture* back_sprite = IMG_LoadTexture(renderer, "back.png");
SDL_SetTextureBlendMode(particle_sprite, SDL_BLENDMODE_ADD);
SDL_SetTextureBlendMode(back_sprite, SDL_BLENDMODE_BLEND);
//Initialise Objects
particle = (struct Particle){16,16,0};
struct Particle particles[PARTICLE_COUNT] = {};
for(int i = 0; i < PARTICLE_COUNT; i++) {
particles[i].life = rand()*BASE_LIFE*5.0 / RAND_MAX;
particles[i].x = -16;
particles[i].y = -16;
}
//GAME LOOP
int t = SDL_GetTicks();
//Time between frames in milliseconds
int dt = 0;
while(1) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0);
SDL_RenderClear(renderer);
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
quit(0);
}
}
dt = SDL_GetTicks() - t;
//Stabilise frames
if(1000.0/60.0 > dt) {
SDL_Delay(1000.0/60.0 - dt);
}
dt = SDL_GetTicks() - t;
printf("%4.3f\n", 1.0/dt*1000.0);
t += dt;
SDL_GetMouseState(&x, &y);
for(int i = 0; i < PARTICLE_COUNT; i++) {
particles[i] = particle_tick(particles[i], dt);
particles[i] = particle_emit_circle(particles[i], x, y, 100);
particle_draw(particles[i], back_sprite, renderer);
}
for(int i = 0; i < PARTICLE_COUNT; i++) {
draw2(particles[i], particle_sprite, renderer);
}
SDL_RenderPresent(renderer);
}
return EXIT_SUCCESS;
}