minor refactor

This commit is contained in:
MihailRis 2024-03-29 12:24:02 +03:00
parent 3b3c5f081c
commit a4960097f0
5 changed files with 386 additions and 390 deletions

View File

@ -3,69 +3,65 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
using glm::vec3; Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position), zoom(1.0f), rotation(1.0f) {
using glm::vec4; updateVectors();
using glm::mat4;
Camera::Camera(vec3 position, float fov) : fov(fov), position(position), zoom(1.0f), rotation(1.0f) {
updateVectors();
} }
void Camera::updateVectors(){ void Camera::updateVectors(){
front = vec3(rotation * vec4(0,0,-1,1)); front = glm::vec3(rotation * glm::vec4(0,0,-1,1));
right = vec3(rotation * vec4(1,0,0,1)); right = glm::vec3(rotation * glm::vec4(1,0,0,1));
up = vec3(rotation * vec4(0,1,0,1)); up = glm::vec3(rotation * glm::vec4(0,1,0,1));
dir = vec3(rotation * vec4(0,0,-1,1)); dir = glm::vec3(rotation * glm::vec4(0,0,-1,1));
dir.y = 0; dir.y = 0;
float len = length(dir); float len = glm::length(dir);
if (len > 0.0f){ if (len > 0.0f){
dir.x /= len; dir.x /= len;
dir.z /= len; dir.z /= len;
} }
} }
void Camera::rotate(float x, float y, float z){ void Camera::rotate(float x, float y, float z){
rotation = glm::rotate(rotation, z, vec3(0,0,1)); rotation = glm::rotate(rotation, z, glm::vec3(0,0,1));
rotation = glm::rotate(rotation, y, vec3(0,1,0)); rotation = glm::rotate(rotation, y, glm::vec3(0,1,0));
rotation = glm::rotate(rotation, x, vec3(1,0,0)); rotation = glm::rotate(rotation, x, glm::vec3(1,0,0));
updateVectors(); updateVectors();
} }
mat4 Camera::getProjection(){ glm::mat4 Camera::getProjection(){
float aspect = this->aspect; float aspect = this->aspect;
if (aspect == 0.0f){ if (aspect == 0.0f){
aspect = (float)Window::width / (float)Window::height; aspect = (float)Window::width / (float)Window::height;
} }
if (perspective) if (perspective)
return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f); return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f);
else else
if (flipped) if (flipped)
return glm::ortho(0.0f, fov*aspect, fov, 0.0f); return glm::ortho(0.0f, fov*aspect, fov, 0.0f);
else else
return glm::ortho(0.0f, fov*aspect, 0.0f, fov); return glm::ortho(0.0f, fov*aspect, 0.0f, fov);
} }
mat4 Camera::getView(bool pos){ glm::mat4 Camera::getView(bool pos){
vec3 position = this->position; glm::vec3 position = this->position;
if (!pos) { if (!pos) {
position = vec3(0.0f); position = glm::vec3(0.0f);
} }
if (perspective) { if (perspective) {
return glm::lookAt(position, position+front, up); return glm::lookAt(position, position+front, up);
} else { } else {
return glm::translate(glm::mat4(1.0f), position); return glm::translate(glm::mat4(1.0f), position);
} }
} }
mat4 Camera::getProjView(bool pos){ glm::mat4 Camera::getProjView(bool pos){
return getProjection()*getView(pos); return getProjection()*getView(pos);
} }
void Camera::setFov(float fov) { void Camera::setFov(float fov) {
this->fov = fov; this->fov = fov;
} }
float Camera::getFov() const { float Camera::getFov() const {
return fov; return fov;
} }

View File

