-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindowControl.cpp
164 lines (118 loc) · 3.87 KB
/
windowControl.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
#include "windowControl.hpp"
#include <iostream>
// #include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "player.hpp"
WindowWrapper::WindowWrapper(int height, int width)
{
WindowWrapper::windowSettings.height = height;
WindowWrapper::windowSettings.width = width;
}
WindowWrapper::~WindowWrapper() {}
void WindowWrapper::createTexture(std::string imagePath)
{
sf::Texture texture;
if (!texture.loadFromFile(imagePath))
{
std::cout << "ERROR: Cannot find image" << std::endl;
return;
}
std::cout << "Loaded texture from " << imagePath << std::endl;
WindowWrapper::textureVector.push_back(texture);
}
void WindowWrapper::testSprite(const sf::Texture &texture)
{
sf::Sprite sprite;
sprite.setPosition(0, 0);
std::cout << "Set position" << std::endl;
// sprite.scale(1, 1);
sprite.setTexture(texture);
WindowWrapper::spriteVector.push_back(sprite);
}
void WindowWrapper::setTexture(int spriteId, int textureId)
{
WindowWrapper::spriteVector[spriteId].setTexture(WindowWrapper::textureVector[textureId]);
}
void WindowWrapper::initialTest()
{
WindowWrapper::createTexture(std::string("textures/grass.jpg"));
WindowWrapper::testSprite(WindowWrapper::textureVector[0]);
// WindowWrapper::setTexture(0, 0);
if (WindowWrapper::textureVector.empty())
{
std::cout << "ERROR: Texture vector is empty!" << std::endl;
}
}
void WindowWrapper::runningLoop()
{
// Todo: Temporary texture setting
int count = 0;
for (auto &sprite : WindowWrapper::spriteVector)
{
WindowWrapper::window->draw(sprite);
}
}
void WindowWrapper::createWindow()
{
// May be better to just return unique ptr to window ?
WindowWrapper::window = std::make_unique<sf::RenderWindow>();
}
void WindowWrapper::createText(std::string textToRender)
{
sf::Font font;
if (!font.loadFromFile("sans.TTF"))
{
std::cout << "ERROR: Cannot load font" << std::endl;
}
sf::Text text;
text.setFont(font);
text.setString(textToRender);
text.setCharacterSize(34);
text.setFillColor(sf::Color::Green);
text.setStyle(sf::Text::Bold);
float height = (WindowWrapper::windowSettings.height / 2);
float width = (WindowWrapper::windowSettings.width / 2);
text.setPosition(height, width);
WindowWrapper::window->draw(text);
}
void WindowWrapper::run()
{
WindowWrapper::createWindow();
WindowWrapper::window->create(sf::VideoMode(667, 360), "SFML Example");
WindowWrapper::window->setFramerateLimit(60);
WindowWrapper::initialTest();
WindowWrapper::window->setKeyRepeatEnabled(true);
Player myPlayer(std::string("textures/peng.jpg"));
while (WindowWrapper::window->isOpen())
{
sf::Event event;
while (WindowWrapper::window->pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
WindowWrapper::window->close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
myPlayer.movePlayer('u', 6.0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
myPlayer.movePlayer('d', 6.0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
myPlayer.movePlayer('r', 6.0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
myPlayer.movePlayer('l', 6.0);
}
}
WindowWrapper::window->clear(sf::Color::Black);
// WindowWrapper::createBackground(std::string("textures/ground.png"));
WindowWrapper::runningLoop();
myPlayer.drawPlayer(WindowWrapper::window.get());
// WindowWrapper::createText(std::string("Balls"));
WindowWrapper::window->display();
}
}