# HG changeset patch # User Paul Boddie # Date 1542128061 -3600 # Node ID ddb4b7db1d9bb48f32927007d836b30343647b7d # Parent ea0474d3418207040aebf08f5ccea9310ac140d9 Separated sprite position and key details from the plotting operation. diff -r ea0474d34182 -r ddb4b7db1d9b examples/vga/main.c --- a/examples/vga/main.c Tue Nov 13 15:17:37 2018 +0100 +++ b/examples/vga/main.c Tue Nov 13 17:54:21 2018 +0100 @@ -94,7 +94,7 @@ extern image_t screendata; extern image_t sprite; -static SpriteOverwriting(scr, &screendata, &display_config, FRAME_COUNT, SOURCE_YSTEP); +static SpriteOverwriting(scr, &screendata, &display_config, FRAME_COUNT, -1, SOURCE_YSTEP); extern uint8_t fontchars[]; extern font_range_t fontbase; @@ -118,7 +118,7 @@ { /* Stores of background details, replotted when moving the sprite. */ - Sprite(s, &sprite, &display_config, FRAME_COUNT, SOURCE_YSTEP); + Sprite(s, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP); /* Sprite position. */ @@ -149,7 +149,8 @@ { for (x = 0; x < display_config.line_length - s.image->width; x++) { - image_plot_sprite(&s, x, y, 0x0c); + image_set_sprite_position(&s, x, y); + image_plot_sprite(&s); /* Update the display with the frame details. */ diff -r ea0474d34182 -r ddb4b7db1d9b include/image.h --- a/include/image.h Tue Nov 13 15:17:37 2018 +0100 +++ b/include/image.h Tue Nov 13 17:54:21 2018 +0100 @@ -81,6 +81,10 @@ position_t *pos; + /* Colour key for transparency. */ + + int key; + /* The vertical scaling factor translating display rows to image coordinates. */ @@ -102,9 +106,9 @@ #define Stored_Regions(NAME, FRAMES, SIZE) \ uint8_t __##NAME##_image[(FRAMES) * (SIZE)]; \ stored_regions_t NAME = { \ - .image=__##NAME##_image, \ - .size=SIZE, \ - .stored=0}; + .image = __##NAME##_image, \ + .size = SIZE, \ + .stored = 0}; /* Access functions. */ @@ -117,44 +121,56 @@ /* Initialise a sprite object using an existing image without the capability of storing regions of the screen. - SpriteOverwriting(, image_t *image, display_config_t *cfg, int frames, int yscale) + SpriteOverwriting(, image_t *image, display_config_t *cfg, int frames, + int key, int yscale) */ -#define SpriteOverwriting(NAME, IMAGE, CFG, FRAMES, YSCALE) \ +#define SpriteOverwriting(NAME, IMAGE, CFG, FRAMES, KEY, YSCALE) \ position_t __##NAME##_pos[FRAMES]; \ + \ sprite_t NAME = { \ - .image=IMAGE, \ - .regions=0, \ - .yscale=YSCALE, \ - .cfg=CFG, \ - .pos=__##NAME##_pos}; + .image = IMAGE, \ + .regions = 0, \ + .key = KEY, \ + .yscale = YSCALE, \ + .cfg = CFG, \ + .pos = __##NAME##_pos}; /* Initialise a sprite object using an existing image, creating stored regions for the animation of the sprite. - Sprite(, image_t *image, display_config_t *cfg, int frames, int yscale) + Sprite(, image_t *image, display_config_t *cfg, int frames, int key, + int yscale) */ -#define Sprite(NAME, IMAGE, CFG, FRAMES, YSCALE) \ +#define Sprite(NAME, IMAGE, CFG, FRAMES, KEY, YSCALE) \ Stored_Regions(__##NAME##_regions, FRAMES, \ (IMAGE)->width * (IMAGE)->height * (YSCALE)); \ position_t __##NAME##_pos[FRAMES]; \ + \ sprite_t NAME = { \ - .image=IMAGE, \ - .regions=&(__##NAME##_regions), \ - .yscale=YSCALE, \ - .cfg=CFG, \ - .pos=__##NAME##_pos}; + .image = IMAGE, \ + .regions = &(__##NAME##_regions), \ + .key = KEY, \ + .yscale = YSCALE, \ + .cfg = CFG, \ + .pos = __##NAME##_pos}; /* Sprite plotting operations. */ -void image_plot_sprite(sprite_t *s, int x, int y, int key); +void image_plot_sprite(sprite_t *s); void image_unplot_sprite(sprite_t *s); void image_unplot_sprite_from_image(sprite_t *s, sprite_t *bg, int xorigin, int yorigin); +/* Update operations. */ + +position_t *image_get_sprite_position(sprite_t *s); + +void image_set_sprite_position(sprite_t *s, int x, int y); + /* Convenience operations. */ void image_plot_sprite_section(sprite_t *s, diff -r ea0474d34182 -r ddb4b7db1d9b lib/image.c --- a/lib/image.c Tue Nov 13 15:17:37 2018 +0100 +++ b/lib/image.c Tue Nov 13 17:54:21 2018 +0100 @@ -39,9 +39,28 @@ +/* Get a sprite's position. */ + +position_t *image_get_sprite_position(sprite_t *s) +{ + return image_get_stored_position(s, s->cfg->frame); +} + +/* Set a sprite's position. */ + +void image_set_sprite_position(sprite_t *s, int x, int y) +{ + position_t *p = image_get_sprite_position(s); + + p->x = x; + p->y = y; +} + + + /* 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) +void image_plot_sprite(sprite_t *s) { int frame = 0; position_t *p = 0; @@ -55,22 +74,19 @@ display_copy(s->cfg, image_get_stored_region(s->regions, frame), s->image->width, s->image->height / s->yscale, 1, - x, y, -1, 0); + p->x, p->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); + p->x, p->y, s->key, 1); if (s->regions) { /* Record the stored background details. */ - p->x = x; - p->y = y; - if (frame >= s->regions->stored) s->regions->stored = frame + 1; }