Minor refactor

pull/6/head
E. Almqvist 2 years ago
parent deeb54913f
commit 218d6a7fe3
  1. 17
      headers/window.hpp
  2. 14
      src/controller.cpp
  3. 14
      src/main.cpp
  4. 2
      src/renderer.cpp
  5. 2
      src/textures.cpp
  6. 18
      src/window.cpp

@ -1,7 +1,7 @@
#pragma once #pragma once
// #include "renderer.hpp"
#include <glad/glad.h> #include <glad/glad.h>
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include <map>
// Default window dimensions // Default window dimensions
#define D_WINDOW_TITLE "Euclid Engine" #define D_WINDOW_TITLE "Euclid Engine"
@ -11,19 +11,24 @@
class Window { class Window {
public: public:
GLFWwindow* win;
// Renderer::Camera cam;
Window(const char* title); Window(const char* title);
Window(const char* title, unsigned int w, unsigned int h); Window(const char* title, unsigned int w, unsigned int h);
~Window();
void spawn(); void spawn();
unsigned int width() { return _width; } // getters and dumb and unsigned int getWidth() { return _width; } // getters and dumb and
unsigned int height() { return _height; } // cpp should have readonly fields... unsigned int getHeight() { return _height; } // cpp should have readonly fields...
GLFWwindow* getWindow() { return _win; }
void updateSize(int w, int h); void updateSize(int w, int h);
void makeCurrent();
void swapBuffers();
private: private:
GLFWwindow* _win;
static std::map<GLFWwindow*, Window*> windowMap;
const char* _title = D_WINDOW_TITLE; const char* _title = D_WINDOW_TITLE;
unsigned int _width = D_WINDOW_WIDTH; unsigned int _width = D_WINDOW_WIDTH;
unsigned int _height = D_WINDOW_HEIGHT; unsigned int _height = D_WINDOW_HEIGHT;

@ -10,7 +10,7 @@
void Controller::processMouseInput(float deltaTime) { void Controller::processMouseInput(float deltaTime) {
double x, y; double x, y;
glfwGetCursorPos(window->win, &x, &y); glfwGetCursorPos(window->getWindow(), &x, &y);
if (firstMouseInput) { if (firstMouseInput) {
lastX = x; lastX = x;
@ -51,22 +51,22 @@ void Controller::processInput(float deltaTime) {
float cameraSpeed = deltaTime * 2.5f; float cameraSpeed = deltaTime * 2.5f;
if (glfwGetKey(window->win, GLFW_KEY_W) == GLFW_PRESS) { if (glfwGetKey(window->getWindow(), GLFW_KEY_W) == GLFW_PRESS) {
translate(cameraSpeed * front); translate(cameraSpeed * front);
} }
if (glfwGetKey(window->win, GLFW_KEY_S) == GLFW_PRESS) { if (glfwGetKey(window->getWindow(), GLFW_KEY_S) == GLFW_PRESS) {
translate(-cameraSpeed * front); translate(-cameraSpeed * front);
} }
if (glfwGetKey(window->win, GLFW_KEY_A) == GLFW_PRESS) { if (glfwGetKey(window->getWindow(), GLFW_KEY_A) == GLFW_PRESS) {
translate(-glm::normalize(glm::cross(front, up)) * cameraSpeed); translate(-glm::normalize(glm::cross(front, up)) * cameraSpeed);
} }
if (glfwGetKey(window->win, GLFW_KEY_D) == GLFW_PRESS) { if (glfwGetKey(window->getWindow(), GLFW_KEY_D) == GLFW_PRESS) {
translate(glm::normalize(glm::cross(front, up)) * cameraSpeed); translate(glm::normalize(glm::cross(front, up)) * cameraSpeed);
} }
if (glfwGetKey(window->win, GLFW_KEY_SPACE) == GLFW_PRESS) { if (glfwGetKey(window->getWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) {
translate(cameraSpeed * up); translate(cameraSpeed * up);
} }
if (glfwGetKey(window->win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { if (glfwGetKey(window->getWindow(), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
translate(cameraSpeed * -up); translate(cameraSpeed * -up);
} }
} }

@ -127,13 +127,13 @@ int main() {
return 1; return 1;
} }
glViewport(0, 0, win.width(), win.height()); glViewport(0, 0, win.getWidth(), win.getHeight());
glfwSetFramebufferSizeCallback(win.win, framebuffer_size_callback); // Framebuffer glfwSetFramebufferSizeCallback(win.getWindow(), framebuffer_size_callback); // Framebuffer
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
// Input // Input
glfwSetInputMode(win.win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor glfwSetInputMode(win.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
@ -152,21 +152,21 @@ int main() {
Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f));
scene.setCamera(&player); scene.setCamera(&player);
while (!glfwWindowShouldClose(win.win)) { while (!glfwWindowShouldClose(win.getWindow())) {
// Handle input // Handle input
player.processInput(scene.deltaTime); player.processInput(scene.deltaTime);
processInput(win.win); processInput(win.getWindow());
ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); ro.translate(glm::vec3(0.0f, 0.0f, 0.001f));
// ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f)); // ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f));
ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f)); // ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f));
ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f)); ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f));
// Render new frame // Render new frame
scene.render(); scene.render();
// glfw // glfw
glfwSwapBuffers(win.win); glfwSwapBuffers(win.getWindow());
glfwPollEvents(); glfwPollEvents();
} }

@ -120,7 +120,7 @@ namespace Renderer {
} }
glm::mat4 Camera::getProjection() { glm::mat4 Camera::getProjection() {
projection = glm::perspective(glm::radians(FOV), (float)window->width() / (float)window->height(), NEAR_PLANE, FAR_PLANE); projection = glm::perspective(glm::radians(FOV), (float)window->getWidth() / (float)window->getHeight(), NEAR_PLANE, FAR_PLANE);
return projection; return projection;
} }

@ -1,5 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include "../headers/textures.hpp" #include "textures.hpp"
#include <glad/glad.h> #include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION

@ -13,6 +13,10 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title
this->_height = h; this->_height = h;
} }
Window::~Window() {
glfwDestroyWindow(_win);
}
void Window::spawn() { void Window::spawn() {
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -20,14 +24,14 @@ void Window::spawn() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_FLOATING, GL_TRUE); glfwWindowHint(GLFW_FLOATING, GL_TRUE);
win = glfwCreateWindow(_width, _height, _title, NULL, NULL); _win = glfwCreateWindow(_width, _height, _title, NULL, NULL);
if (win == NULL) { if (_win == NULL) {
printf("[ERROR] Failed to create a window.\n"); printf("[ERROR] Failed to create a window.\n");
glfwTerminate(); glfwTerminate();
exit(1); exit(1);
} }
glfwMakeContextCurrent(win); glfwMakeContextCurrent(_win);
} }
void Window::updateSize(int w, int h) { void Window::updateSize(int w, int h) {
@ -35,3 +39,11 @@ void Window::updateSize(int w, int h) {
_height = h; _height = h;
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
} }
void Window::makeCurrent() {
glfwMakeContextCurrent(_win);
}
void Window::swapBuffers() {
glfwSwapBuffers(_win);
}

Loading…
Cancel
Save