diff --git a/lib/framework/frame.c b/lib/framework/frame.c index d130f57be..3038810b2 100644 --- a/lib/framework/frame.c +++ b/lib/framework/frame.c @@ -51,12 +51,8 @@ #include "cursors.h" #endif -#include "SDL_framerate.h" - /* Linux specific stuff */ -static FPSmanager wzFPSmanager; - static UWORD currentCursorResID = UWORD_MAX; SDL_Cursor *aCursors[MAX_CURSORS]; @@ -82,7 +78,7 @@ static Uint32 curTicks = 0; // Number of ticks since execution started static Uint32 lastTicks = 0; /* InitFrameStuff - needs to be called once before frame loop commences */ -static void InitFrameStuff( void ) +static void InitFrameStuff( void ) { UDWORD i; @@ -99,7 +95,7 @@ static void InitFrameStuff( void ) } /* MaintainFrameStuff - call this during completion of each frame loop */ -static void MaintainFrameStuff( void ) +static void MaintainFrameStuff( void ) { curTicks = SDL_GetTicks(); curFrames++; @@ -152,18 +148,6 @@ void frameSetCursorFromRes(SWORD resID) } -void setFramerateLimit(Uint32 fpsLimit) -{ - SDL_initFramerate( &wzFPSmanager ); - SDL_setFramerate( &wzFPSmanager, fpsLimit ); -} - -Uint32 getFramerateLimit(void) -{ - return SDL_getFramerate( &wzFPSmanager ); -} - - static void initCursors(void) { aCursors[CURSOR_ARROW - CURSOR_OFFSET] = init_system_cursor(cursor_arrow); @@ -255,38 +239,26 @@ BOOL frameInitialise( } -/* - * frameUpdate - * - * Call this each cycle to allow the framework to deal with - * windows messages, and do general house keeping. - * - * Returns FRAME_STATUS. +/*! + * Call this each cycle to do general house keeping. */ void frameUpdate(void) { /* Tell the input system about the start of another frame */ inputNewFrame(); - /* If things are running normally update the framerate */ - if (focusState == FOCUS_IN) - { - /* Update the frame rate stuff */ - MaintainFrameStuff(); - } - - SDL_framerateDelay(&wzFPSmanager); + /* Update the frame rate stuff */ + MaintainFrameStuff(); } - +/*! + * Cleanup framework + */ void frameShutDown(void) { screenShutDown(); - /* Free the default cursor */ - // DestroyCursor(hCursor); - /* Free all cursors */ freeCursors(); @@ -300,6 +272,7 @@ void frameShutDown(void) resShutDown(); } + /*************************************************************************** Load the file with name pointed to by pFileName into a memory buffer. If AllocateMem is true then the memory is allocated ... else it is diff --git a/lib/framework/frame.h b/lib/framework/frame.h index 974679aff..bb1266cb4 100644 --- a/lib/framework/frame.h +++ b/lib/framework/frame.h @@ -97,7 +97,7 @@ extern void frameUpdate(void); extern void frameSetCursorFromRes(SWORD resID); /* Returns the current frame we're on - used to establish whats on screen */ -extern UDWORD frameGetFrameNumber(void); +extern UDWORD frameGetFrameNumber(void); /** * Average framerate of the last seconds @@ -107,20 +107,6 @@ extern UDWORD frameGetFrameNumber(void); extern UDWORD frameGetAverageRate(void); -/** - * Set the framerate limit - * - * \param fpsLimit Desired framerate - */ -extern void setFramerateLimit(Uint32 fpsLimit); - -/** - * Get the framerate limit - * - * \return Desired framerate - */ -extern Uint32 getFramerateLimit(void); - /* Load the file with name pointed to by pFileName into a memory buffer. */ extern BOOL loadFile(const char *pFileName, // The filename char **ppFileData, // A buffer containing the file contents diff --git a/lib/framework/input.c b/lib/framework/input.c index 3cd990eb3..9a46b0fab 100644 --- a/lib/framework/input.c +++ b/lib/framework/input.c @@ -216,6 +216,9 @@ char inputGetCharKey(void) { } +/*! + * Handle keyboard events + */ void inputHandleKeyEvent(SDL_Event * event) { UDWORD code, vk; @@ -293,6 +296,9 @@ void inputHandleKeyEvent(SDL_Event * event) } +/*! + * Handle mousebutton events + */ void inputHandleMouseButtonEvent(SDL_Event * event) { switch (event->type) @@ -330,6 +336,9 @@ void inputHandleMouseButtonEvent(SDL_Event * event) } +/*! + * Handle mousemotion events + */ void inputHandleMouseMotionEvent(SDL_Event * event) { switch (event->type) @@ -357,10 +366,10 @@ void inputHandleMouseMotionEvent(SDL_Event * event) } -/* This is called once a frame so that the system can tell +/*! + * This is called once a frame so that the system can tell * whether a key was pressed this turn or held down from the last frame. */ -// NOTE This should probably react on events? void inputNewFrame(void) { unsigned int i; @@ -395,6 +404,9 @@ void inputNewFrame(void) } } +/*! + * Release all keys (and buttons) when we loose focus + */ // FIXME This seems to be totally ignored! (Try switching focus while the dragbox is open) void inputLooseFocus(void) { diff --git a/src/clparse.h b/src/clparse.h index 4d438d73a..09bb1aa0c 100644 --- a/src/clparse.h +++ b/src/clparse.h @@ -34,6 +34,22 @@ extern BOOL ParseCommandLineEarly(int argc, char** argv); extern BOOL bAllowDebugMode; +// FIXME The following does not belong here: + +/*! + * Set the framerate limit + * + * \param fpsLimit Desired framerate + */ +extern void setFramerateLimit(Uint32 fpsLimit); + +/*! + * Get the framerate limit + * + * \return Desired framerate + */ +extern Uint32 getFramerateLimit(void); + #endif diff --git a/src/configuration.c b/src/configuration.c index 3d68df338..db714ae83 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -63,6 +63,7 @@ extern UBYTE sPlayer[128]; extern void registry_clear(void); // from configfile.c + // //////////////////////////////////////////////////////////////////////////// BOOL loadConfig(void) { diff --git a/src/main.c b/src/main.c index 9a2208b7c..ae5fb7b39 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,8 @@ #include "lib/framework/configfile.h" #include "lib/framework/input.h" +#include "lib/framework/SDL_framerate.h" + #include "lib/gamelib/gtime.h" #include "lib/ivis_common/piestate.h" #include "lib/ivis_common/rendmode.h" @@ -86,8 +88,6 @@ char * multiplay_mods[MAX_MODS] = { NULL }; // Warzone 2100 . Pumpkin Studios -// Start game in title mode: -GS_GAMEMODE gameStatus = GS_TITLE_SCREEN, lastStatus = GS_TITLE_SCREEN; //flag to indicate when initialisation is complete BOOL videoInitialised = FALSE; BOOL gameInitialised = FALSE; @@ -101,12 +101,31 @@ char MultiPlayersPath[MAX_PATH]; char KeyMapPath[MAX_PATH]; char UserMusicPath[MAX_PATH]; +static FPSmanager wzFPSmanager; + +// Start game in title mode: +static GS_GAMEMODE gameStatus = GS_TITLE_SCREEN; +// Status of the gameloop static int gameLoopStatus = 0; extern FOCUS_STATE focusState; extern void debug_callback_stderr( void**, const char * ); extern void debug_callback_win32debug( void**, const char * ); + +void setFramerateLimit(Uint32 fpsLimit) +{ + SDL_initFramerate( &wzFPSmanager ); + SDL_setFramerate( &wzFPSmanager, fpsLimit ); +} + + +Uint32 getFramerateLimit(void) +{ + return SDL_getFramerate( &wzFPSmanager ); +} + + static BOOL inList( char * list[], const char * item ) { int i = 0; @@ -393,20 +412,31 @@ static void make_dir(char *dest, const char *dirname, const char *subdir) } +/*! + * Preparations before entering the title (mainmenu) loop + * Would start the timer in an event based mainloop + */ static void startTitleLoop(void) { + SetGameMode(GS_TITLE_SCREEN); + screen_RestartBackDrop(); if (!frontendInitialise("wrf/frontend.wrf")) { debug( LOG_ERROR, "Shutting down after failure" ); exit(EXIT_FAILURE); } - - frontendInitialised = TRUE; frontendInitVars(); + + // set a flag for the trigger/event system to indicate initialisation is complete + frontendInitialised = TRUE; } +/*! + * Shutdown/cleanup after the title (mainmenu) loop + * Would stop the timer + */ static void stopTitleLoop(void) { if (!frontendShutdown()) @@ -418,8 +448,14 @@ static void stopTitleLoop(void) } +/*! + * Preparations before entering the game loop + * Would start the timer in an event based mainloop + */ static void startGameLoop(void) { + SetGameMode(GS_NORMAL); + if (!levLoadData(pLevelName, NULL, 0)) { debug( LOG_ERROR, "Shutting down after failure" ); @@ -440,12 +476,17 @@ static void startGameLoop(void) exit(EXIT_FAILURE); } - //set a flag for the trigger/event system to indicate initialisation is complete - gameInitialised = TRUE; screen_StopBackDrop(); + + // set a flag for the trigger/event system to indicate initialisation is complete + gameInitialised = TRUE; } +/*! + * Shutdown/cleanup after the game loop + * Would stop the timer + */ static void stopGameLoop(void) { if (gameLoopStatus != GAMECODE_NEWLEVEL) @@ -462,8 +503,14 @@ static void stopGameLoop(void) } +/*! + * Load a savegame and start into the game loop + * Game data should be initialised afterwards, so that startGameLoop is not necessary anymore. + */ static void initSaveGameLoad(void) { + SetGameMode(GS_SAVEGAMELOAD); + screen_RestartBackDrop(); // load up a save game if (!loadGameInit(saveGameName)) @@ -477,6 +524,9 @@ static void initSaveGameLoad(void) } +/*! + * Run the code inside the gameloop + */ static void runGameLoop(void) { gameLoopStatus = gameLoop(); @@ -488,19 +538,17 @@ static void runGameLoop(void) case GAMECODE_QUITGAME: debug(LOG_MAIN, "GAMECODE_QUITGAME"); stopGameLoop(); - SetGameMode(GS_TITLE_SCREEN); - startTitleLoop(); + startTitleLoop(); // Restart into titleloop break; case GAMECODE_LOADGAME: debug(LOG_MAIN, "GAMECODE_LOADGAME"); stopGameLoop(); - SetGameMode(GS_SAVEGAMELOAD); - startTitleLoop(); + initSaveGameLoad(); // Restart and load a savegame break; case GAMECODE_NEWLEVEL: debug(LOG_MAIN, "GAMECODE_NEWLEVEL"); stopGameLoop(); - startGameLoop(); + startGameLoop(); // Restart gameloop break; // Never trown: case GAMECODE_FASTEXIT: @@ -513,6 +561,9 @@ static void runGameLoop(void) } +/*! + * Run the code inside the titleloop + */ static void runTitleLoop(void) { switch (titleLoop()) @@ -532,14 +583,12 @@ static void runTitleLoop(void) case TITLECODE_SAVEGAMELOAD: debug(LOG_MAIN, "TITLECODE_SAVEGAMELOAD"); stopTitleLoop(); - SetGameMode(GS_SAVEGAMELOAD); - initSaveGameLoad(); + initSaveGameLoad(); // Restart into gameloop and load a savegame break; case TITLECODE_STARTGAME: debug(LOG_MAIN, "TITLECODE_STARTGAME"); stopTitleLoop(); - SetGameMode(GS_NORMAL); - startGameLoop(); + startGameLoop(); // Restart into gameloop break; case TITLECODE_SHOWINTRO: debug(LOG_MAIN, "TITLECODE_SHOWINTRO"); @@ -557,6 +606,9 @@ static void runTitleLoop(void) } +/*! + * Activation (focus change) eventhandler + */ static void handleActiveEvent(SDL_Event * event) { // Ignore focus loss through SDL_APPMOUSEFOCUS, since it mostly happens accidentialy @@ -592,6 +644,10 @@ static void handleActiveEvent(SDL_Event * event) } +/*! + * The mainloop. + * Fetches events, executes appropriate code + */ static void mainLoop(void) { SDL_Event event; @@ -603,6 +659,7 @@ static void mainLoop(void) { switch (event.type) { + // This is uneccessary if we don't have the focus (either we don't get any events or they can't be dealt with anyway) if (focusState == FOCUS_IN) { case SDL_KEYUP: @@ -629,16 +686,16 @@ static void mainLoop(void) if (focusState == FOCUS_IN) { - gameTimeUpdate(); + gameTimeUpdate(); // Update gametime. FIXME There is probably code duplicated with MaintainFrameStuff - if(loop_GetVideoStatus()) - videoLoop(); + if (loop_GetVideoStatus()) + videoLoop(); // Display the video if neccessary else switch (GetGameMode()) { - case GS_NORMAL: + case GS_NORMAL: // Run the gameloop code runGameLoop(); break; - case GS_TITLE_SCREEN: + case GS_TITLE_SCREEN: // Run the titleloop code runTitleLoop(); break; default: @@ -647,6 +704,8 @@ static void mainLoop(void) frameUpdate(); // General housekeeping } + + SDL_framerateDelay(&wzFPSmanager); } } @@ -728,8 +787,10 @@ int main(int argc, char *argv[]) return -1; } + // Save new (commandline) settings saveConfig(); + // Find out where to find the data scanDataDirs(); // find out if the lobby stuff has been disabled @@ -797,6 +858,7 @@ int main(int argc, char *argv[]) debug(LOG_MAIN, "Entering main loop"); + // Enter the mainloop mainLoop(); debug(LOG_MAIN, "Shutting down Warzone 2100"); @@ -805,12 +867,18 @@ int main(int argc, char *argv[]) } +/*! + * Get the mode the game is currently in + */ GS_GAMEMODE GetGameMode(void) { return gameStatus; } +/*! + * Set the current mode + */ void SetGameMode(GS_GAMEMODE status) { gameStatus = status;