# HG changeset patch # User Paul Boddie # Date 1542118657 -3600 # Node ID ea0474d3418207040aebf08f5ccea9310ac140d9 # Parent f98c32b8cbb73e00042abc55fdcd629948207a1a Added support for unplotting sprites using the viewport and background image. diff -r f98c32b8cbb7 -r ea0474d34182 examples/vga/main.c --- a/examples/vga/main.c Tue Nov 13 14:35:22 2018 +0100 +++ b/examples/vga/main.c Tue Nov 13 15:17:37 2018 +0100 @@ -162,7 +162,7 @@ /* Prepare the frame for updates. */ - image_unplot_sprite(&s); + viewport_unplot_sprite_from_image(&v, &s, &scr); /* Scroll in the indicated direction. */ diff -r f98c32b8cbb7 -r ea0474d34182 include/image.h --- a/include/image.h Tue Nov 13 14:35:22 2018 +0100 +++ b/include/image.h Tue Nov 13 15:17:37 2018 +0100 @@ -152,6 +152,9 @@ void image_unplot_sprite(sprite_t *s); +void image_unplot_sprite_from_image(sprite_t *s, sprite_t *bg, + int xorigin, int yorigin); + /* Convenience operations. */ void image_plot_sprite_section(sprite_t *s, @@ -165,4 +168,8 @@ void image_update_scrolled_tiled_image(sprite_t *s, int xorigin, int yorigin, int xstep, int ystep); +void image_update_tiled_image(sprite_t *s, int xorigin, int yorigin, + int width, int height, + int xdisplay, int ydisplay); + #endif /* __IMAGE_H__ */ diff -r f98c32b8cbb7 -r ea0474d34182 include/viewport.h --- a/include/viewport.h Tue Nov 13 14:35:22 2018 +0100 +++ b/include/viewport.h Tue Nov 13 15:17:37 2018 +0100 @@ -21,6 +21,7 @@ #define __VIEWPORT_H__ #include "display.h" +#include "image.h" @@ -77,4 +78,8 @@ void viewport_update_origin(viewport_t *v, int dx, int dy); +/* Update operations. */ + +void viewport_unplot_sprite_from_image(viewport_t *v, sprite_t *s, sprite_t *bg); + #endif /* __VIEWPORT_H__ */ diff -r f98c32b8cbb7 -r ea0474d34182 lib/image.c --- a/lib/image.c Tue Nov 13 14:35:22 2018 +0100 +++ b/lib/image.c Tue Nov 13 15:17:37 2018 +0100 @@ -98,6 +98,23 @@ p->x, p->y, -1, 1); } +/* Unplot a sprite by restoring a region from the background image. */ + +void image_unplot_sprite_from_image(sprite_t *s, sprite_t *bg, + int xorigin, int yorigin) +{ + int frame = s->cfg->frame; + position_t *p = image_get_stored_position(s, frame); + + /* Plot the region of the background using the sprite image dimensions + converted to background image dimensions at the sprite's position on the + display. */ + + image_update_tiled_image(bg, xorigin, yorigin, + s->image->width, (s->image->height / s->yscale) * bg->yscale, + p->x, p->y); +} + /* Plot a section of an image without storing the background beforehand. */ @@ -233,3 +250,21 @@ 0, ydisplay); } } + +/* Plot a region of a tiled image. */ + +void image_update_tiled_image(sprite_t *s, int xorigin, int yorigin, + int width, int height, + int xdisplay, int ydisplay) +{ + /* Find the source position for the region. */ + + int xsource = wrap_value(xorigin + xdisplay, s->image->width); + int ysource = wrap_value(yorigin + ydisplay * s->yscale, s->image->height); + + /* Request tiling in the source coordinates. */ + + image_tile_sprite(s, xsource, ysource, + width, height, + xdisplay, ydisplay); +} diff -r f98c32b8cbb7 -r ea0474d34182 lib/viewport.c --- a/lib/viewport.c Tue Nov 13 14:35:22 2018 +0100 +++ b/lib/viewport.c Tue Nov 13 15:17:37 2018 +0100 @@ -72,3 +72,12 @@ v->xorigins[frame] = v->xorigin; v->yorigins[frame] = v->yorigin; } + +/* Update the display, unplotting a sprite and replotting the background. */ + +void viewport_unplot_sprite_from_image(viewport_t *v, sprite_t *s, sprite_t *bg) +{ + int frame = v->cfg->frame; + + image_unplot_sprite_from_image(s, bg, v->xorigins[frame], v->yorigins[frame]); +}