Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5, Sprite not following mouse.

This thread is locked; no one can reply to it. rss feed Print
Allegro 5, Sprite not following mouse.
Ross Martin
Member #15,080
April 2013

Hello all,
I am currently having problems with the following code. I am not able to have the mouseCursor sprite follow the mouse. It is just stuck at the initial 20, 20 position. Any help would be greatly appreciated

Ross

#SelectExpand
1#include<iostream> 2#include<allegro5\allegro5.h> 3#include<allegro5\allegro_native_dialog.h> 4#include<allegro5\allegro_image.h> 5 6#define Height 640 7#define Width 640 8 9void render(); 10void cursor(); 11bool collision(); 12void loadImages(); 13void spawnControll(); 14 15 16enum direction{down, left, right, up}; 17enum color {red, green, blue}; 18ALLEGRO_DISPLAY *display; 19 20class bubbleEnemy 21{ 22 public: 23 int x , y, moveSpeed, color; 24 void setUpBubble(); 25 26}; 27 28void setUpBubble() 29{ 30 31} 32 33class projectile 34{ 35public: 36 projectile(); 37 ~projectile(); 38 int movespeed, color, x, y, targetX, targetY; 39 ALLEGRO_BITMAP *projectileSprite; 40 41}; 42projectile::projectile() 43{ 44 45 46} 47projectile::~projectile() 48{ 49 50} 51 52class player 53{ 54 public: 55 player(); 56 ~player(); 57 void loadImages(); 58 void playerDraw(); 59 void animate(bool active); 60 int playerMoveSpeed; 61 int sourceX, sourceY; 62 int dir; 63 ALLEGRO_BITMAP *playerSprite; 64 float playerX, playerY; 65 66}; 67 68player::player() 69{ 70 playerMoveSpeed = 4; 71 int dir = down; 72 playerX = 300; 73 playerY = 300; 74 75} 76player::~player() 77{ 78 al_destroy_bitmap(playerSprite); 79} 80 81void player::playerDraw(){ 82 al_draw_bitmap_region(playerSprite, sourceX, sourceY * al_get_bitmap_height(playerSprite) / 4, 32, 32, playerX, playerY, NULL); 83} 84 85void player::animate(bool active) 86{ 87 if(active) 88 sourceX += al_get_bitmap_width(playerSprite) / 3; 89 else 90 sourceX = 32; 91 92 if(sourceX >= al_get_bitmap_width(playerSprite)) 93 sourceX = 0; 94 95 sourceY = dir; 96 97} 98 99class mousePointer 100{ 101public: 102 int mousePointerX, mousePointerY; 103 mousePointer(); 104 ~mousePointer(); 105 ALLEGRO_BITMAP *mousePointerSprite; 106 void mouseDraw(); 107}; 108 109mousePointer::mousePointer() 110{ 111 mousePointerX = 20; 112 mousePointerY = 20; 113} 114 115mousePointer::~mousePointer() 116{ 117 al_destroy_bitmap(mousePointerSprite); 118} 119 120void mousePointer::mouseDraw() 121{ 122 al_draw_bitmap(mousePointerSprite, mousePointerX, mousePointerY, NULL ); 123} 124 125bool done, paused, active; 126bubbleEnemy enemyList[100]; 127int enemyactive[100]; 128projectile projectilelist[4]; 129mousePointer mouseCursor; 130player player1; 131 132int main() 133{ 134 al_init(); 135 al_install_keyboard(); 136 al_init_image_addon(); 137 al_install_mouse(); 138 done = false; 139 int drawSpeed = 10, FPS = 60; 140 141 display = al_create_display(Width, Height); 142 143 if(!display) 144 al_show_native_message_box(NULL, "Error" , NULL, "Could not create Allegro Display" , NULL, NULL); 145 146 al_set_window_position(display, 200, 200); 147 done = false; 148 149 ALLEGRO_KEYBOARD_STATE keyState; 150 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 151 152 ALLEGRO_TIMER *drawTimer = al_create_timer(1.0/drawSpeed); 153 ALLEGRO_TIMER *fpsTimer = al_create_timer(1.0/FPS); 154 155 al_register_event_source(event_queue, al_get_timer_event_source(drawTimer)); 156 al_register_event_source(event_queue, al_get_timer_event_source(fpsTimer)); 157 al_register_event_source(event_queue, al_get_display_event_source(display)); 158 al_register_event_source(event_queue, al_get_keyboard_event_source()); 159 al_register_event_source(event_queue, al_get_mouse_event_source()); 160 161 loadImages(); 162 al_hide_mouse_cursor(display); 163 164 al_start_timer(fpsTimer); 165 al_start_timer(drawTimer); 166 167 while(!done) 168 { 169 ALLEGRO_EVENT events; 170 al_wait_for_event(event_queue, &events); 171 al_get_keyboard_state(&keyState); 172 173 if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 174 { 175 done = true; 176 break; 177 } 178 else if(events.type == ALLEGRO_EVENT_TIMER) 179 { 180 if(events.timer.source == fpsTimer) 181 { 182 active = true; 183 if(events.type == ALLEGRO_EVENT_MOUSE_AXES) 184 { 185 mouseCursor.mousePointerY = events.mouse.y; 186 mouseCursor.mousePointerX = events.mouse.x; 187 } 188 else if(al_key_down(&keyState, ALLEGRO_KEY_S)) 189 { 190 player1.playerY += player1.playerMoveSpeed; 191 player1.dir = down; 192 } 193 else if(al_key_down(&keyState, ALLEGRO_KEY_W)) 194 { 195 player1.playerY -= player1.playerMoveSpeed; 196 player1.dir = up; 197 } 198 else if(al_key_down(&keyState, ALLEGRO_KEY_A)) 199 { 200 player1.playerX -= player1.playerMoveSpeed; 201 player1.dir = left; 202 } 203 else if(al_key_down(&keyState, ALLEGRO_KEY_D)) 204 { 205 player1.playerX += player1.playerMoveSpeed; 206 player1.dir = right; 207 } 208 209 else 210 active = false; 211 212 213 214 } 215 216 else if(events.timer.source == drawTimer) 217 { 218 player1.animate(active); 219 } 220 221 222 } 223 render(); 224 225 } 226 player1.~player(); 227 mouseCursor.~mousePointer(); 228 al_destroy_timer(fpsTimer); 229 al_destroy_timer(drawTimer); 230 al_destroy_event_queue(event_queue); 231 al_destroy_display(display); 232} 233 234void spawnControll() 235{ 236 237} 238 239bool collision(ALLEGRO_BITMAP *player) 240{ 241 242} 243 244void render() 245{ 246 247 player1.playerDraw(); 248 mouseCursor.mouseDraw(); 249 al_flip_display(); 250 al_clear_to_color(al_map_rgb(0,0,0)); 251} 252 253void loadImages(){ 254 255 player1.playerSprite = al_load_bitmap("playerImage.png"); 256 mouseCursor.mousePointerSprite = al_load_bitmap("pointer.png"); 257 al_convert_mask_to_alpha(mouseCursor.mousePointerSprite, al_map_rgb(255,255,255)); 258}

