From f643ff055c5017833f17dc620c4f0a98b10940fa Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Tue, 7 Dec 2010 23:28:38 +0000 Subject: Ajout de stats pour le timing. Pour l'heure gros printf, mais peut etre intégr en bas de l'écran SDL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///var/svn/2010-netlemmings/trunk@187 077b3477-7977-48bd-8428-443f22f7bfda --- src/game.c | 2 +- src/include/timing.h | 2 +- src/netlem.c | 39 ++++++++++++++++++++++++++++++--------- src/timing.c | 8 ++++++-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/game.c b/src/game.c index 8e49012..e692da5 100644 --- a/src/game.c +++ b/src/game.c @@ -5,7 +5,7 @@ #include "utils.h" void play(tick_t tick, int *dirtRectsCount, SDL_Rect **directRects ) { - if (tick%100==0) { printf("tick==%i\n",tick); } + if (tick%100==0) { } //printf("tick==%i\n",tick); } //TODO : boucle de jeu principale ici (maj état de jeu) diff --git a/src/include/timing.h b/src/include/timing.h index 31aea11..927a30a 100644 --- a/src/include/timing.h +++ b/src/include/timing.h @@ -7,6 +7,6 @@ #define TICK_DURATION_MS 30 tick_t getGameCurrentTick(Uint32 startTime_ms); -void waitForNextTick(Uint32 timeBefore_ms, Uint32 drift_ms); +int waitForNextTick(Uint32 timeBefore_ms, Uint32 drift_ms); #endif /*TIME_H*/ diff --git a/src/netlem.c b/src/netlem.c index 5f3dfb2..00afcb9 100644 --- a/src/netlem.c +++ b/src/netlem.c @@ -23,6 +23,7 @@ struct _networkReadProc_args { client_t *client; tick_t *tick; + tick_t *lastServerTick; int *drift_ms; }; @@ -54,9 +55,10 @@ int updateGraphics(gameGraphics_t *gGraph); int main(int argc, char **argv) { int drift_ms=0, endMainLoop, result; - tick_t tick=0; - Uint32 timeBefore_ms; + tick_t tick=0, lastServerTick=0; + Uint32 timeBefore_ms[10], waited[10], t; Uint8 loadProgress=0; + double fps, waitPercent; client_t client; serverParams_t serverParams; @@ -80,7 +82,7 @@ int main(int argc, char **argv) { openLog(NULL); // Starting log message - sprintf(logMsg, "NetLemmings version %i.%i", NetLemmings_VERSION_MAJOR, NetLemmings_VERSION_MINOR); + snprintf(logMsg, 128, "NetLemmings version %i.%i", NetLemmings_VERSION_MAJOR, NetLemmings_VERSION_MINOR); logs(LOG_INFO, logMsg); // Server name resolution and connection @@ -119,6 +121,7 @@ int main(int argc, char **argv) { args.client=&client; args.tick=&tick; args.drift_ms=&drift_ms; + args.lastServerTick=&lastServerTick; SDL_Thread *networkReadThread = SDL_CreateThread(networkReadProc, &args); if(!networkReadThread) { logs2(LOG_ERROR,"main(), SDL_CreateThread()", SDL_GetError()); @@ -143,10 +146,15 @@ int main(int argc, char **argv) { return 6; } + memset(timeBefore_ms, 0, 10*sizeof(Uint32)); + t=0; + // Main game loop endMainLoop=0; while(!endMainLoop) { - timeBefore_ms = SDL_GetTicks(); + // Store loop begin date in a 10 entries rotate buffer + t=(t+1)%10; + timeBefore_ms[t] = SDL_GetTicks(); // Process local player keyboard and mouse events // (note: remote events are processed by network read thread) @@ -157,8 +165,21 @@ int main(int argc, char **argv) { // Display that new game state to the local user updateGraphics(&gGraph); + // Delay that we have to wait for the next frame (depends on execution time and network time drift) - waitForNextTick(timeBefore_ms, drift_ms); + waited[t]=waitForNextTick(timeBefore_ms[t], drift_ms); + + // Compute & display FPS mean value on 10 loops + if (t==0) { + fps=10000.0/(timeBefore_ms[t]-timeBefore_ms[(t+1)%10]); + waitPercent=0; + for(;t<10;t++) waitPercent+=waited[t]; + t=0; + waitPercent=(double)waitPercent*10/TICK_DURATION_MS; + + snprintf(logMsg, 128, "tick:%d\tlastServerTick:%d\tdrift_ms:%d\t\t%.1f FPS\t W:%.1f%%\n", tick, lastServerTick, drift_ms, fps, waitPercent); + logs(LOG_DEBUG, logMsg); + } } // Close communcation with the server, stop threads, close logs and go out @@ -277,7 +298,7 @@ int act(tick_t *tick, int loadProgress, TCPsocket sockClient, int *dirtRectsCoun int networkReadProc(void *a) { int result; event_t e; - char logMsg[128]; + //char logMsg[128]; struct _networkReadProc_args *args = (struct _networkReadProc_args *)a; @@ -292,8 +313,9 @@ int networkReadProc(void *a) { // logs(LOG_DEBUG, "Got event"); *(args->drift_ms)=( *(args->tick) - e.serverTick ); - sprintf(logMsg, "serverTick==%i, tick==%i, drift_ms==%i\n", e.serverTick, *(args->tick), *(args->drift_ms)); - logs(LOG_DEBUG, logMsg); + *(args->lastServerTick)=e.serverTick; +// sprintf(logMsg, "serverTick==%i, tick==%i, drift_ms==%i\n", e.serverTick, *(args->tick), *(args->drift_ms)); +// logs(LOG_DEBUG, logMsg); result=SDL_SemPost(semGameStart); @@ -504,7 +526,6 @@ int updateGraphics(gameGraphics_t *gGraph) { if ( lastViewport.x != gGraph->viewport.x ) { //lastViewport=gGraph->viewport; lastViewport.x=gGraph->viewport.x; - printf("viewport.x==%i\n", gGraph->viewport.x); if ( gGraph->dirtRectsCount > 0 ) { free(gGraph->dirtRects); } diff --git a/src/timing.c b/src/timing.c index 01b0aee..896b973 100644 --- a/src/timing.c +++ b/src/timing.c @@ -8,8 +8,12 @@ inline tick_t getGameCurrentTick(Uint32 startTime_ms) { return t/TICK_DURATION_MS; } -inline void waitForNextTick(Uint32 timeBefore_ms, Uint32 drift_ms) { +inline int waitForNextTick(Uint32 timeBefore_ms, Uint32 drift_ms) { int delay_ms; delay_ms=TICK_DURATION_MS-(SDL_GetTicks()-timeBefore_ms)+drift_ms; - if (delay_ms>0) SDL_Delay(delay_ms); //TODO Si le client rame trop, faut décrocher la partie + if (delay_ms>0) { + SDL_Delay(delay_ms); //TODO Si le client rame trop, faut décrocher la partie + return delay_ms; + } + return 0; } -- cgit v1.2.3