-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindow.hpp
195 lines (178 loc) · 6.09 KB
/
Window.hpp
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
#pragma once
#include <cstdlib>
#include <unistd.h>
#include <vector>
#include <tuple>
#include <string>
#include <map>
#include "incgraphics.h"
#include "Transformation.hpp"
#include "ImageLoader.hpp"
#include "Logger.hpp"
#include "Debug.hpp"
#include "Region.hpp"
#include "ClientObject.hpp"
namespace glfw {
void error_callback(int error, const char* description);
void keypress_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
void size_callback(GLFWwindow *window, int new_width, int new_height);
void cursor_area_callback(GLFWwindow *window, double xpos, double ypos);
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
void mouse_scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
}
std::map <GLFWwindow *, void *> window_reference;
class Window {
protected:
size_t width_, height_;
Region cursor_area{
{0, 1},
{0, 1}
};
glm::vec2 cursor_pos{0, 0};
ClientObject cObject;
std::string dir;
void start() {
init_glfw();
init_controls();
gl_version();
}
void init_glfw() {
glfwSetErrorCallback(glfw::error_callback);
int rc = glfwInit();
ASSERT(rc == 1);
vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
ASSERT(vidmode != nullptr);
// width_ = 1200, height_ = 720;
width_ = vidmode->width;
height_ = vidmode->height - 100;
/* glfwWindowHint(GLFW_SAMPLES, 4); */
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
window = glfwCreateWindow(width(), height(), "Mini FIFA", nullptr, nullptr);
ASSERT(window != nullptr);
window_reference[window] = this;
glfwMakeContextCurrent(window); GLERROR
glfwSetKeyCallback(window, glfw::keypress_callback); GLERROR
glfwSetWindowSizeCallback(window, glfw::size_callback); GLERROR
glfwSetCursorPosCallback(window, glfw::cursor_area_callback); GLERROR
glfwSetMouseButtonCallback(window, glfw::mouse_button_callback); GLERROR
glfwSetScrollCallback(window, glfw::mouse_scroll_callback); GLERROR
glfwSwapInterval(1); GLERROR
glfwMaximizeWindow(window); GLERROR
maximize();
}
void init_controls() {
// ensure we can capture the escape key
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); GLERROR
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); GLERROR
}
const GLFWvidmode *vidmode = nullptr;
public:
GLFWwindow *window = nullptr;
Window(Client &client, const std::string &dir):
width_(0), height_(0),
cObject(client, dir)
{}
size_t width() const { return width_; }
size_t height() const { return height_; }
void gl_version() {
// get version info
const GLubyte* renderer = glGetString(GL_RENDERER); GLERROR // get renderer string
const GLubyte* version = glGetString(GL_VERSION); GLERROR // version as a string
Logger::Info("Renderer: %s\n", renderer);
Logger::Info("OpenGL version supported %s\n", version);
Logger::Info("Supported OpenGL extensions:\n");
GLint no_exts;
glGetIntegerv(GL_NUM_EXTENSIONS, &no_exts);
for(GLint i = 0; i < no_exts; ++i) {
Logger::Info("\t%s\n", glGetStringi(GL_EXTENSIONS, i));
}
}
void run() {
start();
ui::Font::setup();
cObject.init();
int fps = 0;
int current_sec = 0;
int no_frames = 0;
while(!glfwWindowShouldClose(window) && cObject.is_active()) {
int new_s = int(Timer::system_time());
if(new_s != current_sec) {
current_sec = new_s;
fps = no_frames;
//printf("fps=%d\n", fps);
no_frames = 1;
} else {
++no_frames;
}
glfwPollEvents(); GLERROR
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); GLERROR
cObject.mouse(cursor_pos.x, cursor_pos.y);
cObject.display(window, width(), height());
glfwSwapBuffers(window); GLERROR
}
cObject.clear();
ui::Font::cleanup();
glfwDestroyWindow(window); GLERROR
glfwTerminate(); GLERROR
}
void resize(float new_width, float new_height) {
width_ = new_width, height_ = new_height;
}
void maximize() {
int w, h;
glfwGetWindowSize(window, &w, &h); GLERROR
resize(w, h);
}
void keyboard_event(int key, int scancode, int action, int mods) {
if(action == GLFW_PRESS) {
cObject.keypress(key, mods);
}
}
void mouse(double m_x, double m_y) {
m_x /= width(), m_y /= height();
if(m_x >= cursor_area.x2()) {
cursor_area.xs += (m_x - cursor_area.x2());
} else if(m_x <= cursor_area.x1()) {
cursor_area.xs -= (cursor_area.x1() - m_x);
}
if(m_y >= cursor_area.y2()) {
cursor_area.ys += (m_y - cursor_area.y2());
} else if(m_y <= cursor_area.y1()) {
cursor_area.ys -= (cursor_area.y1() - m_y);
}
cursor_pos = {m_x - cursor_area.x1(), m_y - cursor_area.y1()};
}
void mouse_click(int button, int action, int mods) {
cObject.mouse_click(button, action);
}
void mouse_scroll(double xoffset, double yoffset) {
}
};
static Window &get_window(GLFWwindow *window) {
return *((Window *)(window_reference[window]));
}
namespace glfw {
void error_callback(int error, const char* description) {
/* #ifndef NDEBUG */
Logger::Error("[GLFW] code %i msg: %s\n", error, description);
/* #endif */
}
void keypress_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
get_window(window).keyboard_event(key, scancode, action, mods);
}
void size_callback(GLFWwindow *window, int new_width, int new_height) {
get_window(window).resize((float)new_width, (float)new_height);
}
void cursor_area_callback(GLFWwindow *window, double xpos, double ypos) {
get_window(window).mouse(xpos, ypos);
}
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
get_window(window).mouse_click(button, action, mods);
}
void mouse_scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
get_window(window).mouse_scroll(xoffset, yoffset);
}
}