summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/game.c6
-rw-r--r--src/include/data_localgame.h4
-rw-r--r--src/include/game.h3
-rw-r--r--src/netlem.c123
4 files changed, 81 insertions, 55 deletions
diff --git a/src/game.c b/src/game.c
index 7a93832..8e49012 100644
--- a/src/game.c
+++ b/src/game.c
@@ -4,10 +4,14 @@
#include "game.h"
#include "utils.h"
-void play(tick_t tick) {
+void play(tick_t tick, int *dirtRectsCount, SDL_Rect **directRects ) {
if (tick%100==0) { printf("tick==%i\n",tick); }
//TODO : boucle de jeu principale ici (maj état de jeu)
+
+ *dirtRectsCount=0;
+ *directRects=NULL;
+
SDL_Delay(rand()%8);
}
diff --git a/src/include/data_localgame.h b/src/include/data_localgame.h
index 5f67783..b740b27 100644
--- a/src/include/data_localgame.h
+++ b/src/include/data_localgame.h
@@ -11,7 +11,9 @@ typedef struct {
typedef struct {
SDL_Surface *screen, *terrain, *stencil;
- SDL_Rect viewport; // Viewport top-left corner coords in screen
+ SDL_Rect viewport; // Viewport top-left corner coords in screen
+ int dirtRectsCount; // Dirt rectangle to refresh for current tick
+ SDL_Rect *dirtRects;
} gameGraphics_t;
diff --git a/src/include/game.h b/src/include/game.h
index 79d16c4..fba84db 100644
--- a/src/include/game.h
+++ b/src/include/game.h
@@ -2,7 +2,8 @@
#define GAME_H
#include "data_types.h"
+#include "SDL/SDL.h"
-void play(tick_t tick);
+void play(tick_t tick, int *dirtRectsCount, SDL_Rect **directRects );
#endif //GAME_H
diff --git a/src/netlem.c b/src/netlem.c
index b8a7bce..b5147e7 100644
--- a/src/netlem.c
+++ b/src/netlem.c
@@ -22,7 +22,7 @@
// Thread arguments structures
struct _networkReadProc_args {
client_t *client;
- Uint32 *tick;
+ tick_t *tick;
int *drift_ms;
};
@@ -47,13 +47,15 @@ void signals(int signum);
int init(gameConfig_t *conf, gameGraphics_t *gGraph);
void loadGameConfig(gameConfig_t *conf);
void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain);
+int act(tick_t *tick, int loadProgress, TCPsocket sockClient, int *dirtRectsCount, SDL_Rect **dirtRects);
int updateGraphics(gameGraphics_t *gGraph);
int main(int argc, char **argv) {
int drift_ms=0, endMainLoop, result;
- Uint32 tick=0, timeBefore_ms;
+ tick_t tick=0;
+ Uint32 timeBefore_ms;
Uint8 loadProgress=0;
client_t client;
@@ -150,36 +152,7 @@ int main(int argc, char **argv) {
// (note: remote events are processed by network read thread)
processLocalEvents(&(gGraph.viewport), &(gGraph.screen->clip_rect), &(gGraph.terrain->clip_rect));
- switch(getState()) {
- case eMultiLoading:
- if(loadProgress==100) {
- changeStateAndNotify(eMultiWaitLoading,client.sockClient);
- }
- break;
- case eMultiWaitLoading:
- result=SDL_SemTryWait(semGameStart);
- switch (result) {
- case -1:
- logs2(LOG_ERROR, "main(), SDL_SemTryWait()", SDL_GetError());
- return 7;
- break;
- case 0:
-//FIXME : check return value
- changeState(eMultiGame);
- break;
- default:
- break;
- }
- break;
- case eSingleGame:
- case eMultiGame:
- // Make game evolve from the current state to the next time chunk (ie. frame, or tick)
- play(tick++);
- break;
- default:
- endMainLoop=1;
- break;
- }
+ endMainLoop=act(&tick, loadProgress, client.sockClient, &(gGraph.dirtRectsCount), &(gGraph.dirtRects));
// Display that new game state to the local user
updateGraphics(&gGraph);
@@ -217,6 +190,8 @@ void signals(int signum) {
int init(gameConfig_t *conf, gameGraphics_t *gGraph) {
int result;
+ memset(gGraph,0,sizeof(gameGraphics_t));
+
// SDL subsystems initialization
result = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO );
if ( result != 0 ) {
@@ -263,6 +238,42 @@ void loadGameConfig(gameConfig_t *conf) {
//TODO : charger vraiment le fichier de conf ^^
}
+int act(tick_t *tick, int loadProgress, TCPsocket sockClient, int *dirtRectsCount, SDL_Rect **dirtRects) {
+ int res;
+
+ switch(getState()) {
+ case eMultiLoading:
+ if(loadProgress==100) {
+ changeStateAndNotify(eMultiWaitLoading,sockClient);
+ }
+ break;
+ case eMultiWaitLoading:
+ res=SDL_SemTryWait(semGameStart);
+ switch (res) {
+ case -1:
+ logs2(LOG_ERROR, "main(), SDL_SemTryWait()", SDL_GetError());
+ return 7;
+ break;
+ case 0:
+//FIXME : check return value
+ changeState(eMultiGame);
+ break;
+ default:
+ break;
+ }
+ break;
+ case eSingleGame:
+ case eMultiGame:
+ // Make game evolve from the current state to the next time chunk (ie. frame, or tick)
+ play((*tick)++, dirtRectsCount, dirtRects);
+ break;
+ default:
+ return 1;
+ break;
+ }
+ return 0;
+}
+
int networkReadProc(void *a) {
int result;
event_t e;
@@ -271,14 +282,14 @@ int networkReadProc(void *a) {
struct _networkReadProc_args *args = (struct _networkReadProc_args *)a;
while( getState() != eEnd ) {
- logs(LOG_DEBUG, "Waiting event");
+// logs(LOG_DEBUG, "Waiting event");
result=receiveEvent(args->client,&e);
if (result != 0) {
logs(LOG_WARN, "networkReadProc(), receiveEvents() error");
changeState(eEnd);
continue; //TODO : je doute que ça skipe vriament la syncrho du temps :s
}
- logs(LOG_DEBUG, "Got event");
+// 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));
@@ -409,10 +420,10 @@ int loadLevelProc(void *a) {
return 0;
}
-#define BOUND_SENSIBILITE 16
-#define CAM_VITESSE 8
+#define MAP_SCROLL_BOUND 32
+#define MAP_SCROLL_SPEED 16
void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain) {
- int mouseX, mouseY;
+ static int mouseX=100, mouseY=100;
SDL_Event event;
while( SDL_PollEvent( &event ) ) {
@@ -442,19 +453,24 @@ void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain)
}
}
-
- //if(mouseY <= LEVEL_HEIGHT){
- if (mouseX > (screen->w - BOUND_SENSIBILITE)){
- if (viewport->x < (terrain->w - screen->w ) ) {
- viewport->x += CAM_VITESSE;
- }
- }
- if (mouseX < BOUND_SENSIBILITE){
- if (viewport->x >= CAM_VITESSE ) {
- viewport->x -= CAM_VITESSE;
- }
+ switch(getState()) {
+ case eMultiGame:
+ if(mouseY <= terrain->h){
+ if (mouseX > (screen->w - MAP_SCROLL_BOUND)){
+ if (viewport->x < (terrain->w - screen->w ) ) {
+ viewport->x += MAP_SCROLL_SPEED;
+ }
+ }
+ if (mouseX < MAP_SCROLL_BOUND){
+ if (viewport->x >= MAP_SCROLL_SPEED ) {
+ viewport->x -= MAP_SCROLL_SPEED;
+ }
+ }
+ }
+ break;
+ default:
+ break;
}
- //}
return;
}
@@ -469,9 +485,12 @@ int updateGraphics(gameGraphics_t *gGraph) {
break;
case eMultiGame:
// If we had a camera movement, we have to refesh all the screen
- if ( memcmp(&lastViewport, &(gGraph->viewport), sizeof(SDL_Rect)) != 0) {
- lastViewport=gGraph->viewport;
- res=SDL_BlitSurface(gGraph->terrain, NULL, gGraph->screen, &lastViewport);
+// if ( memcmp(&lastViewport, &(gGraph->viewport), sizeof(SDL_Rect)) != 0) {
+ if ( lastViewport.x != gGraph->viewport.x ) {
+ //lastViewport=gGraph->viewport;
+ lastViewport.x=gGraph->viewport.x;
+printf("viewport.x==%i\n", gGraph->viewport.x);
+ res=SDL_BlitSurface(gGraph->terrain, &lastViewport, gGraph->screen, NULL);
if ( res!=0 ) {
logs2(LOG_DEBUG, "updateGraphics(), SDL_BlitSurface()", SDL_GetError());
return res;