Skip to content

Optimize graphx #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
39 changes: 32 additions & 7 deletions examples/library_examples/graphx/sprites_rotate_scale/src/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#include <ti/getcsc.h>
#include <graphx.h>
#include <math.h>
#include <time.h>

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

float cosine_interpolate(float t, float min_val, float max_val) {
/* Interpolates between min_val and max_val on a cosine wave */
/* t = 0 returns min_val */
float amplitude = (min_val - max_val) / 2.0f;
float offset = (min_val + max_val) / 2.0f;
return amplitude * cosf(t) + offset;
}

int main(void)
{
uint8_t x = 0;
const int x_center = GFX_LCD_WIDTH / 2;
const int y_center = GFX_LCD_HEIGHT / 2;

/* Initialize graphics drawing */
gfx_Begin();
Expand All @@ -18,22 +29,36 @@ int main(void)
/* Draw to buffer to avoid artifacts */
gfx_SetDrawBuffer();

/* Record the start time */
const clock_t start_time = clock();

/* Rotate the sprite until a key is pressed */
do
{
do {
/* Get the elasped time in seconds */
float time_elasped = (float)(clock() - start_time) / CLOCKS_PER_SEC;

/* Complete one rotation every 5 seconds */
uint8_t rotation = fmodf(time_elasped * 256.0f / 5.0f, 256.0f);

/* Interpolates between 75% and 250% scale (64 = 100% scale) */
uint8_t scale = (uint8_t)cosine_interpolate(time_elasped, 48, 160);

/* The output size of the sprite can be calculated with this formula */
uint8_t output_size = (scale * star_width) / 64;

/* Calculate the x and y position to center the sprite */
int x_pos = x_center - output_size / 2;
int y_pos = y_center - output_size / 2;

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

/* Show the buffered screen */
gfx_BlitBuffer();

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

/* Change the rotation amount */
x++;

} while (!os_GetCSC());

/* End graphics drawing */
Expand Down
Loading
Loading