summaryrefslogtreecommitdiff
path: root/src/netlem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/netlem.c')
-rw-r--r--src/netlem.c197
1 files changed, 197 insertions, 0 deletions
diff --git a/src/netlem.c b/src/netlem.c
new file mode 100644
index 0000000..e9a4304
--- /dev/null
+++ b/src/netlem.c
@@ -0,0 +1,197 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "SDL/SDL.h"
+#include "SDL/SDL_net.h"
+#include "SDL/SDL_thread.h"
+
+#include "netlem.h"
+#include "game.h"
+#include "events.h"
+#include "utils.h"
+
+int tick=0;
+SDL_sem *semGameStart;
+
+struct _processNetworkEvents_args { int *end; int *drift_ms; client_t *client; };
+
+void processLocalEvents();
+int processNetworkEvents(void *a);
+int updateGraphics();
+
+int main(int argc, char **argv) {
+ const event_t evReady = {0,0,0,eReady,0,0,NULL,NULL};
+ int end=0, drift_ms=0, timeBefore_ms, delay_ms, result, status;
+ client_t client;
+ struct _processNetworkEvents_args args;
+ char logMsg[128];
+
+ // connect to localhost at port 9999 using TCP (client)
+ IPaddress ip;
+
+ if(argc!=2) {
+ fprintf(stderr, "Usage: %s <nom ou ip du serveur>\n", argv[0]);
+ return 1;
+ }
+
+ openLog(NULL);
+
+ sprintf(logMsg, "NetLemmings version %i.%i", NetLemmings_VERSION_MAJOR, NetLemmings_VERSION_MINOR);
+ logs(LOG_INFO, logMsg);
+
+ if(SDLNet_ResolveHost(&ip,argv[1],9999)==-1) {
+ logs2(LOG_ERROR, "main(), SDLNet_ResolveHost()", SDLNet_GetError());
+ return 2;
+ }
+
+ client.sockClient=SDLNet_TCP_Open(&ip);
+ if(!client.sockClient) {
+ logs2(LOG_ERROR,"main(), SDLNet_TCP_Open()", SDLNet_GetError());
+ return 3;
+ }
+
+ result=init();
+ if(result!=0) {
+ logs(LOG_ERROR,"main(), mySDLInit()");
+ return 3;
+ }
+
+ semGameStart=SDL_CreateSemaphore(0);
+
+ args.end=&end;
+ args.drift_ms=&drift_ms;
+ args.client=&client;
+ SDL_Thread *timeSyncThread = SDL_CreateThread(processNetworkEvents, &args);
+ if(!timeSyncThread) {
+ logs2(LOG_ERROR,"main(), SDL_CreateThread()", SDL_GetError());
+ return 4;
+ }
+
+ //Signale au serveur que le client est pret
+ result=sendEvent(client.sockClient, &evReady);
+ if (result!=0) return 6;
+
+ logs(LOG_INFO, "Waiting game start");
+ result=SDL_SemWait(semGameStart);
+ if (result!=0) {
+ logs2(LOG_ERROR, "main(), SDL_SemWait()", SDL_GetError());
+ return 7;
+ }
+ logs(LOG_INFO, "Game started !");
+
+ while(!end) {
+ timeBefore_ms = SDL_GetTicks();
+
+ processLocalEvents();
+ play(tick++);
+ updateGraphics();
+
+ 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
+ }
+
+ SDLNet_TCP_Close(client.sockClient);
+ SDL_WaitThread(timeSyncThread, &status);
+ sprintf(logMsg, "TimeSync thread terminated with code %i", status);
+ logs(LOG_DEBUG, logMsg);
+
+ closeLog();
+ return 0;
+}
+
+int processNetworkEvents(void *a) {
+ int result;
+ event_t e;
+ char logMsg[128];
+
+ struct _processNetworkEvents_args *args = (struct _processNetworkEvents_args *)a;
+
+ while(! *(args->end) ) {
+ // logs(LOG_DEBUG, "Waiting event");
+ result=receiveEvent(args->client,&e);
+ if (result != 0) {
+ logs(LOG_WARN, "processNetworkEvents(), receiveEvents() error");
+ *(args->end)=1;
+ continue; //TODO : je doute que ça skipe vriament la syncrho du temps :s
+ }
+ // logs(LOG_DEBUG, "Got event");
+
+ *(args->drift_ms)=( tick - e.serverTick );
+ sprintf(logMsg, "serverTick==%i, tick==%i, drift_ms==%i\n", e.serverTick, tick, *(args->drift_ms));
+ logs(LOG_DEBUG, logMsg);
+
+
+ result=SDL_SemPost(semGameStart);
+ if (result!=0) {
+ logs2(LOG_ERROR, "main(), SDL_SemPost()", SDL_GetError());
+ return 1;
+ }
+
+
+ switch(e.type) {
+ case eLemAction:
+ //TODO
+ break;
+ case eTimeSync: // Rien à faire dans ce cas, la synchro du temps se fait quelque soti le tyep d'évènement
+ break;
+ default:
+ logs(LOG_ERROR, "serveClient(), Unknown event type");
+ }
+ }
+ // logs(LOG_DEBUG, "processNetworkEvents() : end");
+
+ return 0;
+}
+
+void processLocalEvents() {
+ /*TODO : intégration des variables manipulées dans des structs qui vont bien
+while( SDL_PollEvent( &event ) ) {
+ switch (event.type) {
+ case SDL_KEYBOARDEVENT:
+ switch(event.key.keysym.sym){
+ case SDLK_HOME : decalFps = 0; break;
+ case SDLK_END : decalFps = FPS-11; break;
+ case SDLK_PAGEUP : if(decalFps>0){decalFps -=1;}break;
+ case SDLK_PAGEDOWN : if(decalFps<(FPS-1)){decalFps +=1;}break;
+ case SDLK_w : paint_stencil = (paint_stencil==0)? 1 : 0 ; break;
+ case SDLK_ESCAPE : quit = 1; break;
+ default:break;
+ }
+ break;
+ case SDL_MOUSEMOTION:
+ mouseX = event.motion.x;
+ mouseY = event.motion.y;
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ err=mouse_action(&gInit, mouseX, mouseY,camera.x,camera.y );
+ if(err!=0){return err;} //FIXME : WTF ?
+ break;
+ case SDL_QUIT:
+ end=1;
+ break;
+ }
+ }
+
+ if(mouseY <= LEVEL_HEIGHT){
+ if (mouseX > (SCREEN_WIDTH - BOUND_SENSIBILITE)){
+ if (camera.x < (LEVEL_WIDTH - SCREEN_WIDTH ) )
+ {camera.x += CAM_VITESSE;}
+ }
+ if (mouseX < BOUND_SENSIBILITE){
+ if (camera.x >= CAM_VITESSE )
+ {camera.x -= CAM_VITESSE;}
+ }
+ */
+}
+
+int updateGraphics() {
+ //TODO : modifier les calques
+
+
+ //Mise à jour de l'écran
+
+ /*if( SDL_Flip(screen) == -1 ) {
+ return 4;
+ }*/
+
+ return 0;
+}