Skip to content

Commit 3787862

Browse files
Add framerate limiting example
1 parent 43daf28 commit 3787862

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
obj/
2+
bin/
3+
src/gfx/*.c
4+
src/gfx/*.h
5+
.DS_Store
6+
convimg.out
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
11+
CFLAGS = -Wall -Wextra -Oz
12+
CXXFLAGS = -Wall -Wextra -Oz
13+
14+
# ----------------------------
15+
16+
include $(shell cedev-config --makefile)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Framerate Limiting
2+
3+
This demo demonstrates using `clock()` to limit the framerate of a program.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <ti/screen.h>
2+
#include <ti/getcsc.h>
3+
#include <time.h>
4+
#include <debug.h>
5+
6+
/* The framerate to limit to */
7+
#define TARGET_FRAMERATE 20
8+
9+
/* The (approximate) amount of time each frame should take, in clock ticks */
10+
#define TARGET_FRAME_TIME (CLOCKS_PER_SEC / TARGET_FRAMERATE)
11+
12+
/**
13+
* Draws a spinner to the top-left corner of the screen.
14+
*/
15+
static void draw_spinner(void)
16+
{
17+
static int char_num = 0;
18+
const char characters[] = {'|', '/', '-', '\\'};
19+
20+
char character = characters[char_num];
21+
22+
/* Convert the character into a null-terminated string. */
23+
char str[2] = { character, '\0' };
24+
25+
/* Draw the string to the screen. */
26+
os_SetCursorPos(0, 0);
27+
os_PutStrFull(str);
28+
29+
/* Move to the next character, looping back to the first when
30+
we reach the end. */
31+
char_num = (char_num + 1) % 4;
32+
}
33+
34+
int main(void)
35+
{
36+
/* Clear the homescreen */
37+
os_ClrHome();
38+
39+
/* Wait for a key */
40+
while (!os_GetCSC()) {
41+
/* Get the time that the frame starts. */
42+
clock_t frame_start = clock();
43+
44+
/* Do graphical stuff. */
45+
draw_spinner();
46+
47+
/* Get how much time has elapsed since the start of the frame. */
48+
clock_t frame_time = clock() - frame_start;
49+
50+
if (frame_time > TARGET_FRAME_TIME) {
51+
dbg_printf("Warning! Frame took too long (%li clock ticks). "
52+
"The maximum frame time for %u FPS is %u clock ticks.\n",
53+
frame_time, TARGET_FRAMERATE, TARGET_FRAME_TIME);
54+
}
55+
56+
/* Wait for at least TARGET_FRAME_TIME to have passed since
57+
the start of the frame. */
58+
do {
59+
frame_time = clock() - frame_start;
60+
} while (frame_time < TARGET_FRAME_TIME);
61+
62+
/* Note that there should not be any code below the above loop,
63+
since it would not be counted towards the frame time. */
64+
}
65+
66+
return 0;
67+
}

0 commit comments

Comments
 (0)