From 8b7dd47b533ad358e3e3c5653c191292d123fe05 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 2 Jan 2024 16:28:26 -0500 Subject: [PATCH] states --- include/blt/gfx/input.h | 9 ++++++ include/blt/gfx/window.h | 4 +++ src/blt/gfx/renderer/camera.cpp | 2 ++ src/blt/gfx/window.cpp | 14 ++++++++- tests/src/main.cpp | 54 ++++++++++++++++++--------------- 5 files changed, 57 insertions(+), 26 deletions(-) diff --git a/include/blt/gfx/input.h b/include/blt/gfx/input.h index 47be6d4..f676f17 100644 --- a/include/blt/gfx/input.h +++ b/include/blt/gfx/input.h @@ -32,6 +32,8 @@ namespace blt::gfx double deltaX = 0, deltaY = 0; double scroll = 0; bool mouse_moved = false; + bool key_pressed = false; + bool mouse_pressed = false; private: KEY_STATE* key_state; MOUSE_STATE* mouse_state; @@ -76,6 +78,13 @@ namespace blt::gfx return key_state[key]; } + void clear() + { + key_pressed = false; + mouse_moved = false; + mouse_pressed = false; + } + bool isKeyPressed(std::size_t key) { if (key >= key_size) diff --git a/include/blt/gfx/window.h b/include/blt/gfx/window.h index 3c4cd6f..cfdf6c7 100644 --- a/include/blt/gfx/window.h +++ b/include/blt/gfx/window.h @@ -84,8 +84,12 @@ namespace blt::gfx bool isMousePressed(int button); + bool mousePressedLastFrame(); + bool isKeyPressed(int key); + bool keyPressedLastFrame(); + double getFrameDeltaSeconds(); double getFrameDeltaMilliseconds(); diff --git a/src/blt/gfx/renderer/camera.cpp b/src/blt/gfx/renderer/camera.cpp index b426fe6..5ee4074 100644 --- a/src/blt/gfx/renderer/camera.cpp +++ b/src/blt/gfx/renderer/camera.cpp @@ -25,6 +25,8 @@ void blt::gfx::first_person_camera::update() float speed_multi = 1; + if (isKeyPressed(GLFW_KEY_ESCAPE) && ) + if (isKeyPressed(GLFW_KEY_LEFT_CONTROL)) speed_multi = 10; diff --git a/src/blt/gfx/window.cpp b/src/blt/gfx/window.cpp index 9377b2b..a303cbc 100644 --- a/src/blt/gfx/window.cpp +++ b/src/blt/gfx/window.cpp @@ -59,6 +59,7 @@ namespace blt::gfx state = KEY_STATE::RELEASE; } window_state.inputManager.key(key) = state; + window_state.inputManager.key_pressed = true; }); /* Setup mouse button callback */ @@ -76,6 +77,7 @@ namespace blt::gfx break; } window_state.inputManager.mouse(button) = state; + window_state.inputManager.mouse_pressed = true; }); /* Setup mouse cursor callback */ @@ -160,7 +162,7 @@ namespace blt::gfx ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); /* -- Update GLFW state -- */ - window_state.inputManager.mouse_moved = false; + window_state.inputManager.clear(); glfwSwapBuffers(window_state.window); glfwPollEvents(); @@ -332,4 +334,14 @@ namespace blt::gfx { return window_state.inputManager.mouse_moved; } + + bool mousePressedLastFrame() + { + return window_state.inputManager.mouse_pressed; + } + + bool keyPressedLastFrame() + { + return window_state.inputManager.key_pressed; + } } diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 8b355fb..a6e9830 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -18,6 +18,34 @@ float x = 0, y = 0, z = 0; float bx = 500, by = 500; float mx = 0, my = -9.8; +void local_render(int width, int height) +{ + const float w = 120, h = 120, cf = 30, rf = 15, crf = 10; + + renderer_2d.drawRectangle("ibuythat", (float) width / 2.0f, (float) height / 2.0f, (float) width, (float) height, 90.0f); + renderer_2d.drawRectangle("niko", bx, by, w, h); + + bx += mx * blt::gfx::getFrameDeltaSeconds() * cf; + by += my * blt::gfx::getFrameDeltaSeconds() * cf; + + if (bx < w / 2.0 || bx > width - w / 2.0) + { + mx = -mx; + my += (static_cast(rand()) / static_cast(RAND_MAX) * rf + crf) - (rf + crf) / 2.0f; + } + if (by < h / 2.0 || by > height - h / 2.0) + { + my = -my; + mx += (static_cast(rand()) / static_cast(RAND_MAX) * rf + crf) - (rf + crf) / 2.0f; + } + if (mx > 100 || mx < -100) + { + mx = mx * 0.2; + } + if (my > 100 || my < -100) + my = my * 0.2; +} + void init() { using namespace blt::gfx; @@ -43,33 +71,9 @@ void update(std::int32_t width, std::int32_t height) camera.update(); camera.update_view(global_matrices); - global_matrices.update(); - const float w = 120, h = 120, cf = 30, rf = 15, crf = 10; - - renderer_2d.drawRectangle("ibuythat", (float)width/2.0f, (float)height/2.0f, (float)width, (float)height, 90.0f); - renderer_2d.drawRectangle("niko", bx, by, w, h); - - bx += mx * blt::gfx::getFrameDeltaSeconds() * cf; - by += my * blt::gfx::getFrameDeltaSeconds() * cf; - - if (bx < w / 2.0 || bx > width - w / 2.0) - { - mx = -mx; - my += (static_cast(rand()) / static_cast(RAND_MAX) * rf + crf) - (rf + crf) / 2.0f; - } - if (by < h / 2.0 || by > height - h / 2.0) - { - my = -my; - mx += (static_cast(rand()) / static_cast(RAND_MAX) * rf + crf) - (rf + crf) / 2.0f; - } - if (mx > 100 || mx < -100) - { - mx = mx * 0.2; - } - if (my > 100 || my < -100) - my = my * 0.2; + local_render(width, height); renderer_2d.render(); }