medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 9877eada0008be6159d7407a9ee4457493cfef62
parent afdcaa0d9e5588197eefff90afe3343b0988c6b8
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Aug 10 22:22:12 +0200

Move position/angle etc into persistent game state. Also pass GLFW window to game_frame for now

Diffstat:
game.h | 30+++++++++++++++++++++++++++++-
hm.cc | 61++++++++++++++++++++++++++-----------------------------------
main.cc | 27++++++++++++++++++---------
terrain_manager.cc | 6+++++-
terrain_manager.h | 4+++-
5 files changed, 81 insertions(+), 47 deletions(-)
diff --git a/game.h b/game.h @@ -1,13 +1,41 @@ #ifndef _GAME_H_ #define _GAME_H_ +#include <GLFW/glfw3.h> +#include <glm/glm.hpp> + +#include "int.h" +#include "terrain_manager.h" + +struct GameState { + glm::vec3 pos; + glm::vec3 angles; + TerrainManager tm; +}; + struct GameMemory { + size_t persistent_size; + size_t persistent_used; + u8 * persistent; }; +u8 * reserve_persistent( GameMemory & mem, const size_t size ) { + assert( mem.persistent_used + size <= mem.persistent_size ); + assert( mem.persistent_used + size > mem.persistent_used ); + + u8 * result = mem.persistent + mem.persistent_used; + mem.persistent_used += size; + + return result; +} + struct GameInput { }; -#define GAME_FRAME( name ) void name( GameMemory & mem, const GameInput & input, const float dt ) +#define GAME_INIT( name ) void name( GameState * state ) +typedef GAME_INIT( GameInit ); + +#define GAME_FRAME( name ) void name( GameMemory & mem, GLFWwindow * window, const float dt ) typedef GAME_FRAME( GameFrame ); #endif // _GAME_H_ diff --git a/hm.cc b/hm.cc @@ -43,63 +43,54 @@ void print_vec3( const std::string & name, const glm::vec3 & v ) { printf( "%s: %.3f %.3f %.3f\n", name.c_str(), v.x, v.y, v.z ); } -bool init = false; -glm::vec3 pos( 15000, 3000, 50 ); -glm::vec3 angles( glm::radians( glm::vec3( -90, 45, 0 ) ) ); glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480.0f, 0.1f, 10000.0f ) ); -TerrainManager tm( "Srtm_ramp2.world.21600x10800.jpg.parts" ); -extern "C" GAME_FRAME( game_frame ) { - if( !init ) { - glClearColor( 0, 0.5, 0.7, 1 ); - tm.teleport( pos ); - init = true; - } +extern "C" GAME_INIT( game_init ) { + state->pos = glm::vec3( 15000, 3000, 50 ); + state->angles = glm::vec3( glm::radians( glm::vec3( -90, 45, 0 ) ) ); + state->tm.use( "Srtm_ramp2.world.21600x10800.jpg.parts" ); - // if( glfwGetKey( window, 'Q' ) ) { - // break; - // } + glClearColor( 0, 0.5, 0.7, 1 ); + state->tm.teleport( state->pos ); +} - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); +extern "C" GAME_FRAME( game_frame ) { + GameState * state = ( GameState * ) mem.persistent; - // 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 ); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - // 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 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 = 0; - const int lr = 0; - const int dz = 0; - const int pitch = 0; - const int yaw = 0; + 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 ); - angles.x += pitch * dt * 2; - angles.y += yaw * dt * 2; + state->angles.x += pitch * dt * 2; + state->angles.y += yaw * dt * 2; - pos += angles_to_vector_xy( angles ) * 100.0f * dt * ( float ) fb; - const glm::vec3 sideways = glm::vec3( -cosf( angles.y ), sinf( angles.y ), 0 ); - pos += sideways * 100.0f * dt * ( float ) lr; + state->pos += angles_to_vector_xy( state->angles ) * 100.0f * dt * ( float ) fb; + const glm::vec3 sideways = glm::vec3( -cosf( state->angles.y ), sinf( state->angles.y ), 0 ); + state->pos += sideways * 100.0f * dt * ( float ) lr; // pos.z = hm.height( pos.x, pos.y ) + 8; - pos.z += dz * 50.0f * dt; + state->pos.z += dz * 50.0f * dt; - tm.update( pos ); + state->tm.update( state->pos ); const glm::mat4 VP = glm::translate( glm::rotate( glm::rotate( P, - angles.x, + state->angles.x, glm::vec3( 1, 0, 0 ) ), - angles.y, + state->angles.y, glm::vec3( 0, 0, 1 ) ), - -pos + -state->pos ); - tm.render( VP ); + state->tm.render( VP ); // glLoadIdentity(); // diff --git a/main.cc b/main.cc @@ -14,6 +14,7 @@ struct Game { void * lib; + GameInit * init; GameFrame * frame; time_t lib_write_time; @@ -33,10 +34,14 @@ Game load_game( const char * const path ) { game.lib = dlopen( path, RTLD_NOW ); if( game.lib ) { + game.init = ( GameInit * ) dlsym( game.lib, "game_init" ); game.frame = ( GameFrame * ) dlsym( game.lib, "game_frame" ); - } - printf( "loaded game: %p %p\n", game.lib, game.frame ); + if( !game.init || !game.frame ) { + game.init = nullptr; + game.frame = nullptr; + } + } game.lib_write_time = file_last_write_time( path ); @@ -56,14 +61,20 @@ bool should_reload_game( const char * const path, const time_t lib_write_time ) } int main( int argc, char ** argv ) { - GLFWwindow * const window = GL::init(); - Game game = load_game( GAME_LIBRARY_PATH ); GameMemory mem = { }; + mem.persistent_size = 64LL * 1024 * 1024; + mem.persistent = new u8[ mem.persistent_size ]; + + GameState * state = ( GameState * ) reserve_persistent( mem, sizeof( GameState ) ); + + GLFWwindow * const window = GL::init(); bool running = true; float last_frame_time = glfwGetTime(); + game.init( state ); + while( !glfwWindowShouldClose( window ) ) { const float current_frame_time = glfwGetTime(); const float dt = current_frame_time - last_frame_time; @@ -75,12 +86,10 @@ int main( int argc, char ** argv ) { } } - // TODO: keyboard events - - GameInput input = { }; - + // TODO: it would be nice to do input handling out here instead of + // just passing in window. if( game.frame ) { - game.frame( mem, input, dt ); + game.frame( mem, window, dt ); } glfwSwapBuffers( window ); diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -20,7 +20,11 @@ int iabs( const int x ) { return x < 0 ? -x : x; } -TerrainManager::TerrainManager( const std::string & dir ) : dir( dir ) { +TerrainManager::TerrainManager( const std::string & dir ) { + use( dir ); +} + +void TerrainManager::use( const std::string & dir ) { FILE * dims = fopen( ( dir + "/dims.txt" ).c_str(), "r" ); fscanf( dims, "%d %d", &w, &h ); fclose( dims ); diff --git a/terrain_manager.h b/terrain_manager.h @@ -5,6 +5,7 @@ #include <glm/glm.hpp> #include "int.h" +#include "heightmap.h" class TerrainManager { private: @@ -12,7 +13,7 @@ private: const static int REGION_SIZE = 11; const static int REGION_HALF = REGION_SIZE / 2; - const std::string dir; + std::string dir; int w, h; @@ -26,6 +27,7 @@ private: public: TerrainManager( const std::string & dir ); + void use( const std::string & dir ); std::string tp( const int tx, const int ty ) const; void update( const glm::vec3 & position );