git repos / blockattack-game

blame: source/code/sago/SagoDataHolder.hpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
47fef8e8 sago007 2016-11-24 21:37 2
Copyright (c) 2015 Poul Sander
47fef8e8 sago007 2016-11-24 21:37 3
47fef8e8 sago007 2016-11-24 21:37 4
Permission is hereby granted, free of charge, to any person
47fef8e8 sago007 2016-11-24 21:37 5
obtaining a copy of this software and associated documentation files
47fef8e8 sago007 2016-11-24 21:37 6
(the "Software"), to deal in the Software without restriction,
47fef8e8 sago007 2016-11-24 21:37 7
including without limitation the rights to use, copy, modify, merge,
47fef8e8 sago007 2016-11-24 21:37 8
publish, distribute, sublicense, and/or sell copies of the Software,
47fef8e8 sago007 2016-11-24 21:37 9
and to permit persons to whom the Software is furnished to do so,
47fef8e8 sago007 2016-11-24 21:37 10
subject to the following conditions:
47fef8e8 sago007 2016-11-24 21:37 11
47fef8e8 sago007 2016-11-24 21:37 12
The above copyright notice and this permission notice shall be
47fef8e8 sago007 2016-11-24 21:37 13
included in all copies or substantial portions of the Software.
47fef8e8 sago007 2016-11-24 21:37 14
47fef8e8 sago007 2016-11-24 21:37 15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47fef8e8 sago007 2016-11-24 21:37 16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47fef8e8 sago007 2016-11-24 21:37 17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
47fef8e8 sago007 2016-11-24 21:37 18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
47fef8e8 sago007 2016-11-24 21:37 19
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
47fef8e8 sago007 2016-11-24 21:37 20
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
47fef8e8 sago007 2016-11-24 21:37 21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47fef8e8 sago007 2016-11-24 21:37 22
SOFTWARE.
82da8a1c sago007 2015-12-19 18:00 23
*/
82da8a1c sago007 2015-12-19 18:00 24
82da8a1c sago007 2015-12-19 18:00 25
#include "SDL.h"
82da8a1c sago007 2015-12-19 18:00 26
#include <SDL_mixer.h>      //Used for sound & music
82da8a1c sago007 2015-12-19 18:00 27
#include <SDL_image.h>      //To load PNG images!
82da8a1c sago007 2015-12-19 18:00 28
#include <SDL_ttf.h>
82da8a1c sago007 2015-12-19 18:00 29
#include <physfs.h>         //Abstract file system. To use containers
82da8a1c sago007 2015-12-19 18:00 30
#include <string>
82da8a1c sago007 2015-12-19 18:00 31
82da8a1c sago007 2015-12-19 18:00 32
#ifndef TEXTUREHOLDER_HPP
e94af580 sago007 2020-12-30 17:45 33
#define TEXTUREHOLDER_HPP
82da8a1c sago007 2015-12-19 18:00 34
82da8a1c sago007 2015-12-19 18:00 35
namespace sago {
82da8a1c sago007 2015-12-19 18:00 36
1de94c36 sago007 2017-04-20 19:38 37
class SagoDataHolder;
1de94c36 sago007 2017-04-20 19:38 38
1de94c36 sago007 2017-04-20 19:38 39
class TextureHandler {
1de94c36 sago007 2017-04-20 19:38 40
public:
1de94c36 sago007 2017-04-20 19:38 41
	TextureHandler() {};
e94af580 sago007 2020-12-30 17:45 42
	TextureHandler(const SagoDataHolder* holder, const std::string& textureName);
1de94c36 sago007 2017-04-20 19:38 43
	SDL_Texture* get();
1de94c36 sago007 2017-04-20 19:38 44
private:
1de94c36 sago007 2017-04-20 19:38 45
	std::string textureName;
1de94c36 sago007 2017-04-20 19:38 46
	const SagoDataHolder* holder = nullptr;
1de94c36 sago007 2017-04-20 19:38 47
	SDL_Texture* data = nullptr;
1de94c36 sago007 2017-04-20 19:38 48
	Uint64 version = 0;
1de94c36 sago007 2017-04-20 19:38 49
};
1de94c36 sago007 2017-04-20 19:38 50
524f188c sago007 2017-04-22 09:01 51
2d137b45 sago007 2018-04-02 13:59 52
class MusicHandler final {
524f188c sago007 2017-04-22 09:01 53
public:
524f188c sago007 2017-04-22 09:01 54
	MusicHandler() {};
e94af580 sago007 2020-12-30 17:45 55
	MusicHandler(const SagoDataHolder* holder, const std::string& musicName);
524f188c sago007 2017-04-22 09:01 56
	Mix_Music* get();
524f188c sago007 2017-04-22 09:01 57
private:
524f188c sago007 2017-04-22 09:01 58
	std::string musicName;
524f188c sago007 2017-04-22 09:01 59
	const SagoDataHolder* holder = nullptr;
524f188c sago007 2017-04-22 09:01 60
	Mix_Music* data = nullptr;
524f188c sago007 2017-04-22 09:01 61
	Uint64 version = 0;
524f188c sago007 2017-04-22 09:01 62
};
524f188c sago007 2017-04-22 09:01 63
524f188c sago007 2017-04-22 09:01 64
2d137b45 sago007 2018-04-02 13:59 65
class SoundHandler final {
524f188c sago007 2017-04-22 09:01 66
public:
524f188c sago007 2017-04-22 09:01 67
	SoundHandler() {};
e94af580 sago007 2020-12-30 17:45 68
	SoundHandler(const SagoDataHolder* holder, const std::string& soundName);
524f188c sago007 2017-04-22 09:01 69
	Mix_Chunk* get();
524f188c sago007 2017-04-22 09:01 70
private:
524f188c sago007 2017-04-22 09:01 71
	std::string soundName;
524f188c sago007 2017-04-22 09:01 72
	const SagoDataHolder* holder = nullptr;
524f188c sago007 2017-04-22 09:01 73
	Mix_Chunk* data = nullptr;
524f188c sago007 2017-04-22 09:01 74
	Uint64 version = 0;
524f188c sago007 2017-04-22 09:01 75
};
524f188c sago007 2017-04-22 09:01 76
2d137b45 sago007 2018-04-02 13:59 77
class SagoDataHolder final {
82da8a1c sago007 2015-12-19 18:00 78
public:
e2252ee5 sago007 2017-04-20 19:02 79
	/**
e2252ee5 sago007 2017-04-20 19:02 80
	 * The renderer must be set before requesting a texture.
e2252ee5 sago007 2017-04-20 19:02 81
	 * If the constructor without elements is used then invalidateAll(SDL_Renderer*) must be called before getTexturePtr
e2252ee5 sago007 2017-04-20 19:02 82
	 */
e2252ee5 sago007 2017-04-20 19:02 83
	SagoDataHolder();
55ea55dd sago007 2018-03-24 16:07 84
	explicit SagoDataHolder(SDL_Renderer* renderer);
00ebd0ea sago007 2020-09-15 17:28 85
82da8a1c sago007 2015-12-19 18:00 86
	/**
55ea55dd sago007 2018-03-24 16:07 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.
47fef8e8 sago007 2016-11-24 21:37 88
	 * @param textureName Name of the texture
47fef8e8 sago007 2016-11-24 21:37 89
	 * @return  Pointer to the loaded texture
47fef8e8 sago007 2016-11-24 21:37 90
	 */
e94af580 sago007 2020-12-30 17:45 91
	SDL_Texture* getTexturePtr(const std::string& textureName) const;
e94af580 sago007 2020-12-30 17:45 92
e94af580 sago007 2020-12-30 17:45 93
	TextureHandler getTextureHandler(const std::string& textureName) const;
e94af580 sago007 2020-12-30 17:45 94
	TTF_Font* getFontPtr(const std::string& fontName, int ptsize) const;
e94af580 sago007 2020-12-30 17:45 95
	Mix_Music* getMusicPtr(const std::string& musicName) const;
e94af580 sago007 2020-12-30 17:45 96
	MusicHandler getMusicHandler(const std::string& musicName) const;
e94af580 sago007 2020-12-30 17:45 97
	Mix_Chunk* getSoundPtr(const std::string& soundName) const;
e94af580 sago007 2020-12-30 17:45 98
	SoundHandler getSoundHandler(const std::string& soundName) const;
e297a2e8 sago007 2016-01-21 18:07 99
	void setVerbose(bool value);
00ebd0ea sago007 2020-09-15 17:28 100
e2252ee5 sago007 2017-04-20 19:02 101
	/**
e2252ee5 sago007 2017-04-20 19:02 102
	 * Invalidates all pointers returned by any of the get variables
e2252ee5 sago007 2017-04-20 19:02 103
	 */
e2252ee5 sago007 2017-04-20 19:02 104
	void invalidateAll();
00ebd0ea sago007 2020-09-15 17:28 105
e2252ee5 sago007 2017-04-20 19:02 106
	/**
e2252ee5 sago007 2017-04-20 19:02 107
	 * Invalidates all pointers returned by any of the get variables.
e2252ee5 sago007 2017-04-20 19:02 108
	 * Also sets a new renderer.
e2252ee5 sago007 2017-04-20 19:02 109
	 *
e2252ee5 sago007 2017-04-20 19:02 110
	 * Setting a new renderer might cause all old textures to no longer match the renderer format.
e2252ee5 sago007 2017-04-20 19:02 111
	 */
e2252ee5 sago007 2017-04-20 19:02 112
	void invalidateAll(SDL_Renderer* renderer);
00ebd0ea sago007 2020-09-15 17:28 113
55ea55dd sago007 2018-03-24 16:07 114
	/**
55ea55dd sago007 2018-03-24 16:07 115
	 * The version number. Changes everytime the pointers are invalidated.
55ea55dd sago007 2018-03-24 16:07 116
	 * Can be used to determen if it is neccecary to get a new pointer.
55ea55dd sago007 2018-03-24 16:07 117
	 * @return A globally unique number.
55ea55dd sago007 2018-03-24 16:07 118
	 */
1de94c36 sago007 2017-04-20 19:38 119
	Uint64 getVersion() const;
3affd2d6 Poul Sander 2020-12-27 15:56 120
2d137b45 sago007 2018-04-02 13:59 121
	~SagoDataHolder();
82da8a1c sago007 2015-12-19 18:00 122
private:
82da8a1c sago007 2015-12-19 18:00 123
	SagoDataHolder(const SagoDataHolder& base) = delete;
47fef8e8 sago007 2016-11-24 21:37 124
	SagoDataHolder& operator=(const SagoDataHolder& base) = delete;
82da8a1c sago007 2015-12-19 18:00 125
	struct SagoDataHolderData;
e94af580 sago007 2020-12-30 17:45 126
	mutable SagoDataHolderData* data;
82da8a1c sago007 2015-12-19 18:00 127
};
82da8a1c sago007 2015-12-19 18:00 128
82da8a1c sago007 2015-12-19 18:00 129
} //namespace sago
82da8a1c sago007 2015-12-19 18:00 130
e94af580 sago007 2020-12-30 17:45 131
#endif  /* TEXTUREHOLDER_HPP */
82da8a1c sago007 2015-12-19 18:00 132
1970-01-01 00:00 133