@ -4,31 +4,31 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
class Camera { class Camera {
void updateVectors(); void updateVectors();
float fov; float fov;
public: public:
glm::vec3 front; glm::vec3 front;
glm::vec3 up; glm::vec3 up;
glm::vec3 right; glm::vec3 right;
glm::vec3 dir; glm::vec3 dir;
glm::vec3 position; glm::vec3 position;
float zoom; float zoom;
glm::mat4 rotation; glm::mat4 rotation;
bool perspective = true; bool perspective = true;
bool flipped = false; bool flipped = false;
float aspect = 0.0f; float aspect = 0.0f;
Camera(glm::vec3 position, float fov); Camera(glm::vec3 position, float fov);
void rotate(float x, float y, float z); void rotate(float x, float y, float z);
glm::mat4 getProjection(); glm::mat4 getProjection();
glm::mat4 getView(bool position=true); glm::mat4 getView(bool position=true);
glm::mat4 getProjView(bool position=true); glm::mat4 getProjView(bool position=true);
void setFov(float fov); void setFov(float fov);
float getFov() const; float getFov() const;
}; };
#endif /* WINDOW_CAMERA_H_ */ #endif /* WINDOW_CAMERA_H_ */

View File

@ -24,25 +24,25 @@ void mouse_button_callback(GLFWwindow*, int button, int action, int) {
} }
void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) { void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) {
if (key == GLFW_KEY_UNKNOWN) return; if (key == GLFW_KEY_UNKNOWN) return;
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
Events::setKey(key, true); Events::setKey(key, true);
Events::pressedKeys.push_back(static_cast<keycode>(key)); Events::pressedKeys.push_back(static_cast<keycode>(key));
} }
else if (action == GLFW_RELEASE) { else if (action == GLFW_RELEASE) {
Events::setKey(key, false); Events::setKey(key, false);
} }
else if (action == GLFW_REPEAT) { else if (action == GLFW_REPEAT) {
Events::pressedKeys.push_back(static_cast<keycode>(key)); Events::pressedKeys.push_back(static_cast<keycode>(key));
} }
} }
void scroll_callback(GLFWwindow*, double xoffset, double yoffset) { void scroll_callback(GLFWwindow*, double xoffset, double yoffset) {
Events::scroll += yoffset; Events::scroll += yoffset;
} }
bool Window::isMaximized() { bool Window::isMaximized() {
return glfwGetWindowAttrib(window, GLFW_MAXIMIZED); return glfwGetWindowAttrib(window, GLFW_MAXIMIZED);
} }
bool Window::isIconified() { bool Window::isIconified() {
@ -51,67 +51,67 @@ bool Window::isIconified() {
bool Window::isFocused() bool Window::isFocused()
{ {
return glfwGetWindowAttrib(window, GLFW_FOCUSED); return glfwGetWindowAttrib(window, GLFW_FOCUSED);
} }
void window_size_callback(GLFWwindow*, int width, int height) { void window_size_callback(GLFWwindow*, int width, int height) {
if (width && height) { if (width && height) {
if (Window::isFocused()) { if (Window::isFocused()) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
Window::width = width; Window::width = width;
Window::height = height; Window::height = height;
} }
if (!Window::isFullscreen() && !Window::isMaximized()) { if (!Window::isFullscreen() && !Window::isMaximized()) {
Window::getSettings()->width = width; Window::getSettings()->width = width;
Window::getSettings()->height = height; Window::getSettings()->height = height;
} }
} }
Window::resetScissor(); Window::resetScissor();
} }
void character_callback(GLFWwindow* window, unsigned int codepoint){ void character_callback(GLFWwindow* window, unsigned int codepoint){
Events::codepoints.push_back(codepoint); Events::codepoints.push_back(codepoint);
} }
const char* glfwErrorName(int error) { const char* glfwErrorName(int error) {
switch (error) { switch (error) {
case GLFW_NO_ERROR: return "no error"; case GLFW_NO_ERROR: return "no error";
case GLFW_NOT_INITIALIZED: return "not initialized"; case GLFW_NOT_INITIALIZED: return "not initialized";
case GLFW_NO_CURRENT_CONTEXT: return "no current context"; case GLFW_NO_CURRENT_CONTEXT: return "no current context";
case GLFW_INVALID_ENUM: return "invalid enum"; case GLFW_INVALID_ENUM: return "invalid enum";
case GLFW_INVALID_VALUE: return "invalid value"; case GLFW_INVALID_VALUE: return "invalid value";
case GLFW_OUT_OF_MEMORY: return "out of memory"; case GLFW_OUT_OF_MEMORY: return "out of memory";
case GLFW_API_UNAVAILABLE: return "api unavailable"; case GLFW_API_UNAVAILABLE: return "api unavailable";
case GLFW_VERSION_UNAVAILABLE: return "version unavailable"; case GLFW_VERSION_UNAVAILABLE: return "version unavailable";
case GLFW_PLATFORM_ERROR: return "platform error"; case GLFW_PLATFORM_ERROR: return "platform error";
case GLFW_FORMAT_UNAVAILABLE: return "format unavailable"; case GLFW_FORMAT_UNAVAILABLE: return "format unavailable";
case GLFW_NO_WINDOW_CONTEXT: return "no window context"; case GLFW_NO_WINDOW_CONTEXT: return "no window context";
default: return "unknown error"; default: return "unknown error";
} }
} }
void error_callback(int error, const char* description) { void error_callback(int error, const char* description) {
std::cerr << "GLFW error [0x" << std::hex << error << "]: "; std::cerr << "GLFW error [0x" << std::hex << error << "]: ";
std::cerr << glfwErrorName(error) << std::endl; std::cerr << glfwErrorName(error) << std::endl;
if (description) { if (description) {
std::cerr << description << std::endl; std::cerr << description << std::endl;
} }
} }
int Window::initialize(DisplaySettings& settings){ int Window::initialize(DisplaySettings& settings){
Window::settings = &settings; Window::settings = &settings;
Window::width = settings.width; Window::width = settings.width;
Window::height = settings.height; Window::height = settings.height;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
if (glfwInit() == GLFW_FALSE) { if (glfwInit() == GLFW_FALSE) {
std::cerr << "Failed to initialize GLFW" << std::endl; std::cerr << "Failed to initialize GLFW" << std::endl;
return -1; return -1;
} }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
@ -119,48 +119,48 @@ int Window::initialize(DisplaySettings& settings){
#else #else
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
#endif #endif
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, settings.samples); glfwWindowHint(GLFW_SAMPLES, settings.samples);
window = glfwCreateWindow(width, height, settings.title.c_str(), nullptr, nullptr); window = glfwCreateWindow(width, height, settings.title.c_str(), nullptr, nullptr);
if (window == nullptr){ if (window == nullptr){
std::cerr << "Failed to create GLFW Window" << std::endl; std::cerr << "Failed to create GLFW Window" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
GLenum glewErr = glewInit(); GLenum glewErr = glewInit();
if (glewErr != GLEW_OK){ if (glewErr != GLEW_OK){
std::cerr << "Failed to initialize GLEW: " << std::endl; std::cerr << "Failed to initialize GLEW: " << std::endl;
std::cerr << glewGetErrorString(glewErr) << std::endl; std::cerr << glewGetErrorString(glewErr) << std::endl;
return -1; return -1;
} }
glViewport(0,0, width, height); glViewport(0,0, width, height);
glClearColor(0.0f,0.0f,0.0f, 1); glClearColor(0.0f,0.0f,0.0f, 1);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetWindowSizeCallback(window, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetCharCallback(window, character_callback); glfwSetCharCallback(window, character_callback);
glfwSetScrollCallback(window, scroll_callback); glfwSetScrollCallback(window, scroll_callback);
if (settings.fullscreen) { if (settings.fullscreen) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor); const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
} }
glfwSwapInterval(settings.swapInterval); glfwSwapInterval(settings.swapInterval);
const GLubyte* vendor = glGetString(GL_VENDOR); const GLubyte* vendor = glGetString(GL_VENDOR);
const GLubyte* renderer = glGetString(GL_RENDERER); const GLubyte* renderer = glGetString(GL_RENDERER);
std::cout << "GL Vendor: " << (char*)vendor << std::endl; std::cout << "GL Vendor: " << (char*)vendor << std::endl;
std::cout << "GL Renderer: " << (char*)renderer << std::endl; std::cout << "GL Renderer: " << (char*)renderer << std::endl;
std::cout << "GLFW: " << glfwGetVersionString() << std::endl; std::cout << "GLFW: " << glfwGetVersionString() << std::endl;
return 0; return 0;
} }
void Window::setBlendMode(blendmode mode) { void Window::setBlendMode(blendmode mode) {
@ -178,140 +178,140 @@ void Window::setBlendMode(blendmode mode) {
} }
void Window::clear() { void Window::clear() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
void Window::clearDepth() { void Window::clearDepth() {
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
} }
void Window::setBgColor(glm::vec3 color) { void Window::setBgColor(glm::vec3 color) {
glClearColor(color.r, color.g, color.b, 1.0f); glClearColor(color.r, color.g, color.b, 1.0f);
} }
void Window::setBgColor(glm::vec4 color) { void Window::setBgColor(glm::vec4 color) {
glClearColor(color.r, color.g, color.b, color.a); glClearColor(color.r, color.g, color.b, color.a);
} }
void Window::viewport(int x, int y, int width, int height){ void Window::viewport(int x, int y, int width, int height){
glViewport(x, y, width, height); glViewport(x, y, width, height);
} }
void Window::setCursorMode(int mode){ void Window::setCursorMode(int mode){
glfwSetInputMode(window, GLFW_CURSOR, mode); glfwSetInputMode(window, GLFW_CURSOR, mode);
} }
void Window::resetScissor() { void Window::resetScissor() {
scissorArea = glm::vec4(0.0f, 0.0f, width, height); scissorArea = glm::vec4(0.0f, 0.0f, width, height);
scissorStack = std::stack<glm::vec4>(); scissorStack = std::stack<glm::vec4>();
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
} }
void Window::pushScissor(glm::vec4 area) { void Window::pushScissor(glm::vec4 area) {
if (scissorStack.empty()) { if (scissorStack.empty()) {
glEnable(GL_SCISSOR_TEST); glEnable(GL_SCISSOR_TEST);
} }
scissorStack.push(scissorArea); scissorStack.push(scissorArea);
area.z += area.x; area.z += area.x;
area.w += area.y; area.w += area.y;
area.x = fmax(area.x, scissorArea.x); area.x = fmax(area.x, scissorArea.x);
area.y = fmax(area.y, scissorArea.y); area.y = fmax(area.y, scissorArea.y);
area.z = fmin(area.z, scissorArea.z); area.z = fmin(area.z, scissorArea.z);
area.w = fmin(area.w, scissorArea.w); area.w = fmin(area.w, scissorArea.w);
if (area.z < 0.0f || area.w < 0.0f) { if (area.z < 0.0f || area.w < 0.0f) {
glScissor(0, 0, 0, 0); glScissor(0, 0, 0, 0);
} else { } else {
glScissor(area.x, Window::height-area.w, glScissor(area.x, Window::height-area.w,
std::max(0, int(area.z-area.x)), std::max(0, int(area.z-area.x)),
std::max(0, int(area.w-area.y))); std::max(0, int(area.w-area.y)));
} }
scissorArea = area; scissorArea = area;
} }
void Window::popScissor() { void Window::popScissor() {
if (scissorStack.empty()) { if (scissorStack.empty()) {
std::cerr << "warning: extra Window::popScissor call" << std::endl; std::cerr << "warning: extra Window::popScissor call" << std::endl;
return; return;
} }
glm::vec4 area = scissorStack.top(); glm::vec4 area = scissorStack.top();
scissorStack.pop(); scissorStack.pop();
if (area.z < 0.0f || area.w < 0.0f) { if (area.z < 0.0f || area.w < 0.0f) {
glScissor(0, 0, 0, 0); glScissor(0, 0, 0, 0);
} else { } else {
glScissor(area.x, Window::height-area.w, glScissor(area.x, Window::height-area.w,
std::max(0, int(area.z-area.x)), std::max(0, int(area.z-area.x)),
std::max(0, int(area.w-area.y))); std::max(0, int(area.w-area.y)));
} }
if (scissorStack.empty()) { if (scissorStack.empty()) {
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
} }
scissorArea = area; scissorArea = area;
} }
void Window::terminate(){ void Window::terminate(){
glfwTerminate(); glfwTerminate();
} }
bool Window::isShouldClose(){ bool Window::isShouldClose(){
return glfwWindowShouldClose(window); return glfwWindowShouldClose(window);
} }
void Window::setShouldClose(bool flag){ void Window::setShouldClose(bool flag){
glfwSetWindowShouldClose(window, flag); glfwSetWindowShouldClose(window, flag);
} }
void Window::swapInterval(int interval){ void Window::swapInterval(int interval){
glfwSwapInterval(interval); glfwSwapInterval(interval);
} }
void Window::toggleFullscreen(){ void Window::toggleFullscreen(){
settings->fullscreen = !settings->fullscreen; settings->fullscreen = !settings->fullscreen;
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor); const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if (Events::_cursor_locked) Events::toggleCursor(); if (Events::_cursor_locked) Events::toggleCursor();
if (settings->fullscreen) { if (settings->fullscreen) {
glfwGetWindowPos(window, &posX, &posY); glfwGetWindowPos(window, &posX, &posY);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
} }
else { else {
glfwSetWindowMonitor(window, nullptr, posX, posY, settings->width, settings->height, GLFW_DONT_CARE); glfwSetWindowMonitor(window, nullptr, posX, posY, settings->width, settings->height, GLFW_DONT_CARE);
glfwSetWindowAttrib(window, GLFW_MAXIMIZED, GLFW_FALSE); glfwSetWindowAttrib(window, GLFW_MAXIMIZED, GLFW_FALSE);
} }
double xPos, yPos; double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos); glfwGetCursorPos(window, &xPos, &yPos);
Events::setPosition(xPos, yPos); Events::setPosition(xPos, yPos);
} }
bool Window::isFullscreen() { bool Window::isFullscreen() {
return settings->fullscreen; return settings->fullscreen;
} }
void Window::swapBuffers(){ void Window::swapBuffers(){
glfwSwapBuffers(window); glfwSwapBuffers(window);
Window::resetScissor(); Window::resetScissor();
} }
double Window::time() { double Window::time() {
return glfwGetTime(); return glfwGetTime();
} }
DisplaySettings* Window::getSettings() { DisplaySettings* Window::getSettings() {
return settings; return settings;
} }
ImageData* Window::takeScreenshot() { ImageData* Window::takeScreenshot() {
ubyte* data = new ubyte[width * height * 3]; ubyte* data = new ubyte[width * height * 3];
glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
return new ImageData(ImageFormat::rgb888, width, height, data); return new ImageData(ImageFormat::rgb888, width, height, data);
} }
const char* Window::getClipboardText() { const char* Window::getClipboardText() {
@ -323,19 +323,19 @@ void Window::setClipboardText(const char* text) {
} }
bool Window::tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor) { bool Window::tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor) {
glm::ivec4 windowFrame(0); glm::ivec4 windowFrame(0);
glm::ivec4 workArea(0); glm::ivec4 workArea(0);
glfwGetWindowFrameSize(window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w); glfwGetWindowFrameSize(window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w);
glfwGetMonitorWorkarea(monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w); glfwGetMonitorWorkarea(monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w);
if (Window::width > (uint)workArea.z) Window::width = (uint)workArea.z; if (Window::width > (uint)workArea.z) Window::width = (uint)workArea.z;
if (Window::height > (uint)workArea.w) Window::height = (uint)workArea.w; if (Window::height > (uint)workArea.w) Window::height = (uint)workArea.w;
if (Window::width >= (uint)(workArea.z - (windowFrame.x + windowFrame.z)) && if (Window::width >= (uint)(workArea.z - (windowFrame.x + windowFrame.z)) &&
Window::height >= (uint)(workArea.w - (windowFrame.y + windowFrame.w))) { Window::height >= (uint)(workArea.w - (windowFrame.y + windowFrame.w))) {
glfwMaximizeWindow(window); glfwMaximizeWindow(window);
return true; return true;
} }
glfwSetWindowSize(window, Window::width, Window::height); glfwSetWindowSize(window, Window::width, Window::height);
glfwSetWindowPos(window, workArea.x + (workArea.z - Window::width) / 2, glfwSetWindowPos(window, workArea.x + (workArea.z - Window::width) / 2,
workArea.y + (workArea.w - Window::height) / 2 + windowFrame.y / 2); workArea.y + (workArea.w - Window::height) / 2 + windowFrame.y / 2);
return false; return false;
} }

View File

@ -19,52 +19,52 @@ enum class blendmode {
}; };
class Window { class Window {
static GLFWwindow* window; static GLFWwindow* window;
static DisplaySettings* settings; static DisplaySettings* settings;
static std::stack<glm::vec4> scissorStack; static std::stack<glm::vec4> scissorStack;
static glm::vec4 scissorArea; static glm::vec4 scissorArea;
static bool tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor); static bool tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor);
public: public:
static int posX; static int posX;
static int posY; static int posY;
static uint width; static uint width;
static uint height; static uint height;
static int initialize(DisplaySettings& settings); static int initialize(DisplaySettings& settings);
static void terminate(); static void terminate();
static void viewport(int x, int y, int width, int height); static void viewport(int x, int y, int width, int height);
static void setCursorMode(int mode); static void setCursorMode(int mode);
static bool isShouldClose(); static bool isShouldClose();
static void setShouldClose(bool flag); static void setShouldClose(bool flag);
static void swapBuffers(); static void swapBuffers();
static void swapInterval(int interval); static void swapInterval(int interval);
static void toggleFullscreen(); static void toggleFullscreen();
static bool isFullscreen(); static bool isFullscreen();
static bool isMaximized(); static bool isMaximized();
static bool isFocused(); static bool isFocused();
static bool isIconified(); static bool isIconified();
static void pushScissor(glm::vec4 area); static void pushScissor(glm::vec4 area);
static void popScissor(); static void popScissor();
static void resetScissor(); static void resetScissor();
static void clear(); static void clear();
static void clearDepth(); static void clearDepth();
static void setBgColor(glm::vec3 color); static void setBgColor(glm::vec3 color);
static void setBgColor(glm::vec4 color); static void setBgColor(glm::vec4 color);
static double time(); static double time();
static const char* getClipboardText(); static const char* getClipboardText();
static void setClipboardText(const char* text); static void setClipboardText(const char* text);
static DisplaySettings* getSettings(); static DisplaySettings* getSettings();
static void setBlendMode(blendmode mode); static void setBlendMode(blendmode mode);
static glm::vec2 size() { static glm::vec2 size() {
return glm::vec2(width, height); return glm::vec2(width, height);
} }
static ImageData* takeScreenshot(); static ImageData* takeScreenshot();
}; };
#endif /* WINDOW_WINDOW_H_ */ #endif /* WINDOW_WINDOW_H_ */