pkrcel
Member #14,001
February 2012

do you ever get the MOUSE_AXES event at all?

I'm actually unsure that nesting it into the timer event may mask it at all and you get a MOUSE_WARPED event (?)

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

taron 
Member #10,584
January 2009
avatar

else if(events.type == ALLEGRO_EVENT_TIMER)
       {
           if(events.timer.source == fpsTimer)
           {
               active = true;
               if(events.type == ALLEGRO_EVENT_MOUSE_AXES)
                   {
                       mouseCursor.mousePointerY = events.mouse.y;
                       mouseCursor.mousePointerX = events.mouse.x;
                   }

events.type will not just magically change.
Check for ALLEGRO_EVENT_MOUSE_AXES events outside of your timer event.

Ross Martin
Member #15,080
April 2013

Thank you. That was the problem i don't quite understand why, but it fixed it. Thanks :)

Christopher Bludau
Member #5,401
January 2005

i don't quite understand why

// Let 's say events.type is ALLEGRO_EVENT_TIMER
else if(events.type == ALLEGRO_EVENT_TIMER) // then THIS is true
       {
       [...snip...]
               if(events.type == ALLEGRO_EVENT_MOUSE_AXES) // this will never be true because events.type is STILL ALLEGRO_EVENT_TIMER

Does that make things clearer? Would be good if you understand why this solution fixed your problem. Give a man a fish... etc :D

Go to: