textpsys

view src/effect.cc @ 3:b1c8d2784c72

made the timer internal to the effect, fx_draw doesn't take a time value any more
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 20 Aug 2015 06:40:23 +0300
parents 4b1360a5d54d
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/time.h>
5 #include "opengl.h"
6 #include "effect.h"
7 #include "psys.h"
9 #include "hello1.h"
10 #include "hello2.h"
11 #include "pimg.h"
13 #define HELLO_DURATION 4000
15 static PSysParam ppmain;
16 static PSysParam ppexpl, ppflame;
18 static ParticleSystem psys;
19 static Image *pimg;
20 static Image *simg[2];
21 static int cur_simg = -1;
22 static bool exploding;
24 static void explode();
25 static unsigned long get_msec();
27 bool fx_init()
28 {
29 ppmain.spawn_rate = 20000;
30 ppmain.size = 0.06;
31 ppmain.spawn_range = 0.01;
32 ppmain.life = 0.4;
33 ppmain.gravity = Vector3(0, 0.01, 0);
35 ppmain.pcolor_start = Vector3(0.717, 0.494, 0.951) * 0.6;
36 ppmain.pcolor_end = Vector3(0.9, 0.135, 0.005) * 0.6;
37 ppmain.pcolor_mid = lerp(ppmain.pcolor_start, ppmain.pcolor_mid, 0.5);
39 ppmain.palpha_start = 1.0;
40 ppmain.palpha_mid = 0.5;
41 ppmain.palpha_end = 0.0;
43 ppmain.pscale_start = 1.0;
44 ppmain.pscale_mid = 1.0;
45 ppmain.pscale_end = 7.0;
47 pimg = new Image;
48 pimg->pixels = (unsigned char*)img_particle.pixel_data;
49 pimg->width = img_particle.width;
50 pimg->height = img_particle.height;
52 ppmain.pimg = pimg;
54 simg[0] = new Image;
55 simg[0]->pixels = (unsigned char*)img_hello1.pixel_data;
56 simg[0]->width = img_hello1.width;
57 simg[0]->height = img_hello1.height;
59 simg[1] = new Image;
60 simg[1]->pixels = (unsigned char*)img_hello2.pixel_data;
61 simg[1]->width = img_hello2.width;
62 simg[1]->height = img_hello2.height;
64 ppmain.spawn_map = simg[0];
65 ppmain.spawn_map_speed = 0.8;
66 psys.pp = ppmain;
67 psys.active = false;
69 // explosion parameters
70 ppexpl = ppmain;
71 ppexpl.pcolor_start = ppmain.pcolor_mid;
72 ppexpl.pcolor_mid = lerp(ppexpl.pcolor_start, ppexpl.pcolor_end, 0.5);
74 ppexpl.palpha_start = 1.0;
75 ppexpl.palpha_mid = 0.5;
76 ppexpl.palpha_end = 0.05;
78 ppexpl.pscale_start = 1.0;
79 ppexpl.pscale_mid = 3.0;
80 ppexpl.pscale_end = 5.0;
81 ppexpl.gravity = Vector3(0, -6, 0);
83 // flame parameters
84 ppflame = ppexpl;
85 ppflame.pcolor_start = Vector3(1.0, 0.8, 0.2) * 0.5;
86 ppflame.pcolor_mid = Vector3(1.0, 0.3, 0.2) * 0.5;
87 ppflame.pcolor_end = Vector3(0.1, 0.1, 0.1);
88 //ppflame.pcolor_mid = lerp(ppflame.pcolor_start, ppflame.pcolor_end, 0.6);
90 ppflame.life = 0.5;
91 ppflame.life_range = 0.25;
92 ppflame.size = 0.07;
93 ppflame.size_range = 0.03;
94 ppflame.spawn_rate = 18000;
95 ppflame.gravity = Vector3(0, 2, 0);
97 ppflame.palpha_start *= 0.7;
98 ppflame.palpha_mid *= 0.7;
99 ppflame.palpha_end *= 0.7;
101 return true;
102 }
104 void fx_cleanup()
105 {
106 delete pimg;
107 delete simg[0];
108 delete simg[1];
109 }
111 void fx_draw()
112 {
113 static unsigned long prev_msec, ps_start_time;
114 unsigned long msec = get_msec();
115 float dt = (msec - prev_msec) / 1000.0;
116 prev_msec = msec;
118 if(!psys.alive()) {
119 cur_simg = (cur_simg + 1) & 1;
120 printf("starting %d\n", cur_simg);
122 psys.reset();
123 ppflame.spawn_map = simg[cur_simg];
124 ppexpl.spawn_map = simg[cur_simg];
125 ppmain.spawn_map = simg[cur_simg];
126 psys.pp = ppmain;
127 ps_start_time = msec;
128 exploding = false;
129 } else {
130 if(msec - ps_start_time > HELLO_DURATION && psys.active && !exploding) {
131 exploding = true;
132 explode();
133 }
134 }
136 psys.update(dt);
137 psys.draw();
138 }
140 static void explode()
141 {
142 psys.explode(Vector3(0, 0, 0), 2.5, ppflame.life, 1.5);
143 psys.pp = ppflame;
144 //psys.explode(Vector3(0, -0.2, 0), 3.0, 1.5);
145 //psys.pp = ppexpl;
146 }
148 static unsigned long get_msec()
149 {
150 static struct timeval tv0;
151 struct timeval tv;
153 gettimeofday(&tv, 0);
154 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
155 tv0 = tv;
156 return 0;
157 }
159 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
160 }