-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
114 lines (98 loc) · 2.43 KB
/
Game.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
/**
* I lost again.
* Times this header has made me lose (started 1-12-12): 2
* @author Chris Brenton
* @date 12-26-11
*/
#include "Game.h"
using namespace sf;
Game::Game(RenderWindow *_window) : window(_window)
{
if (!hudFont.LoadFromFile(DEFAULT_FONT))
{
perror("loading font");
exit(EXIT_FAILURE);
}
String fpsText;
fpsText.SetFont(hudFont);
fpsText.SetScale(0.5f, 0.5f);
fpsText.Move(window->GetWidth() - 150.f, window->GetHeight() - 30.f);
fpsText.SetColor(Color(COLOR_MAX, COLOR_MAX, COLOR_MAX));
addText("fps", fpsText);
}
void Game::addText(std::string varName, String varText)
{
textMap.insert(std::pair<std::string, String>(varName, varText));
}
void Game::toggleTextVisibility(std::string varName)
{
std::map<std::string, String>::iterator it;
it = textMap.find(varName);
sf::Color newColor = (*it).second.GetColor();
int a = newColor.a;
// TODO: Make this allow partial text transparency.
if (a == 0)
{
newColor.a = COLOR_MAX;
}
else
{
newColor.a = 0;
}
(*it).second.SetColor(newColor);
}
void Game::updateText(std::string varName, std::string varText)
{
std::map<std::string, String>::iterator it;
it = textMap.find(varName);
(*it).second.SetText(varText);
}
void Game::updateFramerate(float tick)
{
// Get framerate.
float fps = 1.f / tick;
fpsStream.str(std::string());
fpsStream << "FPS: " << std::fixed << std::setprecision(2) << fps;
updateText("fps", fpsStream.str());
}
void Game::update(float tick)
{
// TODO: Update here.
updateFramerate(tick);
}
void Game::toggleFramerate()
{
toggleTextVisibility("fps");
}
void Game::drawAllText()
{
// Before drawing text.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT |
GL_TEXTURE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
glDisable(GL_ALPHA_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
// Draw text.
std::map<std::string, String>::iterator it;
for (it = textMap.begin(); it != textMap.end(); ++it)
{
window->Draw((*it).second);
}
// After drawing the text
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}
void Game::draw()
{
// TODO: Draw everything.
drawAllText();
}