29int main(
int argc,
char* argv[]) {
41 n_abort(
"Could not init Allegro.\n");
43 if (!al_install_audio()) {
44 n_log(
LOG_ERR,
"Unable to initialize audio addon, audio will be disabled or crashing !");
46 if (!al_init_acodec_addon()) {
47 n_log(
LOG_ERR,
"Unable to initialize audio codec addon, audio will be disabled or crashing !");
49 if (!al_init_image_addon()) {
50 n_abort(
"Unable to initialize image addon\n");
52 if (!al_init_primitives_addon()) {
53 n_abort(
"Unable to initialize primitives addon\n");
55 if (!al_init_font_addon()) {
56 n_abort(
"Unable to initialize font addon\n");
58 if (!al_init_ttf_addon()) {
59 n_abort(
"Unable to initialize ttf_font addon\n");
61 if (!al_install_keyboard()) {
62 n_abort(
"Unable to initialize keyboard handler\n");
64 if (!al_install_mouse()) {
65 n_abort(
"Unable to initialize mouse handler\n");
67 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
69 event_queue = al_create_event_queue();
71 fprintf(stderr,
"failed to create event_queue!\n");
76 char ver_str[128] =
"";
77 while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
80 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)", argv[0]);
83 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
87 if (!strncmp(
"NOTICE", optarg, 6)) {
90 if (!strncmp(
"VERBOSE", optarg, 7)) {
93 if (!strncmp(
"ERROR", optarg, 5)) {
96 if (!strncmp(
"DEBUG", optarg, 5)) {
115 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG");
125 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile", argv[0]);
133 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
136 n_abort(
"Unable to create display\n");
138 al_set_window_title(
display, argv[0]);
140 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
153 int key[7] = {
false,
false,
false,
false,
false,
false,
false};
155 al_register_event_source(event_queue, al_get_display_event_source(
display));
159 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
160 al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
162 al_register_event_source(event_queue, al_get_keyboard_event_source());
163 al_register_event_source(event_queue, al_get_mouse_event_source());
165 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(
WIDTH,
HEIGHT);
177 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
178 int do_draw = 0, do_logic = 0;
180 al_clear_keyboard_state(NULL);
181 al_flush_event_queue(event_queue);
186 al_wait_for_event(event_queue, &ev);
188 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
189 switch (ev.keyboard.keycode) {
193 case ALLEGRO_KEY_DOWN:
196 case ALLEGRO_KEY_LEFT:
199 case ALLEGRO_KEY_RIGHT:
202 case ALLEGRO_KEY_ESCAPE:
205 case ALLEGRO_KEY_SPACE:
208 case ALLEGRO_KEY_LCTRL:
209 case ALLEGRO_KEY_RCTRL:
214 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
215 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:
240 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
241 if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
243 }
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
246 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
249 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
250 if (ev.mouse.button == 1)
252 if (ev.mouse.button == 2)
254 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
255 if (ev.mouse.button == 1)
257 if (ev.mouse.button == 2)
259 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
260 al_clear_keyboard_state(
display);
261 al_flush_event_queue(event_queue);
266 }
while (!al_is_event_queue_empty(event_queue));
270 int mouse_button = -1;
277 if (mouse_button == 1) {
283 al_acknowledge_resize(
display);
288 al_set_target_bitmap(scrbuf);
289 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
296 al_set_target_bitmap(al_get_backbuffer(
display));
297 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
298 al_draw_bitmap(scrbuf, 0, 0, 0);
301 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
302 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
308 }
while (!
key[KEY_ESC] && !
DONE);
312 al_uninstall_system();