# HG changeset patch # User Paul Boddie # Date 1541873168 -3600 # Node ID 849db491104b644b2cafb1ddd090c48ad3df5967 # Parent 56b5497336325b89da4405aac3affc5ac2240a48 Moved sprite operations to the image module. diff -r 56b549733632 -r 849db491104b examples/vga/main.c --- a/examples/vga/main.c Sat Nov 10 18:53:06 2018 +0100 +++ b/examples/vga/main.c Sat Nov 10 19:06:08 2018 +0100 @@ -114,47 +114,6 @@ -/* Copy to the store from the display, then blit the image. */ - -static void plot_sprite(sprite_t *s, int x, int y, int key) -{ - int frame = display_config.frame; - position_t *p = image_get_stored_position(s->regions, frame); - - /* Copy to the stored region. */ - - display_copy(&display_config, image_get_stored_region(s->regions, frame), - s->image->width, s->image->height / s->yscale, 1, - x, y, -1, 0); - - /* Plot to the screen. */ - - display_copy(&display_config, s->image->image, - s->image->width, s->image->height, s->yscale, - x, y, key, 1); - - /* Record the stored background details. */ - - p->x = x; - p->y = y; - - if (frame >= s->regions->stored) - s->regions->stored = frame + 1; -} - -/* Copy to the display from the store, restoring the original background. */ - -static void unplot_sprite(sprite_t *s) -{ - int frame = display_config.frame; - position_t *p = image_get_stored_position(s->regions, frame); - - if (s->regions->stored > frame) - display_copy(&display_config, image_get_stored_region(s->regions, frame), - s->image->width, s->image->height / s->yscale, 1, - p->x, p->y, -1, 1); -} - /* Plot the revealed region at the edge of the screen after scrolling. */ static void plot_screen_edge(viewport_t *v, int xorigin, int yorigin, @@ -232,7 +191,7 @@ { /* Stores of background details, replotted when moving the sprite. */ - Sprite(s, &sprite, FRAME_COUNT, SOURCE_YSTEP); + Sprite(s, &sprite, &display_config, SOURCE_YSTEP); /* Sprite position. */ @@ -264,7 +223,7 @@ { for (x = 0; x < display_config.line_length - s.image->width; x++) { - plot_sprite(&s, x, y, 0x8c); + image_plot_sprite(&s, x, y, 0x8c); /* Update the display with the frame details. */ @@ -277,7 +236,7 @@ /* Prepare the frame for updates. */ - unplot_sprite(&s); + image_unplot_sprite(&s); /* Scroll in the indicated direction. */ diff -r 56b549733632 -r 849db491104b include/image.h --- a/include/image.h Sat Nov 10 18:53:06 2018 +0100 +++ b/include/image.h Sat Nov 10 19:06:08 2018 +0100 @@ -86,13 +86,17 @@ int yscale; + /* The display associated with the sprite. */ + + display_config_t *cfg; + } sprite_t; /* Initialise stored regions, allocating memory and a structure to access it. - Stored_Regions(, int, int) + Stored_Regions(, int frames, int size) */ #define Stored_Regions(NAME, FRAMES, SIZE) \ @@ -115,15 +119,22 @@ /* Initialise a sprite object using an existing image, creating stored regions for the animation of the sprite. - Sprite(, image_t *, int, int) + Sprite(, image_t *image, display_config_t *cfg, int yscale) */ -#define Sprite(NAME, IMAGE, FRAMES, YSCALE) \ - Stored_Regions(__##NAME##_regions, FRAMES, \ +#define Sprite(NAME, IMAGE, CFG, YSCALE) \ + Stored_Regions(__##NAME##_regions, (CFG)->frames, \ (IMAGE)->width * (IMAGE)->height * (YSCALE)); \ sprite_t NAME = { \ .image=IMAGE, \ .regions=&(__##NAME##_regions), \ - .yscale=YSCALE}; + .yscale=YSCALE, \ + .cfg=CFG}; + +/* Plotting operations. */ + +void image_plot_sprite(sprite_t *s, int x, int y, int key); + +void image_unplot_sprite(sprite_t *s); #endif /* __IMAGE_H__ */ diff -r 56b549733632 -r 849db491104b lib/image.c --- a/lib/image.c Sat Nov 10 18:53:06 2018 +0100 +++ b/lib/image.c Sat Nov 10 19:06:08 2018 +0100 @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include "display.h" #include "image.h" @@ -34,3 +35,49 @@ { return r->image + r->size * frame; } + + + +/* Copy a region from the screen to the store, then blit the image. */ + +void image_plot_sprite(sprite_t *s, int x, int y, int key) +{ + int frame = s->cfg->frame; + position_t *p = image_get_stored_position(s->regions, frame); + + /* Copy to the stored region. */ + + display_copy(s->cfg, image_get_stored_region(s->regions, frame), + s->image->width, s->image->height / s->yscale, 1, + x, y, -1, 0); + + /* Plot to the screen. */ + + display_copy(s->cfg, s->image->image, + s->image->width, s->image->height, s->yscale, + x, y, key, 1); + + /* Record the stored background details. */ + + p->x = x; + p->y = y; + + if (frame >= s->regions->stored) + s->regions->stored = frame + 1; +} + +/* Copy a region from the store to the screen, restoring the original + background. */ + +void image_unplot_sprite(sprite_t *s) +{ + int frame = s->cfg->frame; + position_t *p = image_get_stored_position(s->regions, frame); + + /* Only unplot the sprite if a region was stored for the frame. */ + + if (s->regions->stored > frame) + display_copy(s->cfg, image_get_stored_region(s->regions, frame), + s->image->width, s->image->height / s->yscale, 1, + p->x, p->y, -1, 1); +}