Core Native GBA 0.0.17
create your own game-engine with just lua for nitendo game boy advance console.
Loading...
Searching...
No Matches
runtime.c
Go to the documentation of this file.
1#include "zeebo.h"
2
3#ifndef FPS_MODE
4/**
5 * @short fps configure
6 * @brief lower FPS in heavier games ensures frame stability.
7 * @details use flag @c -DFPS_MODE=2 in cmake to configure frameskip.
8 * @li @b 0 loop 60 fps / draw 60 fps
9 * @li @b 1 loop 60 fps / draw 30 fps
10 * @li @b 2 loop 30 fps / draw 30 fps
11 * @li @b 3 loop 20 fps / draw 20 fps
12 * @li @b 4 loop 20 fps / draw 10 fps
13 */
14#define FPS_MODE 2
15#endif
16
17#ifndef FPS_DELTA
18#if FPS_MODE <= 1
19#define FPS_DELTA 16
20#elif FPS_MODE <= 3
21#define FPS_DELTA 33
22#elif FPS_MODE <= 4
23#define FPS_DELTA 50
24#else
25#error invalid FPS_MODE
26#endif
27#endif
28
29void runtime(lua_State* L)
30{
31 lua_getglobal(L, "native_callback_loop");
32 int native_callback_loop = luaL_ref(L, LUA_REGISTRYINDEX);
33
34 lua_getglobal(L, "native_callback_draw");
35 int native_callback_draw = luaL_ref(L, LUA_REGISTRYINDEX);
36
37 lua_getglobal(L, "native_callback_keyboard");
38 int native_callback_keyboard = luaL_ref(L, LUA_REGISTRYINDEX);
39
40 static uint8_t page = 0;
41 static uint8_t frameskip = 0;
42
43#if FPS_MODE == 0
44 while (1) {
45 while(*(volatile uint16_t*) 0x04000006 >= 160);
46 keys_callback_update(L, native_callback_keyboard);
47 loop_callback_update(L, native_callback_loop, FPS_DELTA);
48 draw_queue_page(page++);
49 draw_callback_update(L, native_callback_draw);
50 while(*(volatile uint16_t*) 0x04000006 < 160);
51 draw_cmd_mode(0, 1, 0, 1);
52 draw_queue_burn(page++);
53 draw_cmd_mode(0, 0, 0, 1);
54 draw_queue_burn(page++);
55 }
56#elif FPS_MODE == 1
57 while (1) {
58 while(*(volatile uint16_t*) 0x04000006 >= 160);
59 while(*(volatile uint16_t*) 0x04000006 < 160);
60 keys_callback_update(L, native_callback_keyboard);
61 loop_callback_update(L, native_callback_loop, FPS_DELTA);
62 while(*(volatile uint16_t*) 0x04000006 >= 160);
63 while(*(volatile uint16_t*) 0x04000006 < 160);
64 if (++frameskip & 1) {
65 draw_queue_page(page++);
66 draw_callback_update(L, native_callback_draw);
67 } else {
68 draw_cmd_mode(0, 1, 0, 1);
69 draw_queue_burn(page++);
70 draw_cmd_mode(0, 0, 0, 1);
71 draw_queue_burn(page++);
72 }
73 }
74#elif FPS_MODE == 2
75 while (1) {
76 while(*(volatile uint16_t*) 0x04000006 >= 160);
77 while(*(volatile uint16_t*) 0x04000006 < 160);
78 keys_callback_update(L, native_callback_keyboard);
79 loop_callback_update(L, native_callback_loop, FPS_DELTA);
80 draw_queue_page(page++);
81 draw_callback_update(L, native_callback_draw);
82 while(*(volatile uint16_t*) 0x04000006 >= 160);
83 while(*(volatile uint16_t*) 0x04000006 < 160);
84 draw_cmd_mode(0, 1, 0, 1);
85 draw_queue_burn(page++);
86 draw_cmd_mode(0, 0, 0, 1);
87 draw_queue_burn(page++);
88 }
89#elif FPS_MODE == 3
90 while (1) {
91 while(*(volatile uint16_t*) 0x04000006 >= 160);
92 while(*(volatile uint16_t*) 0x04000006 < 160);
93 keys_callback_update(L, native_callback_keyboard);
94 loop_callback_update(L, native_callback_loop, FPS_DELTA);
95 while(*(volatile uint16_t*) 0x04000006 >= 160);
96 while(*(volatile uint16_t*) 0x04000006 < 160);
97 draw_queue_page(page++);
98 draw_callback_update(L, native_callback_draw);
99 while(*(volatile uint16_t*) 0x04000006 >= 160);
100 while(*(volatile uint16_t*) 0x04000006 < 160);
101 draw_cmd_mode(0, 1, 0, 1);
102 draw_queue_burn(page++);
103 draw_cmd_mode(0, 0, 0, 1);
104 draw_queue_burn(page++);
105 }
106#elif FPS_MODE == 4
107 while (1) {
108 while(*(volatile uint16_t*) 0x04000006 >= 160);
109 while(*(volatile uint16_t*) 0x04000006 < 160);
110 keys_callback_update(L, native_callback_keyboard);
111 loop_callback_update(L, native_callback_loop, FPS_DELTA);
112 while(*(volatile uint16_t*) 0x04000006 >= 160);
113 while(*(volatile uint16_t*) 0x04000006 < 160);
114 if (++frameskip & 1) {
115 draw_queue_page(page++);
116 draw_callback_update(L, native_callback_draw);
117 }
118 else {
119 draw_cmd_mode(0, 1, 0, 1);
120 draw_queue_burn(page++);
121 draw_cmd_mode(0, 0, 0, 1);
122 draw_queue_burn(page++);
123 }
124 while(*(volatile uint16_t*) 0x04000006 >= 160);
125 while(*(volatile uint16_t*) 0x04000006 < 160);
126 }
127#else
128#error invalid FPS_MODE
129#endif
130}
void draw_callback_update(lua_State *L, int callback)
Definition draw.c:98
void draw_cmd_mode(uint8_t drawmode, uint8_t flushmode, uint8_t change_mode, uint8_t change_flush)
Definition draw_color.c:40
void draw_queue_burn(uint8_t page)
Definition draw_queue.c:50
void draw_queue_page(uint8_t page)
Definition draw_queue.c:36
void keys_callback_update(lua_State *L, int callback)
Definition keys.c:31
void loop_callback_update(lua_State *L, int callback, uint8_t dt)
Definition loop.c:3
#define FPS_DELTA
Definition runtime.c:21
void runtime(lua_State *L)
Definition runtime.c:29