Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [5]Bad application performance with image objekts.

This thread is locked; no one can reply to it. rss feed Print
[5]Bad application performance with image objekts.
canlot
Member #12,929
June 2011

Hello everyone.
I'm new with Allegro 5.
So i wrote a basic GUI example.
My problem are if i load a image file and draw it to the screen the CPU performance going to be over 30 % or more.
What can i do?
pls help.

#SelectExpand
1#include <stdio.h> 2 #include <stdlib.h> 3 #include <allegro5/allegro.h> 4 #include "allegro5/allegro_image.h" 5 #include <allegro5/allegro_font.h> 6 #include <allegro5/allegro_ttf.h> 7 8 const float FPS = 240; 9 const int SCREEN_W = 1000; 10 const int SCREEN_H = 800; 11 enum MYKEYS { 12 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT 13 }; 14 15 struct window 16 { 17 int window_x; 18 int window_y; 19 int close_x; 20 int close_y; 21 } window_1 = {200 ,100 ,485 ,105 }; 22 23 24 int create_bmp_objekt(ALLEGRO_BITMAP **bmp_objekt, int bmp_w, int bmp_h) 25 { 26 *bmp_objekt = al_create_bitmap(bmp_w, bmp_h); 27 if(!bmp_objekt) 28 { 29 printf(stderr, "failed to create bitmap!\n"); 30 return -1; 31 } 32 printf("Created BMP Objekt\n"); 33 34 } 35 36 int dyeing_objekt(ALLEGRO_BITMAP **objekt, ALLEGRO_DISPLAY **display, int red, int green, int blue) 37 { 38 al_set_target_bitmap(*objekt); 39 al_clear_to_color(al_map_rgb(red, green, blue)); 40 al_set_target_bitmap(al_get_backbuffer(*display)); 41 printf("BITMAP Objekt dyed with colors red: %i green: %i blue: %i\n", red, green, blue); 42 43 } 44 45 46 int main(int argc, char **argv) 47 { 48 49 if(!al_init()) { 50 printf(stderr, "failed to initialize allegro!\n"); 51 return -1; 52 } 53 printf("Allegro installed\n"); 54 55 al_init_image_addon(); 56 al_init_font_addon(); 57 al_init_ttf_addon(); 58 system("color 0a"); 59 ALLEGRO_FONT *font = al_load_ttf_font("Blox2.ttf", 16, 20); 60 char *text = "GUI"; 61 ALLEGRO_DISPLAY *display = NULL; 62 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 63 ALLEGRO_TIMER *timer = NULL; 64 65 ALLEGRO_BITMAP *close = NULL; 66 ALLEGRO_BITMAP *close_move = al_load_bitmap("close_move.bmp"); 67 ALLEGRO_BITMAP *close_unfull = al_load_bitmap("close_unfull.bmp"); 68 ALLEGRO_BITMAP *close_full = al_load_bitmap("close_full.bmp"); 69 ALLEGRO_BITMAP *window_top = al_load_bitmap("window_top.bmp"); 70 ALLEGRO_BITMAP *window_side = al_load_bitmap("window_side.bmp"); 71 ALLEGRO_BITMAP *window_bottom = al_load_bitmap("window_bottom.bmp"); 72 ALLEGRO_BITMAP *full = NULL; 73 int *mouse_x = NULL; 74 int *mouse_y = NULL; 75 int window_allow_create = 1; 76 int *maus_aktuell = calloc(2, sizeof(int)); 77 int move = 0; 78 int merker = 1; 79 80 if(!font) 81 { 82 printf("could not load font\n"); 83 } 84 printf("Font loaded\n"); 85 86 bool key[4] = { false, false, false, false }; 87 bool redraw = true; 88 bool doexit = false; 89 90 al_install_mouse(); 91 92 if(!al_install_keyboard()) { 93 printf(stderr, "failed to initialize the keyboard!\n"); 94 return -1; 95 } 96 printf("Keyboard installed\n"); 97 98 99 timer = al_create_timer(1.0 / FPS); 100 if(!timer) { 101 printf(stderr, "failed to create timer!\n"); 102 return -1; 103 } 104 printf("Timer created\n"); 105 106 display = al_create_display(SCREEN_W, SCREEN_H); 107 if(!display) { 108 printf(stderr, "failed to create display!\n"); 109 al_destroy_timer(timer); 110 return -1; 111 } 112 printf("Display created\n"); 113 114 create_bmp_objekt(&full, 290, 300); 115 dyeing_objekt(&full, &display, 72, 209, 204); 116 close = close_full; 117 118 event_queue = al_create_event_queue(); 119 120 if(!event_queue) { 121 fprintf(stderr, "failed to create event_queue!\n"); 122 al_destroy_display(display); 123 al_destroy_timer(timer); 124 return -1; 125 } 126 printf("Event queue created\n"); 127 128 al_register_event_source(event_queue , al_get_mouse_event_source()); 129 al_register_event_source(event_queue, al_get_display_event_source(display)); 130 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 131 al_register_event_source(event_queue, al_get_keyboard_event_source()); 132 133 al_clear_to_color(al_map_rgb(0,0,0)); 134 al_flip_display(); 135 al_start_timer(timer); 136 137 al_set_window_title(display,"Hallo Allegro"); 138 printf("Title created\n"); 139 140 while(!doexit) 141 { 142 143 ALLEGRO_EVENT ev; 144 al_wait_for_event(event_queue, &ev); 145 146 al_get_mouse_cursor_position(&mouse_x, &mouse_y); 147 printf("Mouse x: %d y: %d\n", mouse_x, mouse_y); 148 149 if(mouse_x == 0 && mouse_y == 0) 150 dyeing_objekt(&full ,&display , 150, 24, 58); 151 152 if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 153 { 154 if((ev.mouse.x >= window_1.close_x && ev.mouse.x <= window_1.close_x + 10) && (ev.mouse.y >= window_1.close_y && ev.mouse.y <= window_1.close_y + 10)) 155 { 156 close = close_unfull; 157 158 } 159 else if((ev.mouse.x >= window_1.window_x && ev.mouse.x <= window_1.window_x + 300) && (ev.mouse.y >= window_1.window_y && ev.mouse.y <= window_1.window_y + 20)) 160 { 161 move = 1; 162 } 163 164 } 165 else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) 166 { 167 if(move == 1) 168 { 169 if(merker == 1) 170 { 171 maus_aktuell[0] = ev.mouse.x - window_1.window_x; 172 maus_aktuell[1] = ev.mouse.y - window_1.window_y; 173 merker = 0; 174 } 175 window_1.window_x = ev.mouse.x - maus_aktuell[0]; 176 window_1.window_y = ev.mouse.y - maus_aktuell[1]; 177 window_1.close_x = window_1.window_x + 285; 178 window_1.close_y = window_1.window_y + 5; 179 } 180 181 if((ev.mouse.x >= window_1.close_x && ev.mouse.x <= window_1.close_x + 10) && (ev.mouse.y >= window_1.close_y && ev.mouse.y <= window_1.close_y + 10)) 182 { 183 close = close_move; 184 } 185 else 186 close = close_full; 187 188 } 189 190 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) 191 { 192 close = close_full; 193 if((ev.mouse.x >= window_1.close_x && ev.mouse.x <= window_1.close_x + 10) && (ev.mouse.y >= window_1.close_y && ev.mouse.y <= window_1.close_y + 10)) 194 { 195 close = close_unfull; 196 window_allow_create = 0; 197 198 } 199 else if((ev.mouse.x >= window_1.window_x && ev.mouse.x <= window_1.window_x + 300) && (ev.mouse.y >= window_1.window_y && ev.mouse.y <= window_1.window_y + 20)) 200 { 201 move = 0; 202 merker = 1; 203 } 204 } 205 206 if(ev.type == ALLEGRO_EVENT_TIMER) { 207 if(key[KEY_UP]) { 208 printf("UP Key pressed\n"); 209 text = "UP AROW"; 210 } 211 212 if(key[KEY_DOWN]) { 213 printf("DOWN Key pressed\n"); 214 text = "DOWN AROW"; 215 } 216 217 if(key[KEY_LEFT]) { 218 printf("LEFT Key pressed\n"); 219 text = "LEFT ARROW"; 220 } 221 222 if(key[KEY_RIGHT]) { 223 printf("RIGHT Key pressed\n"); 224 text = "RIGHT ARROW"; 225 } 226 227 redraw = true; 228 } 229 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 230 break; 231 } 232 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { 233 switch(ev.keyboard.keycode) { 234 case ALLEGRO_KEY_UP: 235 key[KEY_UP] = true; 236 break; 237 238 case ALLEGRO_KEY_DOWN: 239 key[KEY_DOWN] = true; 240 break; 241 242 case ALLEGRO_KEY_LEFT: 243 key[KEY_LEFT] = true; 244 break; 245 246 case ALLEGRO_KEY_RIGHT: 247 key[KEY_RIGHT] = true; 248 break; 249 } 250 } 251 252 else if(ev.type == ALLEGRO_EVENT_KEY_UP) { 253 switch(ev.keyboard.keycode) { 254 case ALLEGRO_KEY_UP: 255 key[KEY_UP] = false; 256 break; 257 258 case ALLEGRO_KEY_DOWN: 259 key[KEY_DOWN] = false; 260 break; 261 262 case ALLEGRO_KEY_LEFT: 263 key[KEY_LEFT] = false; 264 break; 265 266 case ALLEGRO_KEY_RIGHT: 267 key[KEY_RIGHT] = false; 268 break; 269 270 case ALLEGRO_KEY_ESCAPE: 271 doexit = true; 272 break; 273 } 274 } 275 276 if(redraw && al_is_event_queue_empty(event_queue)) { 277 redraw = false; 278 279 al_clear_to_color(al_map_rgb(0,0,0)); 280 281 if(window_allow_create == 1) 282 { 283 al_draw_bitmap(full, window_1.window_x + 5, window_1.window_y + 20, 0); 284 al_draw_bitmap(window_top, window_1.window_x , window_1.window_y , 0); 285 al_draw_bitmap(window_side, window_1.window_x, window_1.window_y + 20, 0); 286 al_draw_bitmap(window_side, window_1.window_x + 296, window_1.window_y + 19, 0); 287 al_draw_bitmap(window_bottom, window_1.window_x, window_1.window_y + 320, 0); 288 al_draw_bitmap(close, window_1.close_x ,window_1.close_y ,0); 289 290 al_draw_text(font, 291 al_map_rgb(250,205,170), 292 window_1.window_x + 10, window_1.window_y +2, 293 ALLEGRO_ALIGN_LEFT, 294 text); 295 } 296 else 297 { 298 doexit = true; 299 } 300 301 al_flip_display(); 302 303 } 304 } 305 306 al_destroy_timer(timer); 307 al_destroy_display(display); 308 al_destroy_event_queue(event_queue); 309 310 return 0; 311 }

I'm sorry for my bad Englisch, because it isn't my native language. ::)
Corrects my Englisch knowledge are welcome too ;D

Matthew Leverton
Supreme Loser
January 1999
avatar

Create the display before loading the bitmaps.

canlot
Member #12,929
June 2011

Thank: Matthew Leverton
It works and it is magic :D
;D ;D

J-Gamer
Member #12,491
January 2011
avatar

canlot said:

It works and it is magic

No, it isn't. Here's why:
When creating images without a display, allegro has to set them to memory bitmaps, which are slow and cpu-expensive when drawing. If there is a display, allegro can attach the bitmaps to it and make video bitmaps of them, which are hardware accelerated and thus require far less from the cpu.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Go to: