diff --git a/src/sago/SagoSprite.cpp b/src/sago/SagoSprite.cpp index 3d59b20..7b6297e 100644 --- a/src/sago/SagoSprite.cpp +++ b/src/sago/SagoSprite.cpp @@ -64,7 +64,16 @@ void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) cons DrawScaled(target, frameTime, x, y, data->imgCord.w, data->imgCord.h); } +void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian) const { + SDL_Point center = {this->data->origin.x, this->data->origin.y}; + DrawScaledAndRotated(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, angleRadian, ¢er, SDL_FLIP_NONE); +} + void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const { + DrawScaledAndRotated(target, frameTime, x, y, w, h, 0.0, nullptr, SDL_FLIP_NONE); +} + +void SagoSprite::DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, const double angleRadian, const SDL_Point* center, const SDL_RendererFlip flip) const { if (!data->tex.get()) { std::cerr << "Texture is null!\n"; } @@ -79,7 +88,8 @@ void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y if (h > 0) { pos.h = h; } - SDL_RenderCopy(target, data->tex.get(), &rect, &pos); + double angleDegress = angleRadian/M_PI*180.0; + SDL_RenderCopyEx(target, data->tex.get(), &rect, &pos, angleDegress, center, flip); } void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const { diff --git a/src/sago/SagoSprite.hpp b/src/sago/SagoSprite.hpp index d57cbed..742cf76 100644 --- a/src/sago/SagoSprite.hpp +++ b/src/sago/SagoSprite.hpp @@ -44,6 +44,16 @@ public: void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) const; /** + * Draws the sprite to a given render window + * @param target The render window to draw on + * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation + * @param x Place to draw the sprite + * @param y Place to draw the sprite + * @param angleRadian Angle to rotate the sprite around origin before drawing + */ + void DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian) const; + + /** * Draws part of the sprite to a given render window * @param target The render window to draw on * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation @@ -71,8 +81,11 @@ public: * @param y Place to draw the sprite */ void DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const; + void DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const; - + void DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, + const double angleRadian, const SDL_Point* center, const SDL_RendererFlip flip) const; + /** * Set a different origin. Normally it is the top left cornor. But in some cases you might want to center the origin or tranform it for other reasons * @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default diff --git a/src/saland/GameDraw.cpp b/src/saland/GameDraw.cpp index 1bd5791..a2cad7d 100644 --- a/src/saland/GameDraw.cpp +++ b/src/saland/GameDraw.cpp @@ -83,13 +83,7 @@ void DrawLayer(SDL_Renderer* renderer, sago::SagoSpriteHolder* sHolder, const sa startY = 0; } for (int i = startX; i < tm.width && i < (topx+globalData.xsize)/32+1; ++i) { - /*if (i < 0) { - continue; - }*/ for (int j = startY; j < tm.height && j < (topy+globalData.ysize)/32+1; ++j) { - /*if (j < 0) { - continue; - }*/ uint32_t gid = sago::tiled::getTileFromLayer(tm, tm.layers.at(layer), i, j); if (gid == 0) { continue; @@ -189,7 +183,12 @@ void DrawProjectile(SDL_Renderer* target, sago::SagoSpriteHolder* sHolder, const (void)sHolder; (void)time; const sago::SagoSprite& mySprite = sHolder->GetSprite("effect_fireball"); - mySprite.Draw(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY); + double angleRadian = 0.0; + if (entity->directionY || entity->directionX) { + //atan2 is defined if either of directionY or directionX is not zero + angleRadian = atan2(entity->directionY, entity->directionX)-M_PI/2.0; + } + mySprite.DrawRotated(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY, angleRadian); DrawCollision(target, entity, offsetX, offsetY, drawCollision); }