Main, pause, options, and in-game menu systems wired up; ready for gameplay

This commit is contained in:
2026-08-24 22:49:16 -05:00
parent 4dacb370a6
commit 0fe31615c5
12 changed files with 327 additions and 88 deletions

View File

@@ -1,20 +1,17 @@
#include "Application.h"
#include "Game.h"
#include "backend/RaylibInput.h"
#include <RmlUi/Core.h>
#include <RmlUi/Core/DataModelHandle.h>
#include <RmlUi/Debugger.h>
#include <iostream>
#include <raylib.h>
Game game;
bool Application::Init(int width, int height, const char *title,
const char *documentPath) {
bool Application::Init(int width, int height, const char *title) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(width, height, title);
SetTargetFPS(60);
SetExitKey(KEY_NULL);
Rml::SetSystemInterface(&system_interface);
Rml::SetRenderInterface(&render_interface);
Rml::Initialise();
@@ -22,26 +19,18 @@ bool Application::Init(int width, int height, const char *title,
context = Rml::CreateContext("main", Rml::Vector2i(width, height));
Rml::Debugger::Initialise(context);
// TODO: load a real font
if (Rml::ElementDocument *document = context->LoadDocument(documentPath)) {
document->Show();
} else {
TraceLog(LOG_WARNING, "Failed to load %s", documentPath);
}
if (!game.Init(context)) {
TraceLog(LOG_ERROR, "Failed to initialize game instance.");
return false;
}
Rml::DataModelHandle game_data_model_handle;
if (!game.setup_rml_data_binding(context, game_data_model_handle,
game.game_data)) {
if (!game.SetupRmlDataBinding(game_data_model_handle)) {
TraceLog(LOG_ERROR, "Failed to initialize data bindings for 'game_data'.");
return false;
}
game.TrySetGameState(START_MENU);
return true;
}
@@ -51,13 +40,17 @@ void Application::Frame() {
return;
}
if (IsWindowResized()) {
context->SetDimensions(Rml::Vector2i(GetScreenWidth(), GetScreenHeight()));
}
ForwardInputToRml(context);
context->Update();
game.Update();
BeginDrawing();
ClearBackground(DARKGRAY);
ClearBackground(BLACK);
game.Draw2D();

View File

@@ -6,7 +6,7 @@
class Application {
public:
bool Init(int width, int height, const char *title, const char *documentPath);
bool Init(int width, int height, const char *title);
void Frame();
void Shutdown();
@@ -16,5 +16,6 @@ private:
RaylibSystemInterface system_interface;
RaylibRenderInterface render_interface;
Rml::Context *context = nullptr;
Game game;
bool running = true;
};

View File

@@ -1,51 +1,208 @@
#include "Game.h"
#include <RmlUi/Core.h>
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/DataModelHandle.h>
#include <iostream>
#include <RmlUi/Core/Types.h>
#include <raylib.h>
void Game::Draw2D() { return; }
namespace {
void Game::Update() { return; }
const char *DocumentPathFor(GameState state) {
switch (state) {
case START_MENU:
return "assets/main-menu.rml";
case PAUSE_MENU:
return "assets/pause-menu.rml";
case STATS_MENU:
return "assets/stats-menu.rml";
case OPTIONS_MENU:
return "assets/options-menu.rml";
case IN_GAME:
return "assets/game-ui.rml";
}
return nullptr;
}
bool IsMenuState(GameState state) {
return state == OPTIONS_MENU || state == STATS_MENU;
}
} // namespace
bool Game::Init(Rml::Context *context) {
if (!ctx)
if (!context)
return false;
ctx = context;
return true;
}
bool Game::setup_rml_data_binding(Rml::Context *ctx,
Rml::DataModelHandle &model_handle,
GameData &game_data) {
bool Game::SetupRmlDataBinding(Rml::DataModelHandle &model_handle) {
Rml::DataModelConstructor dmc = ctx->CreateDataModel("game_data");
if (!dmc) {
TraceLog(LOG_ERROR, "Failed to construct data model..");
TraceLog(LOG_ERROR, "Failed to construct data model.");
return false;
}
// ---------- Member bindings ----------
dmc.Bind("game_state", &game_data.game_state);
// ---------- Event callback bindings ----------
dmc.BindEventCallback("start_game", [this](Rml::DataModelHandle, Rml::Event &,
const Rml::VariantList &) {
TrySetGameState(IN_GAME);
});
dmc.BindEventCallback("pause_game", [this](Rml::DataModelHandle, Rml::Event &,
const Rml::VariantList &) {
TrySetGameState(PAUSE_MENU);
});
dmc.BindEventCallback(
"unpause_game",
[this](Rml::DataModelHandle, Rml::Event &, const Rml::VariantList &) {
TrySetGameState(IN_GAME);
});
dmc.BindEventCallback(
"open_options_menu",
[this](Rml::DataModelHandle, Rml::Event &, const Rml::VariantList &) {
TrySetGameState(OPTIONS_MENU);
});
dmc.BindEventCallback(
"end_current_game",
[this](Rml::DataModelHandle, Rml::Event &, const Rml::VariantList &) {
TrySetGameState(START_MENU);
});
dmc.BindEventCallback("go_back",
[this](Rml::DataModelHandle, Rml::Event &,
const Rml::VariantList &) { GoBack(); });
model_handle = dmc.GetModelHandle();
return true;
}
void Game::ResetGame() {}
void Game::ShowMainMenu() {}
Rml::ElementDocument *Game::LoadOrGetDocument(GameState state) {
if (auto it = documents.find(state); it != documents.end())
return it->second;
const char *path = DocumentPathFor(state);
if (!path)
return nullptr;
Rml::ElementDocument *doc = ctx->LoadDocument(path);
if (!doc) {
TraceLog(LOG_ERROR, "Failed to load document: %s", path);
return nullptr;
}
documents[state] = doc;
return doc;
}
void Game::ShowDocumentForState(GameState state) {
if (activeDocument) {
activeDocument->Hide();
activeDocument = nullptr;
}
if (Rml::ElementDocument *doc = LoadOrGetDocument(state)) {
doc->Show();
activeDocument = doc;
}
}
void Game::TrySetGameState(GameState newState) {
GameState currentState = game_data.game_state;
GameState currentState = CurrentState();
if (IsMenuState(newState) && !IsMenuState(currentState)) {
previousState = currentState;
}
switch (newState) {
case START_MENU:
if (currentState == IN_GAME)
ResetGame();
ShowMainMenu();
break;
case IN_GAME:
if (currentState == START_MENU)
StartNewGame();
break;
default:
break;
}
game_data.game_state = newState;
ShowDocumentForState(newState);
}
void Game::GoBack() {
if (IsMenuState(CurrentState())) {
TrySetGameState(previousState);
}
}
void Game::StartNewGame() {
// TODO
// --- placeholder testing stuff ---
playerPos = {GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f};
}
void Game::ResetGame() {
// TODO
}
void Game::Update() {
HandleInput();
if (CurrentState() != IN_GAME)
return;
// TODO
// --- placeholder testing stuff ---
float dt = GetFrameTime();
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
playerPos.y -= playerSpeed * dt;
if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
playerPos.y += playerSpeed * dt;
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
playerPos.x -= playerSpeed * dt;
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
playerPos.x += playerSpeed * dt;
}
void Game::Draw2D() {
if (CurrentState() != IN_GAME)
return;
// TODO
// --- placeholder testing stuff ---
// DrawText("Hello from raylib!", 50, 50, 20, RAYWHITE);
if (CurrentState() != IN_GAME)
return;
DrawCircleV(playerPos, 24.0f, BLUE);
DrawText("WASD/Arrows to move, ESC to pause", 20, 20, 20, RAYWHITE);
}
void Game::HandleInput() {
if (!IsKeyReleased(KEY_ESCAPE))
return;
switch (CurrentState()) {
case IN_GAME:
TrySetGameState(PAUSE_MENU);
break;
case PAUSE_MENU:
TrySetGameState(IN_GAME);
break;
case OPTIONS_MENU:
case STATS_MENU:
GoBack();
break;
default:
break;
}
}

View File

@@ -2,10 +2,14 @@
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/DataModelHandle.h>
#include <RmlUi/Core/ElementDocument.h>
#include <raylib.h>
#include <unordered_map>
enum GameState { START_MENU, PAUSE_MENU, STATS_MENU, OPTIONS_MENU, IN_GAME };
struct GameData {
GameState game_state = START_MENU;
int game_state = START_MENU;
};
class Game {
@@ -13,22 +17,33 @@ public:
GameData game_data;
bool Init(Rml::Context *context);
bool setup_rml_data_binding(Rml::Context *ctx,
Rml::DataModelHandle &model_handle,
GameData &game_data);
bool SetupRmlDataBinding(Rml::DataModelHandle &model_handle);
void Update();
void Draw2D();
void TrySetGameState(GameState newState);
void GoBack();
GameState CurrentState() const {
return static_cast<GameState>(game_data.game_state);
}
void StartNewGame();
void ResetGame();
void ShowMainMenu();
void ShowPauseMenu();
private:
Rml::ElementDocument *LoadOrGetDocument(GameState state);
void ShowDocumentForState(GameState state);
void HandleInput();
Rml::Context *ctx = nullptr;
std::unordered_map<GameState, Rml::ElementDocument *> documents;
Rml::ElementDocument *activeDocument = nullptr;
GameState previousState = START_MENU;
// --- placeholder testing stuff ---
Vector2 playerPos{};
float playerSpeed = 300.0f;
};

View File

@@ -16,7 +16,7 @@ int main() {
static Application app;
g_app = &app;
app.Init(1280, 720, "gamejam", "assets/main.rml");
app.Init(1280, 720, "gamejam");
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(MainLoopStep, 0, 1);