summaryrefslogtreecommitdiff
path: root/src/netlem.c
blob: e9a430400d113c57ac940e2a1960b5851766e2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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;
}