Skip to content

Commit

Permalink
Some clean up & refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiobenrocha2 committed Mar 22, 2015
1 parent f8f67f3 commit 7d32099
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
4 changes: 3 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2759,7 +2759,9 @@ int config_open( const char *filename ) {
return created;
}

void config_resolution_overwrite( int w, int h ){
void config_resolution_overwrite( int w, int h ) {
config.iface.screen_width = w;
config.iface.screen_height = h;

return;
}
4 changes: 2 additions & 2 deletions include/sdl_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

int sdl_init( void );
void sdl_free( void );
void sdl_frame_delay( void );
void sdl_clear( void );
void sdl_frame_delay( int frame_rate );
//void sdl_clear( void );
void sdl_swap( void );
int sdl_hat_dir_value( int direction );

Expand Down
15 changes: 8 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

static int supress_wait = 0;

void supress( void ) {
supress_wait = config_get()->iface.frame_rate / 5;
void supress( int frame_rate ) {
supress_wait = frame_rate / 5;
}

void clean_up( void ) {
Expand All @@ -47,6 +47,7 @@ void bail( void ) {
}

int main( int argc, char *arvg[] ) {
const struct config *config_m = config_get();
int quit = 0;
int config_status = 0;
int event;
Expand Down Expand Up @@ -132,7 +133,7 @@ int main( int argc, char *arvg[] ) {

event_flush();

if( !config_get()->iface.theme.menu.auto_hide )
if( !config_m->iface.theme.menu.auto_hide )
menu_show();

focus_set( FOCUS_GAMESEL );
Expand All @@ -141,29 +142,29 @@ int main( int argc, char *arvg[] ) {
ogl_clear();
bg_draw();
snap_draw();
if (!config_get()->iface.hide_buttons)
if (!config_m->iface.hide_buttons)
hint_draw();
menu_draw();
submenu_draw();
game_sel_draw();
sdl_swap();
if (Mix_PlayingMusic() != 1 && config_get()->iface.theme_sound && reader_running == 0)
if (Mix_PlayingMusic() != 1 && config_m->iface.theme_sound && reader_running == 0)
playmusic();
if (( event = event_poll() )) {
if( supress_wait == 0 ) {
if( event == EVENT_QUIT ) {
quit = 1;
}
else {
supress();
supress( config_m->iface.frame_rate );
event_process( event );
}
}
}
if( supress_wait > 0 )
supress_wait--;

sdl_frame_delay();
sdl_frame_delay( config_m->iface.frame_rate );
}
clean_up();
return 0;
Expand Down
68 changes: 41 additions & 27 deletions sdl_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,41 @@ static SDL_GLContext *glcontext = NULL;
static const int MAX_FRAME_RATE = 100;
static const char *title = "Cabrio";
static FPSmanager manager;
static int frame_rate = 0;

int sdl_init( void ) {
int mode = SDL_SWSURFACE|SDL_WINDOW_OPENGL;
const struct config *config = config_get();
void sdl_resolution_overwrite( void ) {
SDL_DisplayMode res;

SDL_GetDesktopDisplayMode(0, &res);
config_resolution_overwrite( res.w, res.h );

// debug //
// printf( "\nDisplay resolution: %dx%d\nRefresh rate: %d Hz\n%s\n\n", res.w, res.h, res.refresh_rate, SDL_GetPixelFormatName( res.format ) );

return;
}

void sdl_init_framerate ( int frame_rate ) {
SDL_initFramerate( &manager );

if( frame_rate < 0 ) {
frame_rate = 0;
fprintf( stderr, "Warning: Negative frame rate, setting to 0 (unlimited)\n" );
}
if( frame_rate > MAX_FRAME_RATE ) {
frame_rate = MAX_FRAME_RATE;
fprintf( stderr, "Warning: Frame rate above maximum allowed (%d) setting to maximum\n", MAX_FRAME_RATE );
}
if( frame_rate ) {
SDL_setFramerate( &manager, frame_rate );
}

return;
}

int sdl_init( void ) {
int mode = SDL_SWSURFACE|SDL_WINDOW_OPENGL;
const struct config *c = config_get();

if( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
fprintf(stderr, "Error: Unable to initialise SDL: %s\n", SDL_GetError());
return 1;
Expand All @@ -33,36 +61,22 @@ int sdl_init( void ) {
SDL_ShowCursor(SDL_DISABLE);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_GetDesktopDisplayMode(0, &res);
if( config->iface.full_screen ) {
if( c->iface.full_screen ) {
mode |= SDL_WINDOW_FULLSCREEN; // maybe use SDL_WINDOW_FULLSCREEN_DESKTOP ?
config_resolution_overwrite( res.w, res.h );
sdl_resolution_overwrite();
}
window = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, config->iface.screen_width, config->iface.screen_height, mode );
window = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, c->iface.screen_width, c->iface.screen_height, mode );
if( window == NULL ) {
fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
return 1;
}

glcontext = SDL_GL_CreateContext( window );

screen = SDL_GetWindowSurface( window );

SDL_initFramerate( &manager );

frame_rate = config->iface.frame_rate;
if( frame_rate < 0 ) {
frame_rate = 0;
fprintf( stderr, "Warning: Negative frame rate, setting to 0 (unlimited)\n" );
}
if( frame_rate > MAX_FRAME_RATE ) {
frame_rate = MAX_FRAME_RATE;
fprintf( stderr, "Warning: Frame rate above maximum allowed (%d) setting to maximum\n", MAX_FRAME_RATE );
}
if( frame_rate ) {
SDL_setFramerate( &manager, frame_rate );
}


sdl_init_framerate( c->iface.frame_rate );

return 0;
}

Expand All @@ -76,7 +90,7 @@ void sdl_free( void ) {
SDL_Quit();
}

void sdl_frame_delay( void ) {
void sdl_frame_delay( int frame_rate ) {
if( frame_rate ) {
SDL_framerateDelay( &manager );
}
Expand Down

0 comments on commit 7d32099

Please sign in to comment.