-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
182 lines (147 loc) · 3.25 KB
/
MainWindow.cpp
File metadata and controls
182 lines (147 loc) · 3.25 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
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
/*
* MainWindow.cpp
*
* Created on: Aug 23, 2013
* Author: Tyler
*/
#include "MainWindow.h"
#include <iostream>
#include <gl/glew.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include "MathHelper.h"
MainWindow::MainWindow() {
width = height = 0;
running = false;
blocks = NULL;
}
MainWindow::~MainWindow() {
}
void MainWindow::create(int width, int height, const char* title) {
this->width = width;
this->height = height;
create(sf::VideoMode(width, height, 32), title);
setFramerateLimit(60);
initGL();
MathHelper::init();
camera.init(glm::vec3(0, 6, 0));
blocks = new Block*[36];
for(int x = 0; x < 6; ++x){
for(int z = 0; z < 6; ++z){
blocks[z + x * 6] = new Block(x - 3, z - 3);
}
}
Renderer::setProjectionMatrix(90, width, height, 1000);
}
void MainWindow::run() {
int frames = 0;
long fpsCounter = 0;
sf::Clock clock;
while(running){
float dt = clock.restart().asMicroseconds();
fpsCounter += dt;
frames++;
handleInput();
update(dt);
render();
if(fpsCounter > 1000000){
std::cout << "FPS: " << frames << '\n';
fpsCounter = 0;
frames = 0;
}
}
for(int i = 0; i < 36; ++i){
delete blocks[i];
}
delete[] blocks;
}
void MainWindow::start() {
if(running){
return;
}
running = true;
run();
}
void MainWindow::stop() {
running = false;
}
void MainWindow::initGL() {
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK){
std::cerr << "Glew did not initialize properly\n";
}
glClearColor(0.8f, 0.5f, 0.1f, 1.f);
// glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
// glEnable(GL_CULL_FACE);
}
void MainWindow::handleInput() {
sf::Event event;
static sf::Vector2i lastMousePos = sf::Mouse::getPosition();
sf::Vector2i mousePos = sf::Mouse::getPosition();
sf::Vector2i mouseMove = lastMousePos - mousePos;
lastMousePos = mousePos;
float moveX = 0, moveY = 0, moveZ = 0;
int dx = 0, dy = 0;
while(pollEvent(event)){
if(event.type == sf::Event::Closed){
stop();
return;
}
if(event.type == sf::Event::KeyPressed){
if(event.key.code == sf::Keyboard::Escape){
stop();
return;
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
moveZ += 0.4f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
moveZ -= 0.4f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
moveX -= 0.4f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
moveX += 0.4f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
moveY += 0.4f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
moveY -= 0.4f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
dy += 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
dy -= 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
dx -= 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
dx += 3;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
dx -= mouseMove.x;
dy += mouseMove.y;
}
camera.move(camera.getMoveVector(moveX, moveY, moveZ) * 3.f);
camera.rotateWithMove(dx, dy);
}
void MainWindow::update(time_t dt) {
}
void MainWindow::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (int x = 0; x < 6; ++x) {
for (int z = 0; z < 6; ++z) {
blocks[z + x * 6]->draw(camera.getViewMatrix());
}
}
display();
}
void MainWindow::cleanup() {
}