Adding hot reload + attempting to style stuff

This commit is contained in:
2026-08-06 23:32:23 -05:00
parent 62a6f49eba
commit b3945d9d0e
7 changed files with 274 additions and 54 deletions

View File

@@ -4,7 +4,9 @@
#include "../vendor/stb/stb_image.h"
#include "../vendor/stb/stb_image_write.h"
#include <GLFW/glfw3.h>
#include <RmlUi/Core.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Debugger/Debugger.h>
#include <RmlUi/Lua/Lua.h>
#include <RmlUi_Backend.h>
@@ -18,6 +20,7 @@ static bool is_running = false;
static Rml::Context *ctx = nullptr;
static std::map<int, Critter> critters;
static std::vector<CritterEntry> critter_list;
static Rml::ElementDocument *doc;
static AppData app_data;
void shutdown() {
@@ -53,11 +56,67 @@ bool initialize() {
}
std::cout << "[INFO] Main context initialized.\n";
Rml::LoadFontFace("./assets/fonts/Hubballi-Regular.ttf");
Rml::Debugger::Initialise(ctx);
return true;
}
bool load_document() {
doc = ctx->LoadDocument("./assets/main.rml");
if (!doc) {
std::cout << "[ERROR] Could not load assets/main.rml document, shutting down.\n";
shutdown();
return false;
}
std::cout << "[INFO] Document loaded\n";
doc->Show();
return true;
}
bool reload_doc() {
if (doc) {
Rml::Factory::ClearStyleSheetCache();
Rml::Factory::ClearTemplateCache();
doc->Close();
doc = nullptr;
}
if (!load_document()) {
return false;
}
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;
reload_doc();
} 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 << "[INFO] Starting CritterFolio...\n";
@@ -77,18 +136,19 @@ int main() {
return 1;
}
Rml::ElementDocument *doc = ctx->LoadDocument("./assets/main.rml");
if (!doc) {
std::cout << "[ERROR] Could not load assets/main.rml document, shutting down.\n";
if (!load_document()) {
shutdown();
return 1;
}
std::cout << "[INFO] Main page loaded.\n";
doc->Show();
while (is_running) {
is_running = Backend::ProcessEvents(ctx);
handle_input();
ctx->Update();
Backend::BeginFrame();
ctx->Render();

View File

@@ -83,6 +83,19 @@ bool setup_data_binding(Rml::Context *context, Rml::DataModelHandle &model_handl
}
});
dmc.BindEventCallback("delete_critter", [&app_data, &critters, &critter_list](Rml::DataModelHandle handle, Rml::Event &, const Rml::VariantList &args) {
if (args.empty()) return;
int target_id = args[0].Get<int>();
if (db_delete_critter(target_id)) {
critters.erase(target_id);
sync_map_to_list(critters, critter_list);
handle.DirtyVariable("critters");
std::cout << "[INFO] Deleted critter with ID #" << target_id << std::endl;
}
});
model_handle = dmc.GetModelHandle();
return true;
}