| 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 | #ifndef SAGOSPRITE_HPP |
| 26 | #define SAGOSPRITE_HPP |
| 27 | |
| 28 | #include "SagoDataHolder.hpp" |
| 29 | #include "SagoLogicalResize.hpp" |
| 30 | |
| 31 | namespace sago { |
| 32 | |
| 33 | class SagoSprite final { |
| 34 | public: |
| 35 | SagoSprite(); |
| 36 | SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength); |
| 37 | |
| 38 | /** |
| 39 | * Draws the sprite to a given render window |
| 40 | * @param target The render window to draw on |
| 41 | * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation |
| 42 | * @param x Place to draw the sprite |
| 43 | * @param y Place to draw the sprite |
| 44 | */ |
| 45 | void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, SagoLogicalResize* resize = nullptr) const; |
| 46 | |
| 47 | /** |
| 48 | * Draws the sprite to a given render window |
| 49 | * @param target The render window to draw on |
| 50 | * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation |
| 51 | * @param x Place to draw the sprite |
| 52 | * @param y Place to draw the sprite |
| 53 | * @param angleRadian Angle to rotate the sprite around origin before drawing |
| 54 | */ |
| 55 | void DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian, SagoLogicalResize* resize = nullptr) const; |
| 56 | |
| 57 | /** |
| 58 | * Draws part of the sprite to a given render window |
| 59 | * @param target The render window to draw on |
| 60 | * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation |
| 61 | * @param x Place to draw the sprite |
| 62 | * @param y Place to draw the sprite |
| 63 | * @param part the part of the sprite that should be drawn. |
| 64 | */ |
| 65 | void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part, SagoLogicalResize* resize = nullptr) const; |
| 66 | |
| 67 | /** |
| 68 | * Draws the wprite to the given renderer but makes sure to not draw outside th bounds given |
| 69 | * @param target The render window to draw on |
| 70 | * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation |
| 71 | * @param x Place to draw the sprite |
| 72 | * @param y Place to draw the sprite |
| 73 | * @param bounds A recagular area that we must not draw outside. |
| 74 | */ |
| 75 | void DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds, SagoLogicalResize* resize = nullptr) const; |
| 76 | |
| 77 | /** |
| 78 | * Draws the sprite to a given render window |
| 79 | * @param target The render window to draw on |
| 80 | * @param progress A float with value from 0.0f to 1.0f. Tells how far in the animation that we got |
| 81 | * @param x Place to draw the sprite |
| 82 | * @param y Place to draw the sprite |
| 83 | */ |
| 84 | void DrawProgressive(SDL_Renderer* target, float progress, int x, int y, SagoLogicalResize* resize = nullptr) const; |
| 85 | |
| 86 | void DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, SagoLogicalResize* resize = nullptr) const; |
| 87 | void DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, |
| 88 | const double angleRadian, const SDL_Point* center, const SDL_RendererFlip flip, SagoLogicalResize* resize = nullptr) const; |
| 89 | |
| 90 | /** |
| 91 | * 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 |
| 92 | * @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default |
| 93 | */ |
| 94 | void SetOrigin(const SDL_Rect& newOrigin); |
| 95 | SagoSprite(const SagoSprite& base) = default; |
| 96 | SagoSprite& operator=(const SagoSprite& base) = default; |
| 97 | int GetWidth() const; |
| 98 | int GetHeight() const; |
| 99 | ~SagoSprite() = default; |
| 100 | |
| 101 | const TextureHandler& GetTextureHandler() const { |
| 102 | return this->tex; |
| 103 | } |
| 104 | |
| 105 | const SDL_Rect& GetImageCord() const { |
| 106 | return this->imgCord; |
| 107 | } |
| 108 | |
| 109 | const SDL_Rect& GetOrigin() const { |
| 110 | return this->origin; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | TextureHandler tex; |
| 115 | SDL_Rect imgCord = {}; |
| 116 | SDL_Rect origin = {}; |
| 117 | int aniFrames = 0; |
| 118 | int aniFrameTime = 0; |
| 119 | }; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | #endif /* SAGOSPRITE_HPP */ |
| 124 |