Skip to content

Commit

Permalink
fixed an annoying key bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEArgos committed Jun 24, 2024
1 parent e7d7f98 commit a65e247
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Source/Assets/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.136
0.1.137
14 changes: 2 additions & 12 deletions Source/Modules/Application.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
// members for the key callback.
extern Application* renai;

/**
* @brief A count of the number of render cycles since the last key press. This
* is only available within this file.
*/
static u8 cycles_since_last_key = 1;

/**
* @brief The key callback of the application; this fires every single time a
* key is pressed/actioned upon.
Expand All @@ -24,9 +18,8 @@ static u8 cycles_since_last_key = 1;
void _key_callback(GLFWwindow* window, int key, int scancode, int action,
int mods)
{
HandleInput(renai->keybuffer, renai->window, renai->delta_time, key, action,
cycles_since_last_key);
cycles_since_last_key = 1;
HandleInput(renai->keybuffer, renai->window, renai->delta_time, key,
action);
}

__CREATE_STRUCT_KILLFAIL(Application) CreateApplication(const char* caller)
Expand Down Expand Up @@ -114,9 +107,6 @@ __KILLFAIL RunApplication(Application* application)

// Render the contents of the window.
RenderWindowContent(application->renderer);
// Increment the number of render cycles it's been since the last time a
// key was pressed.
cycles_since_last_key++;

glfwSwapBuffers(application->window->inner_window);

Expand Down
28 changes: 16 additions & 12 deletions Source/Modules/Updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
KeyBuffer* CreateKeyBuffer(void)
{
KeyBuffer* buffer = malloc(sizeof(KeyBuffer));
buffer->cooldown_map = CreateMap(signed32, unsigned32, 179);
// Create the cooldown map with a maximum size of 156, since the highest
// keycode we use is 347, and 347 - 159 - 32 = 156.
buffer->cooldown_map = CreateMap(signed32, signed64, 156);
return buffer;
}

Expand All @@ -13,42 +15,44 @@ void KillKeyBuffer(KeyBuffer* buffer)
free(buffer);
}

__BOOLEAN HandleKey(KeyBuffer* buffer, i32 key, i32 action, u32 time_since_last)
__BOOLEAN HandleKey(KeyBuffer* buffer, i32 key, i32 action)
{
void* keybuffer_value = GetMapItemValue(buffer->cooldown_map, key - 32);
i32 key_number = (key >= GLFW_KEY_ESCAPE ? key - 159 : key) - 32;
void* keybuffer_value = GetMapItemValue(buffer->cooldown_map, key_number);

i64 current_time = GetCurrentTime();
if (keybuffer_value == NULL)
{
AppendMapItem(buffer->cooldown_map, key - 32, 25);
AppendMapItem(buffer->cooldown_map, key_number, current_time);
return true;
}
else if ((i32)VPTT(u32, keybuffer_value) - (i32)time_since_last > 0)
GetMapKeyPair(buffer->cooldown_map, key - 32)->value.unsigned32 -=
time_since_last;
else RemoveMapItem(buffer->cooldown_map, key - 32);
else if ((i32)VPTT(u32, keybuffer_value) - current_time > 0)
GetMapKeyPair(buffer->cooldown_map, key_number)->value.unsigned32 -=
current_time;
else RemoveMapItem(buffer->cooldown_map, key_number);

return false;
}

void HandleInput(KeyBuffer* buffer, Window* key_window, f32 delta_time, i32 key,
i32 action, u32 time_since_last)
i32 action)
{
switch (key)
{
case GLFW_KEY_F11:
if (HandleKey(buffer, GLFW_KEY_F11 - 123, action, time_since_last))
if (HandleKey(buffer, GLFW_KEY_F11, action))
SetWindowFullscreenState(
key_window, !glfwGetWindowAttrib(key_window->inner_window,
GLFW_MAXIMIZED));

return;
case GLFW_KEY_F12:
if (HandleKey(buffer, GLFW_KEY_F12 - 123, action, time_since_last))
if (HandleKey(buffer, GLFW_KEY_F12, action))
glfwSetWindowShouldClose(key_window->inner_window, 1);

return;
case GLFW_KEY_BACKSLASH:
if (HandleKey(buffer, GLFW_KEY_BACKSLASH, action, time_since_last))
if (HandleKey(buffer, GLFW_KEY_BACKSLASH, action))
{
if (glfwGetWindowMonitor(key_window->inner_window) == NULL)
SetWindowFullscreenState(key_window, borderless);
Expand Down
2 changes: 1 addition & 1 deletion Source/Modules/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ KeyBuffer* CreateKeyBuffer(void);
void KillKeyBuffer(KeyBuffer* buffer);

void HandleInput(KeyBuffer* buffer, Window* window, f32 delta_time, i32 key,
i32 action, u32 time_since_last);
i32 action);

#endif // _RENAI_UPDATER
1 change: 1 addition & 0 deletions Source/Types/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct TextureInstance
{
Texture* inherits;
f32 brightness, rotation;
// add a scale option here
f32 x, y;
u8 z;
} TextureInstance;
Expand Down

0 comments on commit a65e247

Please sign in to comment.