commit f548544e6341d0413a29f057ad88c4981149f7e7 parent 65516795a0721f6aa255ce4afdbb061a43c79ae7 Author: Michael Savage <mikejsavage@gmail.com> Date: Fri Aug 21 19:18:25 +0200 Define a GameInput struct so game code doesn't need to know about GLFW Diffstat:
Makefile | | | 2 | +- |
bsp.cc | | | 11 | +++++------ |
game.h | | | 5 | +++-- |
hm.cc | | | 10 | +++++----- |
keys.h | | | 106 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
main.cc | | | 19 | ++++++++++++++++--- |
diff --git a/Makefile b/Makefile @@ -5,7 +5,7 @@ BSPOBJS = bsp.o bsp_renderer.o gl.o memory_arena.o HMOBJS = hm.o heightmap.o terrain_manager.o work_queue.o stb_image.o stb_perlin.o immediate.o PPOBJS = pp.o stb_image.o stb_image_write.o -WARNINGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-write-strings +WARNINGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-write-strings -Wno-char-subscripts CXXFLAGS += -std=c++11 -O2 -pthread -ggdb3 $(WARNINGS) -DGL_GLEXT_PROTOTYPES -DGLFW_INCLUDE_NONE # OS detection diff --git a/bsp.cc b/bsp.cc @@ -3,7 +3,6 @@ #include <math.h> #include "platform_opengl.h" -#include <GLFW/glfw3.h> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> @@ -278,12 +277,12 @@ extern "C" GAME_FRAME( game_frame ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - const int fb = glfwGetKey( window, 'W' ) - glfwGetKey( window, 'S' ); - const int lr = glfwGetKey( window, 'A' ) - glfwGetKey( window, 'D' ); - const int dz = glfwGetKey( window, GLFW_KEY_SPACE ) - glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ); + const int fb = input->keys[ 'w' ] - input->keys[ 's' ]; + const int lr = input->keys[ 'a' ] - input->keys[ 'd' ]; + const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; - const int pitch = glfwGetKey( window, GLFW_KEY_UP ) - glfwGetKey( window, GLFW_KEY_DOWN ); - const int yaw = glfwGetKey( window, GLFW_KEY_RIGHT ) - glfwGetKey( window, GLFW_KEY_LEFT ); + const int pitch = input->keys[ KEY_UPARROW ] - input->keys[ KEY_DOWNARROW ]; + const int yaw = input->keys[ KEY_RIGHTARROW ] - input->keys[ KEY_LEFTARROW ]; state->angles.x += pitch * dt * 2; state->angles.y += yaw * dt * 2; diff --git a/game.h b/game.h @@ -1,7 +1,6 @@ #ifndef _GAME_H_ #define _GAME_H_ -#include <GLFW/glfw3.h> #include <glm/glm.hpp> #include "intrinsics.h" @@ -10,6 +9,7 @@ #include "bsp_renderer.h" #include "immediate.h" #include "work_queue.h" +#include "keys.h" struct GameState { glm::vec3 pos; @@ -46,12 +46,13 @@ inline u8 * reserve_persistent( GameMemory & mem, const size_t size ) { } struct GameInput { + bool keys[ KEY_COUNT ]; }; #define GAME_INIT( name ) void name( GameState * state, GameMemory & mem ) typedef GAME_INIT( GameInit ); -#define GAME_FRAME( name ) void name( GameMemory & mem, GLFWwindow * window, const float dt ) +#define GAME_FRAME( name ) void name( GameMemory & mem, const GameInput * const input, const float dt ) typedef GAME_FRAME( GameFrame ); #endif // _GAME_H_ diff --git a/hm.cc b/hm.cc @@ -124,12 +124,12 @@ extern "C" GAME_FRAME( game_frame ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - const int fb = glfwGetKey( window, 'W' ) - glfwGetKey( window, 'S' ); - const int lr = glfwGetKey( window, 'A' ) - glfwGetKey( window, 'D' ); - const int dz = glfwGetKey( window, GLFW_KEY_SPACE ) - glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ); + const int fb = input->keys[ 'w' ] - input->keys[ 's' ]; + const int lr = input->keys[ 'a' ] - input->keys[ 'd' ]; + const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; - const int pitch = glfwGetKey( window, GLFW_KEY_UP ) - glfwGetKey( window, GLFW_KEY_DOWN ); - const int yaw = glfwGetKey( window, GLFW_KEY_RIGHT ) - glfwGetKey( window, GLFW_KEY_LEFT ); + const int pitch = input->keys[ KEY_UPARROW ] - input->keys[ KEY_DOWNARROW ]; + const int yaw = input->keys[ KEY_RIGHTARROW ] - input->keys[ KEY_LEFTARROW ]; state->angles.x += pitch * dt * 2; state->angles.y += yaw * dt * 2; diff --git a/keys.h b/keys.h @@ -0,0 +1,106 @@ +#ifndef _KEYS_H_ +#define _KEYS_H_ + +#include "intrinsics.h" + +// TODO: decide whether enum class is too annoying +// TODO: decide this layout properly +enum KeyNames { + KEY_NONE, + + // KEY_A, + // KEY_B, + // KEY_C, + // KEY_D, + // KEY_E, + // KEY_F, + // KEY_G, + // KEY_H, + // KEY_I, + // KEY_J, + // KEY_K, + // KEY_L, + // KEY_M, + // KEY_N, + // KEY_O, + // KEY_P, + // KEY_Q, + // KEY_R, + // KEY_S, + // KEY_T, + // KEY_U, + // KEY_V, + // KEY_W, + // KEY_X, + // KEY_Y, + // KEY_Z, + // + // KEY_1, + // KEY_2, + // KEY_3, + // KEY_4, + // KEY_5, + // KEY_6, + // KEY_7, + // KEY_8, + // KEY_9, + // KEY_0, + + KEY_KP1 = 127, + KEY_KP2, + KEY_KP3, + KEY_KP4, + KEY_KP5, + KEY_KP6, + KEY_KP7, + KEY_KP8, + KEY_KP9, + KEY_KP0, + + KEY_KP_PLUS, + KEY_KP_MINUS, + KEY_KP_STAR, + KEY_KP_SLASH, + KEY_KP_DOT, + KEY_KP_ENTER, + + KEY_UPARROW, + KEY_DOWNARROW, + KEY_LEFTARROW, + KEY_RIGHTARROW, + + KEY_F1, + KEY_F2, + KEY_F3, + KEY_F4, + KEY_F5, + KEY_F6, + KEY_F7, + KEY_F8, + KEY_F9, + KEY_F10, + KEY_F11, + KEY_F12, + + KEY_LEFTCTRL, + KEY_RIGHTCTRL, + KEY_LEFTSHIFT, + KEY_RIGHTSHIFT, + KEY_LEFTALT, + KEY_RIGHTALT, + + KEY_SPACE, + KEY_BACKSPACE, + KEY_RETURN, + + KEY_PAGEUP, + KEY_PAGEDOWN, + KEY_INSERT, + KEY_DELETE, + KEY_HOME, + KEY_END, + + KEY_COUNT, +}; + +#endif // _KEYS_H_ diff --git a/main.cc b/main.cc @@ -79,6 +79,8 @@ int main( int argc, char ** argv ) { GameState * state = ( GameState * ) reserve_persistent( mem, sizeof( GameState ) ); + GameInput input = { }; + GLFWwindow * const window = GL::init(); game.init( state, mem ); @@ -102,10 +104,21 @@ int main( int argc, char ** argv ) { } } - // TODO: it would be nice to do input handling out here instead of - // just passing in window. + // TODO: do this properly + input = { }; + input.keys[ 'w' ] = glfwGetKey( window, GLFW_KEY_W ); + input.keys[ 'a' ] = glfwGetKey( window, GLFW_KEY_A ); + input.keys[ 's' ] = glfwGetKey( window, GLFW_KEY_S ); + input.keys[ 'd' ] = glfwGetKey( window, GLFW_KEY_D ); + input.keys[ KEY_SPACE ] = glfwGetKey( window, GLFW_KEY_SPACE ); + input.keys[ KEY_LEFTSHIFT ] = glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ); + input.keys[ KEY_UPARROW ] = glfwGetKey( window, GLFW_KEY_UP ); + input.keys[ KEY_DOWNARROW ] = glfwGetKey( window, GLFW_KEY_DOWN ); + input.keys[ KEY_LEFTARROW ] = glfwGetKey( window, GLFW_KEY_LEFT ); + input.keys[ KEY_RIGHTARROW ] = glfwGetKey( window, GLFW_KEY_RIGHT ); + if( game.frame ) { - game.frame( mem, window, dt ); + game.frame( mem, &input, dt ); } glfwSwapBuffers( window );