-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicsSystem.cpp
211 lines (186 loc) · 6.72 KB
/
graphicsSystem.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "graphicsSystem.h"
#include "CameraEvents.h"
#include "SFML\Graphics\RectangleShape.hpp"
void setPosition(sf::Transformable &trans, const Vector2D &vec)
{
trans.setPosition(sf::Vector2f(vec.getX(), vec.getY()));
}
GraphicsSystem::GraphicsSystem()
{
mWidth = 0;
mHeight = 0;
mCameraZoom = 1;
gpEventSystem->addListener(EventType::PAN_CAMERA_EVENT, this);
gpEventSystem->addListener(EventType::ZOOM_CAMERA_EVENT, this);
}
GraphicsSystem::~GraphicsSystem()
{
cleanup();
}
void GraphicsSystem::init(const rapidjson::Document &doc, int w, int h, const std::string &t)
{
std::string title(t);
mDisplay.create(sf::VideoMode(w, h, 32), sf::String(title));
mWidth = w;
mHeight = h;
mCameraPosition = Vector2D((float)mWidth / 2, (float)mHeight / 2);
mZoomPosition = sf::Vector2i((int)mCameraPosition.getX(), (int)mCameraPosition.getY());
oldOffsetCoords = sf::Vector2i(0, 0);
sf::View view = sf::View(sf::Vector2f((float)mWidth / 2.f, (float)mHeight / 2.f), sf::Vector2f((float)mWidth, (float)mHeight));
mBoardViewport = sf::FloatRect(
doc["window"]["boardViewport"]["startX"].GetFloat(),
doc["window"]["boardViewport"]["startY"].GetFloat(),
doc["window"]["boardViewport"]["widthRatio"].GetFloat(),
doc["window"]["boardViewport"]["heightRatio"].GetFloat());
view.setViewport(mBoardViewport);
mDisplay.setView(view);
}
void GraphicsSystem::cleanup()
{
mDisplay.close();
}
void GraphicsSystem::clear()
{
mDisplay.clear(sf::Color::Black);
}
Vector2D GraphicsSystem::convertToWorldCoordinates(Vector2D pos, const GraphicsLayer layer)
{
sf::View oldView = mDisplay.getView();
update(layer); // switch to view corresponding to given layer to get correct coordinate conversions
sf::Vector2f worldPos = mDisplay.mapPixelToCoords(sf::Vector2i((int)pos.getX(), (int)pos.getY()));
mDisplay.setView(oldView);
return Vector2D(worldPos.x, worldPos.y);
}
// TODO: rotation
void GraphicsSystem::drawOutlineForBounds(const sf::FloatRect &bounds, const Outline &outline)
{
sf::RectangleShape boardOutline;
boardOutline.setSize(sf::Vector2f(bounds.width, bounds.height));
boardOutline.setPosition(sf::Vector2f(bounds.left, bounds.top));
boardOutline.setOutlineColor(outline.borderColor.mColor);
boardOutline.setOutlineThickness((float)outline.thickness);
boardOutline.setFillColor(outline.fillColor.mColor);
mDisplay.draw(boardOutline);
}
void GraphicsSystem::draw(const Vector2D &targetLoc, const Sprite &sprite, double theta, const Vector2D &scale, const Outline &outline)
{
sf::Sprite temp;
GraphicsBuffer *texture = sprite.getTexture();
if(texture)
{
temp = sf::Sprite(sprite.getTexture()->mBitmap, sf::IntRect((int)sprite.getSourceLoc().getX(), (int)sprite.getSourceLoc().getY(), (int)sprite.getWidth(), (int)sprite.getHeight()));
}
//sf::Sprite temp(sprite.getTexture()->mBitmap, sf::IntRect((int)sprite.getSourceLoc().getX(), (int)sprite.getSourceLoc().getY(), (int)sprite.getWidth(), (int)sprite.getHeight()));
double degrees = theta * 180.0 / 3.1415926;
const Vector2D offset(targetLoc.getX(), targetLoc.getY());
setPosition(temp, offset);
temp.setOrigin(sf::Vector2f(sprite.getOrigin().getX(), sprite.getOrigin().getY()));
temp.rotate((float)degrees);
temp.scale(scale.getX(), scale.getY());
if(scale.getX() < 0)
{
temp.move((float)temp.getGlobalBounds().width, 0);
}
if(scale.getY() < 0)
{
temp.move(0, (float)temp.getGlobalBounds().height);
}
temp.setColor(sf::Color(sprite.getColor().mColor));
temp.setColor(sf::Color(temp.getColor().r, temp.getColor().g, temp.getColor().b, (sf::Uint8)sprite.getTransparency()));
drawOutlineForBounds(temp.getGlobalBounds(), outline);
mDisplay.draw(temp);
}
// TODO: rotation
void GraphicsSystem::drawOutline(const Vector2D& targetLoc, const Vector2D& size, const Outline& outline, double theta)
{
sf::FloatRect bounds(sf::Vector2f(targetLoc.getX(), targetLoc.getY()), sf::Vector2f(size.getX(), size.getY()));
drawOutlineForBounds(bounds, outline);
}
void GraphicsSystem::handleEvent(const Event &theEvent)
{
if(theEvent.getType() == EventType::PAN_CAMERA_EVENT)
{
mCameraPosition += static_cast<const PanCameraEvent&>(theEvent).getDelta();
}
else if(theEvent.getType() == EventType::ZOOM_CAMERA_EVENT)
{
const ZoomCameraEvent &ev = static_cast<const ZoomCameraEvent&>(theEvent);
mCameraZoom += ev.getDelta();
mZoomPosition = sf::Vector2i((int)ev.getZoomLocation().getX(), (int)ev.getZoomLocation().getY());
}
}
void GraphicsSystem::flip()
{
mDisplay.display();
}
void GraphicsSystem::zoomViewAt(sf::Vector2i pixel, sf::RenderWindow& window, float zoom)
{
sf::Vector2f vec = sf::Vector2f((float)pixel.x, (float)pixel.y);
sf::View view(sf::Vector2f(mCameraPosition.getX(), mCameraPosition.getY()), sf::Vector2f((float)mWidth, (float)mHeight));
view.setViewport(mBoardViewport);
window.setView(view);
oldOffsetCoords = pixel;
const sf::Vector2f beforeCoord{window.mapPixelToCoords(pixel)};
view.zoom(zoom);
window.setView(view);
const sf::Vector2f afterCoord{window.mapPixelToCoords(pixel)};
const sf::Vector2f offsetCoords{beforeCoord - afterCoord};
view.move(offsetCoords);
window.setView(view);
}
void GraphicsSystem::update(const GraphicsLayer layer)
{
switch(layer)
{
case GraphicsLayer::BASE_VIEW:
{
zoomViewAt(mZoomPosition, mDisplay, (float)mCameraZoom);
sf::View view = mDisplay.getView();
mTopLeft = Vector2D(view.getCenter().x - mWidth / 2, view.getCenter().y - mHeight / 2);
}
break;
case GraphicsLayer::GUI_VIEW:
sf::View view(sf::Vector2f((float)mWidth / 2, (float)mHeight / 2), sf::Vector2f((float)mWidth, (float)mHeight));
mDisplay.setView(view);
break;
}
}
void GraphicsSystem::writeText(const Vector2D &targetLoc, const int fontSize, Font &font, Color &color, const std::string &message, const Outline &background, const Vector2D &scale){
sf::Text temp(message, font.mFont);
temp.setOrigin(sf::Vector2f(temp.getLocalBounds().left, temp.getLocalBounds().top));
setPosition(temp, targetLoc);
temp.move(0, temp.getLocalBounds().top);
temp.setFillColor(color.mColor);
temp.setCharacterSize(fontSize);
temp.scale(scale.getX(), scale.getY());
if(scale.getX() < 0)
{
temp.move((float)temp.getGlobalBounds().width, 0);
}
if(scale.getY() < 0)
{
temp.move(0, (float)temp.getGlobalBounds().height);
}
drawOutlineForBounds(temp.getGlobalBounds(), background);
mDisplay.draw(temp);
}
void GraphicsSystem::setHeight(int h)
{
mHeight = h;
}
void GraphicsSystem::setWidth(int w)
{
mWidth = w;
}
int GraphicsSystem::getHeight()
{
return mHeight;
}
int GraphicsSystem::getWidth()
{
return mWidth;
}
const Vector2D & GraphicsSystem::getTopLeft() const
{
return mTopLeft;
}