From 79c6f91e7533f842a0f67770a641fceb5ad278e0 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 19 Feb 2013 00:13:20 -0500 Subject: Fix graphics glitch with negative viewport coords. --- diff --git a/src/area.c b/src/area.c index ae8b071..674a81c 100644 --- a/src/area.c +++ b/src/area.c @@ -118,35 +118,54 @@ render_area_to_viewport(struct area *area, struct viewport *vp) void render_area_to_surface(struct area *area, SDL_Rect *arearect, - SDL_Surface *dst, SDL_Rect *dstrect) + SDL_Surface *surface, SDL_Rect *surfacerect) { - debug("Rendering %dx%d area rect at (%d,%d) " - "onto %dx%d surface rect at (%d,%d)...", - arearect->w, arearect->h, arearect->x, arearect->y, - dstrect->w, dstrect->h, dstrect->x, dstrect->y); + SDL_Rect srcrect, dstrect; + + memcpy(&srcrect, arearect, sizeof(srcrect)); + memcpy(&dstrect, surfacerect, sizeof(dstrect)); /* Fast fill with black to avoid artifacts in off-map pixels. */ /* NB: This is gray, at least temporarily. */ - SDL_FillRect(dst, dstrect, SDL_MapRGB(dst->format, 63, 63, 63)); + SDL_FillRect(surface, &dstrect, + SDL_MapRGB(surface->format, 63, 63, 63)); + + /* Avoid fun glitches due to negative coordinates in source + * rectangles. */ + if (srcrect.x < 0) { + srcrect.x = 0; + dstrect.x = surfacerect->x - arearect->x; + } + if (srcrect.y < 0) { + srcrect.y = 0; + dstrect.y = surfacerect->y - arearect->y; + } + +#if 0 + debug("Rendering %dx%d area rect at (%d,%d) " + "onto %dx%d surface rect at (%d,%d)...", + srcrect.w, srcrect.h, srcrect.x, srcrect.y, + dstrect.w, dstrect.h, dstrect.x, dstrect.y); +#endif /* Blit ground layer. */ - SDL_BlitSurface(area->map_layers[AREA_LAYER_GROUND], arearect, - dst, dstrect); + SDL_BlitSurface(area->map_layers[AREA_LAYER_GROUND], &srcrect, + surface, &dstrect); /* Blit low objects layer. */ - SDL_BlitSurface(area->map_layers[AREA_LAYER_OBJ_LOW], arearect, - dst, dstrect); + SDL_BlitSurface(area->map_layers[AREA_LAYER_OBJ_LOW], &srcrect, + surface, &dstrect); /* return;*/ /* TODO: Blit low sprites. */ /* Blit middle objects layer. */ - SDL_BlitSurface(area->map_layers[AREA_LAYER_OBJ_MID], arearect, - dst, dstrect); + SDL_BlitSurface(area->map_layers[AREA_LAYER_OBJ_MID], &srcrect, + surface, &dstrect); /* TODO: Blit high sprites. */ /* Blit high objects layer. */ - SDL_BlitSurface(area->map_layers[AREA_LAYER_OBJ_HIGH], arearect, - dst, dstrect); + SDL_BlitSurface(area->map_layers[AREA_LAYER_OBJ_HIGH], &srcrect, + surface, &dstrect); } diff --git a/src/area.h b/src/area.h index a1b7e92..aaddf1f 100644 --- a/src/area.h +++ b/src/area.h @@ -13,6 +13,6 @@ struct area { struct area *area_new(struct map *map); void render_area_to_viewport(struct area *area, struct viewport *vp); void render_area_to_surface(struct area *area, SDL_Rect *arearect, - SDL_Surface *dst, SDL_Rect *dstrect); + SDL_Surface *surface, SDL_Rect *surfacerect); #endif -- cgit v0.9.1