-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightMap.h
executable file
·106 lines (85 loc) · 2.64 KB
/
LightMap.h
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
#pragma once
#include "Node.h"
#include "GridMaker.h"
#include <vector>
class LightMap : public Node {
private:
const sf::Vector2f offset = sf::Vector2f(-1,-1);
int tileX;
int tileY;
unsigned int width;
unsigned int height;
//Color values
float ambientIntensity;
float absorb;
sf::Color lightColor;
//Tile mapping
float **tiles;
Indexer indexes;
//Light sources
std::vector<sf::Vector2f> sourcePosition;
std::vector<float> sourceIntensity;
int nextIndex = 0;
//Graphical storage
sf::VertexArray vertices;
bool singular = true;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
states.transform *= getTransform();
if(singular)
states.blendMode = sf::BlendMultiply;
target.draw(vertices, states);
}
sf::Color applyIntensity(unsigned int x, unsigned int y);
sf::Vector2f getTilePos(unsigned int x, unsigned int y);
sf::Vector2f transformOctant(int row, int col, int octant);
void lightOctant(sf::Vector2f light, int octant, float maxIntensity);
public:
LightMap(int _tileX, int _tileY, float _ambient, float _absorb, Indexer _indexes,
Layer layer, bool indexLights=true, sf::Color _lightColor=sf::Color::White);
~LightMap() {
for(unsigned int y = 0; y < height; y++)
delete[] tiles[y];
delete[] tiles;
}
void reload();
sf::VertexArray *getVertices();
//Moving lights
sf::Vector2f scalePosition(sf::Vector2f pos);
int addSource(sf::Vector2f light, float intensity, bool scale=true);
void moveSource(int i, sf::Vector2f light);
void deleteSource(int i);
void markCollection();
};
class LightMapCollection : public Node {
private:
std::vector<LightMap*> lightmaps;
sf::RenderTexture *buffer;
//Draw selected tilemap
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
states.transform *= getTransform();
states.blendMode = sf::BlendMultiply;
buffer->clear(sf::Color::Black);
for(unsigned int i = 0; i < lightmaps.size(); i++)
buffer->draw(*(lightmaps[i]), sf::BlendMax);
buffer->display();
sf::Sprite sprite(buffer->getTexture());
target.draw(sprite, states);
}
public:
LightMapCollection(int tileX, int tileY, Indexer indexes, Layer layer) : Node(layer) {
int width = tileX * (indexes.getSize().x * indexes.getScale().x + 1);
int height = tileY * (indexes.getSize().y * indexes.getScale().y + 1);
//Set up buffer texture
buffer = new sf::RenderTexture();
if(!buffer->create(width, height))
throw std::logic_error("Error creating LightMap buffer");
}
void addLightMap(LightMap *map) {
map->markCollection();
lightmaps.push_back(map);
}
void reloadAll() {
for(unsigned int i = 0; i < lightmaps.size(); i++)
lightmaps[i]->reload();
}
};