Skip to content

Commit 13b8b39

Browse files
committed
Updated the sprites_rotate_scale demo
1 parent ce16bd5 commit 13b8b39

File tree

1 file changed

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

1 file changed

+32
-7
lines changed

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
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;
1021

1122
/* Initialize graphics drawing */
1223
gfx_Begin();
@@ -18,22 +29,36 @@ int main(void)
1829
/* Draw to buffer to avoid artifacts */
1930
gfx_SetDrawBuffer();
2031

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

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

2856
/* Show the buffered screen */
2957
gfx_BlitBuffer();
3058

3159
/* Clear the old drawn sprite */
3260
gfx_FillScreen(1);
3361

34-
/* Change the rotation amount */
35-
x++;
36-
3762
} while (!os_GetCSC());
3863

3964
/* End graphics drawing */

0 commit comments

Comments
 (0)