Make Application create a GLFW Window

This commit is contained in:
Karl 2021-04-30 22:40:42 +02:00
parent 05a4fde2e7
commit ff576182c4
4 changed files with 109 additions and 6 deletions

View File

@ -1,21 +1,28 @@
#include "Gedeng/Application.h"
#include "Gedeng/Time.h"
#include "Gedeng/Input.h"
#include "Gedeng/Logger.h"
#include "Gedeng/RenderBackend.h"
#include "Gedeng/Time.h"
namespace Gedeng {
void Application::run() {
// Setup Rendering
// FIXME: Make these parameters variable, maybe move this to a different function
RenderBackend::initialize_window(1920, 1080, String("Application"));
Input::initialize(RenderBackend::get_window());
unsigned long previous_time_ms = Time::get_time_ms();
unsigned long time_until_fixed_update_ms = 0.0;
while (true) {
while (!RenderBackend::does_window_want_to_close()) {
unsigned long current_time_ms = Time::get_time_ms();
unsigned long elapsed_time_ms = current_time_ms - previous_time_ms;
previous_time_ms = current_time_ms;
time_until_fixed_update_ms += elapsed_time_ms;
// Process Input
Input::poll_input();
// Update fixed time step
while (time_until_fixed_update_ms >= MS_PER_UPDATE) {

44
cpp/RenderBackend.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "Gedeng/RenderBackend.h"
namespace Gedeng {
void RenderBackend::initialize_window(unsigned int width, unsigned int height, String title) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
window = glfwCreateWindow(width, height, title, NULL, NULL);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
}
bool RenderBackend::is_window_created() {
return window != nullptr;
}
void RenderBackend::set_depth_test_enabled(bool enabled) {
if (enabled) {
glEnable(GL_DEPTH_TEST);
} else {
glDisable(GL_DEPTH_TEST);
}
}
bool RenderBackend::does_window_want_to_close() {
return glfwWindowShouldClose(window);
}
GLFWwindow *RenderBackend::get_window() {
return window;
}
void RenderBackend::render() {
glfwSwapBuffers(window);
}
} // namespace Gedeng

View File

@ -1,13 +1,32 @@
#pragma once
// Must be the first include
#include <glad/glad.h>
// Other includes
#include <GLFW/glfw3.h>
#include "RenderBackend.h"
#include <map>
namespace Gedeng {
class Input {
public:
inline static std::map<int, bool> keys_down;
static void initialize(GLFWwindow *window) {
glfwSetKeyCallback(window, key_callback);
}
// FIXME: Ignore warnings produced by these unused variables -- they're required for the callback to work
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE);
Input::set_key_down(key, action == GLFW_RELEASE ? false : true);
}
static void poll_input() {
glfwPollEvents();
}
static void set_key_down(int key, bool down) {
keys_down[key] = down;
@ -16,6 +35,8 @@ class Input {
static bool is_key_down(int key) {
return keys_down[key];
}
inline static std::map<int, bool> keys_down;
};
}
} // namespace Gedeng

View File

@ -0,0 +1,31 @@
#pragma once
// Must be the first include
#include <glad/glad.h>
// Other includes
#include <GLFW/glfw3.h>
#include "Gedeng/String.h"
namespace Gedeng {
class RenderBackend {
private:
static inline GLFWwindow *window;
public:
static void initialize_window(unsigned int width, unsigned int height, String title);
static bool is_window_created();
static void set_depth_test_enabled(bool enabled);
static bool does_window_want_to_close();
static GLFWwindow *get_window();
static void render();
};
} // namespace Gedeng