- Comment,s comments, comments

- Move wzFPSmanager over to main.c, so the delay can be dealt with directly in the mainloop instead of the detour through frameUpdate
  - Adding the functions directly to main.c is probably not the wisest idea, but may be fixed later


git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1623 4a71c877-e1ca-e34f-864e-861f7616d084
master
Dennis Schridde 2007-05-12 23:41:29 +00:00
parent f5439cfa16
commit 10128153aa
6 changed files with 130 additions and 74 deletions

View File

@ -51,12 +51,8 @@
#include "cursors.h" #include "cursors.h"
#endif #endif
#include "SDL_framerate.h"
/* Linux specific stuff */ /* Linux specific stuff */
static FPSmanager wzFPSmanager;
static UWORD currentCursorResID = UWORD_MAX; static UWORD currentCursorResID = UWORD_MAX;
SDL_Cursor *aCursors[MAX_CURSORS]; SDL_Cursor *aCursors[MAX_CURSORS];
@ -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) static void initCursors(void)
{ {
aCursors[CURSOR_ARROW - CURSOR_OFFSET] = init_system_cursor(cursor_arrow); aCursors[CURSOR_ARROW - CURSOR_OFFSET] = init_system_cursor(cursor_arrow);
@ -255,38 +239,26 @@ BOOL frameInitialise(
} }
/* /*!
* frameUpdate * Call this each cycle to do general house keeping.
*
* Call this each cycle to allow the framework to deal with
* windows messages, and do general house keeping.
*
* Returns FRAME_STATUS.
*/ */
void frameUpdate(void) void frameUpdate(void)
{ {
/* Tell the input system about the start of another frame */ /* Tell the input system about the start of another frame */
inputNewFrame(); inputNewFrame();
/* If things are running normally update the framerate */
if (focusState == FOCUS_IN)
{
/* Update the frame rate stuff */ /* Update the frame rate stuff */
MaintainFrameStuff(); MaintainFrameStuff();
}
SDL_framerateDelay(&wzFPSmanager);
} }
/*!
* Cleanup framework
*/
void frameShutDown(void) void frameShutDown(void)
{ {
screenShutDown(); screenShutDown();
/* Free the default cursor */
// DestroyCursor(hCursor);
/* Free all cursors */ /* Free all cursors */
freeCursors(); freeCursors();
@ -300,6 +272,7 @@ void frameShutDown(void)
resShutDown(); resShutDown();
} }
/*************************************************************************** /***************************************************************************
Load the file with name pointed to by pFileName into a memory buffer. 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 If AllocateMem is true then the memory is allocated ... else it is

View File

@ -107,20 +107,6 @@ extern UDWORD frameGetFrameNumber(void);
extern UDWORD frameGetAverageRate(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. */ /* Load the file with name pointed to by pFileName into a memory buffer. */
extern BOOL loadFile(const char *pFileName, // The filename extern BOOL loadFile(const char *pFileName, // The filename
char **ppFileData, // A buffer containing the file contents char **ppFileData, // A buffer containing the file contents

View File

@ -216,6 +216,9 @@ char inputGetCharKey(void) {
} }
/*!
* Handle keyboard events
*/
void inputHandleKeyEvent(SDL_Event * event) void inputHandleKeyEvent(SDL_Event * event)
{ {
UDWORD code, vk; UDWORD code, vk;
@ -293,6 +296,9 @@ void inputHandleKeyEvent(SDL_Event * event)
} }
/*!
* Handle mousebutton events
*/
void inputHandleMouseButtonEvent(SDL_Event * event) void inputHandleMouseButtonEvent(SDL_Event * event)
{ {
switch (event->type) switch (event->type)
@ -330,6 +336,9 @@ void inputHandleMouseButtonEvent(SDL_Event * event)
} }
/*!
* Handle mousemotion events
*/
void inputHandleMouseMotionEvent(SDL_Event * event) void inputHandleMouseMotionEvent(SDL_Event * event)
{ {
switch (event->type) 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. * whether a key was pressed this turn or held down from the last frame.
*/ */
// NOTE This should probably react on events?
void inputNewFrame(void) void inputNewFrame(void)
{ {
unsigned int i; 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) // FIXME This seems to be totally ignored! (Try switching focus while the dragbox is open)
void inputLooseFocus(void) void inputLooseFocus(void)
{ {

View File

@ -34,6 +34,22 @@ extern BOOL ParseCommandLineEarly(int argc, char** argv);
extern BOOL bAllowDebugMode; 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 #endif

View File

@ -63,6 +63,7 @@ extern UBYTE sPlayer[128];
extern void registry_clear(void); // from configfile.c extern void registry_clear(void); // from configfile.c
// //////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////
BOOL loadConfig(void) BOOL loadConfig(void)
{ {

View File

@ -38,6 +38,8 @@
#include "lib/framework/configfile.h" #include "lib/framework/configfile.h"
#include "lib/framework/input.h" #include "lib/framework/input.h"
#include "lib/framework/SDL_framerate.h"
#include "lib/gamelib/gtime.h" #include "lib/gamelib/gtime.h"
#include "lib/ivis_common/piestate.h" #include "lib/ivis_common/piestate.h"
#include "lib/ivis_common/rendmode.h" #include "lib/ivis_common/rendmode.h"
@ -86,8 +88,6 @@ char * multiplay_mods[MAX_MODS] = { NULL };
// Warzone 2100 . Pumpkin Studios // 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 //flag to indicate when initialisation is complete
BOOL videoInitialised = FALSE; BOOL videoInitialised = FALSE;
BOOL gameInitialised = FALSE; BOOL gameInitialised = FALSE;
@ -101,12 +101,31 @@ char MultiPlayersPath[MAX_PATH];
char KeyMapPath[MAX_PATH]; char KeyMapPath[MAX_PATH];
char UserMusicPath[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; static int gameLoopStatus = 0;
extern FOCUS_STATE focusState; extern FOCUS_STATE focusState;
extern void debug_callback_stderr( void**, const char * ); extern void debug_callback_stderr( void**, const char * );
extern void debug_callback_win32debug( 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 ) static BOOL inList( char * list[], const char * item )
{ {
int i = 0; 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) static void startTitleLoop(void)
{ {
SetGameMode(GS_TITLE_SCREEN);
screen_RestartBackDrop(); screen_RestartBackDrop();
if (!frontendInitialise("wrf/frontend.wrf")) if (!frontendInitialise("wrf/frontend.wrf"))
{ {
debug( LOG_ERROR, "Shutting down after failure" ); debug( LOG_ERROR, "Shutting down after failure" );
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
frontendInitialised = TRUE;
frontendInitVars(); 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) static void stopTitleLoop(void)
{ {
if (!frontendShutdown()) 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) static void startGameLoop(void)
{ {
SetGameMode(GS_NORMAL);
if (!levLoadData(pLevelName, NULL, 0)) if (!levLoadData(pLevelName, NULL, 0))
{ {
debug( LOG_ERROR, "Shutting down after failure" ); debug( LOG_ERROR, "Shutting down after failure" );
@ -440,12 +476,17 @@ static void startGameLoop(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
//set a flag for the trigger/event system to indicate initialisation is complete
gameInitialised = TRUE;
screen_StopBackDrop(); 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) static void stopGameLoop(void)
{ {
if (gameLoopStatus != GAMECODE_NEWLEVEL) 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) static void initSaveGameLoad(void)
{ {
SetGameMode(GS_SAVEGAMELOAD);
screen_RestartBackDrop(); screen_RestartBackDrop();
// load up a save game // load up a save game
if (!loadGameInit(saveGameName)) if (!loadGameInit(saveGameName))
@ -477,6 +524,9 @@ static void initSaveGameLoad(void)
} }
/*!
* Run the code inside the gameloop
*/
static void runGameLoop(void) static void runGameLoop(void)
{ {
gameLoopStatus = gameLoop(); gameLoopStatus = gameLoop();
@ -488,19 +538,17 @@ static void runGameLoop(void)
case GAMECODE_QUITGAME: case GAMECODE_QUITGAME:
debug(LOG_MAIN, "GAMECODE_QUITGAME"); debug(LOG_MAIN, "GAMECODE_QUITGAME");
stopGameLoop(); stopGameLoop();
SetGameMode(GS_TITLE_SCREEN); startTitleLoop(); // Restart into titleloop
startTitleLoop();
break; break;
case GAMECODE_LOADGAME: case GAMECODE_LOADGAME:
debug(LOG_MAIN, "GAMECODE_LOADGAME"); debug(LOG_MAIN, "GAMECODE_LOADGAME");
stopGameLoop(); stopGameLoop();
SetGameMode(GS_SAVEGAMELOAD); initSaveGameLoad(); // Restart and load a savegame
startTitleLoop();
break; break;
case GAMECODE_NEWLEVEL: case GAMECODE_NEWLEVEL:
debug(LOG_MAIN, "GAMECODE_NEWLEVEL"); debug(LOG_MAIN, "GAMECODE_NEWLEVEL");
stopGameLoop(); stopGameLoop();
startGameLoop(); startGameLoop(); // Restart gameloop
break; break;
// Never trown: // Never trown:
case GAMECODE_FASTEXIT: case GAMECODE_FASTEXIT:
@ -513,6 +561,9 @@ static void runGameLoop(void)
} }
/*!
* Run the code inside the titleloop
*/
static void runTitleLoop(void) static void runTitleLoop(void)
{ {
switch (titleLoop()) switch (titleLoop())
@ -532,14 +583,12 @@ static void runTitleLoop(void)
case TITLECODE_SAVEGAMELOAD: case TITLECODE_SAVEGAMELOAD:
debug(LOG_MAIN, "TITLECODE_SAVEGAMELOAD"); debug(LOG_MAIN, "TITLECODE_SAVEGAMELOAD");
stopTitleLoop(); stopTitleLoop();
SetGameMode(GS_SAVEGAMELOAD); initSaveGameLoad(); // Restart into gameloop and load a savegame
initSaveGameLoad();
break; break;
case TITLECODE_STARTGAME: case TITLECODE_STARTGAME:
debug(LOG_MAIN, "TITLECODE_STARTGAME"); debug(LOG_MAIN, "TITLECODE_STARTGAME");
stopTitleLoop(); stopTitleLoop();
SetGameMode(GS_NORMAL); startGameLoop(); // Restart into gameloop
startGameLoop();
break; break;
case TITLECODE_SHOWINTRO: case TITLECODE_SHOWINTRO:
debug(LOG_MAIN, "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) static void handleActiveEvent(SDL_Event * event)
{ {
// Ignore focus loss through SDL_APPMOUSEFOCUS, since it mostly happens accidentialy // 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) static void mainLoop(void)
{ {
SDL_Event event; SDL_Event event;
@ -603,6 +659,7 @@ static void mainLoop(void)
{ {
switch (event.type) 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) if (focusState == FOCUS_IN)
{ {
case SDL_KEYUP: case SDL_KEYUP:
@ -629,16 +686,16 @@ static void mainLoop(void)
if (focusState == FOCUS_IN) if (focusState == FOCUS_IN)
{ {
gameTimeUpdate(); gameTimeUpdate(); // Update gametime. FIXME There is probably code duplicated with MaintainFrameStuff
if(loop_GetVideoStatus()) if (loop_GetVideoStatus())
videoLoop(); videoLoop(); // Display the video if neccessary
else switch (GetGameMode()) else switch (GetGameMode())
{ {
case GS_NORMAL: case GS_NORMAL: // Run the gameloop code
runGameLoop(); runGameLoop();
break; break;
case GS_TITLE_SCREEN: case GS_TITLE_SCREEN: // Run the titleloop code
runTitleLoop(); runTitleLoop();
break; break;
default: default:
@ -647,6 +704,8 @@ static void mainLoop(void)
frameUpdate(); // General housekeeping frameUpdate(); // General housekeeping
} }
SDL_framerateDelay(&wzFPSmanager);
} }
} }
@ -728,8 +787,10 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
// Save new (commandline) settings
saveConfig(); saveConfig();
// Find out where to find the data
scanDataDirs(); scanDataDirs();
// find out if the lobby stuff has been disabled // 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"); debug(LOG_MAIN, "Entering main loop");
// Enter the mainloop
mainLoop(); mainLoop();
debug(LOG_MAIN, "Shutting down Warzone 2100"); 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) GS_GAMEMODE GetGameMode(void)
{ {
return gameStatus; return gameStatus;
} }
/*!
* Set the current mode
*/
void SetGameMode(GS_GAMEMODE status) void SetGameMode(GS_GAMEMODE status)
{ {
gameStatus = status; gameStatus = status;