Files
basic1/games/lua_game.h
Adolfo Reyna e6e4eca188 Add Lua 5.4 scripting integration for dynamic game loading
- Integrated Lua 5.4 engine (32-bit mode for embedded ARM)
- Created LuaGame wrapper class implementing Game interface
- Added C++ bindings exposing renderer, game state, and input to Lua
- Implemented SD card loader for automatic .lua game discovery
- Updated GameLauncher to support std::function for lambda captures
- Made Game class members public for Lua bindings access
- Added example Lua games: counter, snake, bouncing ball
- Included comprehensive API documentation

Games can now be written as .lua text files on SD card and loaded
without recompilation. Build size: 747KB UF2, Lua VM uses ~50-80KB RAM.
2026-02-07 11:56:03 -05:00

110 lines
3.0 KiB
C++

// ============================================================================
// LUA GAME WRAPPER - HEADER
// ============================================================================
// Allows Lua scripts to implement the Game interface
#ifndef LUA_GAME_H
#define LUA_GAME_H
#include "game.h"
#include <string>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/**
* @brief Game implementation that runs Lua scripts
*
* Loads a .lua file from SD card and calls its init(), update(), and draw()
* functions. The Lua script has access to drawing primitives, input events,
* and persistent state variables.
*/
class LuaGame : public Game {
public:
/**
* @brief Construct a new Lua Game
* @param script_path Path to .lua file on SD card (e.g., "/games/pong.lua")
* @param width Display width in pixels
* @param height Display height in pixels
* @param renderer Pointer to low-level rendering interface
* @param gui Pointer to GUI drawing primitives
* @param input_manager Pointer to input manager
*/
LuaGame(const char* script_path, uint16_t width, uint16_t height,
LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager);
/**
* @brief Destructor - clean up Lua state
*/
~LuaGame() override;
/**
* @brief Initialize game - calls Lua init() function
*/
void init() override;
/**
* @brief Update game logic - calls Lua update(event) function
* @param event Input event from InputManager
* @return true if screen redraw is needed
*/
bool update(const InputEvent& event) override;
/**
* @brief Draw the game - calls Lua draw() function
*/
void draw() override;
/**
* @brief Check if game wants to exit
* @return true if Lua script requested exit
*/
bool wants_to_exit() const override;
/**
* @brief Get Lua state for bindings access
*/
lua_State* get_lua_state() { return L; }
/**
* @brief Check if Lua script loaded successfully
*/
bool is_loaded() const { return loaded; }
/**
* @brief Get last error message if load failed
*/
const char* get_error() const { return error_message.c_str(); }
private:
lua_State* L;
std::string script_path;
std::string error_message;
bool loaded;
/**
* @brief Load and execute Lua script file from SD card
* @return true if successful, false on error
*/
bool load_script();
/**
* @brief Call a Lua function safely with error handling
* @param func_name Name of Lua function to call
* @param nargs Number of arguments on stack
* @param nresults Number of expected return values
* @return true if successful, false on error
*/
bool call_lua_function(const char* func_name, int nargs = 0, int nresults = 0);
/**
* @brief Report Lua error to console and store message
*/
void report_error(const char* context);
};
#endif // LUA_GAME_H