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

11
assets/game-ui.rml Normal file
View File

@@ -0,0 +1,11 @@
<rml>
<head>
<title>game_ui</title>
<link type="text/rcss" href="globals.rcss" />
</head>
<body data-model="game_data">
<h1>Hello from RmlUi!</h1>
</body>
</rml>

42
assets/globals.rcss Normal file
View File

@@ -0,0 +1,42 @@
body {
--primary-color: #446043;
--primary-background-color: #2c3e2b;
--text-color: #ffffea;
font-family: rmlui-debugger-font;
font-size: 16dp;
color: var(--text-color);
background-color: #ffffff00;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
p {
margin-bottom: 15px;
}
.hidden {
display: none;
}
.menu-btn {
display: block;
padding: 8px;
width: 200dp;
text-align: center;
background-color: var(--primary-color);
border: 1px #ffffff00;
font-size: 22dp;
}
.menu-btn:hover {
background-color: #4a7eb5;
}
.menu-btn:active {
background-color: #2e4e70;
}

16
assets/main-menu.rcss Normal file
View File

@@ -0,0 +1,16 @@
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: var(--primary-background-color);
}
#start_menu {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
min-width: 80%;
}

16
assets/main-menu.rml Normal file
View File

@@ -0,0 +1,16 @@
<rml>
<head>
<title>gamejam</title>
<link type="text/rcss" href="globals.rcss" />
<link type="text/rcss" href="main-menu.rcss" />
</head>
<body data-model="game_data">
<div id="start_menu">
<h1>Curious Critters</h1>
<button class="menu-btn" id="start-btn" data-event-click="start_game">Start</button>
<button class="menu-btn" id="options-btn" data-event-click="open_options_menu">Options</button>
</div>
</body>
</rml>

View File

@@ -1,45 +0,0 @@
body {
font-family: rmlui-debugger-font;
font-size: 16px;
color: white;
width: 100vw;
height: 100vh;
}
#panel {
position: absolute;
top: 40px;
left: 40px;
width: 300px;
padding: 20px;
background-color: rgba(20, 20, 20, 220);
border: 2px #888;
}
h1 {
font-size: 24px;
color: #ffd35c;
margin-bottom: 10px;
}
p {
margin-bottom: 15px;
}
.hidden {
display: none;
}
.menu-btn {
display: block;
width: 100px;
padding: 8px;
text-align: center;
background-color: #3a6ea5;
color: white;
border: 1px #5a8ec5;
}
.menu-btn:hover {
background-color: #4a7eb5;
}

15
assets/options-menu.rml Normal file
View File

@@ -0,0 +1,15 @@
<rml>
<head>
<title>options_menu</title>
<link type="text/rcss" href="globals.rcss" />
<link type="text/rcss" href="main-menu.rcss" />
</head>
<body data-model="game_data">
<div id="start_menu">
<h1>Options</h1>
<button class="menu-btn" id="back-btn" data-event-click="go_back">Done</button>
</div>
</body>
</rml>

18
assets/pause-menu.rml Normal file
View File

@@ -0,0 +1,18 @@
<rml>
<head>
<title>pause_menu</title>
<link type="text/rcss" href="globals.rcss" />
<link type="text/rcss" href="main-menu.rcss" />
</head>
<body data-model="game_data">
<div id="start_menu">
<h1>Pause</h1>
<button class="menu-btn" id="continue-btn" data-event-click="unpause_game">Continue</button>
<button class="menu-btn" id="options-btn" data-event-click="open_options_menu">Options</button>
<button class="menu-btn" id="end-game-btn" data-event-click="end_current_game">End Game (All progress will
be lost)</button>
</div>
</body>
</rml>

View File

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

View File

@@ -6,7 +6,7 @@
class Application { class Application {
public: 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 Frame();
void Shutdown(); void Shutdown();
@@ -16,5 +16,6 @@ private:
RaylibSystemInterface system_interface; RaylibSystemInterface system_interface;
RaylibRenderInterface render_interface; RaylibRenderInterface render_interface;
Rml::Context *context = nullptr; Rml::Context *context = nullptr;
Game game;
bool running = true; bool running = true;
}; };

View File

@@ -1,51 +1,208 @@
#include "Game.h" #include "Game.h"
#include <RmlUi/Core.h>
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/DataModelHandle.h> #include <RmlUi/Core/DataModelHandle.h>
#include <iostream> #include <RmlUi/Core/Types.h>
#include <raylib.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) { bool Game::Init(Rml::Context *context) {
if (!ctx) if (!context)
return false; return false;
ctx = context; ctx = context;
return true; return true;
} }
bool Game::setup_rml_data_binding(Rml::Context *ctx, bool Game::SetupRmlDataBinding(Rml::DataModelHandle &model_handle) {
Rml::DataModelHandle &model_handle,
GameData &game_data) {
Rml::DataModelConstructor dmc = ctx->CreateDataModel("game_data"); Rml::DataModelConstructor dmc = ctx->CreateDataModel("game_data");
if (!dmc) { if (!dmc) {
TraceLog(LOG_ERROR, "Failed to construct data model.."); TraceLog(LOG_ERROR, "Failed to construct data model.");
return false; return false;
} }
// ---------- Member bindings ----------
dmc.Bind("game_state", &game_data.game_state); 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; return true;
} }
void Game::ResetGame() {} Rml::ElementDocument *Game::LoadOrGetDocument(GameState state) {
void Game::ShowMainMenu() {} 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) { void Game::TrySetGameState(GameState newState) {
GameState currentState = game_data.game_state; GameState currentState = CurrentState();
if (IsMenuState(newState) && !IsMenuState(currentState)) {
previousState = currentState;
}
switch (newState) { switch (newState) {
case START_MENU: case START_MENU:
if (currentState == IN_GAME) if (currentState == IN_GAME)
ResetGame(); ResetGame();
ShowMainMenu(); break;
case IN_GAME:
if (currentState == START_MENU)
StartNewGame();
break; break;
default: default:
break; break;
} }
game_data.game_state = newState; 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/Context.h>
#include <RmlUi/Core/DataModelHandle.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 }; enum GameState { START_MENU, PAUSE_MENU, STATS_MENU, OPTIONS_MENU, IN_GAME };
struct GameData { struct GameData {
GameState game_state = START_MENU; int game_state = START_MENU;
}; };
class Game { class Game {
@@ -13,22 +17,33 @@ public:
GameData game_data; GameData game_data;
bool Init(Rml::Context *context); bool Init(Rml::Context *context);
bool SetupRmlDataBinding(Rml::DataModelHandle &model_handle);
bool setup_rml_data_binding(Rml::Context *ctx,
Rml::DataModelHandle &model_handle,
GameData &game_data);
void Update(); void Update();
void Draw2D(); void Draw2D();
void TrySetGameState(GameState newState); void TrySetGameState(GameState newState);
void GoBack();
GameState CurrentState() const {
return static_cast<GameState>(game_data.game_state);
}
void StartNewGame(); void StartNewGame();
void ResetGame(); void ResetGame();
void ShowMainMenu();
void ShowPauseMenu();
private: private:
Rml::ElementDocument *LoadOrGetDocument(GameState state);
void ShowDocumentForState(GameState state);
void HandleInput();
Rml::Context *ctx = nullptr; 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; static Application app;
g_app = &app; g_app = &app;
app.Init(1280, 720, "gamejam", "assets/main.rml"); app.Init(1280, 720, "gamejam");
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
emscripten_set_main_loop(MainLoopStep, 0, 1); emscripten_set_main_loop(MainLoopStep, 0, 1);