10#define ALLEGRO_UNSTABLE 1
14ALLEGRO_DISPLAY *display = NULL ;
20ALLEGRO_BITMAP *scr_buf = NULL ;
22ALLEGRO_TIMER *fps_timer = NULL ;
23ALLEGRO_TIMER *logic_timer = NULL ;
27int main(
int argc,
char *argv[] )
42 n_abort(
"Could not init Allegro.\n");
44 if (!al_install_audio())
46 n_log(
LOG_ERR ,
"Unable to initialize audio addon, audio will be disabled or crashing !");
48 if (!al_init_acodec_addon())
50 n_log(
LOG_ERR ,
"Unable to initialize audio codec addon, audio will be disabled or crashing !");
52 if (!al_init_image_addon())
54 n_abort(
"Unable to initialize image addon\n");
56 if (!al_init_primitives_addon() )
58 n_abort(
"Unable to initialize primitives addon\n");
60 if( !al_init_font_addon() )
62 n_abort(
"Unable to initialize font addon\n");
64 if( !al_init_ttf_addon() )
66 n_abort(
"Unable to initialize ttf_font addon\n");
68 if( !al_install_keyboard() )
70 n_abort(
"Unable to initialize keyboard handler\n");
72 if( !al_install_mouse())
74 n_abort(
"Unable to initialize mouse handler\n");
76 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
78 event_queue = al_create_event_queue();
81 fprintf(stderr,
"failed to create event_queue!\n");
82 al_destroy_display(display);
86 char ver_str[ 128 ] =
"" ;
87 while( ( getoptret = getopt( argc, argv,
"hvV:L:" ) ) != EOF )
92 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[ 0 ] );
95 sprintf( ver_str,
"%s %s", __DATE__, __TIME__ );
99 if( !strncmp(
"NOTICE", optarg, 6 ) )
105 if( !strncmp(
"VERBOSE", optarg, 7 ) )
111 if( !strncmp(
"ERROR", optarg, 5 ) )
117 if( !strncmp(
"DEBUG", optarg, 5 ) )
123 n_log(
LOG_ERR,
"%s is not a valid log level\n", optarg );
141 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n" );
144 n_log(
LOG_ERR,
"\nPlease specify a log file after -L\n" );
149 __attribute__ ((fallthrough));
151 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[ 0 ] );
156 fps_timer = al_create_timer( 1.0/30.0 );
157 logic_timer = al_create_timer( 1.0/50.0 );
159 al_set_new_display_flags( ALLEGRO_OPENGL|ALLEGRO_WINDOWED );
160 display = al_create_display( WIDTH, HEIGHT );
163 n_abort(
"Unable to create display\n");
165 al_set_window_title( display, argv[ 0 ] );
167 al_set_new_bitmap_flags( ALLEGRO_VIDEO_BITMAP );
173 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ESC, KEY_SPACE, KEY_CTRL
175 int key[ 7 ] = {
false,
false,
false,
false,
false,
false,
false};
177 al_register_event_source(event_queue, al_get_display_event_source(display));
179 al_start_timer( fps_timer );
180 al_start_timer( logic_timer );
181 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
182 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
184 al_register_event_source(event_queue, al_get_keyboard_event_source());
185 al_register_event_source(event_queue, al_get_mouse_event_source());
187 ALLEGRO_BITMAP *scrbuf = al_create_bitmap( WIDTH, HEIGHT );
189 al_hide_mouse_cursor(display);
199 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0 ;
200 int do_draw = 0, do_logic = 0 ;
202 al_clear_keyboard_state( NULL );
203 al_flush_event_queue( event_queue );
210 al_wait_for_event(event_queue, &ev);
212 if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
214 switch(ev.keyboard.keycode)
219 case ALLEGRO_KEY_DOWN:
222 case ALLEGRO_KEY_LEFT:
225 case ALLEGRO_KEY_RIGHT:
228 case ALLEGRO_KEY_ESCAPE:
231 case ALLEGRO_KEY_SPACE:
234 case ALLEGRO_KEY_LCTRL:
235 case ALLEGRO_KEY_RCTRL:
241 else if(ev.type == ALLEGRO_EVENT_KEY_UP)
243 switch(ev.keyboard.keycode)
248 case ALLEGRO_KEY_DOWN:
251 case ALLEGRO_KEY_LEFT:
254 case ALLEGRO_KEY_RIGHT:
257 case ALLEGRO_KEY_ESCAPE:
260 case ALLEGRO_KEY_SPACE:
263 case ALLEGRO_KEY_LCTRL:
264 case ALLEGRO_KEY_RCTRL:
270 else if( ev.type == ALLEGRO_EVENT_TIMER )
272 if( al_get_timer_event_source( fps_timer ) == ev.any.source )
276 else if( al_get_timer_event_source( logic_timer ) == ev.any.source )
281 else if( ev.type == ALLEGRO_EVENT_MOUSE_AXES )
286 else if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
288 if( ev.mouse.button == 1 )
290 if( ev.mouse.button == 2 )
293 else if( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP )
295 if( ev.mouse.button == 1 )
297 if( ev.mouse.button == 2 )
300 else if( ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT )
302 al_clear_keyboard_state( display );
303 al_flush_event_queue( event_queue );
311 while( !al_is_event_queue_empty( event_queue) );
316 int mouse_button = -1 ;
325 if( mouse_button == 1 )
333 al_acknowledge_resize( display );
338 al_set_target_bitmap( scrbuf );
339 al_clear_to_color( al_map_rgba( 0, 0, 0, 255 ) );
347 al_set_target_bitmap( al_get_backbuffer( display ) );
348 al_clear_to_color( al_map_rgba( 0, 0, 0, 255 ) );
349 al_draw_bitmap( scrbuf, 0, 0, 0 );
352 al_draw_line( mx - 5, my, mx + 5, my, al_map_rgb( 255, 0, 0 ), 1 );
353 al_draw_line( mx, my + 5, mx, my - 5, al_map_rgb( 255, 0, 0 ), 1 );
360 while( !key[KEY_ESC] && !DONE );
void n_abort(char const *format,...)
abort program with a text
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
int set_log_file(char *file)
Set the logging to a file instead of stderr.
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
#define nstrprintf(__nstr_var,...)
Macro to quickly allocate and sprintf to N_STR
A box including a string and his lenght.
static FILE * log_file
static FILE handling if logging to file is enabled