Files
basic1/lib/game.h
T
Adolfo Reyna 2a472fc29f 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
2026-02-07 13:20:10 -05:00

126 lines
3.9 KiB
C++

// ============================================================================
// ABSTRACT GAME BASE CLASS
// ============================================================================
// Defines the interface all games must implement
// Provides polymorphic game architecture for modular design
#ifndef GAME_H
#define GAME_H
#include <stdint.h>
#include "input_event.h"
#include "../display/low_level_render.h"
#include "../display/low_level_gui.h"
// Forward declaration
class InputManager;
/**
* @brief Abstract base class for all games
*
* Games inherit from this class and implement the three core methods:
* - init(): Set up initial game state
* - update(): Process input events and update game logic
* - draw(): Render the game to the display buffer
*
* The main loop in basic1.cpp calls these methods polymorphically,
* allowing different games to run with the same infrastructure.
*/
class Game {
public:
enum class Type {
BASE,
MONOPOLY_DICE,
MONOPOLY_PROPERTY,
MONOPOLY_BOARD,
MONOPOLY_CHANCE,
MONOPOLY_COMMUNITY_CHEST,
MONOPOLY_TURN,
MONOPOLY_PAYMENT
};
/**
* @brief Construct a new Game
* @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 for capability queries
*/
Game(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager)
: width(width), height(height), renderer(renderer), gui(gui), input_manager(input_manager) {
}
/**
* @brief Virtual destructor for proper cleanup of derived classes
*/
virtual ~Game() {}
/**
* @brief Initialize game state
*
* Called once when the game starts. Set up initial values,
* reset statistics, prepare the game board, etc.
*/
virtual void init() = 0;
/**
* @brief Update game state based on input event
*
* Process the input event and update game logic accordingly.
* This is where game rules, win conditions, state transitions happen.
*
* @param event Input event from InputManager
* @return true if screen redraw is needed, false otherwise
*/
virtual bool update(const InputEvent& event) = 0;
/**
* @brief Draw the game to the display buffer
*
* Render all game graphics using the renderer and gui objects.
* This is called after update() returns true.
* The actual screen refresh happens in the main loop on Core 1.
*/
virtual void draw() = 0;
/**
* @brief Check if game wants to exit back to launcher
*
* Games can override this to signal they want to return to the menu.
* Default implementation returns false.
*
* @return true if game wants to exit, false to continue playing
*/
virtual bool wants_to_exit() const { return false; }
/**
* @brief Check if game wants continuous frame updates
*
* Games that need animation or continuous updates (like physics simulations)
* can override this to return true. They will receive INPUT_FRAME_TICK events
* every frame, even without user input.
*
* @return true if game needs frame updates, false for event-driven only
*/
virtual bool wants_frame_updates() const { return false; }
/**
* @brief Get the type of game for safe downcasting without RTTI
*/
virtual Type get_type() const { return Type::BASE; }
// Display dimensions (public for Lua bindings)
uint16_t width;
uint16_t height;
// Rendering interfaces (provided by basic1.cpp, public for Lua bindings)
LowLevelRenderer* renderer;
LowLevelGUI* gui;
// Input manager for capability queries
InputManager* input_manager;
};
#endif // GAME_H