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[] )
35{
37
38 N_STR *log_file = NULL ;
39 nstrprintf( log_file, "%s.log", argv[ 0 ] ) ;
40 /*set_log_file( _nstr( log_file ) );*/
42
43 n_log( LOG_NOTICE, "%s is starting ...", argv[ 0 ] );
44
45
46 /* allegro 5 + addons loading */
47 if (!al_init())
48 {
49 n_abort("Could not init Allegro.\n");
50 }
51 if (!al_install_audio())
52 {
53 n_abort("Unable to initialize audio addon\n");
54 }
55 if (!al_init_acodec_addon())
56 {
57 n_abort("Unable to initialize acoded addon\n");
58 }
59 if (!al_init_image_addon())
60 {
61 n_abort("Unable to initialize image addon\n");
62 }
63 if (!al_init_primitives_addon() )
64 {
65 n_abort("Unable to initialize primitives addon\n");
66 }
67 if( !al_init_font_addon() )
68 {
69 n_abort("Unable to initialize font addon\n");
70 }
71 if( !al_init_ttf_addon() )
72 {
73 n_abort("Unable to initialize ttf_font addon\n");
74 }
75 if( !al_install_keyboard() )
76 {
77 n_abort("Unable to initialize keyboard handler\n");
78 }
79 if( !al_install_mouse())
80 {
81 n_abort("Unable to initialize mouse handler\n");
82 }
83 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
84
85 event_queue = al_create_event_queue();
86 if(!event_queue)
87 {
88 fprintf(stderr, "failed to create event_queue!\n");
89 al_destroy_display(display);
90 return -1;
91 }
92
93 char ver_str[ 128 ] = "" ;
94 while( ( getoptret = getopt( argc, argv, "hvV:L:" ) ) != EOF )
95 {
96 switch( getoptret )
97 {
98 case 'h':
99 n_log( LOG_NOTICE, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[ 0 ] );
100 exit( TRUE );
101 case 'v':
102 sprintf( ver_str, "%s %s", __DATE__, __TIME__ );
103 exit( TRUE );
104 break ;
105 case 'V':
106 if( !strncmp( "NOTICE", optarg, 6 ) )
107 {
108 log_level = LOG_INFO ;
109 }
110 else
111 {
112 if( !strncmp( "VERBOSE", optarg, 7 ) )
113 {
114 log_level = LOG_NOTICE ;
115 }
116 else
117 {
118 if( !strncmp( "ERROR", optarg, 5 ) )
119 {
120 log_level = LOG_ERR ;
121 }
122 else
123 {
124 if( !strncmp( "DEBUG", optarg, 5 ) )
125 {
126 log_level = LOG_DEBUG ;
127 }
128 else
129 {
130 n_log( LOG_ERR, "%s is not a valid log level\n", optarg );
131 exit( FALSE );
132 }
133 }
134 }
135 }
136 n_log( LOG_NOTICE, "LOG LEVEL UP TO: %d\n", log_level );
137 set_log_level( log_level );
138 break;
139 case 'L' :
140 n_log( LOG_NOTICE, "LOG FILE: %s\n", optarg );
141 set_log_file( optarg );
142 break ;
143 case '?' :
144 {
145 switch( optopt )
146 {
147 case 'V' :
148 n_log( LOG_ERR, "\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n" );
149 break;
150 case 'L' :
151 n_log( LOG_ERR, "\nPlease specify a log file after -L\n" );
152 default:
153 break;
154 }
155 }
156 __attribute__ ((fallthrough));
157 default:
158 n_log( LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[ 0 ] );
159 exit( FALSE );
160 }
161 }
162
163 fps_timer = al_create_timer( 1.0/30.0 );
164 logic_timer = al_create_timer( 1.0/50.0 );
165
166 al_set_new_display_flags( ALLEGRO_OPENGL|ALLEGRO_WINDOWED );
167 display = al_create_display( WIDTH, HEIGHT );
168 if( !display )
169 {
170 n_abort("Unable to create display\n");
171 }
172 al_set_window_title( display, argv[ 0 ] );
173
174 al_set_new_bitmap_flags( ALLEGRO_VIDEO_BITMAP );
175
176 DONE = 0 ;
177
178 active_object = new_generic_list( -1 );
179
180
181 enum APP_KEYS
182 {
183 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ESC, KEY_SPACE, KEY_CTRL
184 };
185 int key[ 7 ] = {false,false,false,false,false,false,false};
186
187 al_register_event_source(event_queue, al_get_display_event_source(display));
188
189 al_start_timer( fps_timer );
190 al_start_timer( logic_timer );
191 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
192 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
193
194 al_register_event_source(event_queue, al_get_keyboard_event_source());
195 al_register_event_source(event_queue, al_get_mouse_event_source());
196
197 ALLEGRO_BITMAP *scrbuf = al_create_bitmap( WIDTH, HEIGHT );
198
199 al_hide_mouse_cursor(display);
200
201 init_particle_system( &particle_system_effects, 10000, 0, 0, 0, 100 );
202
203 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0 ;
204 int do_draw = 0, do_logic = 0 ;
205
206 al_clear_keyboard_state( NULL );
207 al_flush_event_queue( event_queue );
208 do
209 {
210 do
211 {
212 ALLEGRO_EVENT ev ;
213
214 al_wait_for_event(event_queue, &ev);
215
216 if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
217 {
218 switch(ev.keyboard.keycode)
219 {
220 case ALLEGRO_KEY_UP:
221 key[KEY_UP] = 1;
222 break;
223 case ALLEGRO_KEY_DOWN:
224 key[KEY_DOWN] = 1;
225 break;
226 case ALLEGRO_KEY_LEFT:
227 key[KEY_LEFT] = 1;
228 break;
229 case ALLEGRO_KEY_RIGHT:
230 key[KEY_RIGHT] = 1;
231 break;
232 case ALLEGRO_KEY_ESCAPE:
233 key[KEY_ESC] = 1 ;
234 break;
235 case ALLEGRO_KEY_SPACE:
236 key[KEY_SPACE] = 1 ;
237 break;
238 case ALLEGRO_KEY_LCTRL:
239 case ALLEGRO_KEY_RCTRL:
240 key[KEY_CTRL] = 1 ;
241 default:
242 break;
243 }
244 }
245 else if(ev.type == ALLEGRO_EVENT_KEY_UP)
246 {
247 switch(ev.keyboard.keycode)
248 {
249 case ALLEGRO_KEY_UP:
250 key[KEY_UP] = 0;
251 break;
252 case ALLEGRO_KEY_DOWN:
253 key[KEY_DOWN] = 0;
254 break;
255 case ALLEGRO_KEY_LEFT:
256 key[KEY_LEFT] = 0;
257 break;
258 case ALLEGRO_KEY_RIGHT:
259 key[KEY_RIGHT] =0;
260 break;
261 case ALLEGRO_KEY_ESCAPE:
262 key[KEY_ESC] = 0 ;
263 break;
264 case ALLEGRO_KEY_SPACE:
265 key[KEY_SPACE] = 0 ;
266 break;
267 case ALLEGRO_KEY_LCTRL:
268 case ALLEGRO_KEY_RCTRL:
269 key[KEY_CTRL] = 0 ;
270 default:
271 break;
272 }
273 }
274 else if( ev.type == ALLEGRO_EVENT_TIMER )
275 {
276 if( al_get_timer_event_source( fps_timer ) == ev.any.source )
277 {
278 do_draw = 1 ;
279 }
280 else if( al_get_timer_event_source( logic_timer ) == ev.any.source )
281 {
282 do_logic = 1;
283 }
284 }
285 else if( ev.type == ALLEGRO_EVENT_MOUSE_AXES )
286 {
287 mx = ev.mouse.x;
288 my = ev.mouse.y;
289 }
290 else if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
291 {
292 if( ev.mouse.button == 1 )
293 mouse_b1 = 1 ;
294 if( ev.mouse.button == 2 )
295 mouse_b2 = 1 ;
296 }
297 else if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP )
298 {
299 if( ev.mouse.button == 1 )
300 mouse_b1 = 0 ;
301 if( ev.mouse.button == 2 )
302 mouse_b2 = 0 ;
303 }
304 else if( ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT )
305 {
306 al_clear_keyboard_state( display );
307 al_flush_event_queue( event_queue );
308 }
309 else
310 {
311 /* Processing inputs */
312 // get_keyboard( chat_line , ev );
313 }
314 }
315 while( !al_is_event_queue_empty( event_queue) );
316
317
318 /* dev mod: right click to temporary delete a block
319 left click to temporary add a block */
320 int mouse_button = -1 ;
321 if( mouse_b1==1 )
322 mouse_button = 1 ;
323 if( mouse_b2==1 )
324 mouse_button = 2 ;
325
326
327 if( do_logic == 1 )
328 {
329 manage_particle( particle_system_effects );
330 if( mouse_button == 1 )
331 {
332 PHYSICS tmp_part ;
333 tmp_part . sz = 10 ;
334 for( int it = 0 ; it < 100 ; it ++ )
335 {
336 VECTOR3D_SET( tmp_part . speed,
337 500 - rand()%1000,
338 500 - rand()%1000,
339 0.0 );
340 VECTOR3D_SET( tmp_part . position, mx, my, 0.0 );
341 VECTOR3D_SET( tmp_part . acceleration, 0.0, 0.0, 0.0 );
342 VECTOR3D_SET( tmp_part . orientation, 0.0, 0.0, 0.0 );
343 add_particle( particle_system_effects, -1, PIXEL_PART, 1000 + rand()%10000, 1+rand()%3,
344 al_map_rgba( 55 + rand()%200, 55 + rand()%200, 55 + rand()%200, 10 + rand()%245 ), tmp_part );
345
346 }
347 }
348 do_logic = 0 ;
349 }
350
351 if( do_draw == 1 )
352 {
353 al_acknowledge_resize( display );
354 int w = al_get_display_width( display );
355 int h = al_get_display_height( display );
356
357 al_set_target_bitmap( scrbuf );
358 al_clear_to_color( al_map_rgba( 0, 0, 0, 255 ) );
359 draw_particle( particle_system_effects, 0, 0, w, h, 50 );
360
361
362
363 al_set_target_bitmap( al_get_backbuffer( display ) );
364
365 al_clear_to_color( al_map_rgba( 0, 0, 0, 255 ) );
366 al_draw_bitmap( scrbuf, 0, 0, 0 );
367
368 /* mouse pointer */
369 al_draw_line( mx - 5, my, mx + 5, my, al_map_rgb( 255, 0, 0 ), 1 );
370 al_draw_line( mx, my + 5, mx, my - 5, al_map_rgb( 255, 0, 0 ), 1 );
371
372 al_flip_display();
373 do_draw = 0 ;
374 }
375
376 }
377 while( !key[KEY_ESC] && !DONE );
378
379 list_destroy( &active_object );
380
381 return 0;
382
383}
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:40
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:605
LIST * new_generic_list(int max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:20
Structure of a generic LIST container.
Definition n_list.h:45
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition n_log.h:66
#define LOG_ERR
error conditions
Definition n_log.h:58
int set_log_file(char *file)
Set the logging to a file instead of stderr.
Definition n_log.c:135
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:97
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:62
#define LOG_INFO
informational
Definition n_log.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:222
#define nstrprintf(__nstr_var,...)
Macro to quickly allocate and sprintf to N_STR
Definition n_str.h:97
A box including a string and his lenght.
Definition n_str.h:173
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:59
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:24
Structure of a particle system.
Definition n_particles.h:78
#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:50
Animations graphics and animations parameters.
Common headers and low-level hugly functions & define.
static FILE * log_file
static FILE handling if logging to file is enabled
Definition n_log.c:53
Particles management.