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[]) {
39 n_abort(
"Could not init Allegro.\n");
41 if (!al_install_audio()) {
42 n_log(
LOG_ERR,
"Unable to initialize audio addon, audio will be disabled or crashing !");
44 if (!al_init_acodec_addon()) {
45 n_log(
LOG_ERR,
"Unable to initialize audio codec addon, audio will be disabled or crashing !");
47 if (!al_init_image_addon()) {
48 n_abort(
"Unable to initialize image addon\n");
50 if (!al_init_primitives_addon()) {
51 n_abort(
"Unable to initialize primitives addon\n");
53 if (!al_init_font_addon()) {
54 n_abort(
"Unable to initialize font addon\n");
56 if (!al_init_ttf_addon()) {
57 n_abort(
"Unable to initialize ttf_font addon\n");
59 if (!al_install_keyboard()) {
60 n_abort(
"Unable to initialize keyboard handler\n");
62 if (!al_install_mouse()) {
63 n_abort(
"Unable to initialize mouse handler\n");
65 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
67 event_queue = al_create_event_queue();
69 fprintf(stderr,
"failed to create event_queue!\n");
70 al_destroy_display(display);
74 char ver_str[128] =
"";
75 while ((getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
78 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
81 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
85 if (!strncmp(
"NOTICE", optarg, 6)) {
88 if (!strncmp(
"VERBOSE", optarg, 7)) {
91 if (!strncmp(
"ERROR", optarg, 5)) {
94 if (!strncmp(
"DEBUG", optarg, 5)) {
113 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
116 n_log(
LOG_ERR,
"\nPlease specify a log file after -L\n");
121 __attribute__((fallthrough));
123 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
128 fps_timer = al_create_timer(1.0 / 30.0);
129 logic_timer = al_create_timer(1.0 / 50.0);
131 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
132 display = al_create_display(WIDTH, HEIGHT);
134 n_abort(
"Unable to create display\n");
136 al_set_window_title(display, argv[0]);
138 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
151 int key[7] = {
false,
false,
false,
false,
false,
false,
false};
153 al_register_event_source(event_queue, al_get_display_event_source(display));
155 al_start_timer(fps_timer);
156 al_start_timer(logic_timer);
157 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
158 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
160 al_register_event_source(event_queue, al_get_keyboard_event_source());
161 al_register_event_source(event_queue, al_get_mouse_event_source());
163 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
165 al_hide_mouse_cursor(display);
175 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
176 int do_draw = 0, do_logic = 0;
178 al_clear_keyboard_state(NULL);
179 al_flush_event_queue(event_queue);
184 al_wait_for_event(event_queue, &ev);
186 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
187 switch (ev.keyboard.keycode) {
191 case ALLEGRO_KEY_DOWN:
194 case ALLEGRO_KEY_LEFT:
197 case ALLEGRO_KEY_RIGHT:
200 case ALLEGRO_KEY_ESCAPE:
203 case ALLEGRO_KEY_SPACE:
206 case ALLEGRO_KEY_LCTRL:
207 case ALLEGRO_KEY_RCTRL:
212 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
213 switch (ev.keyboard.keycode) {
217 case ALLEGRO_KEY_DOWN:
220 case ALLEGRO_KEY_LEFT:
223 case ALLEGRO_KEY_RIGHT:
226 case ALLEGRO_KEY_ESCAPE:
229 case ALLEGRO_KEY_SPACE:
232 case ALLEGRO_KEY_LCTRL:
233 case ALLEGRO_KEY_RCTRL:
238 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
239 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
241 }
else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
244 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
247 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
248 if (ev.mouse.button == 1)
250 if (ev.mouse.button == 2)
252 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
253 if (ev.mouse.button == 1)
255 if (ev.mouse.button == 2)
257 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
258 al_clear_keyboard_state(display);
259 al_flush_event_queue(event_queue);
264 }
while (!al_is_event_queue_empty(event_queue));
268 int mouse_button = -1;
275 if (mouse_button == 1) {
281 al_acknowledge_resize(display);
286 al_set_target_bitmap(scrbuf);
287 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
294 al_set_target_bitmap(al_get_backbuffer(display));
295 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
296 al_draw_bitmap(scrbuf, 0, 0, 0);
299 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
300 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
306 }
while (!key[KEY_ESC] && !DONE);
308 al_destroy_bitmap(scr_buf);
310 al_uninstall_system();
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, __format,...)
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