1er commit
This commit is contained in:
134
src/window/Display.cpp
Normal file
134
src/window/Display.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Display.cpp
|
||||
*
|
||||
* Created on: 4 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include "window/Display.h"
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include "game/GameManager.h"
|
||||
#include "render/Renderer.h"
|
||||
#include "render/gui/TowerGui.h"
|
||||
#include "render/WorldRenderer.h"
|
||||
|
||||
#define WINDOW_NAME "Tower Defense"
|
||||
#define WINDOW_WIDTH 800
|
||||
#define WINDOW_HEIGHT 600
|
||||
|
||||
namespace Display {
|
||||
|
||||
static GLFWwindow* window;
|
||||
static bool closeRequested = false;
|
||||
|
||||
static int lastWidth = 0, lastHeight = 0;
|
||||
static float aspectRatio;
|
||||
|
||||
void error_callback(int error, const char* description){
|
||||
fprintf(stderr, "Error: %s\n", description);
|
||||
}
|
||||
|
||||
void keyboard_event(GLFWwindow* window, int key, int scancode, int actions, int mods){
|
||||
std::cout << "Key : " << key << " " << scancode << " " << actions << " " << mods << std::endl;
|
||||
}
|
||||
|
||||
void mouseCursorEvent(GLFWwindow* window, double xPos, double yPos){
|
||||
static double lastX = xPos;
|
||||
static double lastY = yPos;
|
||||
if(isMouseDown(0)){
|
||||
const float relativeX = (float) (xPos - lastX) / (float) lastWidth * 2;
|
||||
const float relativeY = (float) (yPos - lastY) / (float) lastHeight * 2;
|
||||
td::render::WorldRenderer::moveCam(relativeX, relativeY, aspectRatio);
|
||||
}
|
||||
lastX = xPos;
|
||||
lastY = yPos;
|
||||
}
|
||||
|
||||
void windowResizeEvent(GLFWwindow* window, int width, int height){
|
||||
aspectRatio = (float) width / height;
|
||||
td::render::Renderer::resize(width, height);
|
||||
lastWidth = width;
|
||||
lastHeight = height;
|
||||
}
|
||||
|
||||
void mouseScroll(GLFWwindow* window, double x, double y){
|
||||
td::render::WorldRenderer::changeZoom(y);
|
||||
}
|
||||
|
||||
void registerCallbacks(){
|
||||
glfwSetKeyCallback(window, &keyboard_event);
|
||||
glfwSetCursorPosCallback(window, &mouseCursorEvent);
|
||||
glfwSetWindowSizeCallback(window, &windowResizeEvent);
|
||||
glfwSetScrollCallback(window, &mouseScroll);
|
||||
}
|
||||
|
||||
void create() {
|
||||
glfwSetErrorCallback(&error_callback);
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_NAME, nullptr, nullptr);
|
||||
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
//registerCallbacks();
|
||||
glfwMakeContextCurrent(window);
|
||||
if(!td::render::Renderer::init()){
|
||||
exit(1);
|
||||
}
|
||||
TowerGui::init(window);
|
||||
windowResizeEvent(window, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
}
|
||||
|
||||
void render() {
|
||||
td::render::Renderer::prepare();
|
||||
td::render::WorldRenderer::render();
|
||||
TowerGui::render();
|
||||
}
|
||||
|
||||
void update() {
|
||||
glfwSwapBuffers(window);
|
||||
int windowWidth, windowHeight;
|
||||
glfwGetWindowSize(window, &windowWidth, &windowHeight);
|
||||
if(windowWidth != lastWidth || windowHeight != lastHeight){
|
||||
windowResizeEvent(window, windowWidth, windowHeight);
|
||||
}
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
td::render::Renderer::destroy();
|
||||
td::render::WorldRenderer::destroy();
|
||||
TowerGui::destroy();
|
||||
glfwDestroyWindow(window);
|
||||
window = NULL;
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
void pollEvents() {
|
||||
glfwPollEvents();
|
||||
td::render::WorldRenderer::update();
|
||||
}
|
||||
|
||||
bool isCloseRequested() {
|
||||
return glfwWindowShouldClose(window);
|
||||
}
|
||||
|
||||
const bool isMouseDown(int button) {
|
||||
return glfwGetMouseButton(window, button);
|
||||
}
|
||||
|
||||
float getAspectRatio(){
|
||||
return aspectRatio;
|
||||
}
|
||||
|
||||
int getWindowWidth(){
|
||||
return lastWidth;
|
||||
}
|
||||
int getWindowHeight(){
|
||||
return lastHeight;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user