@ -5,7 +5,7 @@ Game::Game()
/* Set the appropriate priority level for the default log category so either info level messages
* and higher are enabled or debug level messages are enabled , depending on the global configuration */
SDL_LogPriority default_log_category_priority ;
if ( get_ configuration( ) [ " log " ] [ " debug-to-file " ] | | get_ configuration( ) [ " log " ] [ " debug-to-stdout " ] )
if ( configuration( ) [ " log " ] [ " debug-to-file " ] | | configuration( ) [ " log " ] [ " debug-to-stdout " ] )
{
default_log_category_priority = SDL_LOG_PRIORITY_DEBUG ;
}
@ -18,13 +18,13 @@ Game::Game()
SDL_LogSetOutputFunction ( & Game : : sdl_log_override , this ) ;
/* pretty print config to debug log */
std : : ostringstream log_message ;
log_message < < std : : setw ( 4 ) < < get_ configuration( ) < < std : : endl ;
log_message < < std : : setw ( 4 ) < < configuration( ) < < std : : endl ;
sb : : Log : : log ( log_message , sb : : Log : : DEBUG ) ;
/* tell SDL which render driver you will be requesting when calling SDL_CreateRenderer */
SDL_SetHint ( SDL_HINT_RENDER_DRIVER , get_ configuration( ) [ " display " ] [ " render driver " ] . get < std : : string > ( ) . c_str ( ) ) ;
SDL_SetHint ( SDL_HINT_RENDER_DRIVER , configuration( ) [ " display " ] [ " render driver " ] . get < std : : string > ( ) . c_str ( ) ) ;
/* initialize the buffer of frame lengths which will be used to calculate FPS */
frame_length_history . reserve ( 5000 ) ;
set_framerate ( get_ configuration( ) [ " display " ] [ " framerate " ] ) ;
set_framerate ( configuration( ) [ " display " ] [ " framerate " ] ) ;
delegate . subscribe ( & Game : : handle_quit_event , this , SDL_QUIT ) ;
/* Needed for displaying fullscreen correctly on Linux (?) Also might need SDL_VIDEO_CENTERED (?) */
std : : string fullscreen_env_assigment = " SDL_VIDEO_X11_LEGACY_FULLSCREEN=0 " ;
@ -49,20 +49,20 @@ Game::Game()
log_message = std : : ostringstream ( ) ;
log_message < < " GLEW " < < glewGetString ( GLEW_VERSION ) ;
sb : : Log : : log ( log_message . str ( ) ) ;
glm : : ivec2 window_size = get_ configuration( ) [ " display " ] [ " dimensions " ] . get < glm : : ivec2 > ( ) ;
glm : : ivec2 window_size = configuration( ) [ " display " ] [ " dimensions " ] . get < glm : : ivec2 > ( ) ;
/* Create a window with dimensions set in the config, centered, and flagged to be usable in OpenGL context */
window = SDL_CreateWindow (
get_ configuration( ) [ " display " ] [ " title " ] . get_ref < const std : : string & > ( ) . c_str ( ) , SDL_WINDOWPOS_CENTERED ,
_ window = SDL_CreateWindow (
configuration( ) [ " display " ] [ " title " ] . get_ref < const std : : string & > ( ) . c_str ( ) , SDL_WINDOWPOS_CENTERED ,
SDL_WINDOWPOS_CENTERED , window_size . x , window_size . y , SDL_WINDOW_OPENGL ) ;
if ( window = = nullptr )
if ( _ window = = nullptr )
{
sb : : Log : : sdl_error ( " Could not create window " ) ;
flag_to_end ( ) ;
}
/* Create an SDL renderer for clearing the screen to black and for logging renderer properties. Destroy renderer
* when finished .
*/
if ( ( renderer = SDL_CreateRenderer ( window, - 1 , SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED ) ) = = nullptr )
/* Create an SDL renderer for clearing the screen to black and for logging renderer properties. Destroy renderer when finished.
* Skip this in emscripten because it causes a mouse event bug . */
# ifndef __EMSCRIPTEN__
if ( ( renderer = SDL_CreateRenderer ( _ window, - 1 , SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ) ) = = nullptr )
{
sb : : Log : : sdl_error ( " Could not create renderer " ) ;
flag_to_end ( ) ;
@ -83,7 +83,8 @@ Game::Game()
SDL_RenderFlush ( renderer ) ;
SDL_DestroyRenderer ( renderer ) ;
}
SDL_ShowCursor ( get_configuration ( ) [ " display " ] [ " show-cursor " ] ) ;
# endif
SDL_ShowCursor ( configuration ( ) [ " display " ] [ " show-cursor " ] ) ;
if ( TTF_Init ( ) < 0 )
{
sb : : Log : : sdl_error ( " Could not initialize SDL ttf " ) ;
@ -101,7 +102,7 @@ Game::Game()
if ( Mix_Init ( MIX_INIT_OGG ) = = 0 )
{
sb : : Log : : sdl_error ( " Could not initialize SDL mixer " ) ;
flag_to_end ( ) ;
// flag_to_end();
}
else
{
@ -147,7 +148,7 @@ void Game::load_sdl_context()
SDL_GetRenderDriverInfo ( ii , & renderer_info ) ;
log_renderer_info ( renderer_info ) ;
}
if ( ( renderer = SDL_CreateRenderer ( window, - 1 , SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED ) ) = = nullptr )
if ( ( renderer = SDL_CreateRenderer ( _ window, - 1 , SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ) ) = = nullptr )
{
sb : : Log : : sdl_error ( " Could not create renderer " ) ;
flag_to_end ( ) ;
@ -170,14 +171,16 @@ void Game::load_gl_context()
SDL_DestroyRenderer ( renderer ) ;
renderer = nullptr ;
}
# ifndef __EMSCRIPTEN__
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MAJOR_VERSION , 3 ) ;
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MINOR_VERSION , 2 ) ;
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_PROFILE_MASK , SDL_GL_CONTEXT_PROFILE_CORE ) ;
# endif
SDL_GL_SetAttribute ( SDL_GL_DEPTH_SIZE , 16 ) ;
SDL_GL_SetAttribute ( SDL_GL_RED_SIZE , 8 ) ;
SDL_GL_SetAttribute ( SDL_GL_GREEN_SIZE , 8 ) ;
SDL_GL_SetAttribute ( SDL_GL_BLUE_SIZE , 8 ) ;
if ( ( glcontext = SDL_GL_CreateContext ( window) ) = = nullptr )
if ( ( glcontext = SDL_GL_CreateContext ( _ window) ) = = nullptr )
{
sb : : Log : : sdl_error ( " Could not get GL context " ) ;
flag_to_end ( ) ;
@ -213,14 +216,14 @@ void Game::sdl_log_override(void* userdata, int category, SDL_LogPriority priori
Game * game = static_cast < Game * > ( userdata ) ;
std : : ostream & out = ( priority > SDL_LOG_PRIORITY_WARN ) ? std : : cerr : std : : cout ;
/* print to stdout/stderr if priority is higher than debug or debug statements are enabled */
if ( priority > SDL_LOG_PRIORITY_DEBUG | | game - > get_ configuration( ) [ " log " ] [ " debug-to-stdout " ] )
if ( priority > SDL_LOG_PRIORITY_DEBUG | | game - > configuration( ) [ " log " ] [ " debug-to-stdout " ] )
{
out < < message < < std : : endl ;
}
/* handle writing to log file */
if ( game - > get_ configuration( ) [ " log " ] [ " enabled " ] )
if ( game - > configuration( ) [ " log " ] [ " enabled " ] )
{
fs : : path path = game - > get_ configuration( ) [ " log " ] [ " output-directory " ] ;
fs : : path path = game - > configuration( ) [ " log " ] [ " output-directory " ] ;
if ( ! fs : : exists ( path ) )
{
fs : : create_directories ( path ) ;
@ -230,16 +233,16 @@ void Game::sdl_log_override(void* userdata, int category, SDL_LogPriority priori
std : : stringstream stamped_message ;
stamped_message < < std : : put_time ( std : : localtime ( & now ) , " %F %T " ) < < message ;
/* if debug is enabled, append message to debug log file */
if ( game - > get_ configuration( ) [ " log " ] [ " debug-to-file " ] )
if ( game - > configuration( ) [ " log " ] [ " debug-to-file " ] )
{
fs : : path debug_path = path / game - > get_ configuration( ) [ " log " ] [ " debug-file-name " ] ;
fs : : path debug_path = path / game - > configuration( ) [ " log " ] [ " debug-file-name " ] ;
std : : ofstream debug_stream ( debug_path , std : : ios_base : : app ) ;
debug_stream < < stamped_message . str ( ) < < std : : endl ;
}
/* only append messages to the info log that are higher than debug priority */
if ( priority > SDL_LOG_PRIORITY_DEBUG )
{
fs : : path info_path = path / game - > get_ configuration( ) [ " log " ] [ " info-file-name " ] ;
fs : : path info_path = path / game - > configuration( ) [ " log " ] [ " info-file-name " ] ;
std : : ofstream info_stream ( info_path , std : : ios_base : : app ) ;
info_stream < < stamped_message . str ( ) < < std : : endl ;
}
@ -483,14 +486,24 @@ std::string Game::get_pixel_format_string(Uint32 format)
return pixel_format ;
}
const SDL_Window * Game : : get_window ( ) const
const nlohmann : : json & Game : : configuration ( ) const
{
return _configuration . config ;
}
nlohmann : : json & Game : : configuration ( )
{
return window ;
return _configuration. config ;
}
SDL_Window * Game : : get_window ( )
const SDL_Window * Game : : window( ) const
{
return window ;
return _window ;
}
SDL_Window * Game : : window ( )
{
return _window ;
}
const SDL_Renderer * Game : : get_renderer ( ) const
@ -525,6 +538,7 @@ glm::vec2 Game::weight(glm::vec2 motion)
return { weight ( motion . x ) , weight ( motion . y ) } ;
}
void Game : : run ( )
{
SDL_FlushEvents ( SDL_FIRSTEVENT , SDL_LASTEVENT ) ;
@ -562,7 +576,7 @@ void Game::frame(float ticks)
input . unsuppress_animation . update ( ) ;
update ( ) ;
framerate_indicator . update ( ) ;
configuration. update ( ) ;
_ configuration. update ( ) ;
if ( ! is_gl_context )
{
SDL_SetRenderTarget ( renderer , nullptr ) ;
@ -639,9 +653,9 @@ void Game::quit()
{
SDL_DestroyRenderer ( renderer ) ;
}
if ( window ! = nullptr )
if ( _ window ! = nullptr )
{
SDL_DestroyWindow ( window) ;
SDL_DestroyWindow ( _ window) ;
}
if ( TTF_WasInit ( ) )
{