Htlcs
Htlcs

Reputation: 565

rotating a sprite image in SDL

I am making a 2D game using c++ SDL 2.0.

I got everything setup. I have different classes for my Sprite which uses SDL_QueryTexture to load all my sprite textures. I have a main player class which uses the sprite class to draw. I have another main class which runs my game loop and draw loop which again references both my player and sprite.

So I have everything working fine till now. I am able to move around my player on mouse click on the screen. Now what I want to do is essentially rotate my player in the direction it is moving. So if the player direction is left, I want to rotate my player sprite by 90 degrees to the left and so on for other directions. There is no rotate property for sdl textures so I am kind of stuck at this point.

Problem: Can this just be implemented using sin and cos angles?

I tried doing it but it does not work. Any help here would be appreciated.

Upvotes: 0

Views: 4420

Answers (1)

roymcclure
roymcclure

Reputation: 404

As Benjamin as pointed out in his comment, the signature of the method you use for rendering sprites

int SDL_RenderCopyEx(SDL_Renderer*          renderer,
                 SDL_Texture*           texture,
                 const SDL_Rect*        srcrect,
                 const SDL_Rect*        dstrect,
                 const double           angle,
                 const SDL_Point*       center,
                 const SDL_RendererFlip flip)

provides you with two parameters, one to set a pivot point and another to set angles of rotation. Please note that the pivot point takes the (0,0) of the sprite as the reference.

Upvotes: 1

Related Questions