Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Slow movement over background

This thread is locked; no one can reply to it. rss feed Print
Slow movement over background
agonvs
Member #15,917
March 2015

Hi all,

Any ideas why the ball moves so slowly over this background?

#SelectExpand
1#include<allegro5/allegro.h> 2#include<allegro5/allegro_primitives.h> 3#include<allegro5/allegro_image.h> 4#include<iostream> 5using namespace std; 6 7void render() 8{ 9 cout << endl; 10} 11 12enum directions {STILL, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST}; 13enum buttons {NONE,ZERO,ONE,EIGHT,NINE}; 14 15int main() 16{ 17 18 float gameTime = 0; 19 int frames = 0; 20 int gameFPS = 0; 21 if(!(al_init())) 22 cout << "Couldn't initialize Allegro." << cout; 23 if(!(al_install_joystick())) 24 cout << "Couldn't install joystick." << endl; 25 al_init_image_addon(); 26 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 27 event_queue = al_create_event_queue(); 28 if(!event_queue) 29 cout << "al_create_event_queue failed." << endl; 30 ALLEGRO_TIMER *timer; 31 timer = al_create_timer(1.0/60); 32 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 33 al_start_timer(timer); 34 gameTime = al_current_time(); 35 al_register_event_source(event_queue, al_get_joystick_event_source()); 36 ALLEGRO_JOYSTICK* joy = al_get_joystick(0); 37 float ballposX=0; 38 float ballposY=0; 39 ALLEGRO_JOYSTICK_STATE jst; 40 directions dir = STILL; 41 buttons button = NONE; 42 bool render = false; 43 ALLEGRO_DISPLAY *display = NULL; 44 ALLEGRO_BITMAP *image = NULL; 45 ALLEGRO_BITMAP *texture = NULL; 46 texture = al_load_bitmap("texture.jpg"); 47 image = al_load_bitmap("goldenball.png"); 48 display = al_create_display(1280,720); 49 bool alive = true; 50 while(alive) 51 { 52 ALLEGRO_EVENT ev; 53 al_wait_for_event(event_queue, &ev); 54 al_get_joystick_state(joy, &jst); 55 if (ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 56 { 57 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 0)) 58 dir = STILL; 59 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == -1)) 60 dir = NORTH; 61 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == -1)) 62 dir = NORTHEAST; 63 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 0)) 64 dir = EAST; 65 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 1)) 66 dir = SOUTHEAST; 67 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 1)) 68 dir = SOUTH; 69 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 1)) 70 dir = SOUTHWEST; 71 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 0)) 72 dir = WEST; 73 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == -1)) 74 dir = NORTHWEST; 75 76 } 77 if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) 78 { 79 if (jst.button[0]) 80 button = ZERO; 81 if (jst.button[1]) 82 button = ONE; 83 if (jst.button[8]) 84 button = EIGHT; 85 if (jst.button[9]) 86 button = NINE; 87 } 88 else if (ev.type == ALLEGRO_EVENT_TIMER) 89 { 90 render = true; 91 frames++; 92 if (al_current_time() - gameTime >= 1) 93 { 94 gameTime = al_current_time(); 95 gameFPS = frames; 96 frames = 0; 97 } 98 } 99 if (render && al_is_event_queue_empty(event_queue)) 100 { 101 render = false; 102 switch (dir) 103 { 104 case NORTH: 105 ballposY -= 3; 106 break; 107 case NORTHEAST: 108 ballposY -= 2; 109 ballposX += 2; 110 break; 111 case EAST: 112 ballposX += 3; 113 break; 114 case SOUTHEAST: 115 ballposY += 2; 116 ballposX += 2; 117 break; 118 case SOUTH: 119 ballposY += 3; 120 break; 121 case SOUTHWEST: 122 ballposX -= 2; 123 ballposY += 2; 124 break; 125 case WEST: 126 ballposX -= 3; 127 break; 128 case NORTHWEST: 129 ballposY -= 2; 130 ballposX -= 2; 131 break; 132 } 133 switch (button) 134 { 135 case ZERO: 136 if (jst.button[0]) 137 image = al_load_bitmap("goldenball.png"); 138 break; 139 case ONE: 140 if (jst.button[1]) 141 image = al_load_bitmap("silverball.png"); 142 break; 143 case EIGHT: 144 if (jst.button[8]) 145 break; 146 case NINE: 147 if (jst.button[9]) 148 alive = false; 149 break; 150 } 151 152 153// BTW I've tried this with a grid of 128x128 tiles. Both JPEG and pcx with identical results. 154 al_draw_bitmap(image, ballposX, ballposY, 0); 155 al_flip_display(); 156 al_clear_to_color(al_map_rgb(0,0,0)); 157 al_draw_bitmap(texture, 0,0,0); 158 } 159 } 160 al_destroy_bitmap(image); 161 al_destroy_timer(timer); 162 al_destroy_event_queue(event_queue); 163 return 0; 164}

Elias
Member #358
May 2000

[Edit] ah, read the code wrong

Try loading the bitmaps after the display is created.

--
"Either help out or stop whining" - Evert

agonvs
Member #15,917
March 2015

Read it wrong? You mean me? How so?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Also, don't start your timer until you enter your main loop. You're doing it before loading bitmaps and creating the display which takes time, which means timer events will pile up before you get started.

And you are also loading your golden and silver ball bitmaps every time the button is pressed and a frame is rendered. That is a big memory leak. You should only load them once and then use them later by assignment.

ALLEGRO_BITMAP* goldenball = al_load_bitmap("goldenball.png");
ALLEGRO_BITMAP* silverball = al_load_bitmap("silverball.png");
if (!goldenball || !silverball) {
   return 1;
}
ALLEGRO_BITMAP* ball = goldenball;

//...

if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) {
//
   if (button[0]) {ball = goldenball;}
   if (button[1]) {ball = silverball;}

Chris Katko
Member #1,881
January 2002
avatar

agonvs said:

Read it wrong? You mean me? How so?

No, he means what he originally wrote doesn't apply upon further inspection because he misread your code.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Go to: