summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 8015b8982a5c06f178526d4efcdd6f2122a3dad6 (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
#include "game.h"
#include "utils.h"
#include "events.h"

#include <stdlib.h> // calloc
#include <string.h> // memset

inline tick_t getGameCurrentTick(Uint32 startTime_ms) {
/*	char buf[128];
	sprintf(buf, "SDL_GetTicks()==%i, startTime_ms==%i, TICK_DURATION_MS==%i", SDL_GetTicks(), startTime_ms, TICK_DURATION_MS);
	logs(LOG_DEBUG, buf);*/
	tick_t t = SDL_GetTicks()-startTime_ms; //FIXME Débodements possibles ?!?
	return t/TICK_DURATION_MS;
}

int initNetGgame(netGame_t *ng, id_t lemCount) {
	memset(ng,0,sizeof(netGame_t));

	eventListInit(&(ng->elist));

	ng->lemCount=lemCount;

	ng->local.lems = calloc(lemCount,sizeof(stateLem_t));
	if (ng->local.lems==NULL) { logp(LOG_ERROR, "initNetGame(), calloc()"); return 3; } 

	ng->net.lems = calloc(lemCount,sizeof(stateLem_t));
	if (ng->net.lems==NULL) { logp(LOG_ERROR, "initNetGame(), calloc()"); return 4; } 

	return 0;
}

void freeNetGame(netGame_t *ng) {
	eventListFree(&(ng->elist));
	free(ng->local.lems);
	free(ng->net.lems);
}

// Serialize en envoi une trame d'évènement sur le réseau
int sendEvent(TCPsocket sockDest, const event_t *e) {
	int msgLen, result;
	char *buf;

	msgLen=eventSerializedSize();
	buf=malloc(msgLen);
	if ( buf==NULL ) {
		logp(LOG_ERROR, "sendEvent(), malloc()");
		return 1;
	}
	eventSerialize(e, buf);

	result=SDLNet_TCP_Send(sockDest,buf,msgLen);
	free(buf);
	if(result<msgLen) {
		logs2(LOG_WARN, "sendEvent(), SDLNet_TCP_Send() error", SDLNet_GetError());
		return 2;
	}

	return 0;
}

// Attends de recevoir une trame d'évènement du réseau pour un client donné et l'envoi dans la file à traiter
int receiveEvent(client_t *c, event_t *e) {
	int result, bufLen;
	char *buf;

	bufLen=eventSerializedSize();
	buf=malloc(bufLen);
	if (buf==NULL) {
		logp(LOG_ERROR, "receiveEvent(), malloc()");
		return 1;
	}

	// Réception de l'évènement
	result=SDLNet_TCP_Recv(c->sockClient,buf,bufLen); // Appel bloquant
	if(result<=0) {
		logs(LOG_ERROR, "receiveEvent(), SDLNet_TCP_Recv() error");
		free(buf);
		return 2;
	}

	// Désérialisation de l'évènement
	result=eventUnserialize(e,buf);
	if ( result!=0 ) {
		logs(LOG_WARN, "receiveEvent(), eventUnserialize() error");
		free(buf);
		return 4;
	}

	free(buf);
	return 0;
}

int init() {
	int result;
	/*TODO : Avoir un struct de paramètre de config (taille écran) et un struct avec tous les éléments SDL manipulés dans le jeu

	//Initialisation des sous-systèmes de SDL
	result = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO );
	if ( result != 0 ) {
		logs2(LOG_ERROR, "init(), SDL_Init()", SDL_GetError());
		return 1;
	}
	atexit(SDL_Quit);

	//Mise en place de l'écran
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 24, SDL_HWSURFACE | SDL_DOUBLEBUF );
	if( screen == NULL ) {
		logs2(LOG_ERROR, "init(), SDL_SetVideoMode()", SDL_GetError());
		return 2;
	}

	//Titre de la fenêtre SDL
	SDL_WM_SetCaption(WIN_CAPTION, NULL);

	//Désactiver le pointeur de la souris
	SDL_ShowCursor(0);

	// Allocation et initialisation des différents calques d'affichage et de collision
	pTerrain = SDL_CreateRGBSurface(SDL_HWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, SCREEN_BPP,0,0,0,0);
	if( pTerrain == NULL ) {
		logs2(LOG_ERROR, "init(), SDL_CreateRGBSurface()", SDL_GetError());
		return 3;
	}

	pSpr_Lem = SDL_CreateRGBSurface(SDL_HWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT,SCREEN_BPP,0,0,0,0);
	if( pSpr_Lem == NULL ) {
		logs2(LOG_ERROR, "init(), SDL_CreateRGBSurface()", SDL_GetError());
		return 3;
	}

	pStencil = SDL_CreateRGBSurface(SDL_SWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, SCREEN_BPP,0,0,0,0);
	if( pStencil == NULL ) {
		logs2(LOG_ERROR, "init(), SDL_CreateRGBSurface()", SDL_GetError());
		return 3;
	}

	pStencilFixe = SDL_CreateRGBSurface(SDL_SWSURFACE, LEVEL_WIDTH, LEVEL_HEIGHT, SCREEN_BPP,0,0,0,0);
	if( pStencil == NULL ) {
		logs2(LOG_ERROR, "init(), SDL_CreateRGBSurface()", SDL_GetError());
		return 3;
	}
*/
	//Si tout s'est bien passé
	return 0;
}

void play(int tick) {
	if (tick%100==0) { printf("tick==%i\n",tick); }

	//TODO : boucle de jeu principale ici (maj état de jeu)
	SDL_Delay(rand()%8);
}