|
Rotation problem |
Simsimius
Member #6,017
July 2005
|
I'm trying to get back into programming, but I'm still as useless as I've always been. So, starting from scratch with a certain template, I decided to start off with some basics - such as rotating a sprite. But that's... not as easy as I thought.
____ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Store the rotation in a variable, alter the variable when the turning keys are pressed, and display it using the rotation variable when you draw your buffer. The problem currently is that you draw the sprite into b when you press the rotation key, but when you call your Draw function, it just draws the sprite normally. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Simsimius
Member #6,017
July 2005
|
So, something like this? void GetControls() { if(key[KEY_UP]) { if(ypos - speed < 0) ypos = 0; else ypos -= speed; } if(key[KEY_DOWN]) { if(ypos + speed + sprite->h > SCREEN_H) ypos = SCREEN_H - sprite->h; else ypos += speed; } if(key[KEY_LEFT]) { if(xpos - speed < 0) xpos = 0; else xpos -= speed; } if(key[KEY_RIGHT]) { if(xpos + speed + sprite->w > SCREEN_W) xpos = SCREEN_W - sprite->w; else xpos += speed; } if(key[KEY_A]) { rot -= 40 } if(key[KEY_D]) { rot += 40} } void Draw(BITMAP* b) { //draw_sprite(b, sprite, xpos, ypos); rotate_sprite(b, sprite, xpos, ypos, itofix(rot)); } Although, I'm using the variable, and the intofix, wrongly, as it still isn't rotating. How should I be using it? ____ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Remember that rotate_sprite takes allegro degrees [0 to 256), so adjusting by 40 every time the key is down is a large turn. Are you calling Draw(buffer);, and then displaying your buffer onto the screen? Show more code. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Simsimius
Member #6,017
July 2005
|
Well, here's the code that draws to the buffer then screen.
It's not rotating at all.
____ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
You're still missing semicolon statement endings on these two lines : Are you calling MyPlayer.GetControls(); in your logic loop? If that's not it, just attach the whole source file. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Simsimius
Member #6,017
July 2005
|
It's being called. I'll paste the source code - it's basically just a template I got from AGDN that I always use as a base for any Allegro work.
____ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Quote: LOCK_FUNCTION((void*)Timer); The cast doesn't belong there. END_OF_MAIN() doesn't use a semi-colon at the end. There shouldn't be a comma after rot(0) in your constructor initialization list : // CPlayer(): xpos(0), ypos(0), speed(5), rot(0), { sprite = load_bitmap("test.bmp", NULL); } // It's also not a good idea to load bitmaps in your constructor or destroy bitmaps in your destructor because the objects can't be global, and you have to make another function call to see if they loaded properly anyway. Which brings us to - check the return values of functions that can fail like allegro_init() , set_gfx_mode(), load_bitmap(), etc... After fixing those things and replacing load_bitmap with create_bitmap, it ran fine for me. It also shouldn't have been compiling for you, as it came up with an error when the extra comma was there. Use -Wall to turn on most all of the compiler warnings. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Simsimius
Member #6,017
July 2005
|
Thanks for helping out. Quote:
Edgar Reynaldo: So, what do I do? Quote: It's also not a good idea to load bitmaps in your constructor or destroy bitmaps in your destructor because the objects can't be global, and you have to make another function call to see if they loaded properly anyway. Which brings us to - check the return values of functions that can fail like allegro_init() , set_gfx_mode(), load_bitmap(), etc... What should I do with the bitmaps instead? Quote: After fixing those things and replacing load_bitmap with create_bitmap, it ran fine for me. It also shouldn't have been compiling for you, as it came up with an error when the extra comma was there. Use -Wall to turn on most all of the compiler warnings. Turned it on. ____ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Quote: So, what do I do? Just get rid of the cast. LOCK_FUNCTION(Timer);
Quote: What should I do with the bitmaps instead? Just make some load/unload member functions. That way you can detect and return an error if anything is missing or doesn't load properly. Or you can separate out all your loading and resource freeing calls into global load_resources() and free_resources() functions. If the loading is successful, just assign the appropriate BITMAP*s to the objects that need them. Another option is to pass the BITMAP* as a parameter to your constructor after it has been loaded and verified. In that case, make sure not to destroy it in the destructor, as it is only using it as a reference and doesn't really own the resource, but rather the free_resources() function does. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Simsimius
Member #6,017
July 2005
|
I'm still somewhat confused. public: int sload( sprite = create_bitmap("test.bmp", NULL);); right? And then call the function elsewhere in the class - where exactly should that be? I apologize for asking such questions, but it's been a very long time since I touched this stuff. I thought this would a simple thing to do [rotating a sprite], that would be a good step back and get back with everything. It appears I was mistaken . ____ |
Don Freeman
Member #5,110
October 2004
|
Don't give up so easily. You are nearly there! You can call it anywhere after you create your object and you have initiated allegro and setup the video mode. Just make sure that the function returns at least a bool if the function did indeed succeed in loading the bitmap.
Then you use it like:
-- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Quote:
I'm still somewhat confused. Sure, or you can leave the loading out of your class, and just set the sprite in your constructor call after it's been loaded and verified.
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
jclarkrichards
Member #10,404
November 2008
|
Hey, I'm not a C++ programmer, but here's a simple program in C that I wrote that might help with your problem. This basically just takes an image and rotates it. You rotate the image by passing different angles to the itofix() function depending on the key you pressed. For the image to be facing to the left, the angle has to be 0, facing right, the angle is 128, facing up, angle is 64, and facing down, angle is 192. Hope this helps. I should also mention that the initial direction of the sprite in this example is facing left. So, if your sprite initially faces another direction, you may just need to rearrange then angles in the rotateImage() function.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Use code tags please. You can edit the post with the pen and paper icon that appears in your post, and in the post editing page there is a help button that explains the rest of the forum markup tags. <code> Code goes here... </code>
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Simsimius
Member #6,017
July 2005
|
Alright, I'll try it now. I'll mark this thread as "solved". ____ |
|