1.1 --- a/examples/vga/main.c Tue Nov 13 15:17:37 2018 +0100
1.2 +++ b/examples/vga/main.c Tue Nov 13 17:54:21 2018 +0100
1.3 @@ -94,7 +94,7 @@
1.4 extern image_t screendata;
1.5 extern image_t sprite;
1.6
1.7 -static SpriteOverwriting(scr, &screendata, &display_config, FRAME_COUNT, SOURCE_YSTEP);
1.8 +static SpriteOverwriting(scr, &screendata, &display_config, FRAME_COUNT, -1, SOURCE_YSTEP);
1.9
1.10 extern uint8_t fontchars[];
1.11 extern font_range_t fontbase;
1.12 @@ -118,7 +118,7 @@
1.13 {
1.14 /* Stores of background details, replotted when moving the sprite. */
1.15
1.16 - Sprite(s, &sprite, &display_config, FRAME_COUNT, SOURCE_YSTEP);
1.17 + Sprite(s, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP);
1.18
1.19 /* Sprite position. */
1.20
1.21 @@ -149,7 +149,8 @@
1.22 {
1.23 for (x = 0; x < display_config.line_length - s.image->width; x++)
1.24 {
1.25 - image_plot_sprite(&s, x, y, 0x0c);
1.26 + image_set_sprite_position(&s, x, y);
1.27 + image_plot_sprite(&s);
1.28
1.29 /* Update the display with the frame details. */
1.30
2.1 --- a/include/image.h Tue Nov 13 15:17:37 2018 +0100
2.2 +++ b/include/image.h Tue Nov 13 17:54:21 2018 +0100
2.3 @@ -81,6 +81,10 @@
2.4
2.5 position_t *pos;
2.6
2.7 + /* Colour key for transparency. */
2.8 +
2.9 + int key;
2.10 +
2.11 /* The vertical scaling factor translating display rows to image
2.12 coordinates. */
2.13
2.14 @@ -102,9 +106,9 @@
2.15 #define Stored_Regions(NAME, FRAMES, SIZE) \
2.16 uint8_t __##NAME##_image[(FRAMES) * (SIZE)]; \
2.17 stored_regions_t NAME = { \
2.18 - .image=__##NAME##_image, \
2.19 - .size=SIZE, \
2.20 - .stored=0};
2.21 + .image = __##NAME##_image, \
2.22 + .size = SIZE, \
2.23 + .stored = 0};
2.24
2.25 /* Access functions. */
2.26
2.27 @@ -117,44 +121,56 @@
2.28 /* Initialise a sprite object using an existing image without the capability of
2.29 storing regions of the screen.
2.30
2.31 - SpriteOverwriting(<name>, image_t *image, display_config_t *cfg, int frames, int yscale)
2.32 + SpriteOverwriting(<name>, image_t *image, display_config_t *cfg, int frames,
2.33 + int key, int yscale)
2.34 */
2.35
2.36 -#define SpriteOverwriting(NAME, IMAGE, CFG, FRAMES, YSCALE) \
2.37 +#define SpriteOverwriting(NAME, IMAGE, CFG, FRAMES, KEY, YSCALE) \
2.38 position_t __##NAME##_pos[FRAMES]; \
2.39 + \
2.40 sprite_t NAME = { \
2.41 - .image=IMAGE, \
2.42 - .regions=0, \
2.43 - .yscale=YSCALE, \
2.44 - .cfg=CFG, \
2.45 - .pos=__##NAME##_pos};
2.46 + .image = IMAGE, \
2.47 + .regions = 0, \
2.48 + .key = KEY, \
2.49 + .yscale = YSCALE, \
2.50 + .cfg = CFG, \
2.51 + .pos = __##NAME##_pos};
2.52
2.53 /* Initialise a sprite object using an existing image, creating stored regions
2.54 for the animation of the sprite.
2.55
2.56 - Sprite(<name>, image_t *image, display_config_t *cfg, int frames, int yscale)
2.57 + Sprite(<name>, image_t *image, display_config_t *cfg, int frames, int key,
2.58 + int yscale)
2.59 */
2.60
2.61 -#define Sprite(NAME, IMAGE, CFG, FRAMES, YSCALE) \
2.62 +#define Sprite(NAME, IMAGE, CFG, FRAMES, KEY, YSCALE) \
2.63 Stored_Regions(__##NAME##_regions, FRAMES, \
2.64 (IMAGE)->width * (IMAGE)->height * (YSCALE)); \
2.65 position_t __##NAME##_pos[FRAMES]; \
2.66 + \
2.67 sprite_t NAME = { \
2.68 - .image=IMAGE, \
2.69 - .regions=&(__##NAME##_regions), \
2.70 - .yscale=YSCALE, \
2.71 - .cfg=CFG, \
2.72 - .pos=__##NAME##_pos};
2.73 + .image = IMAGE, \
2.74 + .regions = &(__##NAME##_regions), \
2.75 + .key = KEY, \
2.76 + .yscale = YSCALE, \
2.77 + .cfg = CFG, \
2.78 + .pos = __##NAME##_pos};
2.79
2.80 /* Sprite plotting operations. */
2.81
2.82 -void image_plot_sprite(sprite_t *s, int x, int y, int key);
2.83 +void image_plot_sprite(sprite_t *s);
2.84
2.85 void image_unplot_sprite(sprite_t *s);
2.86
2.87 void image_unplot_sprite_from_image(sprite_t *s, sprite_t *bg,
2.88 int xorigin, int yorigin);
2.89
2.90 +/* Update operations. */
2.91 +
2.92 +position_t *image_get_sprite_position(sprite_t *s);
2.93 +
2.94 +void image_set_sprite_position(sprite_t *s, int x, int y);
2.95 +
2.96 /* Convenience operations. */
2.97
2.98 void image_plot_sprite_section(sprite_t *s,
3.1 --- a/lib/image.c Tue Nov 13 15:17:37 2018 +0100
3.2 +++ b/lib/image.c Tue Nov 13 17:54:21 2018 +0100
3.3 @@ -39,9 +39,28 @@
3.4
3.5
3.6
3.7 +/* Get a sprite's position. */
3.8 +
3.9 +position_t *image_get_sprite_position(sprite_t *s)
3.10 +{
3.11 + return image_get_stored_position(s, s->cfg->frame);
3.12 +}
3.13 +
3.14 +/* Set a sprite's position. */
3.15 +
3.16 +void image_set_sprite_position(sprite_t *s, int x, int y)
3.17 +{
3.18 + position_t *p = image_get_sprite_position(s);
3.19 +
3.20 + p->x = x;
3.21 + p->y = y;
3.22 +}
3.23 +
3.24 +
3.25 +
3.26 /* Copy a region from the screen to the store, then blit the image. */
3.27
3.28 -void image_plot_sprite(sprite_t *s, int x, int y, int key)
3.29 +void image_plot_sprite(sprite_t *s)
3.30 {
3.31 int frame = 0;
3.32 position_t *p = 0;
3.33 @@ -55,22 +74,19 @@
3.34
3.35 display_copy(s->cfg, image_get_stored_region(s->regions, frame),
3.36 s->image->width, s->image->height / s->yscale, 1,
3.37 - x, y, -1, 0);
3.38 + p->x, p->y, -1, 0);
3.39 }
3.40
3.41 /* Plot to the screen. */
3.42
3.43 display_copy(s->cfg, s->image->image,
3.44 s->image->width, s->image->height, s->yscale,
3.45 - x, y, key, 1);
3.46 + p->x, p->y, s->key, 1);
3.47
3.48 if (s->regions)
3.49 {
3.50 /* Record the stored background details. */
3.51
3.52 - p->x = x;
3.53 - p->y = y;
3.54 -
3.55 if (frame >= s->regions->stored)
3.56 s->regions->stored = frame + 1;
3.57 }