-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.hpp
53 lines (39 loc) · 1.5 KB
/
Sprite.hpp
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
#pragma once
/*
* A sprite is a rectangular area in a atlas texture.
* Sprites are loaded by creating a 'SpriteAtlas' (which both loads a texture
* image and sprite position metadata); you can then look up individual
* 'Sprite's in the 'SpriteAtlas' using its lookup() function.
*/
#include "GL.hpp"
#include <glm/glm.hpp>
#include <unordered_map>
#include <string>
struct Sprite {
//Sprites are rectangles in an atlas texture:
glm::vec2 min_px; //position of lower left corner (in pixels; ll-origin)
glm::vec2 max_px; //position of upper right corner (in pixels; ll-origin)
glm::vec2 anchor_px; //position of 'anchor' (in pixels; ll-origin)
//NOTE:
//The 'anchor' is the "center" or "pivot point" of the sprite --
// a value defined when authoring the sprite which is used as the
// position of the sprite when drawing.
//Generally, you set it to something meaningful like the center of mass
// or the position of the feet.
};
struct SpriteAtlas {
//load from filebase.png and filebase.atlas:
SpriteAtlas(std::string const &filebase);
~SpriteAtlas();
//look up sprite in list of loaded sprites:
// throws an error if name is missing
Sprite const &lookup(std::string const &name) const;
//this is the atlas texture; used when drawing sprites:
GLuint tex = 0;
glm::uvec2 tex_size = glm::uvec2(0);
//---- internal data ---
//table of loaded sprites, sorted by name:
std::unordered_map< std::string, Sprite > sprites;
//path to atlas, stored for debugging purposes:
std::string atlas_path;
};