1.1 --- a/examples/vga/main.c Sat Nov 10 18:53:06 2018 +0100
1.2 +++ b/examples/vga/main.c Sat Nov 10 19:06:08 2018 +0100
1.3 @@ -114,47 +114,6 @@
1.4
1.5
1.6
1.7 -/* Copy to the store from the display, then blit the image. */
1.8 -
1.9 -static void plot_sprite(sprite_t *s, int x, int y, int key)
1.10 -{
1.11 - int frame = display_config.frame;
1.12 - position_t *p = image_get_stored_position(s->regions, frame);
1.13 -
1.14 - /* Copy to the stored region. */
1.15 -
1.16 - display_copy(&display_config, image_get_stored_region(s->regions, frame),
1.17 - s->image->width, s->image->height / s->yscale, 1,
1.18 - x, y, -1, 0);
1.19 -
1.20 - /* Plot to the screen. */
1.21 -
1.22 - display_copy(&display_config, s->image->image,
1.23 - s->image->width, s->image->height, s->yscale,
1.24 - x, y, key, 1);
1.25 -
1.26 - /* Record the stored background details. */
1.27 -
1.28 - p->x = x;
1.29 - p->y = y;
1.30 -
1.31 - if (frame >= s->regions->stored)
1.32 - s->regions->stored = frame + 1;
1.33 -}
1.34 -
1.35 -/* Copy to the display from the store, restoring the original background. */
1.36 -
1.37 -static void unplot_sprite(sprite_t *s)
1.38 -{
1.39 - int frame = display_config.frame;
1.40 - position_t *p = image_get_stored_position(s->regions, frame);
1.41 -
1.42 - if (s->regions->stored > frame)
1.43 - display_copy(&display_config, image_get_stored_region(s->regions, frame),
1.44 - s->image->width, s->image->height / s->yscale, 1,
1.45 - p->x, p->y, -1, 1);
1.46 -}
1.47 -
1.48 /* Plot the revealed region at the edge of the screen after scrolling. */
1.49
1.50 static void plot_screen_edge(viewport_t *v, int xorigin, int yorigin,
1.51 @@ -232,7 +191,7 @@
1.52 {
1.53 /* Stores of background details, replotted when moving the sprite. */
1.54
1.55 - Sprite(s, &sprite, FRAME_COUNT, SOURCE_YSTEP);
1.56 + Sprite(s, &sprite, &display_config, SOURCE_YSTEP);
1.57
1.58 /* Sprite position. */
1.59
1.60 @@ -264,7 +223,7 @@
1.61 {
1.62 for (x = 0; x < display_config.line_length - s.image->width; x++)
1.63 {
1.64 - plot_sprite(&s, x, y, 0x8c);
1.65 + image_plot_sprite(&s, x, y, 0x8c);
1.66
1.67 /* Update the display with the frame details. */
1.68
1.69 @@ -277,7 +236,7 @@
1.70
1.71 /* Prepare the frame for updates. */
1.72
1.73 - unplot_sprite(&s);
1.74 + image_unplot_sprite(&s);
1.75
1.76 /* Scroll in the indicated direction. */
1.77
2.1 --- a/include/image.h Sat Nov 10 18:53:06 2018 +0100
2.2 +++ b/include/image.h Sat Nov 10 19:06:08 2018 +0100
2.3 @@ -86,13 +86,17 @@
2.4
2.5 int yscale;
2.6
2.7 + /* The display associated with the sprite. */
2.8 +
2.9 + display_config_t *cfg;
2.10 +
2.11 } sprite_t;
2.12
2.13
2.14
2.15 /* Initialise stored regions, allocating memory and a structure to access it.
2.16
2.17 - Stored_Regions(<name>, int, int)
2.18 + Stored_Regions(<name>, int frames, int size)
2.19 */
2.20
2.21 #define Stored_Regions(NAME, FRAMES, SIZE) \
2.22 @@ -115,15 +119,22 @@
2.23 /* Initialise a sprite object using an existing image, creating stored regions
2.24 for the animation of the sprite.
2.25
2.26 - Sprite(<name>, image_t *, int, int)
2.27 + Sprite(<name>, image_t *image, display_config_t *cfg, int yscale)
2.28 */
2.29
2.30 -#define Sprite(NAME, IMAGE, FRAMES, YSCALE) \
2.31 - Stored_Regions(__##NAME##_regions, FRAMES, \
2.32 +#define Sprite(NAME, IMAGE, CFG, YSCALE) \
2.33 + Stored_Regions(__##NAME##_regions, (CFG)->frames, \
2.34 (IMAGE)->width * (IMAGE)->height * (YSCALE)); \
2.35 sprite_t NAME = { \
2.36 .image=IMAGE, \
2.37 .regions=&(__##NAME##_regions), \
2.38 - .yscale=YSCALE};
2.39 + .yscale=YSCALE, \
2.40 + .cfg=CFG};
2.41 +
2.42 +/* Plotting operations. */
2.43 +
2.44 +void image_plot_sprite(sprite_t *s, int x, int y, int key);
2.45 +
2.46 +void image_unplot_sprite(sprite_t *s);
2.47
2.48 #endif /* __IMAGE_H__ */
3.1 --- a/lib/image.c Sat Nov 10 18:53:06 2018 +0100
3.2 +++ b/lib/image.c Sat Nov 10 19:06:08 2018 +0100
3.3 @@ -17,6 +17,7 @@
3.4 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3.5 */
3.6
3.7 +#include "display.h"
3.8 #include "image.h"
3.9
3.10
3.11 @@ -34,3 +35,49 @@
3.12 {
3.13 return r->image + r->size * frame;
3.14 }
3.15 +
3.16 +
3.17 +
3.18 +/* Copy a region from the screen to the store, then blit the image. */
3.19 +
3.20 +void image_plot_sprite(sprite_t *s, int x, int y, int key)
3.21 +{
3.22 + int frame = s->cfg->frame;
3.23 + position_t *p = image_get_stored_position(s->regions, frame);
3.24 +
3.25 + /* Copy to the stored region. */
3.26 +
3.27 + display_copy(s->cfg, image_get_stored_region(s->regions, frame),
3.28 + s->image->width, s->image->height / s->yscale, 1,
3.29 + x, y, -1, 0);
3.30 +
3.31 + /* Plot to the screen. */
3.32 +
3.33 + display_copy(s->cfg, s->image->image,
3.34 + s->image->width, s->image->height, s->yscale,
3.35 + x, y, key, 1);
3.36 +
3.37 + /* Record the stored background details. */
3.38 +
3.39 + p->x = x;
3.40 + p->y = y;
3.41 +
3.42 + if (frame >= s->regions->stored)
3.43 + s->regions->stored = frame + 1;
3.44 +}
3.45 +
3.46 +/* Copy a region from the store to the screen, restoring the original
3.47 + background. */
3.48 +
3.49 +void image_unplot_sprite(sprite_t *s)
3.50 +{
3.51 + int frame = s->cfg->frame;
3.52 + position_t *p = image_get_stored_position(s->regions, frame);
3.53 +
3.54 + /* Only unplot the sprite if a region was stored for the frame. */
3.55 +
3.56 + if (s->regions->stored > frame)
3.57 + display_copy(s->cfg, image_get_stored_region(s->regions, frame),
3.58 + s->image->width, s->image->height / s->yscale, 1,
3.59 + p->x, p->y, -1, 1);
3.60 +}