Merge pull request #6 from E-Almqvist/window_wrapper

Window wrapper
main
Elias Almqvist 2 years ago committed by GitHub
commit b6af64548f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      headers/renderer.hpp
  2. 20
      headers/window.hpp
  3. 20
      src/controller.cpp
  4. 114
      src/main.cpp
  5. 7
      src/renderer.cpp
  6. 4
      src/textures.cpp
  7. 36
      src/window.cpp

@ -84,7 +84,7 @@ namespace Renderer {
class Scene {
public:
float deltaTime = 0.0f;
float deltaTime = 0.0f; // Seconds
Scene(Window* win);
Scene(Window* win, std::vector<RenderObject*> ROs);

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

@ -10,15 +10,15 @@
void Controller::processMouseInput(float deltaTime) {
double x, y;
glfwGetCursorPos(window->win, &x, &y);
glfwGetCursorPos(window->getWindow(), &x, &y);
if (firstMouseInput) {
lastX = x;
lastY = y;
firstMouseInput = false;
}
}
float xOffset = x - lastX;
float xOffset = x - lastX;
float yOffset = y - lastY;
lastX = x;
lastY = y;
@ -49,24 +49,24 @@ void Controller::processMouseInput(float deltaTime) {
void Controller::processInput(float deltaTime) {
processMouseInput(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);
}
if (glfwGetKey(window->win, GLFW_KEY_S) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_S) == GLFW_PRESS) {
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);
}
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);
}
if (glfwGetKey(window->win, GLFW_KEY_SPACE) == GLFW_PRESS) {
if (glfwGetKey(window->getWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) {
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);
}
}

@ -15,19 +15,19 @@
std::vector<float> verts({
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,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.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
});
// Vert struc: x y z r g b tx ty
std::vector<unsigned int> indices({
0, 1, 3,
std::vector<unsigned int> indices({
0, 1, 3,
1, 2, 3,
5, 6, 7,
7, 8, 9,
9, 10, 11,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
});
// Window for the game
Window win("Euclid Engine: Demo");
Renderer::Scene scene(&win);
void framebuffer_size_callback(GLFWwindow* glfwWindow, int w, int h) {
win.updateSize(w, h);
}
void processInput(GLFWwindow *win) {
int action = glfwGetKey(win, GLFW_KEY_ESCAPE);
if (action == GLFW_PRESS) {
@ -120,6 +113,9 @@ void processInput(GLFWwindow *win) {
int main() {
// Spawn window
Window win("Euclid Engine: Demo");
Renderer::Scene scene(&win);
win.spawn();
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -127,13 +123,10 @@ int main() {
return 1;
}
glViewport(0, 0, win.width(), win.height());
glfwSetFramebufferSizeCallback(win.win, framebuffer_size_callback); // Framebuffer
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
// 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};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
@ -152,22 +145,23 @@ int main() {
Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f));
scene.setCamera(&player);
while (!glfwWindowShouldClose(win.win)) {
while (!win.shouldClose()) {
// Handle input
player.processInput(scene.deltaTime);
processInput(win.win);
processInput(win.getWindow());
ro.translate(glm::vec3(0.0f, 0.0f, 0.001f));
// ro2.setPosition(glm::vec3(0.0f, 0.0f, -1000.0f));
// ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f));
// ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f));
ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f));
// Render new frame
scene.render();
// glfw
glfwSwapBuffers(win.win);
win.swapBuffers();
glfwPollEvents();
}
glfwTerminate();
return 0;
}

@ -2,6 +2,7 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <stdio.h>
#include "GLFW/glfw3.h"
#include "glm/ext/matrix_transform.hpp"
@ -72,7 +73,7 @@ namespace Renderer {
// Scene
Scene::Scene(Window* win) {
window = win;
window = win;
}
Scene::Scene(Window* win, std::vector<RenderObject*> ROs) : Scene(win) {
@ -92,6 +93,8 @@ namespace Renderer {
float curFrame = glfwGetTime();
deltaTime = curFrame - lastFrame;
printf("\rdeltaTime=%f FPS=%f", deltaTime, 1/(deltaTime+0.0001f));
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -117,7 +120,7 @@ namespace Renderer {
}
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;
}

@ -1,5 +1,5 @@
#include <stdio.h>
#include "../headers/textures.hpp"
#include "textures.hpp"
#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
@ -30,7 +30,7 @@ namespace Textures {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);

@ -4,6 +4,8 @@
#include <cstdlib>
#include <stdio.h>
std::map<GLFWwindow*, Window*> Window::windowMap;
Window::Window(const char* title) {
this->_title = title;
}
@ -13,21 +15,30 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title
this->_height = h;
}
Window::~Window() {
glfwDestroyWindow(_win);
windowMap.erase(_win);
}
void Window::spawn() {
glfwInit();
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_FLOATING, GL_TRUE);
win = glfwCreateWindow(_width, _height, _title, NULL, NULL);
if (win == NULL) {
_win = glfwCreateWindow(_width, _height, _title, NULL, NULL);
if (_win == NULL) {
printf("[ERROR] Failed to create a window.\n");
glfwTerminate();
exit(1);
}
glfwMakeContextCurrent(win);
// Register window in the std::map
windowMap[_win] = this;
glfwSetFramebufferSizeCallback(_win, framebufferSizeCallback);
glfwMakeContextCurrent(_win);
}
void Window::updateSize(int w, int h) {
@ -35,3 +46,20 @@ void Window::updateSize(int w, int h) {
_height = h;
glViewport(0, 0, w, h);
}
void Window::makeCurrent() {
glfwMakeContextCurrent(_win);
}
void Window::swapBuffers() {
glfwSwapBuffers(_win);
}
bool Window::shouldClose() {
return glfwWindowShouldClose(_win);
}
// Framebuffer Size Callback, called each time the window "updates"
void Window::framebufferSizeCallback(GLFWwindow* win, int width, int height) {
windowMap[win]->updateSize(width, height);
}

Loading…
Cancel
Save