-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathLayers.cpp
127 lines (111 loc) · 3.23 KB
/
Layers.cpp
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
#include <SFML/Graphics.hpp>
#include "mapgen/Layers.hpp"
#include "mapgen/utils.hpp"
Layer::Layer(std::string n) : name(n) {
cache = new sf::RenderTexture();
cache->setSmooth(true);
};
void Layer::draw(sf::RenderTarget& target, sf::RenderStates states) const {
if (!enabled) {
return;
}
if (direct) {
for (auto shape : shapes) {
target.draw(*shape);
}
return;
}
sf::Sprite sprite;
sprite.setTexture(cache->getTexture());
target.draw(sprite, states);
};
void Layer::update(sf::RenderWindow* window) {
mg::info("Draw to cache:", name);
cache->create(window->getSize().x, window->getSize().y, sf::ContextSettings(0, 0, 8));
cache->clear(sf::Color::Transparent);
for (auto shape : shapes) {
cache->draw(*shape);
}
if (shader != nullptr) {
mg::info("Draw shadered", std::string(""));
sf::RenderTexture temp;
temp.setSmooth(true);
temp.create(window->getSize().x, window->getSize().y, sf::ContextSettings(0, 0, 8));
temp.clear(sf::Color::Transparent);
sf::Sprite sprite;
cache->display();
sprite.setTexture(cache->getTexture());
temp.draw(sprite, shader);
temp.display();
sprite.setTexture(temp.getTexture());
cache->draw(sprite);
}
if (mask != nullptr) {
mg::info("Draw masked:", mask->name);
shader_mask->setUniform("mask", mask->cache->getTexture());
sf::RenderTexture temp;
temp.setSmooth(true);
temp.create(window->getSize().x, window->getSize().y, sf::ContextSettings(0, 0, 8));
temp.clear(sf::Color::Transparent);
temp.display();
//WHY this code fixes missing temp content?! SFML 2.5.0
sf::RenderTexture temp2;
temp2.create(window->getSize().x, window->getSize().y, sf::ContextSettings(0, 0, 8));
temp2.clear(sf::Color::Green);
temp2.display();
//
sf::Sprite sprite;
cache->display();
sprite.setTexture(cache->getTexture());
temp.draw(sprite, shader_mask);
temp.display();
sprite.setTexture(temp.getTexture());
cache->clear(sf::Color::Transparent);
cache->draw(sprite);
}
damaged = false;
cache->display();
}
void Layer::clear() {
shapes.clear();
}
void Layer::add(sf::Drawable* shape) {
shapes.push_back(shape);
}
LayersManager::LayersManager(sf::RenderWindow* w, sf::Shader* m) : window(w), shader_mask(m){};
void LayersManager::draw(sf::RenderTarget& target, sf::RenderStates states) const {
for (auto l: layers) {
target.draw(*l, states);
}
}
Layer* LayersManager::addLayer(std::string name) {
auto l = new Layer(name);
layers.push_back(l);
return l;
}
Layer* LayersManager::getLayer(std::string name) {
auto l = std::find_if(layers.begin(), layers.end(), [&](Layer* l) {
return l->name == name;
});
if (l == layers.end()) {
return addLayer(name);
}
return *l;
}
void LayersManager::setLayerEnabled(std::string name, bool enabled) {
auto l = getLayer(name);
l->enabled = enabled;
}
void LayersManager::invalidateLayer(std::string name) {
auto l = getLayer(name);
l->update(window);
}
void LayersManager::setShader(std::string name, sf::Shader* shader) {
auto l = getLayer(name);
l->shader = shader;
}
void LayersManager::setMask(std::string name, Layer* mask) {
auto l = getLayer(name);
l->mask = mask;
l->shader_mask = shader_mask;
}