Skip to content

Commit 9a5609c

Browse files
committed
Updated the sprites_rotate_scale demo
1 parent 3c3c112 commit 9a5609c

File tree

1 file changed

+32
-6
lines changed
  • examples/library_examples/graphx/sprites_rotate_scale/src

1 file changed

+32
-6
lines changed

examples/library_examples/graphx/sprites_rotate_scale/src/main.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
#include <ti/getcsc.h>
22
#include <graphx.h>
3+
#include <math.h>
4+
#include <time.h>
35

46
/* Include the converted image data */
57
#include "gfx/gfx.h"
68

9+
float cosine_interpolate(float t, float min_val, float max_val) {
10+
/* Interpolates between min_val and max_val on a cosine wave */
11+
/* t = 0 returns min_val */
12+
float amplitude = (min_val - max_val) / 2.0f;
13+
float offset = (min_val + max_val) / 2.0f;
14+
return amplitude * cosf(t) + offset;
15+
}
16+
717
int main(void)
818
{
9-
uint8_t x = 0;
19+
const int x_center = GFX_LCD_WIDTH / 2;
20+
const int y_center = GFX_LCD_HEIGHT / 2;
21+
uint8_t rotation = 0;
1022

1123
/* Initialize graphics drawing */
1224
gfx_Begin();
@@ -18,21 +30,35 @@ int main(void)
1830
/* Draw to buffer to avoid artifacts */
1931
gfx_SetDrawBuffer();
2032

33+
/* Record the start time */
34+
const clock_t start_time = clock();
35+
2136
/* Rotate the sprite until a key is pressed */
22-
do
23-
{
37+
do {
38+
/* Get the elasped time in seconds */
39+
float time_elasped = (float)(clock() - start_time) / CLOCKS_PER_SEC;
40+
41+
/* Interpolates between 75% and 200% scale (64 = 100% scale) */
42+
uint8_t scale = (uint8_t)cosine_interpolate(time_elasped, 48, 160);
43+
44+
/* The output size of the sprite can be calculated with this formula */
45+
uint8_t output_size = (scale * star_width) / 64;
46+
47+
/* Calculate the x and y position to center the sprite */
48+
int x_pos = x_center - output_size / 2;
49+
int y_pos = y_center - output_size / 2;
2450

2551
/* Draw a rotated transparent scaled spite */
26-
gfx_RotatedScaledTransparentSprite_NoClip(star, 120, 80, 256 - x, 128);
52+
gfx_RotatedScaledTransparentSprite_NoClip(star, x_pos, y_pos, rotation, scale);
2753

2854
/* Show the buffered screen */
2955
gfx_BlitBuffer();
3056

3157
/* Clear the old drawn sprite */
3258
gfx_FillScreen(1);
33-
59+
3460
/* Change the rotation amount */
35-
x++;
61+
rotation += 3;
3662

3763
} while (!os_GetCSC());
3864

0 commit comments

Comments
 (0)