-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderSystem.cpp
More file actions
88 lines (80 loc) · 2.44 KB
/
RenderSystem.cpp
File metadata and controls
88 lines (80 loc) · 2.44 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
85
86
87
88
/** Render System Implementation
*
* @author Caleb Geiger
* Created: 8-2-2013
* Source File: RenderSystem.cpp
*/
#include "Entity.h"
#include "IRenderComponent.h"
#include "RenderSystem.h"
#include "Logger.h"
#include <algorithm>
void RenderSystem::Update(sf::Time dt)
{
//sort queue
//iterate through queue and render.
for(int i = _RenderQueue.size() - 1; i >= 0; i--)
{
if(_sort)
{
std::sort (_RenderQueue[i].begin(), _RenderQueue[i].end(), _sort);
}
for(unsigned int j = 0; j < _RenderQueue[i].size(); j++)
{
this->GetWindow()->draw(_RenderQueue[i][j]->GetDrawable());
}
}
}
void RenderSystem::insertRenderComponent(unsigned int id, const char* type)
{
Entity* e = this->GetEntity(id);
IRenderComponent* rc = e->GetComponent<IRenderComponent>(type);
if(rc->GetPriorityZ() >= _RenderQueue.size())
{
g_Logger << __FILE__ << ": " << __LINE__ << "-" << "Priority Z of " << rc->GetPriorityZ() << " for Entity ";
g_Logger << id << "is not in depth buffer\n";
return;
}
_RenderQueue[rc->GetPriorityZ()].push_back(rc);
}
void RenderSystem::removeRenderComponent(unsigned int id, const char* type)
{
Entity* e = this->GetEntity(id);
IRenderComponent* rc = e->GetComponent<IRenderComponent>(type);
if(rc->GetPriorityZ() < _RenderQueue.size())
{
//Find and remove
for(auto it = _RenderQueue[rc->GetPriorityZ()].begin(); it != _RenderQueue[rc->GetPriorityZ()].end(); it++)
{
if((*it) == rc)
{
_RenderQueue[rc->GetPriorityZ()].erase(it);
return;
}
}
}
}
bool RenderSystem::ValidateEntity(unsigned int ID)
{
Entity* e = this->GetEntity(ID);
bool validated = false;
for(const char* type : _renderTypes) {
if(e->HasComponent(type)) {
insertRenderComponent(ID, type);
validated = true;
}
}
return validated;
}
void RenderSystem::OnMessage(EntityMovedMessage& msg)
{
if(_EntitiesToUpdate.find(msg.ID) != _EntitiesToUpdate.end())
{
Entity* e = this->GetEntity(msg.ID);
for(const char* type : _renderTypes) {
IRenderComponent* rc = e->GetComponent<IRenderComponent>(type);
if(rc)
rc->SetPosition(sf::Vector2f(msg.newPosition.x + rc->GetOffset().x, msg.newPosition.y + rc->GetOffset().y));
}
}
}