Remove input dependency on GLFW in game code

Create own platform abstraction for keyboard input for use in game code.
This commit is contained in:
Doyle Thai 2016-08-04 18:19:02 +10:00
parent 523fe0f2b1
commit aa505ed0b3
6 changed files with 112 additions and 22 deletions

Binary file not shown.

View File

@ -432,6 +432,7 @@ void debug_drawUi(GameState *state, f32 dt)
DEBUG_PUSH_VAR("FreeEntityIndex: %d", world->freeEntityIndex, "i32");
DEBUG_PUSH_VAR("glDrawArray Calls: %d",
GLOBAL_debug.callCount[debugcallcount_drawArrays], "i32");
DEBUG_PUSH_VAR("Mouse Pos: %06.2f, %06.2f", state->input.mouse, "v2");
i32 debug_bAllocated = state->arena.bytesAllocated;
DEBUG_PUSH_VAR("TotalMemoryAllocated: %db", debug_bAllocated, "i32");

View File

@ -34,7 +34,6 @@ typedef struct EventQueue
i32 numEvents;
} EventQueue;
INTERNAL Entity *getHeroEntity(World *world)
{
Entity *result = &world->entities[entity_getIndex(world, world->heroId)];
@ -376,24 +375,24 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
// if a button ended down
LOCAL_PERSIST b32 spaceBarWasDown = FALSE;
if (state->keys[GLFW_KEY_RIGHT])
if (state->input.right)
{
ddPos.x = 1.0f;
hero->direction = direction_east;
}
if (state->keys[GLFW_KEY_LEFT])
if (state->input.left)
{
ddPos.x = -1.0f;
hero->direction = direction_west;
}
if (state->keys[GLFW_KEY_UP])
if (state->input.up)
{
ddPos.y = 1.0f;
}
if (state->keys[GLFW_KEY_DOWN])
if (state->input.down)
{
ddPos.y = -1.0f;
}
@ -409,7 +408,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
LOCAL_PERSIST b32 toggleFlag = TRUE;
// TODO(doyle): Revisit key input with state checking for last ended down
if (state->keys[GLFW_KEY_SPACE] && spaceBarWasDown == FALSE)
if (state->input.space && spaceBarWasDown == FALSE)
{
Renderer *renderer = &state->renderer;
f32 yPos = CAST(f32)(rand() % CAST(i32)renderer->size.h);
@ -420,7 +419,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
pos);
spaceBarWasDown = TRUE;
}
else if (!state->keys[GLFW_KEY_SPACE])
else if (!state->input.space)
{
spaceBarWasDown = FALSE;
}
@ -445,7 +444,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
}
f32 heroSpeed = 6.2f * METERS_TO_PIXEL;
if (state->keys[GLFW_KEY_LEFT_SHIFT])
if (state->input.leftShift)
heroSpeed = CAST(f32)(22.0f * 10.0f * METERS_TO_PIXEL);
ddPos = v2_scale(ddPos, heroSpeed);
@ -877,6 +876,7 @@ INTERNAL void sortWorldEntityList(World *world, i32 numDeadEntities)
world->freeEntityIndex -= numDeadEntities;
}
UiState uiState = {0};
void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
{
if (dt >= 1.0f) dt = 1.0f;

View File

@ -5,7 +5,14 @@
#include "Dengine/OpenGL.h"
#include "Dengine/WorldTraveller.h"
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
INTERNAL inline void processKey(b32 *currState, int key, int action)
{
if (action == GLFW_PRESS) *currState = TRUE;
else if (action == GLFW_RELEASE) *currState = FALSE;
}
INTERNAL void keyCallback(GLFWwindow *window, int key, int scancode, int action,
int mode)
{
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
@ -14,20 +21,61 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, int mod
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key >= 0 && key < NUM_KEYS)
switch (key)
{
if (action == GLFW_PRESS)
game->keys[key] = TRUE;
else if (action == GLFW_RELEASE)
game->keys[key] = FALSE;
case GLFW_KEY_UP:
processKey(&game->input.up, key, action);
break;
case GLFW_KEY_DOWN:
processKey(&game->input.down, key, action);
break;
case GLFW_KEY_LEFT:
processKey(&game->input.left, key, action);
break;
case GLFW_KEY_RIGHT:
processKey(&game->input.right, key, action);
break;
case GLFW_KEY_SPACE:
processKey(&game->input.space, key, action);
break;
case GLFW_KEY_LEFT_SHIFT:
processKey(&game->input.leftShift, key, action);
break;
default:
break;
}
}
void mouse_callback(GLFWwindow *window, double xPos, double yPos) {}
INTERNAL void mouseCallback(GLFWwindow *window, double xPos, double yPos)
{
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
void scroll_callback(GLFWwindow *window, double xOffset, double yOffset) {}
// NOTE(doyle): x(0), y(0) of mouse starts from the top left of window
v2 windowSize = game->renderer.size;
f32 flipYPos = windowSize.h - CAST(f32) yPos;
game->input.mouse = V2(CAST(f32) xPos, flipYPos);
}
int main()
INTERNAL void mouseButtonCallback(GLFWwindow *window, int button, int action,
int mods)
{
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
switch(button)
{
case GLFW_MOUSE_BUTTON_LEFT:
processKey(&game->input.mouseLeft, button, action);
break;
default:
break;
}
}
INTERNAL void scrollCallback(GLFWwindow *window, double xOffset, double yOffset)
{
}
i32 main(void)
{
/*
**************************
@ -75,11 +123,12 @@ int main()
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, keyCallback);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetScrollCallback(window, scrollCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);

View File

@ -2,10 +2,41 @@
#define DENGINE_PLATFORM_H
#include "Dengine/Common.h"
#include "Dengine/Math.h"
/* Forward Declaration */
typedef struct MemoryArena MemoryArena;
enum KeyCodes
{
keycode_up,
keycode_down,
keycode_left,
keycode_right,
keycode_space,
keycode_mouseLeft,
keycode_count,
};
typedef struct KeyInput
{
v2 mouse;
union
{
b32 keys[keycode_count - 1];
struct
{
b32 up;
b32 down;
b32 left;
b32 right;
b32 space;
b32 leftShift;
b32 mouseLeft;
};
};
} KeyInput;
typedef struct PlatformFileRead
{
void *buffer;

View File

@ -7,11 +7,18 @@
#include "Dengine/Entity.h"
#include "Dengine/Math.h"
#include "Dengine/MemoryArena.h"
#include "Dengine/Platform.h"
#include "Dengine/Renderer.h"
#define NUM_KEYS 1024
#define METERS_TO_PIXEL 240
typedef struct UiState
{
i32 hotItem;
i32 activeItem;
} UiState;
typedef struct World
{
Entity *entities;
@ -34,7 +41,8 @@ typedef struct World
typedef struct GameState
{
enum State state;
b32 keys[NUM_KEYS];
KeyInput input;
v2 mouse;
Renderer renderer;
@ -45,6 +53,7 @@ typedef struct GameState
AssetManager assetManager;
AudioManager audioManager;
MemoryArena arena;
UiState uiState;
} GameState;
void worldTraveller_gameInit(GameState *state, v2 windowSize);