-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
149 lines (115 loc) · 3.52 KB
/
main.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
#include "common.h"
#include "Coords.h"
#include "Image.h"
#include "InputState.h"
#include "Level.h"
#include "LevelMap.h"
#include "MapElements.h"
#include "Player.h"
#include "Tile.h"
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
static InputState input;
void OnKeyboardPressed(GLFWwindow* window, int key, int scancode, int action, int mode)
{
switch (key) {
case GLFW_KEY_ESCAPE:
if (action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_1:
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
break;
case GLFW_KEY_2:
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
break;
default:
if (action == GLFW_PRESS)
input.keys[key] = true;
else if (action == GLFW_RELEASE)
input.keys[key] = false;
}
}
void OnMouseButtonClicked(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE) {
input.captureMouse = !input.captureMouse;
}
if (input.captureMouse) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
input.capturedMouseJustNow = true;
} else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
void OnMouseMove(GLFWwindow* window, double xpos, double ypos)
{
if (input.firstMouse) {
input.lastX = float(xpos);
input.lastY = float(ypos);
input.firstMouse = false;
}
GLfloat xoffset = float(xpos) - input.lastX;
GLfloat yoffset = input.lastY - float(ypos);
input.lastX = float(xpos);
input.lastY = float(ypos);
}
int initGL()
{
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;
std::cout << "Version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
std::cout << "Controls: "<< std::endl;
std::cout << "press right mouse button to capture/release mouse cursor "<< std::endl;
std::cout << "W, A, S, D - movement "<< std::endl;
std::cout << "press ESC to exit" << std::endl;
return 0;
}
int main(int argc, char **argv)
{
if(!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
constexpr int window_width = Level::window_width;
constexpr int window_height = Level::window_height;
GLFWwindow *window = glfwCreateWindow(window_width, window_height, "roguelike", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback (window, OnKeyboardPressed);
glfwSetCursorPosCallback (window, OnMouseMove);
glfwSetMouseButtonCallback(window, OnMouseButtonClicked);
if(initGL() != 0) {
return -1;
}
//Reset any OpenGL errors which could be present for some reason
GLenum gl_error = glGetError();
while (gl_error != GL_NO_ERROR){
gl_error = glGetError();
}
glViewport(0, 0, window_width, window_height); GL_CHECK_ERRORS;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GL_CHECK_ERRORS;
LevelResult game_result{ LevelResult::WON };
// game loop
for (const auto &level_config : level_configs) {
Level level(level_config);
level.ShowOpening(window);
game_result = level.Run(window, input);
if (game_result != LevelResult::WON) {
break;
}
}
Level::ShowEnding(window, game_result);
return 0;
}