| 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 "SDL.h" |
| 26 | #include <SDL_mixer.h> //Used for sound & music |
| 27 | #include <SDL_image.h> //To load PNG images! |
| 28 | #include <SDL_ttf.h> |
| 29 | #include <physfs.h> //Abstract file system. To use containers |
| 30 | #include <string> |
| 31 | |
| 32 | #ifndef TEXTUREHOLDER_HPP |
| 33 | #define TEXTUREHOLDER_HPP |
| 34 | |
| 35 | namespace sago { |
| 36 | |
| 37 | class SagoDataHolder; |
| 38 | |
| 39 | class TextureHandler { |
| 40 | public: |
| 41 | TextureHandler() {}; |
| 42 | TextureHandler(const SagoDataHolder* holder, const std::string& textureName); |
| 43 | SDL_Texture* get() const; |
| 44 | private: |
| 45 | std::string textureName; |
| 46 | const SagoDataHolder* holder = nullptr; |
| 47 | mutable SDL_Texture* data = nullptr; |
| 48 | Uint64 version = 0; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | class MusicHandler final { |
| 53 | public: |
| 54 | MusicHandler() {}; |
| 55 | MusicHandler(const SagoDataHolder* holder, const std::string& musicName); |
| 56 | Mix_Music* get(); |
| 57 | private: |
| 58 | std::string musicName; |
| 59 | const SagoDataHolder* holder = nullptr; |
| 60 | Mix_Music* data = nullptr; |
| 61 | Uint64 version = 0; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | class SoundHandler final { |
| 66 | public: |
| 67 | SoundHandler() {}; |
| 68 | SoundHandler(const SagoDataHolder* holder, const std::string& soundName); |
| 69 | Mix_Chunk* get(); |
| 70 | private: |
| 71 | std::string soundName; |
| 72 | const SagoDataHolder* holder = nullptr; |
| 73 | Mix_Chunk* data = nullptr; |
| 74 | Uint64 version = 0; |
| 75 | }; |
| 76 | |
| 77 | class SagoDataHolder final { |
| 78 | public: |
| 79 | /** |
| 80 | * The renderer must be set before requesting a texture. |
| 81 | * If the constructor without elements is used then invalidateAll(SDL_Renderer*) must be called before getTexturePtr |
| 82 | */ |
| 83 | SagoDataHolder(); |
| 84 | explicit SagoDataHolder(SDL_Renderer* renderer); |
| 85 | |
| 86 | /** |
| 87 | * Return a pointer to the given texture. The pointer is valid for the lifetime of SagoDataHolder object it was taken from or until invalidateAll is called. |
| 88 | * @param textureName Name of the texture |
| 89 | * @return Pointer to the loaded texture |
| 90 | */ |
| 91 | SDL_Texture* getTexturePtr(const std::string& textureName) const; |
| 92 | |
| 93 | TextureHandler getTextureHandler(const std::string& textureName) const; |
| 94 | TTF_Font* getFontPtr(const std::string& fontName, int ptsize) const; |
| 95 | Mix_Music* getMusicPtr(const std::string& musicName) const; |
| 96 | MusicHandler getMusicHandler(const std::string& musicName) const; |
| 97 | Mix_Chunk* getSoundPtr(const std::string& soundName) const; |
| 98 | SoundHandler getSoundHandler(const std::string& soundName) const; |
| 99 | void setVerbose(bool value); |
| 100 | |
| 101 | /** |
| 102 | * Invalidates all pointers returned by any of the get variables |
| 103 | */ |
| 104 | void invalidateAll(); |
| 105 | |
| 106 | /** |
| 107 | * Invalidates all pointers returned by any of the get variables. |
| 108 | * Also sets a new renderer. |
| 109 | * |
| 110 | * Setting a new renderer might cause all old textures to no longer match the renderer format. |
| 111 | */ |
| 112 | void invalidateAll(SDL_Renderer* renderer); |
| 113 | |
| 114 | /** |
| 115 | * The version number. Changes everytime the pointers are invalidated. |
| 116 | * Can be used to determen if it is neccecary to get a new pointer. |
| 117 | * @return A globally unique number. |
| 118 | */ |
| 119 | Uint64 getVersion() const; |
| 120 | |
| 121 | ~SagoDataHolder(); |
| 122 | private: |
| 123 | SagoDataHolder(const SagoDataHolder& base) = delete; |
| 124 | SagoDataHolder& operator=(const SagoDataHolder& base) = delete; |
| 125 | struct SagoDataHolderData; |
| 126 | mutable SagoDataHolderData* data; |
| 127 | }; |
| 128 | |
| 129 | } //namespace sago |
| 130 | |
| 131 | #endif /* TEXTUREHOLDER_HPP */ |
| 132 |