132 lines
4.3 KiB
C++
132 lines
4.3 KiB
C++
// ============================================================================
|
|
// GAME LAUNCHER IMPLEMENTATION
|
|
// ============================================================================
|
|
// Menu system for selecting and launching games
|
|
|
|
#include "game_launcher.h"
|
|
#include "display/low_level_render.h"
|
|
#include "display/low_level_gui.h"
|
|
#include <stdio.h>
|
|
|
|
GameLauncher::GameLauncher(uint16_t width, uint16_t height,
|
|
LowLevelRenderer* renderer, LowLevelGUI* gui)
|
|
: width(width), height(height), renderer(renderer), gui(gui),
|
|
selected_index(0), selected_game(nullptr) {
|
|
}
|
|
|
|
void GameLauncher::register_game(const char* name, const char* description,
|
|
Game* (*factory)(uint16_t, uint16_t, LowLevelRenderer*, LowLevelGUI*)) {
|
|
GameEntry entry;
|
|
entry.name = name;
|
|
entry.description = description;
|
|
entry.factory = factory;
|
|
games.push_back(entry);
|
|
|
|
printf("Registered game: %s - %s\n", name, description);
|
|
}
|
|
|
|
void GameLauncher::draw() {
|
|
// Draw main window
|
|
gui->draw_new_window(10, 10, width - 20, height - 20, "Game Launcher");
|
|
|
|
// Draw title
|
|
renderer->draw_string(30, 30, "Select a Game:", true);
|
|
|
|
// Draw game list
|
|
for (size_t i = 0; i < games.size(); i++) {
|
|
int y = MENU_Y_START + (i * MENU_ITEM_HEIGHT);
|
|
|
|
// Highlight selected item
|
|
if ((int)i == selected_index) {
|
|
// Draw selection box
|
|
renderer->draw_rectangle(20, y - 5, width - 40, MENU_ITEM_HEIGHT - 10, true, 2);
|
|
renderer->draw_string(30, y + 2, ">", true);
|
|
}
|
|
|
|
// Draw game name
|
|
renderer->draw_string(45, y + 2, games[i].name, true);
|
|
|
|
// Draw description (smaller, below name)
|
|
renderer->draw_string(45, y + 15, games[i].description, true);
|
|
}
|
|
|
|
// Draw instructions at bottom
|
|
const char* instructions;
|
|
#ifdef BUTTON_KEY0_PIN
|
|
instructions = "Touch game or use KEY0/KEY1";
|
|
#else
|
|
instructions = "Touch game to play";
|
|
#endif
|
|
renderer->draw_string(30, height - 35, instructions, true);
|
|
}
|
|
|
|
bool GameLauncher::update(const InputEvent& event) {
|
|
bool needs_refresh = false;
|
|
|
|
switch (event.type) {
|
|
case INPUT_TOUCH_DOWN: {
|
|
printf("Touch at (%d,%d) in launcher\n", event.x, event.y);
|
|
|
|
// Check if touch is on a game entry
|
|
for (size_t i = 0; i < games.size(); i++) {
|
|
int y = MENU_Y_START + (i * MENU_ITEM_HEIGHT);
|
|
|
|
// Touch area is the entire menu item
|
|
if (event.y >= y - 5 && event.y < y + MENU_ITEM_HEIGHT - 5) {
|
|
// Game selected - create instance
|
|
printf("Selected game: %s\n", games[i].name);
|
|
selected_game = games[i].factory(width, height, renderer, gui);
|
|
if (selected_game) {
|
|
selected_game->init();
|
|
return true; // Signal game selected
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
case INPUT_BUTTON_0: {
|
|
// Navigate menu
|
|
if (games.size() > 0) {
|
|
selected_index = (selected_index + 1) % games.size();
|
|
needs_refresh = true;
|
|
printf("Menu selection: %d (%s)\n", selected_index, games[selected_index].name);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case INPUT_BUTTON_1: {
|
|
// Select current game
|
|
if (selected_index >= 0 && selected_index < (int)games.size()) {
|
|
printf("Selected game: %s\n", games[selected_index].name);
|
|
selected_game = games[selected_index].factory(width, height, renderer, gui);
|
|
if (selected_game) {
|
|
selected_game->init();
|
|
return true; // Signal game selected
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return needs_refresh;
|
|
}
|
|
|
|
Game* GameLauncher::get_selected_game() {
|
|
return selected_game;
|
|
}
|
|
|
|
void GameLauncher::reset() {
|
|
// Clean up current game if any
|
|
if (selected_game) {
|
|
delete selected_game;
|
|
selected_game = nullptr;
|
|
}
|
|
selected_index = 0;
|
|
printf("Launcher reset - returning to menu\n");
|
|
}
|