aboutsummaryrefslogtreecommitdiffstats
path: root/include/texture.hpp
blob: d4bcfa9a70d941f59807e484cd12244ba1e02b71 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * @file Texture.h
 * @brief Defines a method for loading textures.
 * This file gives facilities for easily loading and binding textures.
 */
#ifndef TEXTURE_HPP_
#define TEXTURE_HPP_

#include <string>
#include <vector>

#include <GL/glew.h> // must
#include <SDL2/SDL_opengl.h>

#include <color.hpp>
#include <vector2.hpp>

/**
 * When defined, DEBUG allows extra messages to be printed to the terminal for
 * debugging purposes.
 */
#define DEBUG

/**
 * @class Texture
 * Handles a single texture, loaded from the given file.
 */
class Texture {
protected:
	std::string name; /**< The name (path) of the loaded file. */
	GLuint tex = 0;   /**< The GLuint for the loaded texture. */
	vec2 dim;         /**< The dimensions of the loaded texture. */

public:
	/**
	 * Attempts to create a texture.
	 * Either:
	 * - Creates an empty class (no arguments given)
	 * - Load a texture from the given file, crashing if file not found
	 * - Fills the class with the given values
	 * @param file the path to the desired texture
	 * @param t the GLuint for the texture, if known
	 * @param v the size of the texture, if known
	 * @param hline if true, divides texture dim. by HLINE
	 */
	Texture(const std::string& file = "", const GLuint& t = 0xFFFFF, const vec2& v = vec2(0, 0));

	Texture(const std::string& file, bool hline);

	/**
	 * Gets the name (path) of the loaded texture.
	 * @return the texture's name
	 */
	const std::string& getName(void) const;

	/**
	 * Gets the dimensions of the loaded texture.
	 * @return the texture's dimensions
	 */
	const vec2& getDim(void) const;

	/**
	 * Binds the texture, so it may be used for rendering.
	 */
	inline void use(void) const
	{ glBindTexture(GL_TEXTURE_2D, tex); }

	/**
	 * Frees GPU resources for the loaded texture.
	 */
	inline void destroy(void)
	{ glDeleteTextures(1, &tex), tex = 0xFFFFF; }

	/**
	 * Checks if a texture is currently loaded in this class.
	 * @return true if there is not a loaded texture
	 */
	inline bool isEmpty(void) const
	{ return (tex == 0xFFFFF); }
};

/**
 * @class ColorTex
 * Creates a single-pixel texture of the given color.
 */
class ColorTex : public Texture {
public:
	ColorTex(void);

	/**
	 * Creates a texture of the given color.
	 * @param color the desired color
	 */
	ColorTex(const Color& color);
};

/**
 * A collection of preloaded colors, to save resources.
 */
namespace Colors {
	extern ColorTex white; /**< A solid white texture. */
	extern ColorTex black; /**< A solid black texture. */
	extern ColorTex red;   /**< A solid red texture. */
	extern ColorTex blue;  /**< A solid blue texture. */

	extern GLfloat texCoord[12];

	/**
	 * Creates the colors.
	 */
	void init(void);
}

/**
 * @class TextureIterator
 * Keeps a collection of textures for easy usage/looping.
 */
class TextureIterator {
private:
	/**
	 * The set of textures to loop through.
	 */
	std::vector<Texture> textures;

	/**
	 * The current position in the texture array.
	 * @see textures
	 */
	std::vector<Texture>::iterator position;
public:
	TextureIterator(void)
		: position(std::begin(textures)) {}
	~TextureIterator(void) {}

	/**
	 * Constructs a set of textures from the given list.
	 * @param l the list of textures
	 */
	TextureIterator(const std::vector<std::string> &l);

	void appendGIF(const std::string& gif);

	/**
	 * Shifts to the next texture in the array, stopping at the end if we're there.
	 * Also binds the texture.
	 */
	void operator++(int) noexcept;

	/**
	 * Shifts back in the array, staying at the beginning if we're there.
	 * Also binds the texture.
	 */
	void operator--(int) noexcept;

	/**
	 * Goes to the given index in the list.
	 * @param index the index to use
	 */
	void operator()(const int& index);

	/**
	 * Gets the dimensions of the currently selected texture.
	 * @return the texture's dimensions
	 */
	inline const vec2& getTextureDim(void) const
	{ return position->getDim(); }

	inline unsigned int size(void) const
	{ return textures.size(); }
};

/**
 * Frees all loaded textures, rendering them all useless.
 */
void unloadTextures(void);

#endif //TEXTURE_HPP_