summaryrefslogtreecommitdiff
path: root/src/netlem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/netlem.c')
-rw-r--r--src/netlem.c39
1 files changed, 30 insertions, 9 deletions
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);
}