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