Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » program running slow...

This thread is locked; no one can reply to it. rss feed Print
program running slow...
xirb22
Member #13,917
January 2012

This program is running extremely slow, even though my pc is a beast...
I think it has to do because of it having to do too much every timer event, but I can't manage to do it any other way.
I need to draw some text and a bullseye, which stays in the same location until it's clicked so it doesn't really need to be constantly redrawn, and a crosshair which is used as cursor (ALLEGRO_CURSOR won't work but I'm looking into it soon) and needs to be redrawn every tick.
I really don't get why everythink takes so long, because when I press escape or ALT+F4, it takes 3 to 20 seconds to close, depending on how long it has ran.
Don't bother looking at the rest of the code because I'm still messing around and testing stuff.

#SelectExpand
1#include <iostream> 2#include <stdlib.h> 3#include <stdio.h> 4#include <allegro5\allegro.h> 5#include <allegro5\allegro_image.h> 6#include <allegro5\allegro_primitives.h> 7#include <allegro5\allegro_native_dialog.h> 8#include <allegro5\allegro_ttf.h> 9 10const int window_w = 800, 11 window_h = 600; 12 13using namespace std; 14 15int main() { 16 al_init(); 17 al_init_primitives_addon(); 18 al_init_image_addon(); 19 al_init_font_addon(); 20 al_init_ttf_addon(); 21 al_install_keyboard(); 22 al_install_mouse(); 23 srand(time(NULL)); 24 25 bool running = true, 26 hit = true, 27 redraw_bullseye = true; 28 int test = 50; 29 int score = 1; 30 unsigned int bullseye_x,bullseye_y; 31 int crosshair_x,crosshair_y; 32 enum direction {UP, DOWN, RIGHT, LEFT}; 33 34 ALLEGRO_DISPLAY* main_display; 35 ALLEGRO_EVENT_QUEUE* main_queue = al_create_event_queue(); 36 ALLEGRO_TIMER* main_timer = al_create_timer(1.0/60.0); 37 ALLEGRO_BITMAP* bullseye = al_load_bitmap("bullseye.png"); 38 ALLEGRO_BITMAP* crosshair = al_load_bitmap("crosshair.png"); 39 ALLEGRO_EVENT window_event; 40 ALLEGRO_DISPLAY_MODE display_data; 41 ALLEGRO_FONT* arialblack = al_load_ttf_font("ariblk.ttf",20,0); 42 ALLEGRO_COLOR white = al_map_rgb(255,255,255); 43 44 al_get_display_mode(al_get_num_display_modes()-1,&display_data); 45 al_set_new_display_flags(ALLEGRO_FULLSCREEN); 46 main_display = al_create_display(display_data.width,display_data.height); 47 al_register_event_source(main_queue,al_get_display_event_source(main_display)); 48 al_register_event_source(main_queue,al_get_timer_event_source(main_timer)); 49 al_register_event_source(main_queue,al_get_keyboard_event_source()); 50 al_register_event_source(main_queue,al_get_mouse_event_source()); 51 al_clear_to_color(al_map_rgb(0,0,0)); 52 al_hide_mouse_cursor(main_display); 53 al_start_timer(main_timer); 54 bullseye_x = 500; 55 bullseye_y = 500; 56 57 while(running) { 58 al_get_next_event(main_queue,&window_event); 59 switch(window_event.type) { 60 case ALLEGRO_EVENT_DISPLAY_CLOSE: 61 running = false; 62 break; 63 case ALLEGRO_EVENT_KEY_DOWN: 64 if (window_event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 65 running = false; 66 else break; 67 case ALLEGRO_EVENT_TIMER: 68 al_get_mouse_cursor_position(&crosshair_x,&crosshair_y); 69 al_clear_to_color(al_map_rgb(0,0,0)); 70 al_draw_bitmap(bullseye,bullseye_x,bullseye_y,0); 71 al_draw_textf(arialblack,purple,10,30,0,"bullseye_x: %d",bullseye_x); 72 al_draw_textf(arialblack,purple,10,60,0,"bullseye_y: %d",bullseye_y); 73 al_draw_bitmap(crosshair,crosshair_x-25,crosshair_y-25,0); 74 al_flip_display(); 75 if(hit == true) { 76 bullseye_x = (time(0) * time(0) / window_h + window_w*3) % (window_w - 200); 77 bullseye_y = (bullseye_x*time(0) / 4) % (window_h - 200); 78 hit = false; 79 } 80 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 81 al_get_mouse_cursor_position(&crosshair_x,&crosshair_y); 82 if((window_event.mouse.x+25 > bullseye_x && window_event.mouse.x+25 < bullseye_x+200) 83 && (window_event.mouse.y+25 > bullseye_y && window_event.mouse.y+25 < bullseye_y+200)) { 84 hit = true; 85 } 86 87 } 88 } 89 al_destroy_bitmap(bullseye); 90 al_destroy_bitmap(crosshair); 91 al_destroy_display(main_display); 92 al_destroy_timer(main_timer); 93 al_destroy_event_queue(main_queue); 94 exit(EXIT_SUCCESS); 95}

Thanks in advance :)

SiegeLord
Member #7,827
October 2006
avatar

Load bitmaps after creating the display, not before. If you load them before, you load them as memory bitmaps which are very slow to draw.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

xirb22
Member #13,917
January 2012

Totally fixed it!
thanks again, you already helped me on my other thread xD

Go to: