# HG changeset patch # User Paul Boddie # Date 1541865738 -3600 # Node ID 39834d788dcc06bbb37cfe1be562d41c3a7d3c56 # Parent 9e84b8d477a3cf4af12ebf3d1a8c76bc3dbf26fb Introduced an abstraction for stored screen region details. diff -r 9e84b8d477a3 -r 39834d788dcc examples/vga/main.c --- a/examples/vga/main.c Fri Nov 09 22:50:30 2018 +0100 +++ b/examples/vga/main.c Sat Nov 10 17:02:18 2018 +0100 @@ -115,25 +115,65 @@ +/* Stored display regions. */ + +typedef struct +{ + /* Stored region overplotted by the sprite. */ + + uint8_t *image; + int stored; + + /* Position of the stored region. */ + + int x, y; + +} stored_region_t; + +/* Initialise the stored regions. */ + +void init_stored_regions(stored_region_t *r, int frames, + uint8_t *regions, uint32_t region_size) +{ + int frame; + + for (frame = 0; frame < frames; frame++) + { + r->stored = 0; + r->image = regions; + regions += region_size; + r++; + } +} + + + /* Copy to the store from the display, then blit the image. */ -static void plot_sprite(uint8_t *background, int x, int y) +static void plot_sprite(stored_region_t *r, int x, int y, int key) { - display_copy(&display_config, background, + display_copy(&display_config, r->image, sprite_width, sprite_height / SOURCE_YSTEP, 1, x, y, -1, 0); display_copy(&display_config, sprite, sprite_width, sprite_height, SOURCE_YSTEP, - x, y, 0x8c, 1); + x, y, key, 1); + + /* Record the stored background. */ + + r->x = x; + r->y = y; + r->stored = 1; } /* Copy to the display from the store, restoring the original background. */ -static void unplot_sprite(uint8_t *background, int x, int y) +static void unplot_sprite(stored_region_t *r) { - display_copy(&display_config, background, - sprite_width, sprite_height / SOURCE_YSTEP, 1, - x, y, -1, 1); + if (r->stored) + display_copy(&display_config, r->image, + sprite_width, sprite_height / SOURCE_YSTEP, 1, + r->x, r->y, -1, 1); } /* Plot the revealed region at the edge of the screen after scrolling. */ @@ -211,14 +251,10 @@ static void animate(uint32_t delay) { - /* Stored region behind the sprite. */ - - uint8_t background[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP]; + /* Stores of background details, replotted when moving the sprite. */ - /* Positions of the stored regions for each frame. */ - - int background_x[FRAME_COUNT], background_y[FRAME_COUNT]; - int background_stored[FRAME_COUNT]; + uint8_t backgrounds[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP]; + stored_region_t regions[FRAME_COUNT]; /* Sprite position. */ @@ -237,18 +273,14 @@ viewport_t v; - /* Initialise the backing stores. */ - - int frame; - - for (frame = 0; frame < display_config.frames; frame++) - background_stored[frame] = 0; - /* Initialise the viewport. */ init_viewport(&v, &display_config, xorigins, yorigins, SCROLL_XSTEP, SOURCE_YSTEP, plot_screen_edge); + init_stored_regions(regions, display_config.frames, (uint8_t *) backgrounds, + (sprite_width * sprite_height) / SOURCE_YSTEP); + /* Animation loop. */ while (1) @@ -257,13 +289,7 @@ { for (x = 0; x < display_config.line_length - sprite_width; x++) { - plot_sprite(background[display_config.frame], x, y); - - /* Record the stored background. */ - - background_x[display_config.frame] = x; - background_y[display_config.frame] = y; - background_stored[display_config.frame] = 1; + plot_sprite(®ions[display_config.frame], x, y, 0x8c); /* Update the display with the frame details. */ @@ -276,10 +302,7 @@ /* Prepare the frame for updates. */ - if (background_stored[display_config.frame]) - unplot_sprite(background[display_config.frame], - background_x[display_config.frame], - background_y[display_config.frame]); + unplot_sprite(®ions[display_config.frame]); /* Scroll in the indicated direction. */