1.1 --- a/examples/vga/main.c Tue Nov 13 17:54:21 2018 +0100
1.2 +++ b/examples/vga/main.c Tue Nov 13 18:39:07 2018 +0100
1.3 @@ -112,26 +112,47 @@
1.4
1.5
1.6
1.7 +/* Update the sprite position. */
1.8 +
1.9 +static void update_sprite_position(sprite_t *s, int *xdir, int *ydir)
1.10 +{
1.11 + position_t *last = image_get_stored_position(s, wrap_value(display_config.frame - 1, display_config.frames));
1.12 + position_t *next = image_get_sprite_position(s);
1.13 +
1.14 + if (((*xdir > 0) && (last->x < display_config.line_length - s->image->width - *xdir)) ||
1.15 + ((*xdir < 0) && (last->x > -*xdir)))
1.16 + {
1.17 + next->x = last->x + *xdir;
1.18 + }
1.19 + else
1.20 + {
1.21 + next->x = last->x;
1.22 + if (*xdir)
1.23 + *xdir = -*xdir;
1.24 + }
1.25 +
1.26 + if (((*ydir > 0) && (last->y < display_config.line_count - s->image->height / s->yscale - *ydir)) ||
1.27 + ((*ydir < 0) && (last->y > -*ydir)))
1.28 + {
1.29 + next->y = last->y + *ydir;
1.30 + }
1.31 + else
1.32 + {
1.33 + next->y = last->y;
1.34 + if (*ydir)
1.35 + *ydir = -*ydir;
1.36 + }
1.37 +}
1.38 +
1.39 /* Move a sprite around on the framebuffer. */
1.40
1.41 static void animate(uint32_t delay)
1.42 {
1.43 /* Stores of background details, replotted when moving the sprite. */
1.44
1.45 - Sprite(s, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP);
1.46 -
1.47 - /* Sprite position. */
1.48 -
1.49 - int x, y;
1.50 -
1.51 - /* Scrolling directions. */
1.52 -
1.53 - int dir[] = {1, 0, -1, 0, 1};
1.54 - int dirindex = 0;
1.55 -
1.56 - /* Scroll increments. */
1.57 -
1.58 - int xdir, ydir;
1.59 + Sprite(s1, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP);
1.60 + Sprite(s2, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP);
1.61 + Sprite(s3, &sprite, &display_config, FRAME_COUNT, 0x0c, SOURCE_YSTEP);
1.62
1.63 /* Scrolling viewport. */
1.64
1.65 @@ -141,53 +162,57 @@
1.66
1.67 viewport_set_origin(&v, 0, 0);
1.68
1.69 + /* Sprite and scrolling directions. */
1.70 +
1.71 + int xdir[] = {1, -1, 0}, ydir[] = {1, 0, -1};
1.72 +
1.73 + /* Sprite positions. */
1.74 +
1.75 + image_set_sprite_position(&s1, 0, 0);
1.76 + image_set_sprite_position(&s2, display_config.line_length - s2.image->width, 0);
1.77 + image_set_sprite_position(&s3, 0, display_config.line_count - s3.image->height);
1.78 +
1.79 /* Animation loop. */
1.80
1.81 while (1)
1.82 {
1.83 - for (y = 0; y < display_config.line_count - s.image->height; y++)
1.84 - {
1.85 - for (x = 0; x < display_config.line_length - s.image->width; x++)
1.86 - {
1.87 - image_set_sprite_position(&s, x, y);
1.88 - image_plot_sprite(&s);
1.89 + image_plot_sprite(&s1);
1.90 + image_plot_sprite(&s2);
1.91 + image_plot_sprite(&s3);
1.92
1.93 - /* Update the display with the frame details. */
1.94 + /* Update the display with the frame details. */
1.95 +
1.96 + vga_set_frame(&display_config, 1);
1.97 + wait(delay);
1.98
1.99 - vga_set_frame(&display_config, 1);
1.100 - wait(delay);
1.101 + /* Select the next frame to plot to. */
1.102
1.103 - /* Select the next frame to plot to. */
1.104 + display_select_next_frame(&display_config);
1.105
1.106 - display_select_next_frame(&display_config);
1.107 -
1.108 - /* Prepare the frame for updates. */
1.109 + /* Prepare the frame for updates. */
1.110
1.111 - viewport_unplot_sprite_from_image(&v, &s, &scr);
1.112 -
1.113 - /* Scroll in the indicated direction. */
1.114 + viewport_unplot_sprite_from_image(&v, &s1, &scr);
1.115 + viewport_unplot_sprite_from_image(&v, &s2, &scr);
1.116 + viewport_unplot_sprite_from_image(&v, &s3, &scr);
1.117
1.118 - xdir = dir[dirindex];
1.119 - ydir = dir[dirindex + 1];
1.120 -
1.121 - /* Update the origin if appropriate. */
1.122 + /* Update the origin to scroll the screen. */
1.123
1.124 - /* Due to the effect of a simple screen start increment in the
1.125 - dual channel configuration, horizontal scrolling involves two
1.126 - pixel increments and thus requires a two-pixel column to be
1.127 - plotted per scrolling increment. */
1.128 + /* Due to the effect of a simple screen start increment in the
1.129 + dual channel configuration, horizontal scrolling involves two
1.130 + pixel increments and thus requires a two-pixel column to be
1.131 + plotted per scrolling increment. */
1.132
1.133 - /* For vertically-scaled backgrounds, the full resolution image
1.134 - is traversed by multiples of the scrolling increment. */
1.135 + /* For vertically-scaled backgrounds, the full resolution image
1.136 + is traversed by multiples of the scrolling increment. */
1.137 +
1.138 + viewport_update_origin(&v, xdir[0] * SCROLL_XSTEP,
1.139 + ydir[0] * SOURCE_YSTEP);
1.140
1.141 - viewport_update_origin(&v, xdir * SCROLL_XSTEP,
1.142 - ydir * SOURCE_YSTEP);
1.143 - }
1.144 + /* Update the position of the sprite being plotted. */
1.145
1.146 - /* Switch direction periodically. */
1.147 -
1.148 - dirindex = wrap_value(dirindex + 1, 4);
1.149 - }
1.150 + update_sprite_position(&s1, &xdir[0], &ydir[0]);
1.151 + update_sprite_position(&s2, &xdir[1], &ydir[1]);
1.152 + update_sprite_position(&s3, &xdir[2], &ydir[2]);
1.153 }
1.154 }
1.155
1.156 @@ -292,7 +317,7 @@
1.157 /* Move a sprite around on the screen with a delay between each movement. */
1.158
1.159 setup();
1.160 - animate(1 << 18);
1.161 + animate(1);
1.162 }
1.163
1.164