Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_particles.c
1
7#define WIDTH 640
8#define HEIGHT 480
9
10#define ALLEGRO_UNSTABLE 1
11
12#include "nilorea/n_common.h"
13#include "nilorea/n_particles.h"
14#include "nilorea/n_anim.h"
15// #include "nilorea/n_resources.h"
16// #include <allegro5/allegro_native_dialog.h>
17#include <allegro5/allegro_ttf.h>
18
19ALLEGRO_DISPLAY* display = NULL;
20
21int DONE = 0, /* Flag to check if we are always running */
22 getoptret = 0, /* launch parameter check */
23 log_level = LOG_ERR; /* default LOG_LEVEL */
24
25ALLEGRO_BITMAP* scr_buf = NULL;
26
27ALLEGRO_TIMER* fps_timer = NULL;
28ALLEGRO_TIMER* logic_timer = NULL;
29LIST* active_object = NULL; /* list of active objects */
30PARTICLE_SYSTEM* particle_system_effects = NULL;
31
32// RESOURCES *resources = NULL ;
33
34int main(int argc, char* argv[]) {
36
37 N_STR* log_file = NULL;
38 nstrprintf(log_file, "%s.log", argv[0]);
39 /*set_log_file( _nstr( log_file ) );*/
41
42 n_log(LOG_NOTICE, "%s is starting ...", argv[0]);
43
44 /* allegro 5 + addons loading */
45 if (!al_init()) {
46 n_abort("Could not init Allegro.\n");
47 }
48 if (!al_install_audio()) {
49 n_abort("Unable to initialize audio addon\n");
50 }
51 if (!al_init_acodec_addon()) {
52 n_abort("Unable to initialize acoded addon\n");
53 }
54 if (!al_init_image_addon()) {
55 n_abort("Unable to initialize image addon\n");
56 }
57 if (!al_init_primitives_addon()) {
58 n_abort("Unable to initialize primitives addon\n");
59 }
60 if (!al_init_font_addon()) {
61 n_abort("Unable to initialize font addon\n");
62 }
63 if (!al_init_ttf_addon()) {
64 n_abort("Unable to initialize ttf_font addon\n");
65 }
66 if (!al_install_keyboard()) {
67 n_abort("Unable to initialize keyboard handler\n");
68 }
69 if (!al_install_mouse()) {
70 n_abort("Unable to initialize mouse handler\n");
71 }
72 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
73
74 event_queue = al_create_event_queue();
75 if (!event_queue) {
76 fprintf(stderr, "failed to create event_queue!\n");
77 al_destroy_display(display);
78 return -1;
79 }
80
81 char ver_str[128] = "";
82 while ((getoptret = getopt(argc, argv, "hvV:L:")) != EOF) {
83 switch (getoptret) {
84 case 'h':
85 n_log(LOG_NOTICE, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
86 exit(TRUE);
87 case 'v':
88 sprintf(ver_str, "%s %s", __DATE__, __TIME__);
89 exit(TRUE);
90 break;
91 case 'V':
92 if (!strncmp("NOTICE", optarg, 6)) {
93 log_level = LOG_INFO;
94 } else {
95 if (!strncmp("VERBOSE", optarg, 7)) {
96 log_level = LOG_NOTICE;
97 } else {
98 if (!strncmp("ERROR", optarg, 5)) {
99 log_level = LOG_ERR;
100 } else {
101 if (!strncmp("DEBUG", optarg, 5)) {
102 log_level = LOG_DEBUG;
103 } else {
104 n_log(LOG_ERR, "%s is not a valid log level\n", optarg);
105 exit(FALSE);
106 }
107 }
108 }
109 }
110 n_log(LOG_NOTICE, "LOG LEVEL UP TO: %d\n", log_level);
111 set_log_level(log_level);
112 break;
113 case 'L':
114 n_log(LOG_NOTICE, "LOG FILE: %s\n", optarg);
115 set_log_file(optarg);
116 break;
117 case '?': {
118 switch (optopt) {
119 case 'V':
120 n_log(LOG_ERR, "\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
121 break;
122 case 'L':
123 n_log(LOG_ERR, "\nPlease specify a log file after -L\n");
124 default:
125 break;
126 }
127 }
128 __attribute__((fallthrough));
129 default:
130 n_log(LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
131 exit(FALSE);
132 }
133 }
134
135 fps_timer = al_create_timer(1.0 / 30.0);
136 logic_timer = al_create_timer(1.0 / 50.0);
137
138 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
139 display = al_create_display(WIDTH, HEIGHT);
140 if (!display) {
141 n_abort("Unable to create display\n");
142 }
143 al_set_window_title(display, argv[0]);
144
145 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
146
147 DONE = 0;
148
149 active_object = new_generic_list(MAX_LIST_ITEMS);
150
151 enum APP_KEYS {
152 KEY_UP,
153 KEY_DOWN,
154 KEY_LEFT,
155 KEY_RIGHT,
156 KEY_ESC,
157 KEY_SPACE,
158 KEY_CTRL
159 };
160 int key[7] = {false, false, false, false, false, false, false};
161
162 al_register_event_source(event_queue, al_get_display_event_source(display));
163
164 al_start_timer(fps_timer);
165 al_start_timer(logic_timer);
166 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
167 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
168
169 al_register_event_source(event_queue, al_get_keyboard_event_source());
170 al_register_event_source(event_queue, al_get_mouse_event_source());
171
172 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
173
174 al_hide_mouse_cursor(display);
175
176 init_particle_system(&particle_system_effects, 10000, 0, 0, 0, 100);
177
178 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
179 int do_draw = 0, do_logic = 0;
180
181 al_clear_keyboard_state(NULL);
182 al_flush_event_queue(event_queue);
183 do {
184 do {
185 ALLEGRO_EVENT ev;
186
187 al_wait_for_event(event_queue, &ev);
188
189 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
190 switch (ev.keyboard.keycode) {
191 case ALLEGRO_KEY_UP:
192 key[KEY_UP] = 1;
193 break;
194 case ALLEGRO_KEY_DOWN:
195 key[KEY_DOWN] = 1;
196 break;
197 case ALLEGRO_KEY_LEFT:
198 key[KEY_LEFT] = 1;
199 break;
200 case ALLEGRO_KEY_RIGHT:
201 key[KEY_RIGHT] = 1;
202 break;
203 case ALLEGRO_KEY_ESCAPE:
204 key[KEY_ESC] = 1;
205 break;
206 case ALLEGRO_KEY_SPACE:
207 key[KEY_SPACE] = 1;
208 break;
209 case ALLEGRO_KEY_LCTRL:
210 case ALLEGRO_KEY_RCTRL:
211 key[KEY_CTRL] = 1;
212 default:
213 break;
214 }
215 } else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
216 switch (ev.keyboard.keycode) {
217 case ALLEGRO_KEY_UP:
218 key[KEY_UP] = 0;
219 break;
220 case ALLEGRO_KEY_DOWN:
221 key[KEY_DOWN] = 0;
222 break;
223 case ALLEGRO_KEY_LEFT:
224 key[KEY_LEFT] = 0;
225 break;
226 case ALLEGRO_KEY_RIGHT:
227 key[KEY_RIGHT] = 0;
228 break;
229 case ALLEGRO_KEY_ESCAPE:
230 key[KEY_ESC] = 0;
231 break;
232 case ALLEGRO_KEY_SPACE:
233 key[KEY_SPACE] = 0;
234 break;
235 case ALLEGRO_KEY_LCTRL:
236 case ALLEGRO_KEY_RCTRL:
237 key[KEY_CTRL] = 0;
238 default:
239 break;
240 }
241 } else if (ev.type == ALLEGRO_EVENT_TIMER) {
242 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
243 do_draw = 1;
244 } else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
245 do_logic = 1;
246 }
247 } else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
248 mx = ev.mouse.x;
249 my = ev.mouse.y;
250 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
251 if (ev.mouse.button == 1)
252 mouse_b1 = 1;
253 if (ev.mouse.button == 2)
254 mouse_b2 = 1;
255 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
256 if (ev.mouse.button == 1)
257 mouse_b1 = 0;
258 if (ev.mouse.button == 2)
259 mouse_b2 = 0;
260 } else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
261 al_clear_keyboard_state(display);
262 al_flush_event_queue(event_queue);
263 } else {
264 /* Processing inputs */
265 // get_keyboard( chat_line , ev );
266 }
267 } while (!al_is_event_queue_empty(event_queue));
268
269 /* dev mod: right click to temporary delete a block
270 left click to temporary add a block */
271 int mouse_button = -1;
272 if (mouse_b1 == 1)
273 mouse_button = 1;
274 if (mouse_b2 == 1)
275 mouse_button = 2;
276
277 if (do_logic == 1) {
278 manage_particle(particle_system_effects);
279 if (mouse_button == 1) {
280 PHYSICS tmp_part;
281 tmp_part.sz = 10;
282 for (int it = 0; it < 100; it++) {
283 VECTOR3D_SET(tmp_part.speed,
284 500 - rand() % 1000,
285 500 - rand() % 1000,
286 0.0);
287 VECTOR3D_SET(tmp_part.position, mx, my, 0.0);
288 VECTOR3D_SET(tmp_part.acceleration, 0.0, 0.0, 0.0);
289 VECTOR3D_SET(tmp_part.orientation, 0.0, 0.0, 0.0);
290 add_particle(particle_system_effects, -1, PIXEL_PART, 1000 + rand() % 10000, 1 + rand() % 3,
291 al_map_rgba(55 + rand() % 200, 55 + rand() % 200, 55 + rand() % 200, 10 + rand() % 245), tmp_part);
292 }
293 }
294 do_logic = 0;
295 }
296
297 if (do_draw == 1) {
298 al_acknowledge_resize(display);
299 int w = al_get_display_width(display);
300 int h = al_get_display_height(display);
301
302 al_set_target_bitmap(scrbuf);
303 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
304 draw_particle(particle_system_effects, 0, 0, w, h, 50);
305
306 al_set_target_bitmap(al_get_backbuffer(display));
307
308 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
309 al_draw_bitmap(scrbuf, 0, 0, 0);
310
311 /* mouse pointer */
312 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
313 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
314
315 al_flip_display();
316 do_draw = 0;
317 }
318
319 } while (!key[KEY_ESC] && !DONE);
320
321 list_destroy(&active_object);
322
323 al_destroy_bitmap(scr_buf);
324
325 al_uninstall_system();
326
327 return 0;
328}
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:38
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:518
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Definition n_list.h:55
Structure of a generic LIST container.
Definition n_list.h:39
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
#define LOG_ERR
error conditions
Definition n_log.h:56
int set_log_file(char *file)
Set the logging to a file instead of stderr.
Definition n_log.c:120
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:91
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:60
#define LOG_INFO
informational
Definition n_log.h:62
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
A box including a string and his lenght.
Definition n_str.h:39
int draw_particle(PARTICLE_SYSTEM *psys, double xpos, double ypos, int w, int h, double range)
draw particles of a particle system
#define PIXEL_PART
pixel particle
Definition n_particles.h:48
int manage_particle(PARTICLE_SYSTEM *psys)
update particles positions usting particle system internal timer
int add_particle(PARTICLE_SYSTEM *psys, int spr, int mode, int lifetime, int size, ALLEGRO_COLOR color, PHYSICS object)
add a particle to a particle system
Definition n_particles.c:53
int init_particle_system(PARTICLE_SYSTEM **psys, int max, double x, double y, double z, int max_sprites)
initialize a particle system
Definition n_particles.c:21
Structure of a particle system.
Definition n_particles.h:74
VECTOR3D speed
vx,vy,vz actual speed
Definition n_3d.h:54
VECTOR3D orientation
ax,ay,az actual rotation position
Definition n_3d.h:58
int sz
size
Definition n_3d.h:68
VECTOR3D position
x,y,z actual position
Definition n_3d.h:52
VECTOR3D acceleration
ax,ay,az actual acceleration
Definition n_3d.h:56
#define VECTOR3D_SET(VECTOR, X, Y, Z)
helper to set a VECTOR3D position
Definition n_3d.h:42
structure of the physics of an object
Definition n_3d.h:48
Animations graphics and animations parameters.
Common headers and low-level functions & define.
static FILE * log_file
static FILE handling if logging to file is enabled
Definition n_log.c:54
Particles management.