Add frame tick system for continuous animation
- Added INPUT_FRAME_TICK event type to input_event.h - Added wants_frame_updates() virtual method to Game base class - Implemented frame tick logic in main loop (basic1.cpp and emulator/main.cpp) - Added Lua bindings: game.set_frame_updates(bool) and INPUT.FRAME_TICK - Updated LuaGame to support frame updates via registry flag - Updated ball.lua to use continuous frame updates for smooth animation - Both hardware and emulator now support continuous animation for physics/games
This commit is contained in:
@@ -186,6 +186,21 @@ static int lua_game_exit(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// game.set_frame_updates(enabled) - enable/disable continuous frame tick events
|
||||
static int lua_game_set_frame_updates(lua_State* L) {
|
||||
LuaGame* game = get_game(L);
|
||||
if (!game) return 0;
|
||||
|
||||
bool enabled = lua_toboolean(L, 1);
|
||||
|
||||
// Set frame updates flag (will be checked in wants_frame_updates())
|
||||
lua_pushstring(L, "__wants_frame_updates");
|
||||
lua_pushboolean(L, enabled);
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// INPUT TYPE CONSTANTS
|
||||
// ============================================================================
|
||||
@@ -283,6 +298,10 @@ void lua_bindings_register(lua_State* L, LuaGame* game) {
|
||||
lua_pushcfunction(L, lua_game_exit);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushstring(L, "set_frame_updates");
|
||||
lua_pushcfunction(L, lua_game_set_frame_updates);
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Create empty vars table for persistent state
|
||||
lua_pushstring(L, "vars");
|
||||
lua_newtable(L);
|
||||
|
||||
@@ -14,6 +14,9 @@ function init()
|
||||
game.vars.radius = 10
|
||||
game.vars.frame_count = 0
|
||||
|
||||
-- Enable continuous frame updates for smooth animation
|
||||
game.set_frame_updates(true)
|
||||
|
||||
print("Bouncing Ball initialized")
|
||||
end
|
||||
|
||||
@@ -28,8 +31,8 @@ function update(event)
|
||||
return true
|
||||
end
|
||||
|
||||
-- Update physics if running
|
||||
if game.vars.state == STATE_RUNNING then
|
||||
-- Update physics if running (on any frame tick)
|
||||
if event.type == INPUT.FRAME_TICK and game.vars.state == STATE_RUNNING then
|
||||
-- Move ball
|
||||
game.vars.ball_x = game.vars.ball_x + game.vars.vel_x
|
||||
game.vars.ball_y = game.vars.ball_y + game.vars.vel_y
|
||||
|
||||
@@ -193,6 +193,18 @@ bool LuaGame::wants_to_exit() const {
|
||||
return exit;
|
||||
}
|
||||
|
||||
bool LuaGame::wants_frame_updates() const {
|
||||
if (!L) return false;
|
||||
|
||||
// Check if Lua script wants continuous frame updates
|
||||
lua_pushstring(L, "__wants_frame_updates");
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
bool wants_updates = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
return wants_updates;
|
||||
}
|
||||
|
||||
bool LuaGame::call_lua_function(const char* func_name, int nargs, int nresults) {
|
||||
int result = lua_pcall(L, nargs, nresults, 0);
|
||||
if (result != LUA_OK) {
|
||||
|
||||
@@ -64,6 +64,12 @@ public:
|
||||
*/
|
||||
bool wants_to_exit() const override;
|
||||
|
||||
/**
|
||||
* @brief Check if game wants continuous frame updates
|
||||
* @return true if Lua script set __wants_frame_updates flag
|
||||
*/
|
||||
bool wants_frame_updates() const override;
|
||||
|
||||
/**
|
||||
* @brief Get Lua state for bindings access
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user