-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureManager.h
More file actions
84 lines (71 loc) · 2.32 KB
/
Copy pathTextureManager.h
File metadata and controls
84 lines (71 loc) · 2.32 KB
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
#pragma once
#include <SFML\Graphics.hpp>
#include <algorithm>
class TextureManager
{
public:
static sf::Image * loadImage(const std::string &filename)
{
sf::Image image;
if (!image.loadFromFile(filename))
return nullptr;
_image.emplace_back(image);
return &_image.at(_image.size() - 1);
}
static sf::Image * addImage(const sf::Image &image)
{
_image.emplace_back(image);
return &_image.at(_image.size() - 1);
}
static sf::Texture * loadTextureFromImage(sf::Image &image, sf::IntRect &area = sf::IntRect())
{
sf::Texture texture;
texture.loadFromImage(image, area);
_texture.emplace_back(texture);
return &_texture.at(_texture.size() - 1);
}
static sf::Sprite * makeSprite(const sf::Texture *texture = nullptr, sf::IntRect &area = sf::IntRect(),
sf::Vector2f position = sf::Vector2f(0, 0), float rotation = 0,
sf::Color color = sf::Color(255, 255, 255, 255), sf::Vector2f scale = sf::Vector2f(0, 0))
{
sf::Sprite sprite;
if (texture != nullptr)
sprite.setTexture(*texture);
if (area != sf::IntRect())
sprite.setTextureRect(area);
sprite.setRotation(rotation);
sprite.setPosition(position);
sprite.setScale(scale);
sprite.setColor(color);
_sprite.emplace_back(sprite);
return &_sprite.at(_sprite.size() - 1);
}
static sf::Texture * addTexture(const sf::Texture & texture)
{
_texture.emplace_back(texture);
return &_texture.at(_texture.size() - 1);
}
static sf::Sprite * addSprite(const sf::Sprite &sprite)
{
_sprite.emplace_back(sprite);
return &_sprite.at(_sprite.size() - 1);
}
static void draw(sf::RenderWindow &window, const sf::Drawable &drawable)
{
window.draw(drawable);
}
static void quickDraw(sf::RenderWindow &window, const std::string &filename, sf::IntRect &area = sf::IntRect(),
sf::Vector2f position = sf::Vector2f(0, 0), float rotation = 0,
sf::Color color = sf::Color(255, 255, 255, 255), sf::Vector2f scale = sf::Vector2f(0, 0))
{
sf::Texture texture;
texture.loadFromFile(filename);
void * ind = addTexture(texture);
ind = makeSprite((sf::Texture*)ind, area, position, rotation, color, scale);
draw(window, *(sf::Sprite*)ind);
}
private:
static std::vector<sf::Texture> _texture;
static std::vector<sf::Image> _image;
static std::vector<sf::Sprite> _sprite;
};