Skip to content

Add clipped sprite rotation #621

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
obj/
bin/
src/gfx/*.c
src/gfx/*.h
src/gfx/*.8xv
.DS_Store
convimg.yaml.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"transfer_files":
[
"bin/DEMO.8xp"
],
"target":
{
"name": "DEMO",
"isASM": true
},
"sequence":
[
"action|launch",
"delay|500",
"key|enter",
"delay|100",
"hashWait|1"
],
"hashes":
{
"1":
{
"description": "Test program exit",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [ "FFAF89BA", "101734A5", "9DA19F44", "A32840C8", "349F4775" ]
}
}
}
15 changes: 15 additions & 0 deletions examples/library_examples/graphx/sprites_rotate_clipped/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ----------------------------
# Makefile Options
# ----------------------------

NAME = DEMO
ICON = icon.png
DESCRIPTION = "CE C Toolchain Demo"
COMPRESSED = NO

CFLAGS = -Wall -Wextra -Oz
CXXFLAGS = -Wall -Wextra -Oz

# ----------------------------

include $(shell cedev-config --makefile)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Clipped Rotation Demo

Demonstrates the clipped/unclipped and opaque/transparent rotated sprite routines.

![Screenshot](screenshot.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
palettes:
- name: global_palette
fixed-entries:
- color: {index: 0, r: 255, g: 0, b: 128}
- color: {index: 1, r: 255, g: 255, b: 255}
images: automatic

converts:
- name: sprites
palette: global_palette
transparent-color-index: 0
images:
- star.png

outputs:
- type: c
include-file: gfx.h
palettes:
- global_palette
converts:
- sprites


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions examples/library_examples/graphx/sprites_rotate_clipped/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <ti/getcsc.h>
#include <graphx.h>
#include <math.h>
#include <time.h>

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

int main(void)
{
/* Center of the screen */
const int x_center = GFX_LCD_WIDTH / 2;
const int y_center = GFX_LCD_HEIGHT / 2;

/* Parameters for the clipping box */
const int box_width = 80;
const int box_height = 80;
const float box_pivot_radius = 20.0f;

/* The positions of the 4 sprites on the screen */
const int distance_from_center = 30;
const int pos_left = x_center - distance_from_center - (star_width / 2);
const int pos_right = x_center + distance_from_center - (star_width / 2);
const int pos_top = y_center - distance_from_center - (star_height / 2);
const int pos_bottom = y_center + distance_from_center - (star_height / 2);

/* Initialize graphics drawing */
gfx_Begin();

/* Set the palette used by the sprites */
gfx_SetPalette(global_palette, sizeof_global_palette, 0);
gfx_SetTransparentColor(0);

/* Draw to buffer to avoid artifacts */
gfx_SetDrawBuffer();

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

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

/* Calculate the clipping region */
int x_offset = cosf(time_elasped) * box_pivot_radius;
int y_offset = sinf(time_elasped) * box_pivot_radius;
int box_left = x_center - (box_width / 2) + x_offset;
int box_right = x_center + (box_width / 2) + x_offset;
int box_top = y_center - (box_height / 2) + y_offset;
int box_bottom = y_center + (box_height / 2) + y_offset;
gfx_SetClipRegion(box_left, box_top, box_right, box_bottom);

/* Draw a rectangle to indicate the clipping region */
gfx_SetColor(2);
gfx_Rectangle_NoClip(box_left - 1, box_top - 1, box_width + 2, box_height + 2);

/* Complete one rotation every 6 seconds */
uint8_t sprite_angle = fmodf(time_elasped * 256.0f / 6.0f, 256.0f);

/* Draws an unclipped rotated sprite */
gfx_RotatedSprite_NoClip(star, pos_left, pos_bottom, sprite_angle);

/* Draws a rotated sprite that is clipped to the rectangular box */
gfx_RotatedSprite(star, pos_right, pos_bottom, sprite_angle);

/* Draws an unclipped rotated sprite with transparency */
gfx_RotatedTransparentSprite_NoClip(star, pos_right, pos_top, sprite_angle);

/* Draws a rotated sprite with transparency that is clipped to the rectangular box */
gfx_RotatedTransparentSprite(star, pos_left, pos_top, sprite_angle);

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

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

} while (!os_GetCSC());

/* End graphics drawing */
gfx_End();

return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ int main(void)
/* 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);
/* Interpolates between 75% and 225% scale (64 = 100% scale) */
uint8_t scale = (uint8_t)cosine_interpolate(time_elasped, 48, 144);

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