Nilorea Library gui api test.
#define WIDTH 640
#define HEIGHT 480
#define ALLEGRO_UNSTABLE 1
int main(
int argc,
char* argv[]) {
if (!al_init()) {
n_abort(
"Could not init Allegro.\n");
}
if (!al_install_audio()) {
n_log(
LOG_ERR,
"Unable to initialize audio addon, audio will be disabled or crashing !");
}
if (!al_init_acodec_addon()) {
n_log(
LOG_ERR,
"Unable to initialize audio codec addon, audio will be disabled or crashing !");
}
if (!al_init_image_addon()) {
n_abort(
"Unable to initialize image addon\n");
}
if (!al_init_primitives_addon()) {
n_abort(
"Unable to initialize primitives addon\n");
}
if (!al_init_font_addon()) {
n_abort(
"Unable to initialize font addon\n");
}
if (!al_init_ttf_addon()) {
n_abort(
"Unable to initialize ttf_font addon\n");
}
if (!al_install_keyboard()) {
n_abort(
"Unable to initialize keyboard handler\n");
}
if (!al_install_mouse()) {
n_abort(
"Unable to initialize mouse handler\n");
}
ALLEGRO_EVENT_QUEUE* event_queue = NULL;
event_queue = al_create_event_queue();
if (!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
return -1;
}
char ver_str[128] = "";
while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
case 'h':
n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)", argv[0]);
exit(TRUE);
case 'v':
sprintf(ver_str, "%s %s", __DATE__, __TIME__);
exit(TRUE);
break;
case 'V':
if (!strncmp("NOTICE", optarg, 6)) {
} else {
if (!strncmp("VERBOSE", optarg, 7)) {
} else {
if (!strncmp("ERROR", optarg, 5)) {
} else {
if (!strncmp("DEBUG", optarg, 5)) {
} else {
exit(FALSE);
}
}
}
}
break;
case 'L':
break;
case '?': {
switch (optopt) {
case 'V':
n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG");
break;
case 'L':
default:
break;
}
}
default:
n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile", argv[0]);
exit(FALSE);
}
}
al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
n_abort(
"Unable to create display\n");
}
al_set_window_title(
display, argv[0]);
al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
enum APP_KEYS {
KEY_UP,
KEY_DOWN,
KEY_LEFT,
KEY_RIGHT,
KEY_ESC,
KEY_SPACE,
KEY_CTRL
};
int key[7] = {
false,
false,
false,
false,
false,
false,
false};
al_register_event_source(event_queue, al_get_display_event_source(
display));
al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_mouse_event_source());
ALLEGRO_BITMAP* scrbuf = al_create_bitmap(
WIDTH,
HEIGHT);
int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
int do_draw = 0, do_logic = 0;
al_clear_keyboard_state(NULL);
al_flush_event_queue(event_queue);
do {
do {
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
break;
case ALLEGRO_KEY_DOWN:
break;
case ALLEGRO_KEY_LEFT:
break;
case ALLEGRO_KEY_RIGHT:
break;
case ALLEGRO_KEY_ESCAPE:
break;
case ALLEGRO_KEY_SPACE:
break;
case ALLEGRO_KEY_LCTRL:
case ALLEGRO_KEY_RCTRL:
default:
break;
}
} else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
break;
case ALLEGRO_KEY_DOWN:
break;
case ALLEGRO_KEY_LEFT:
break;
case ALLEGRO_KEY_RIGHT:
break;
case ALLEGRO_KEY_ESCAPE:
break;
case ALLEGRO_KEY_SPACE:
break;
case ALLEGRO_KEY_LCTRL:
case ALLEGRO_KEY_RCTRL:
default:
break;
}
} else if (ev.type == ALLEGRO_EVENT_TIMER) {
if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
do_draw = 1;
}
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
do_logic = 1;
}
} else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
mx = ev.mouse.x;
my = ev.mouse.y;
} else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
if (ev.mouse.button == 1)
mouse_b1 = 1;
if (ev.mouse.button == 2)
mouse_b2 = 1;
} else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
if (ev.mouse.button == 1)
mouse_b1 = 0;
if (ev.mouse.button == 2)
mouse_b2 = 0;
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
al_flush_event_queue(event_queue);
} else {
}
} while (!al_is_event_queue_empty(event_queue));
int mouse_button = -1;
if (mouse_b1 == 1)
mouse_button = 1;
if (mouse_b2 == 1)
mouse_button = 2;
if (do_logic == 1) {
if (mouse_button == 1) {
}
do_logic = 0;
}
if (do_draw == 1) {
al_set_target_bitmap(scrbuf);
al_clear_to_color(al_map_rgba(0, 0, 0, 255));
al_set_target_bitmap(al_get_backbuffer(
display));
al_clear_to_color(al_map_rgba(0, 0, 0, 255));
al_draw_bitmap(scrbuf, 0, 0, 0);
al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
al_flip_display();
do_draw = 0;
}
al_uninstall_system();
return 0;
}
ALLEGRO_TIMER * fps_timer
ALLEGRO_TIMER * logic_timer
ALLEGRO_DISPLAY * display
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