Files
rmlui-app-template/src/main.cpp
2026-07-12 22:19:26 -05:00

137 lines
3.6 KiB
C++

#include <GLFW/glfw3.h>
#include <RmlUi/Core/ElementDocument.h>
#include <RmlUi/Core/Event.h>
#include <RmlUi/Debugger/Debugger.h>
#include <RmlUi/Lua/Interpreter.h>
#include <iostream>
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/Math.h>
#include <RmlUi/Core.h>
#include <RmlUi_Backend.h>
#include <RmlUi/Debugger.h>
#include <RmlUi/Lua/Lua.h>
#include <memory>
#include "app.hpp"
#include "pages/test_page.hpp"
#include "ButtonClickListener.hpp"
#include "lui_app_api.hpp"
static bool is_initalized = false;
static bool is_running = false;
static Rml::Context *ctx;
static App *app;
bool InitalizeApplication() {
if (!Backend::Initialize("RmlUi App Template", 800, 600, true)) {
std::cout << "[ERROR] backend was not initalized successfully.\n";
return false;
}
Rml::SetSystemInterface(Backend::GetSystemInterface());
Rml::SetRenderInterface(Backend::GetRenderInterface());
Rml::Initialise();
Rml::Lua::Initialise();
ctx = Rml::CreateContext("main", Rml::Vector2i(800,600));
if (!ctx) {
std::cout << "[ERROR] context was null.\n";
Rml::Shutdown();
Backend::Shutdown();
return false;
}
Rml::Debugger::Initialise(ctx);
app = new App(ctx, nullptr);
if (!app) {
std::cout << "[ERROR] app was null.\n";
Rml::Shutdown();
Backend::Shutdown();
return false;
}
lua_State* L = Rml::Lua::Interpreter::GetLuaState();
AppLuaApi *lua_app_api = new AppLuaApi(app, L);
lua_app_api->RegisterLuaBindings();
std::cout << "Application initalized...\n";
return true;
}
bool f5_was_pressed = false;
bool f8_was_pressed = false;
void handle_input() {
GLFWwindow *window = glfwGetCurrentContext();
if(window) {
// Reload ui
bool f5_state = glfwGetKey(window, GLFW_KEY_F5);
if (f5_state == GLFW_PRESS && !f5_was_pressed) {
f5_was_pressed = true;
app->Reload();
} else if (f5_state == GLFW_RELEASE) {
f5_was_pressed = false;
}
// Toggle debugger window
bool f8_state = glfwGetKey(window, GLFW_KEY_F8);
if (f8_state == GLFW_PRESS && !f8_was_pressed) {
f8_was_pressed = true;
bool is_visible = Rml::Debugger::IsVisible();
Rml::Debugger::SetVisible(!is_visible);
} else if (f8_state == GLFW_RELEASE) {
f8_was_pressed = false;
}
}
}
int main() {
std::cout << "Application starting...\n";
is_initalized = InitalizeApplication();
if (!is_initalized) return 1;
is_running = true;
std::cout << "Loading UI...\n";
app->RegisterPage<TestPagePage>("test-page");
app->SetupListeners([](Rml::ElementDocument *doc) {
if (auto *btn = doc->GetElementById("nav-test")) {
app->RegisterListener(btn, std::make_unique<ButtonClickListener>([](Rml::Event&){
app->NavigateTo("test-page");
}));
}
if (auto *btn = doc->GetElementById("nav-home")) {
app->RegisterListener(btn, std::make_unique<ButtonClickListener>([](Rml::Event&){
app->NavigateTo("home");
}));
}
});
app->LoadUi();
app->NavigateTo("home");
std::cout << "UI Loaded\n";
while (is_running) {
is_running = Backend::ProcessEvents(ctx);
handle_input();
ctx->Update();
Backend::BeginFrame();
ctx->Render();
Backend::PresentFrame();
}
app->Shutdown();
Rml::Shutdown();
delete app;
Backend::Shutdown();
return 0;
}