Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » rotate with mouse

This thread is locked; no one can reply to it. rss feed Print
rotate with mouse
neil dwyer
Member #7,237
May 2006

I am trying to use the mouse to make a bitmap rotate(if the mouse moves along the x axis, the bitmap rotates accordingly). I have been trying to do it with get_mouse_mickeys() and rotate_sprite() but now luck. I do have a feeling I am doing it all wrong. Here's the code:

1#include <allegro.h>
2#include <stdlib.h>
3 
4int player1_x = 320;
5int player1_y = 240;
6int player2_x;
7int player2_y;
8int player1_rotate;
9int player2_rotate;
10int mickeyx = 0;
11int mickeyy = 0;
12 
13int player1_movement()
14{
15if(key[KEY_A])
16{
17player1_x --;
18}
19if(key[KEY_D])
20{
21player1_x ++;
22}
23if(key[KEY_W])
24{
25player1_y --;
26}
27if(key[KEY_S])
28{
29player1_y ++;
30}
31position_mouse(320, 240);
32get_mouse_mickeys(&mickeyx, &mickeyy);
33if(mickeyx > 1)
34{
35player1_rotate ++;
36}
37if(mickeyx < 1)
38{
39player1_rotate --;
40}
41}
42 
43int main(int argc, char *argv[])
44{
45allegro_init();
46install_keyboard();
47install_mouse();
48set_color_depth(16);
49set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
50 
51 
52BITMAP *buffer = create_bitmap(640,480);
53 
54BITMAP *player1 = load_bitmap("player.bmp", NULL);
55
56BITMAP *player2 = load_bitmap("player.bmp", NULL);
57
58while(!key[KEY_ESC])
59{
60player1_movement();
61 
62rotate_sprite(buffer, player1, player1_x, player1_y, player1_rotate);
63blit(buffer, screen,0,0,0,0,640,480);
64clear_bitmap(buffer);
65}
66 
67destroy_bitmap(player1);
68destroy_bitmap(player2);
69destroy_bitmap(buffer);
70 
71return 0;
72 
73}
74END_OF_MAIN()

Right now I am focusing on player1 just to get it working properly.

Vasco Freitas
Member #6,904
February 2006
avatar

You should have "if(mickeyx > 0)" and "if(mickeyx < 0)". And if you want it to rotate accordingly to the speed you move the mouse, you should have something like "player1_rotate += mickeyx * SPEED;". Probably there's something else wrong but that's all I see now. What happens when you try to rotate the bitmap?

neil dwyer
Member #7,237
May 2006

Yes I was going to do your idea but I wanted to see if I could get it to rotate first.
The program is working exactly as if I would have used draw_sprite instead of rotate_sprite.

Edit: Ok, it wasn't rotating because it had to be fixed, but the mouse isn't working.

Jonatan Hedborg
Member #4,886
July 2004
avatar

you are giving it an int. It wants a fixed. use itofix(plater1_rotate) in the rotate_sprite last argument

Go to: