Adding hot reload + attempting to style stuff
This commit is contained in:
68
src/main.cpp
68
src/main.cpp
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user