Strange lags in really simple project
Skygjel

Hi, I'm actually total newbie so please don't scold me if i had murdered any program ;)

I'm trying to create little project, so i start from creating display, two bitmaps (one static 800 x800 and 2nd moveable 40x40) and bind lesser one to arrows. After I take first attempt to run program, i was able to see delay in move of lesser one; After long research i found that drawing large bitmap take much more time then expected.
I'll place here most important parts of program:
Resources.h:

#SelectExpand
1class player 2{ 3 public: 4 float player_x; 5 float player_y; 6 int lifes; 7 int firepower; 8 int speed_of_player; 9 ALLEGRO_BITMAP *player_image; 10 11}; 12class game 13{ 14 public: 15 ALLEGRO_DISPLAY *display; 16 ALLEGRO_EVENT_QUEUE *event_queue; 17 ALLEGRO_TIMER *timer; 18 ALLEGRO_DISPLAY_MODE disp_data; 19 player *tab2; 20 monster *tab; 21 int map[15][15]; 22 game(); 23 void gejm(); 24};

Functions.h

#SelectExpand
1void game::gejm() 2{ 3 ALLEGRO_BITMAP *kwadrat=NULL; 4 kwadrat=al_load_bitmap("square.bmp"); 5 player gracz; 6 gracz.player_x=500; 7 gracz.player_y=500; 8 gracz.player_image=NULL; 9 gracz.player_image=al_load_bitmap("player.bmp"); 10 gracz.speed_of_player=1; 11 bool redraw=false; 12 bool key[4]={false,false,false,false}; 13 14 al_get_display_mode(al_get_num_display_modes() - 1,&disp_data); 15 al_set_new_display_flags(ALLEGRO_FULLSCREEN); 16 display=al_create_display(disp_data.width,disp_data.height); 17 event_queue=al_create_event_queue(); 18 timer=al_create_timer(1.0/FPS); 19 al_set_target_bitmap(al_get_backbuffer(display)); 20 al_register_event_source(event_queue,al_get_keyboard_event_source()); 21 al_register_event_source(event_queue,al_get_timer_event_source(timer)); 22 al_register_event_source(event_queue,al_get_display_event_source(display)); 23 al_start_timer(timer); 24 25 while(1) 26 { 27 ALLEGRO_EVENT ev; 28 al_wait_for_event(event_queue,&ev); 29 if(ev.keyboard.keycode==ALLEGRO_KEY_ESCAPE) 30 break; 31 else 32 if(ev.type==ALLEGRO_EVENT_KEY_DOWN) 33 switch(ev.keyboard.keycode) 34 { 35 case ALLEGRO_KEY_LEFT: 36 key[KEY_LEFT]=true; 37 } 38 else 39 if(ev.type==ALLEGRO_EVENT_KEY_UP) 40 switch(ev.keyboard.keycode) 41 { 42 case ALLEGRO_KEY_LEFT: 43 key[KEY_LEFT]=false; 44 } 45 46 else 47 if(ev.type==ALLEGRO_EVENT_TIMER) 48 { 49 redraw=true; 50 //if(key[KEY_LEFT]) 51 gracz.player_x-=gracz.speed_of_player; 52 } 53 if(redraw==true&&al_is_event_queue_empty(event_queue)) 54 { 55 al_clear_to_color(al_map_rgb(0,0,0)); 56 al_draw_bitmap(square,0,0,0); 57 al_draw_bitmap(gracz.player_image,gracz.player_x,gracz.player_y,0); 58 al_flip_display(); 59 redraw=false; 60 61 } 62 63 } 64 al_destroy_display(display); 65 al_destroy_event_queue(event_queue); 66 al_destroy_timer(timer); 67} 68game::game() 69{ 70 monster a[10]; 71 player b[10]; 72 tab=&a[10]; 73 tab2=&b[10]; 74}

Main:

#SelectExpand
1#include"Functions.h" 2 3int main() 4{ 5 if(!initialize_allegro()) 6 { 7 return -1; 8 } 9 game gra; 10 gra.gejm(); 11 return 0; 12}

So if you would know where did i done mistake, i would be gratefull for pointing it ;)

PS. sorry for my bad English

jmasterx

1) Can you please supply the bitmap you use?
2) What is the expected behavior?
3) What is the current behavior?

Thanks

A large bitmap will almost certainly lead to lag spikes.

J-Gamer

There is also a really serious memory access bug in your game constructor:
game::game() { monster a[10]; player b[10]; tab=&a[10]; tab2=&b[10]; }
First of all, a and b are stack-allocated, and don't exist any more after the constructor ends. Second, you're making tab and tab2 point to the locations after the end of the arrays(ie: uninitialized memory).

Skygjel

I use simple white and red squares made in paint :/
I'm expecting from program to moving smoothly lesser square, while actually my squre make move after some time (less then 1 second but more then timer (1/60)) and he appears after all distance he should done through this time (sorry if i made it twisted but i don't know easier way to describe that).
I found that this if "if(redraw==true&&al_is_event_queue_empty(event_queue)) " is being made in after few events of timer (about 10-15 timers event beetwen update of image).

J-Gamer thanks for advice, but can i put arrays of other object into constructor? And eventually would you mind to explain how to do that? (Guess that it's basic but i'm reall newbie :/)

DanielH

1. You need to load bitmaps after you create the display. Otherwise this might cause some lag because the display and the bitmap are of different formats.

2. With your arrays, what are you trying to do? Would tab and tab2 ever be assigned to another set of arrays (dynamic) or are they static?

Static:

player tab2[10];
monster tab[10];

Dynamic:

#SelectExpand
1player *tab2; 2monster *tab; 3 4game::game() 5{ 6 tab2 = new player[10]; 7 tab = new monster[10]; 8} 9 10game::~game() 11{ 12 if (tab2) 13 { 14 delete tab2; 15 } 16 17 if (tab) 18 { 19 delete tab; 20 } 21}

3. Advice for the future, do some checks. I frequently look at other people's code (even my old code) and notice a serious lack of function verification. Is it that everyone assumes no function of theirs would ever fail? Your simple program might always work fine for now, but what about in the future? What about when you start a larger game?

display=al_create_display(disp_data.width,disp_data.height);
if (!display) { /* something went wrong*/ }

or simplify

if (!(display=al_create_display(disp_data.width,disp_data.height)) { /* something went wrong*/}

Skygjel

Thank you! It's working all right now. Thanks to evryone for quick replies ;).

Thread #614613. Printed from Allegro.cc