main
Brett 2024-01-02 16:28:26 -05:00
parent b13b114d94
commit 8b7dd47b53
5 changed files with 57 additions and 26 deletions

View File

@ -32,6 +32,8 @@ namespace blt::gfx
double deltaX = 0, deltaY = 0; double deltaX = 0, deltaY = 0;
double scroll = 0; double scroll = 0;
bool mouse_moved = false; bool mouse_moved = false;
bool key_pressed = false;
bool mouse_pressed = false;
private: private:
KEY_STATE* key_state; KEY_STATE* key_state;
MOUSE_STATE* mouse_state; MOUSE_STATE* mouse_state;
@ -76,6 +78,13 @@ namespace blt::gfx
return key_state[key]; return key_state[key];
} }
void clear()
{
key_pressed = false;
mouse_moved = false;
mouse_pressed = false;
}
bool isKeyPressed(std::size_t key) bool isKeyPressed(std::size_t key)
{ {
if (key >= key_size) if (key >= key_size)

View File

@ -84,8 +84,12 @@ namespace blt::gfx
bool isMousePressed(int button); bool isMousePressed(int button);
bool mousePressedLastFrame();
bool isKeyPressed(int key); bool isKeyPressed(int key);
bool keyPressedLastFrame();
double getFrameDeltaSeconds(); double getFrameDeltaSeconds();
double getFrameDeltaMilliseconds(); double getFrameDeltaMilliseconds();

View File

@ -25,6 +25,8 @@ void blt::gfx::first_person_camera::update()
float speed_multi = 1; float speed_multi = 1;
if (isKeyPressed(GLFW_KEY_ESCAPE) && )
if (isKeyPressed(GLFW_KEY_LEFT_CONTROL)) if (isKeyPressed(GLFW_KEY_LEFT_CONTROL))
speed_multi = 10; speed_multi = 10;

View File

@ -59,6 +59,7 @@ namespace blt::gfx
state = KEY_STATE::RELEASE; state = KEY_STATE::RELEASE;
} }
window_state.inputManager.key(key) = state; window_state.inputManager.key(key) = state;
window_state.inputManager.key_pressed = true;
}); });
/* Setup mouse button callback */ /* Setup mouse button callback */
@ -76,6 +77,7 @@ namespace blt::gfx
break; break;
} }
window_state.inputManager.mouse(button) = state; window_state.inputManager.mouse(button) = state;
window_state.inputManager.mouse_pressed = true;
}); });
/* Setup mouse cursor callback */ /* Setup mouse cursor callback */
@ -160,7 +162,7 @@ namespace blt::gfx
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
/* -- Update GLFW state -- */ /* -- Update GLFW state -- */
window_state.inputManager.mouse_moved = false; window_state.inputManager.clear();
glfwSwapBuffers(window_state.window); glfwSwapBuffers(window_state.window);
glfwPollEvents(); glfwPollEvents();
@ -332,4 +334,14 @@ namespace blt::gfx
{ {
return window_state.inputManager.mouse_moved; return window_state.inputManager.mouse_moved;
} }
bool mousePressedLastFrame()
{
return window_state.inputManager.mouse_pressed;
}
bool keyPressedLastFrame()
{
return window_state.inputManager.key_pressed;
}
} }

View File

@ -18,6 +18,34 @@ float x = 0, y = 0, z = 0;
float bx = 500, by = 500; float bx = 500, by = 500;
float mx = 0, my = -9.8; 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<float>(rand()) / static_cast<float>(RAND_MAX) * rf + crf) - (rf + crf) / 2.0f;
}
if (by < h / 2.0 || by > height - h / 2.0)
{
my = -my;
mx += (static_cast<float>(rand()) / static_cast<float>(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() void init()
{ {
using namespace blt::gfx; using namespace blt::gfx;
@ -43,33 +71,9 @@ void update(std::int32_t width, std::int32_t height)
camera.update(); camera.update();
camera.update_view(global_matrices); camera.update_view(global_matrices);
global_matrices.update(); global_matrices.update();
const float w = 120, h = 120, cf = 30, rf = 15, crf = 10; local_render(width, height);
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<float>(rand()) / static_cast<float>(RAND_MAX) * rf + crf) - (rf + crf) / 2.0f;
}
if (by < h / 2.0 || by > height - h / 2.0)
{
my = -my;
mx += (static_cast<float>(rand()) / static_cast<float>(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;
renderer_2d.render(); renderer_2d.render();
} }