| 1 | /* |
| 2 | Copyright (c) 2015 Poul Sander |
| 3 | |
| 4 | Permission is hereby granted, free of charge, to any person |
| 5 | obtaining a copy of this software and associated documentation files |
| 6 | (the "Software"), to deal in the Software without restriction, |
| 7 | including without limitation the rights to use, copy, modify, merge, |
| 8 | publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | and to permit persons to whom the Software is furnished to do so, |
| 10 | subject to the following conditions: |
| 11 | |
| 12 | The above copyright notice and this permission notice shall be |
| 13 | included in all copies or substantial portions of the Software. |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "SagoSprite.hpp" |
| 26 | #include <iostream> |
| 27 | |
| 28 | #ifndef M_PI |
| 29 | // M_PI is a custom extension that most C++ toolchains provide. Out Windows compiler does not provide it. |
| 30 | # define M_PI 3.14159265358979323846 |
| 31 | #endif |
| 32 | |
| 33 | namespace sago { |
| 34 | |
| 35 | |
| 36 | SagoSprite::SagoSprite() { |
| 37 | } |
| 38 | |
| 39 | SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength) { |
| 40 | tex = texHolder.getTextureHandler(texture); |
| 41 | imgCord = initImage; |
| 42 | aniFrames = animationFrames; |
| 43 | aniFrameTime = animationFrameLength; |
| 44 | } |
| 45 | |
| 46 | void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, SagoLogicalResize* resize) const { |
| 47 | DrawScaled(target, frameTime, x, y, imgCord.w, imgCord.h, resize); |
| 48 | } |
| 49 | |
| 50 | void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian, SagoLogicalResize* resize) const { |
| 51 | SDL_Point center = {this->origin.x, this->origin.y}; |
| 52 | DrawScaledAndRotated(target, frameTime, x, y, imgCord.w, imgCord.h, angleRadian, ¢er, SDL_FLIP_NONE, resize); |
| 53 | } |
| 54 | |
| 55 | void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, SagoLogicalResize* resize) const { |
| 56 | DrawScaledAndRotated(target, frameTime, x, y, w, h, 0.0, nullptr, SDL_FLIP_NONE, resize); |
| 57 | } |
| 58 | |
| 59 | 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, SagoLogicalResize* resize) const { |
| 60 | if (!tex.get()) { |
| 61 | std::cerr << "Texture is null!\n"; |
| 62 | } |
| 63 | SDL_Rect rect = imgCord; |
| 64 | rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames); |
| 65 | SDL_Rect pos = rect; |
| 66 | pos.x = x - this->origin.x; |
| 67 | pos.y = y - this->origin.y; |
| 68 | if (w > 0) { |
| 69 | pos.w = w; |
| 70 | } |
| 71 | if (h > 0) { |
| 72 | pos.h = h; |
| 73 | } |
| 74 | double angleDegress = angleRadian/M_PI*180.0; |
| 75 | if (resize) { |
| 76 | resize->LogicalToPhysical(pos); |
| 77 | } |
| 78 | SDL_RenderCopyEx(target, tex.get(), &rect, &pos, angleDegress, center, flip); |
| 79 | } |
| 80 | |
| 81 | void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part, SagoLogicalResize* resize) const { |
| 82 | SDL_Rect rect = imgCord; |
| 83 | rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames); |
| 84 | rect.x += part.x; |
| 85 | rect.y += part.y; |
| 86 | rect.w = part.w; |
| 87 | rect.h = part.h; |
| 88 | SDL_Rect pos = rect; |
| 89 | pos.x = x - this->origin.x; |
| 90 | pos.y = y - this->origin.y; |
| 91 | if (resize) { |
| 92 | resize->LogicalToPhysical(pos); |
| 93 | } |
| 94 | SDL_RenderCopy(target, tex.get(), &rect, &pos); |
| 95 | } |
| 96 | |
| 97 | void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y, SagoLogicalResize* resize) const { |
| 98 | Sint32 frameNumber = progress*aniFrames; |
| 99 | Sint32 frameTime = frameNumber*aniFrameTime; |
| 100 | Draw(target, frameTime, x, y, resize); |
| 101 | } |
| 102 | |
| 103 | void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds, SagoLogicalResize* resize) const { |
| 104 | SDL_Rect rect = imgCord; |
| 105 | rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames); |
| 106 | SDL_Rect pos = rect; |
| 107 | pos.x = x; |
| 108 | pos.y = y; |
| 109 | if (pos.x > bounds.x+bounds.w) { |
| 110 | return; |
| 111 | } |
| 112 | if (pos.y > bounds.y+bounds.h) { |
| 113 | return; |
| 114 | } |
| 115 | if (pos.x+pos.w < bounds.x) { |
| 116 | return; |
| 117 | } |
| 118 | if (pos.y+pos.h < bounds.y) { |
| 119 | return; |
| 120 | } |
| 121 | if (pos.x < bounds.x) { |
| 122 | Sint16 absDiff = bounds.x-pos.x; |
| 123 | pos.x+=absDiff; |
| 124 | rect.x+=absDiff; |
| 125 | pos.w-=absDiff; |
| 126 | rect.w-=absDiff; |
| 127 | } |
| 128 | if (pos.y < bounds.y) { |
| 129 | Sint16 absDiff = bounds.y-pos.y; |
| 130 | pos.y+=absDiff; |
| 131 | rect.y+=absDiff; |
| 132 | pos.h-=absDiff; |
| 133 | rect.h-=absDiff; |
| 134 | } |
| 135 | if (pos.x+pos.w > bounds.x+bounds.w) { |
| 136 | Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w); |
| 137 | pos.w -= absDiff; |
| 138 | rect.w -= absDiff; |
| 139 | } |
| 140 | if (pos.y+pos.h > bounds.y+bounds.h) { |
| 141 | Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h); |
| 142 | pos.h -= absDiff; |
| 143 | rect.h -= absDiff; |
| 144 | } |
| 145 | |
| 146 | if (resize) { |
| 147 | resize->LogicalToPhysical(pos); |
| 148 | } |
| 149 | SDL_RenderCopy(target, tex.get(), &rect, &pos); |
| 150 | } |
| 151 | |
| 152 | void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) { |
| 153 | origin = newOrigin; |
| 154 | } |
| 155 | |
| 156 | int SagoSprite::GetWidth() const { |
| 157 | return imgCord.w; |
| 158 | } |
| 159 | int SagoSprite::GetHeight() const { |
| 160 | return imgCord.h; |
| 161 | } |
| 162 | |
| 163 | } //namespace sago |