File: SagoDataHolder.hpp

package info (click to toggle)
blockattack 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,992 kB
  • sloc: cpp: 26,060; pascal: 351; sh: 172; makefile: 15
file content (126 lines) | stat: -rw-r--r-- 4,101 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright (c) 2015 Poul Sander

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "SDL.h"
#include <SDL_mixer.h>      //Used for sound & music
#include <SDL_image.h>      //To load PNG images!
#include <SDL_ttf.h>
#include <physfs.h>         //Abstract file system. To use containers
#include <string>

#ifndef TEXTUREHOLDER_HPP
#define	TEXTUREHOLDER_HPP

namespace sago {

class SagoDataHolder;

class TextureHandler {
public:
	TextureHandler() {};
	TextureHandler(const SagoDataHolder* holder, const std::string &textureName);
	SDL_Texture* get();
private:
	std::string textureName;
	const SagoDataHolder* holder = nullptr;
	SDL_Texture* data = nullptr;
	Uint64 version = 0;
};


class MusicHandler final {
public:
	MusicHandler() {};
	MusicHandler(const SagoDataHolder* holder, const std::string &musicName);
	Mix_Music* get();
private:
	std::string musicName;
	const SagoDataHolder* holder = nullptr;
	Mix_Music* data = nullptr;
	Uint64 version = 0;
};


class SoundHandler final {
public:
	SoundHandler() {};
	SoundHandler(const SagoDataHolder* holder, const std::string &soundName);
	Mix_Chunk* get();
private:
	std::string soundName;
	const SagoDataHolder* holder = nullptr;
	Mix_Chunk* data = nullptr;
	Uint64 version = 0;
};

class SagoDataHolder final {
public:
	/**
	 * The renderer must be set before requesting a texture.
	 * If the constructor without elements is used then invalidateAll(SDL_Renderer*) must be called before getTexturePtr
	 */
	SagoDataHolder();
	explicit SagoDataHolder(SDL_Renderer* renderer);
	/**
	 * 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.
	 * @param textureName Name of the texture
	 * @return  Pointer to the loaded texture
	 */
	SDL_Texture* getTexturePtr(const std::string &textureName) const;
	TextureHandler getTextureHandler(const std::string &textureName) const;
	TTF_Font* getFontPtr(const std::string &fontName, int ptsize) const;
	Mix_Music* getMusicPtr(const std::string &musicName) const;
	MusicHandler getMusicHandler(const std::string &musicName) const;
	Mix_Chunk* getSoundPtr(const std::string &soundName) const;
	SoundHandler getSoundHandler(const std::string &soundName) const;
	void setVerbose(bool value);
	/**
	 * Invalidates all pointers returned by any of the get variables
	 */
	void invalidateAll();
	/**
	 * Invalidates all pointers returned by any of the get variables.
	 * Also sets a new renderer.
	 *
	 * Setting a new renderer might cause all old textures to no longer match the renderer format.
	 */
	void invalidateAll(SDL_Renderer* renderer);
	/**
	 * The version number. Changes everytime the pointers are invalidated.
	 * Can be used to determen if it is neccecary to get a new pointer.
	 * @return A globally unique number.
	 */
	Uint64 getVersion() const;
	~SagoDataHolder();
private:
	SagoDataHolder(const SagoDataHolder& base) = delete;
	SagoDataHolder& operator=(const SagoDataHolder& base) = delete;
	struct SagoDataHolderData;
	mutable SagoDataHolderData *data;
};

} //namespace sago

#endif	/* TEXTUREHOLDER_HPP */