From 090e3f110c0a6f6309064706784d634b93b9c0f6 Mon Sep 17 00:00:00 2001 From: Rasmus Hugosson Date: Wed, 18 May 2022 17:42:23 +0200 Subject: [PATCH] Add basic input handling --- src/game.cpp | 31 +++++++++++++++++++++++++++-- src/keyboard.cpp | 31 +++++++++++++++++++++++++++++ src/keyboard.h | 16 +++++++++++++++ src/mouse.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/mouse.h | 20 +++++++++++++++++++ src/renderer.cpp | 44 +++++++++++++++++++++++++++++++++++----- src/renderer.h | 2 +- src/shader.h | 2 ++ 8 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 src/keyboard.cpp create mode 100644 src/keyboard.h create mode 100644 src/mouse.cpp create mode 100644 src/mouse.h diff --git a/src/game.cpp b/src/game.cpp index 23c3ba6..ad9a8a0 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -28,14 +28,41 @@ void game_run() CreateThings(); + double delta_time = 1000.0 / 100.0f; + std::chrono::system_clock::time_point last_frame = std::chrono::system_clock::now(); + std::chrono::duration work_time; + + std::chrono::system_clock::time_point last_print = std::chrono::system_clock::now(); + std::chrono::system_clock::time_point current_time = std::chrono::system_clock::now(); + std::chrono::duration since_last_print; + + int frames = 0; + while (s_running) { + last_frame = std::chrono::system_clock::now(); + renderer_prepare(); renderer_render(); renderer_present(); - // HACK: Delta time should be calculated correctly - std::this_thread::sleep_for(std::chrono::duration(16)); + frames++; + + since_last_print = current_time - last_print; + + if (since_last_print.count() > 1000.0f) + { + last_print = std::chrono::system_clock::now(); + std::cout << "frames: " << frames << std::endl; + frames = 0; + } + + current_time = std::chrono::system_clock::now(); + work_time = current_time - last_frame; + + std::chrono::duration delta_ms(delta_time - work_time.count()); + auto delta_ms_duration = std::chrono::duration_cast(delta_ms); + std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count())); } } diff --git a/src/keyboard.cpp b/src/keyboard.cpp new file mode 100644 index 0000000..d83b3d0 --- /dev/null +++ b/src/keyboard.cpp @@ -0,0 +1,31 @@ +#include "keyboard.h" + +static keyboard_data s_data; + +void keyboard_init(GLFWwindow* window) +{ + glfwSetKeyCallback(window, keyboard_callback); +} + +bool keyboard_is_pressed(int key) +{ + return s_data.keys[key]; +} + +void keyboard_state_set(int key, bool state) +{ + s_data.keys[key] = state; +} + +void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + if (action == GLFW_PRESS) + { + keyboard_state_set(key, true); + } + + else if (action == GLFW_RELEASE) + { + keyboard_state_set(key, false); + } +} diff --git a/src/keyboard.h b/src/keyboard.h new file mode 100644 index 0000000..8080078 --- /dev/null +++ b/src/keyboard.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +struct keyboard_data +{ + GLFWwindow* window; + std::bitset<1024> keys; +}; + +void keyboard_init(GLFWwindow* window); +bool keyboard_is_pressed(int key); +void keyboard_state_set(int key, bool state); + +void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods); \ No newline at end of file diff --git a/src/mouse.cpp b/src/mouse.cpp new file mode 100644 index 0000000..a0ba277 --- /dev/null +++ b/src/mouse.cpp @@ -0,0 +1,52 @@ +#include "mouse.h" + +static mouse_data s_data; + +void mouse_init(GLFWwindow* window) +{ + s_data.window = window; + glfwSetInputMode(s_data.window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + s_data.position_x = 0.0f; + s_data.position_y = 0.0f; +} + +float mouse_position_get_x() +{ + double x, y; + glfwGetCursorPos(s_data.window, &x, &y); + + return (float)x; +} + +float mouse_position_get_y() +{ + double x, y; + glfwGetCursorPos(s_data.window, &x, &y); + + return (float)y; +} + +void mouse_position_set(float x, float y) +{ + s_data.position_x = x; + s_data.position_y = y; + glfwSetCursorPos(s_data.window, (double)x, (double)y); +} + +bool mouse_is_left() +{ + int state = glfwGetMouseButton(s_data.window, GLFW_MOUSE_BUTTON_LEFT); + return state == GLFW_PRESS; +} + +bool mouse_is_right() +{ + int state = glfwGetMouseButton(s_data.window, GLFW_MOUSE_BUTTON_RIGHT); + return state == GLFW_PRESS; +} + +bool mouse_is_middle() +{ + int state = glfwGetMouseButton(s_data.window, GLFW_MOUSE_BUTTON_MIDDLE); + return state == GLFW_PRESS; +} diff --git a/src/mouse.h b/src/mouse.h new file mode 100644 index 0000000..c308444 --- /dev/null +++ b/src/mouse.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +struct mouse_data +{ + GLFWwindow* window; + float position_x; + float position_y; +}; + +void mouse_init(GLFWwindow* window); +float mouse_position_get_x(); +float mouse_position_get_y(); + +void mouse_position_set(float x, float y); + +bool mouse_is_left(); +bool mouse_is_right(); +bool mouse_is_middle(); \ No newline at end of file diff --git a/src/renderer.cpp b/src/renderer.cpp index 96c1fa8..9a6dd7a 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -6,6 +6,8 @@ #include "shader.h" #include "game.h" +#include "keyboard.h" +#include "mouse.h" #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 @@ -38,7 +40,22 @@ bool renderer_init() glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif - s_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Rayman", NULL, NULL); + int monitor_count = 0; + GLFWmonitor** monitors = glfwGetMonitors(&monitor_count); + printf("\nMonitor count: %d\n", monitor_count); + const GLFWvidmode* mode = glfwGetVideoMode(monitors[0]); + + glfwWindowHint(GLFW_RED_BITS, mode->redBits); + glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); + glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); + glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); + + printf("Color format: %d bit RGB\n\n", mode->redBits + mode->greenBits + mode->blueBits); + printf("Refresh rate: %d Hz\n\n", mode->refreshRate); + + s_window = glfwCreateWindow(mode->width, mode->height, "Rayman", monitors[0], NULL); + + //s_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Rayman", NULL, NULL); if (!s_window) @@ -61,9 +78,17 @@ bool renderer_init() std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; + keyboard_init(s_window); + mouse_init(s_window); + mouse_position_set(256.0f, 256.0f); + return true; } +static GLint cam_pos_location = 0; +static GLint target_pos_location = 0; +static GLint power_location = 0; + void CreateThings() { unsigned int va; @@ -71,10 +96,10 @@ void CreateThings() unsigned int ib; float verts[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.5f, 0.5f, 0.0f, - -0.5f, 0.5f, 0.0f + -1.0f, -1.0f, 0.0f, + 1.0f, -1.0f, 0.0f, + 1.0f, 1.0f, 0.0f, + -1.0f, 1.0f, 0.0f }; unsigned int indices[6] = { @@ -92,6 +117,10 @@ void CreateThings() GLuint program = CreateProgram(vertexShaderSource, fragmentShaderSource); glUseProgram(program); + + cam_pos_location = glGetUniformLocation(program, "camera_position"); + target_pos_location = glGetUniformLocation(program, "target_position"); + power_location = glGetUniformLocation(program, "fractal_power"); } void renderer_prepare() @@ -107,6 +136,11 @@ void renderer_render() game_close(); } + else if (keyboard_is_pressed(GLFW_KEY_ESCAPE)) + { + game_close(); + } + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); } diff --git a/src/renderer.h b/src/renderer.h index a33d803..7284e60 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -41,5 +41,5 @@ void GenVertexArray(unsigned int *id); void DeleteVertexArray(unsigned int *id); void GenIndexBuffer(unsigned int *id, const unsigned int *data, int count); void DeleteIndexBuffer(unsigned int *id); -void ConfigVertexArrrayLayout(unsigned int *va, unsigned int *vb, const Layout &layout); +void ConfigVertexArrayLayout(unsigned int *va, unsigned int *vb, const Layout &layout); void AddToLayout(Layout &layout, GLuint type, unsigned int count, bool normalize = false); diff --git a/src/shader.h b/src/shader.h index 2f54396..c9ebc9b 100644 --- a/src/shader.h +++ b/src/shader.h @@ -1,5 +1,7 @@ #pragma once +#include + std::string ReadFile(const char *path); GLuint CreateProgram(const char* VertexPath, const char* FragPath); void DeleteProgram(GLuint *id); \ No newline at end of file