summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..8015b89
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,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);
+}
+