View File

@ -5,84 +5,84 @@
/// @brief Represents glfw3 keycode values. /// @brief Represents glfw3 keycode values.
enum class keycode : int { enum class keycode : int {
ENTER = 257, ENTER = 257,
TAB = 258, TAB = 258,
SPACE = 32, SPACE = 32,
BACKSPACE = 259, BACKSPACE = 259,
LEFT_SHIFT = 340, LEFT_SHIFT = 340,
LEFT_CONTROL = 341, LEFT_CONTROL = 341,
LEFT_ALT = 342, LEFT_ALT = 342,
RIGHT_SHIFT = 344, RIGHT_SHIFT = 344,
RIGHT_CONTROL = 345, RIGHT_CONTROL = 345,
RIGHT_ALT = 346, RIGHT_ALT = 346,
ESCAPE = 256, ESCAPE = 256,
CAPS_LOCK = 280, CAPS_LOCK = 280,
LEFT = 263, LEFT = 263,
RIGHT = 262, RIGHT = 262,
DOWN = 264, DOWN = 264,
UP = 265, UP = 265,
F1 = 290, F1 = 290,
F2 = 291, F2 = 291,
F3 = 292, F3 = 292,
F4 = 293, F4 = 293,
F5 = 294, F5 = 294,
F6 = 295, F6 = 295,
F7 = 296, F7 = 296,
F8 = 297, F8 = 297,
F9 = 298, F9 = 298,
F10 = 299, F10 = 299,
F11 = 300, F11 = 300,
F12 = 301, F12 = 301,
A = 65, A = 65,
B = 66, B = 66,
C = 67, C = 67,
D = 68, D = 68,
E = 69, E = 69,
F = 70, F = 70,
G = 71, G = 71,
H = 72, H = 72,
I = 73, I = 73,
J = 74, J = 74,
K = 75, K = 75,
L = 76, L = 76,
M = 77, M = 77,
N = 78, N = 78,
O = 79, O = 79,
P = 80, P = 80,
Q = 81, Q = 81,
R = 82, R = 82,
S = 83, S = 83,
T = 84, T = 84,
U = 85, U = 85,
V = 86, V = 86,
W = 87, W = 87,
X = 88, X = 88,
Y = 89, Y = 89,
Z = 90, Z = 90,
NUM_0 = 48, NUM_0 = 48,
NUM_1 = 49, NUM_1 = 49,
NUM_2 = 50, NUM_2 = 50,
NUM_3 = 51, NUM_3 = 51,
NUM_4 = 52, NUM_4 = 52,
NUM_5 = 53, NUM_5 = 53,
NUM_6 = 54, NUM_6 = 54,
NUM_7 = 55, NUM_7 = 55,
NUM_8 = 56, NUM_8 = 56,
NUM_9 = 57, NUM_9 = 57,
MENU = 348, MENU = 348,
PAUSE = 284, PAUSE = 284,
INSERT = 260, INSERT = 260,
LEFT_SUPER = 343, LEFT_SUPER = 343,
RIGHT_SUPER = 347, RIGHT_SUPER = 347,
DELETE = 261, DELETE = 261,
PAGE_UP = 266, PAGE_UP = 266,
PAGE_DOWN = 267, PAGE_DOWN = 267,
HOME = 268, HOME = 268,
END = 269, END = 269,
PRINT_SCREEN = 283, PRINT_SCREEN = 283,
NUM_LOCK = 282, NUM_LOCK = 282,
LEFT_BRACKET = 91, LEFT_BRACKET = 91,
RIGHT_BRACKET = 93, RIGHT_BRACKET = 93,
}; };
@ -90,58 +90,58 @@ enum class keycode : int {
/// @brief Represents glfw3 mouse button IDs. /// @brief Represents glfw3 mouse button IDs.
/// @details There is a subset of glfw3 mouse button IDs. /// @details There is a subset of glfw3 mouse button IDs.
enum class mousecode : int { enum class mousecode : int {
BUTTON_1 = 0, // Left mouse button BUTTON_1 = 0, // Left mouse button
BUTTON_2 = 1, // Right mouse button BUTTON_2 = 1, // Right mouse button
BUTTON_3 = 2, // Middle mouse button BUTTON_3 = 2, // Middle mouse button
}; };
inline mousecode MOUSECODES_ALL[] { inline mousecode MOUSECODES_ALL[] {
mousecode::BUTTON_1, mousecode::BUTTON_1,
mousecode::BUTTON_2, mousecode::BUTTON_2,
mousecode::BUTTON_3 mousecode::BUTTON_3
}; };
namespace input_util { namespace input_util {
/// @return Key label by keycode /// @return Key label by keycode
std::string to_string(keycode code); std::string to_string(keycode code);
/// @return Mouse button label by keycode /// @return Mouse button label by keycode
std::string to_string(mousecode code); std::string to_string(mousecode code);
} }
enum class inputtype { enum class inputtype {
keyboard, keyboard,
mouse, mouse,
}; };
struct Binding { struct Binding {
inputtype type; inputtype type;
int code; int code;
bool state = false; bool state = false;
bool justChange = false; bool justChange = false;
bool active() const { bool active() const {
return state; return state;
} }
bool jactive() const { bool jactive() const {
return state && justChange; return state && justChange;
} }
void reset(inputtype, int); void reset(inputtype, int);
void reset(keycode); void reset(keycode);
void reset(mousecode); void reset(mousecode);
inline const std::string text() const { inline const std::string text() const {
switch (type) { switch (type) {
case inputtype::keyboard: { case inputtype::keyboard: {
return input_util::to_string(static_cast<keycode>(code)); return input_util::to_string(static_cast<keycode>(code));
} }
case inputtype::mouse: { case inputtype::mouse: {
return input_util::to_string(static_cast<mousecode>(code)); return input_util::to_string(static_cast<mousecode>(code));
} }
} }
return "<unknown input type>"; return "<unknown input type>";
} }
}; };