diff options
Diffstat (limited to 'src/netlem.c')
-rw-r--r-- | src/netlem.c | 146 |
1 files changed, 70 insertions, 76 deletions
diff --git a/src/netlem.c b/src/netlem.c index 47d8126..2b62fb2 100644 --- a/src/netlem.c +++ b/src/netlem.c @@ -45,14 +45,10 @@ int loadLevelProc(void *a); // Client-specific functions 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); +void processLocalEvents(gameGraphics_t *gGraph); 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; @@ -105,11 +101,13 @@ int main(int argc, char **argv) { loadGameConfig(&conf); // Libraries initialization - result=init(&conf, &gGraph); + result=init(WIN_CAPTION, &conf, &gGraph); if(result!=0) { logs(LOG_ERROR,"main(), init()"); return 3; } + atexit(SDL_Quit); + signal(2,signals); // Synchronization tools intialization semGameStart=SDL_CreateSemaphore(0); @@ -162,7 +160,7 @@ int main(int argc, char **argv) { // Process local player keyboard and mouse events // (note: remote events are processed by network read thread) - processLocalEvents(&(gGraph.viewport), &(gGraph.screen->clip_rect), &(gGraph.surfaces.terrain->clip_rect)); + processLocalEvents(&gGraph); endMainLoop=act(&tick, loadProgress, client.sockClient, &(gGraph.dirtRectsCount), &(gGraph.dirtRects)); @@ -237,57 +235,6 @@ 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 ) { - logs2(LOG_ERROR, "init(), SDL_Init()", SDL_GetError()); - return 1; - } - atexit(SDL_Quit); - signal(2,signals); - - // Screen setup - gGraph->screen = SDL_SetVideoMode(conf->screen.w, conf->screen.h, 32, SDL_HWSURFACE | SDL_ASYNCBLIT); - if( gGraph->screen == NULL ) { - logs2(LOG_ERROR, "init(), SDL_SetVideoMode()", SDL_GetError()); - return 2; - } - - // SDL main window caption - SDL_WM_SetCaption(WIN_CAPTION, NULL); - - // We dont want to see the standard mouse cursor in our main window - //TODO: SDL_ShowCursor(0); - - // Memory allocation and initialization for all display layers - gGraph->surfaces.terrain = createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); - if( gGraph->surfaces.terrain == NULL ) { - logs2(LOG_ERROR, "init(), SDL_createSurface()", SDL_GetError()); - return 3; - } - - gGraph->surfaces.stencil = createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); - if( gGraph->surfaces.stencil == NULL ) { - logs2(LOG_ERROR, "init(), SDL_createSurface()", SDL_GetError()); - return 3; - } - - return 0; -} - -void loadGameConfig(gameConfig_t *conf) { - conf->screen.w=640; - conf->screen.h=480; - conf->screen.x=0; - conf->screen.y=0; - //TODO : charger vraiment le fichier de conf ^^ -} - int act(tick_t *tick, int loadProgress, TCPsocket sockClient, int *dirtRectsCount, SDL_Rect **dirtRects) { int res; @@ -475,7 +422,9 @@ int loadLevelProc(void *a) { #define MAP_SCROLL_BOUND 32 #define MAP_SCROLL_SPEED 16 -void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain) { +//FIXME : tous les champs de gGraph utilisés dans cette méthode devraient être déplacés dans une structure autre +//void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain, int *debugFlags) { +void processLocalEvents(gameGraphics_t *gGraph) { static int mouseActive=1, mouseX=100, mouseY=100; SDL_Event event; @@ -503,8 +452,14 @@ void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain) // if(zoomY <= 0.) {zoomY=0.1;} // break; // } - case SDLK_ESCAPE : changeState(eEnd); break; - default:break; + case SDLK_ESCAPE: + changeState(eEnd); + break; + case SDLK_d: + gGraph->debugFlags ^= DEBUG_DIRTYRECTANGLES; + break; + default: + break; } break; case SDL_ACTIVEEVENT: @@ -515,6 +470,15 @@ void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain) case SDL_MOUSEMOTION: mouseX = event.motion.x; mouseY = event.motion.y; + + //FIXME : utiliser une méthode addDirtRect + gGraph->dirtRects=malloc(1*sizeof(SDL_Rect)); + gGraph->dirtRects[0].x=mouseX-16; + gGraph->dirtRects[0].y=mouseY-16; + gGraph->dirtRects[0].w=32; + gGraph->dirtRects[0].h=32; + gGraph->dirtRectsCount=1; + break; case SDL_MOUSEBUTTONDOWN: //err=mouse_action(&gInit, mouseX, mouseY,camera.x,camera.y ); @@ -529,15 +493,15 @@ void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain) switch(getState()) { case eMultiGame: if (mouseActive) { - if(mouseY <= terrain->h){ - if (mouseX > (screen->w - MAP_SCROLL_BOUND)){ - if (viewport->x < (terrain->w - screen->w ) ) { - viewport->x += MAP_SCROLL_SPEED; + if(mouseY <= gGraph->surfaces.terrain->h){ + if (mouseX > (gGraph->screen->w - MAP_SCROLL_BOUND)){ + if (gGraph->viewport.x < (gGraph->surfaces.terrain->w - gGraph->screen->clip_rect.w ) ) { + gGraph->viewport.x += MAP_SCROLL_SPEED; } } if (mouseX < MAP_SCROLL_BOUND){ - if (viewport->x >= MAP_SCROLL_SPEED ) { - viewport->x -= MAP_SCROLL_SPEED; + if (gGraph->viewport.x >= MAP_SCROLL_SPEED ) { + gGraph->viewport.x -= MAP_SCROLL_SPEED; } } } @@ -553,44 +517,74 @@ void processLocalEvents(SDL_Rect *viewport, SDL_Rect *screen, SDL_Rect *terrain) int updateGraphics(gameGraphics_t *gGraph) { int i, res; static SDL_Rect lastViewport= {-1,-1,-1,-1}; - SDL_Rect srcRect/*, dstRect*/; + SDL_Rect srcRect; + SDL_Surface *tmpSurf; //TODO : modifier les calques switch(getState()) { case eMultiWaitLoading: + //TODO 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) { + //if ( memcmp(&lastViewport, &(gGraph->viewport), sizeof(SDL_Rect)) != 0) { if ( lastViewport.x != gGraph->viewport.x ) { //lastViewport=gGraph->viewport; lastViewport.x=gGraph->viewport.x; if ( gGraph->dirtRectsCount > 0 ) { free(gGraph->dirtRects); } - // Put only one dirt rectagle, of the size of the screen + // Put only one dirt rectangle, of the size of the screen gGraph->dirtRects=malloc(1*sizeof(SDL_Rect)); memcpy(gGraph->dirtRects, &(gGraph->screen->clip_rect), sizeof(SDL_Rect)); gGraph->dirtRectsCount=1; } - // We use a dirt rectangle method for performance + // We use a dirty rectangle method for performance + tmpSurf=gGraph->surfaces.tmpSurf; for(i=0; i<gGraph->dirtRectsCount; i++) { srcRect.x=gGraph->dirtRects[i].x+lastViewport.x; srcRect.y=gGraph->dirtRects[i].y+lastViewport.y; srcRect.w=gGraph->dirtRects[i].w; srcRect.h=gGraph->dirtRects[i].h; - res=repaint(&gGraph->surfaces, &srcRect, gGraph->screen, gGraph->dirtRects[i]); + res=repaint(&gGraph->surfaces, &srcRect, tmpSurf, srcRect); if ( res != 0 ) { - logs(LOG_WARN, "updateGraphics(), repain() failed"); + logs(LOG_WARN, "updateGraphics(), repaint() failed"); } - SDL_UpdateRect(gGraph->screen, + if ( (gGraph->debugFlags & DEBUG_DIRTYRECTANGLES) == 0 ) { + res=SDL_BlitSurface(tmpSurf, &srcRect, gGraph->screen, gGraph->dirtRects+i); + if ( res!=0 ) { + logs2(LOG_DEBUG, "repaint(), SDL_BlitSurface()", SDL_GetError()); + } + + SDL_UpdateRect(gGraph->screen, gGraph->dirtRects[i].x, gGraph->dirtRects[i].y, gGraph->dirtRects[i].w, gGraph->dirtRects[i].h); + } + } + if (gGraph->dirtRectsCount > 0) { + gGraph->dirtRectsCount=0; + free(gGraph->dirtRects); + } + + if ( (gGraph->debugFlags & DEBUG_DIRTYRECTANGLES) == DEBUG_DIRTYRECTANGLES ) { + srcRect.x=lastViewport.x; + srcRect.y=lastViewport.y; + srcRect.w=gGraph->screen->clip_rect.w; + srcRect.h=gGraph->screen->clip_rect.h; + res=SDL_BlitSurface(tmpSurf, &srcRect, gGraph->screen, NULL); + if ( res!=0 ) { + logs2(LOG_DEBUG, "repaint(), SDL_BlitSurface()", SDL_GetError()); + } + SDL_UpdateRect(gGraph->screen, 0,0,0,0); + + SDL_LockSurface(tmpSurf); + for(i=0; i < tmpSurf->pitch * tmpSurf->h ; ++i) { + ((Uint8 *)tmpSurf->pixels)[i] *= 0.9; + } + SDL_UnlockSurface(tmpSurf); } - gGraph->dirtRectsCount=0; - free(gGraph->dirtRects); break; default: break; |