1.1 --- a/examples/vga/main.c Fri Nov 09 22:50:30 2018 +0100
1.2 +++ b/examples/vga/main.c Sat Nov 10 17:02:18 2018 +0100
1.3 @@ -115,25 +115,65 @@
1.4
1.5
1.6
1.7 +/* Stored display regions. */
1.8 +
1.9 +typedef struct
1.10 +{
1.11 + /* Stored region overplotted by the sprite. */
1.12 +
1.13 + uint8_t *image;
1.14 + int stored;
1.15 +
1.16 + /* Position of the stored region. */
1.17 +
1.18 + int x, y;
1.19 +
1.20 +} stored_region_t;
1.21 +
1.22 +/* Initialise the stored regions. */
1.23 +
1.24 +void init_stored_regions(stored_region_t *r, int frames,
1.25 + uint8_t *regions, uint32_t region_size)
1.26 +{
1.27 + int frame;
1.28 +
1.29 + for (frame = 0; frame < frames; frame++)
1.30 + {
1.31 + r->stored = 0;
1.32 + r->image = regions;
1.33 + regions += region_size;
1.34 + r++;
1.35 + }
1.36 +}
1.37 +
1.38 +
1.39 +
1.40 /* Copy to the store from the display, then blit the image. */
1.41
1.42 -static void plot_sprite(uint8_t *background, int x, int y)
1.43 +static void plot_sprite(stored_region_t *r, int x, int y, int key)
1.44 {
1.45 - display_copy(&display_config, background,
1.46 + display_copy(&display_config, r->image,
1.47 sprite_width, sprite_height / SOURCE_YSTEP, 1,
1.48 x, y, -1, 0);
1.49 display_copy(&display_config, sprite,
1.50 sprite_width, sprite_height, SOURCE_YSTEP,
1.51 - x, y, 0x8c, 1);
1.52 + x, y, key, 1);
1.53 +
1.54 + /* Record the stored background. */
1.55 +
1.56 + r->x = x;
1.57 + r->y = y;
1.58 + r->stored = 1;
1.59 }
1.60
1.61 /* Copy to the display from the store, restoring the original background. */
1.62
1.63 -static void unplot_sprite(uint8_t *background, int x, int y)
1.64 +static void unplot_sprite(stored_region_t *r)
1.65 {
1.66 - display_copy(&display_config, background,
1.67 - sprite_width, sprite_height / SOURCE_YSTEP, 1,
1.68 - x, y, -1, 1);
1.69 + if (r->stored)
1.70 + display_copy(&display_config, r->image,
1.71 + sprite_width, sprite_height / SOURCE_YSTEP, 1,
1.72 + r->x, r->y, -1, 1);
1.73 }
1.74
1.75 /* Plot the revealed region at the edge of the screen after scrolling. */
1.76 @@ -211,14 +251,10 @@
1.77
1.78 static void animate(uint32_t delay)
1.79 {
1.80 - /* Stored region behind the sprite. */
1.81 -
1.82 - uint8_t background[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP];
1.83 + /* Stores of background details, replotted when moving the sprite. */
1.84
1.85 - /* Positions of the stored regions for each frame. */
1.86 -
1.87 - int background_x[FRAME_COUNT], background_y[FRAME_COUNT];
1.88 - int background_stored[FRAME_COUNT];
1.89 + uint8_t backgrounds[FRAME_COUNT][(sprite_width * sprite_height) / SOURCE_YSTEP];
1.90 + stored_region_t regions[FRAME_COUNT];
1.91
1.92 /* Sprite position. */
1.93
1.94 @@ -237,18 +273,14 @@
1.95
1.96 viewport_t v;
1.97
1.98 - /* Initialise the backing stores. */
1.99 -
1.100 - int frame;
1.101 -
1.102 - for (frame = 0; frame < display_config.frames; frame++)
1.103 - background_stored[frame] = 0;
1.104 -
1.105 /* Initialise the viewport. */
1.106
1.107 init_viewport(&v, &display_config, xorigins, yorigins,
1.108 SCROLL_XSTEP, SOURCE_YSTEP, plot_screen_edge);
1.109
1.110 + init_stored_regions(regions, display_config.frames, (uint8_t *) backgrounds,
1.111 + (sprite_width * sprite_height) / SOURCE_YSTEP);
1.112 +
1.113 /* Animation loop. */
1.114
1.115 while (1)
1.116 @@ -257,13 +289,7 @@
1.117 {
1.118 for (x = 0; x < display_config.line_length - sprite_width; x++)
1.119 {
1.120 - plot_sprite(background[display_config.frame], x, y);
1.121 -
1.122 - /* Record the stored background. */
1.123 -
1.124 - background_x[display_config.frame] = x;
1.125 - background_y[display_config.frame] = y;
1.126 - background_stored[display_config.frame] = 1;
1.127 + plot_sprite(®ions[display_config.frame], x, y, 0x8c);
1.128
1.129 /* Update the display with the frame details. */
1.130
1.131 @@ -276,10 +302,7 @@
1.132
1.133 /* Prepare the frame for updates. */
1.134
1.135 - if (background_stored[display_config.frame])
1.136 - unplot_sprite(background[display_config.frame],
1.137 - background_x[display_config.frame],
1.138 - background_y[display_config.frame]);
1.139 + unplot_sprite(®ions[display_config.frame]);
1.140
1.141 /* Scroll in the indicated direction. */
1.142