Skip to content

Commit 67bdab1

Browse files
committed
sdl_engine is now distributed as a shared object to act as a sort of "SDK" for devs (basically me, but feel free to try it out!)
1 parent 74cd0b7 commit 67bdab1

File tree

7 files changed

+142
-45
lines changed

7 files changed

+142
-45
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ ifeq ($(LOG_FRAME), 1)
2020
endif
2121

2222
NAME = BurntEngine
23-
TARGET = main
23+
TARGET = libengine.so.1
2424
SRC = $(sort $(wildcard src/*.cpp))
2525
OBJ = $(SRC:.cpp=.o)
26-
LDFLAGS += -lassimp -lfmt -lSDL3 -lvulkan -lfreetype
26+
LDFLAGS += -lassimp -lfmt -lSDL3 -lvulkan -lfreetype -shared -fno-PIE -Wl,-soname,libengine.so.1
2727
CXXFLAGS ?= -mtune=generic -march=native
28-
CXXFLAGS += -funroll-all-loops -Iinclude -isystem/usr/include/freetype2 -std=c++17 $(VARS)
28+
CXXFLAGS += -funroll-all-loops -Iinclude -isystem/usr/include/freetype2 -fPIC -std=c++17 $(VARS)
2929

3030
all: $(TARGET)
3131

include/engine.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ class Engine {
182182
void LoadModel(Model *model); // this is the first function created to be used by main.cpp
183183
void UnloadModel(Model *model);
184184

185+
void AddUIChildren(UI::GenericElement *element);
186+
void RemoveUIChildren(UI::GenericElement *element);
187+
185188
// Find out the type through the "type" member variable and call the appropriate function for you!
186189
void AddUIGenericElement(UI::GenericElement *element);
190+
void RemoveUIGenericElement(UI::GenericElement *element);
187191

188192
void AddUIWaypoint(UI::Waypoint *waypoint);
189193
void RemoveUIWaypoint(UI::Waypoint *waypoint);

libengine.so

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/debug/libengine.so.1

settings.toml

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/engine.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,18 @@ void Engine::UnloadModel(Model *model) {
533533
}
534534
}
535535

536+
void Engine::AddUIChildren(UI::GenericElement *element) {
537+
for (UI::GenericElement *child : element->GetChildren()) {
538+
AddUIGenericElement(child);
539+
}
540+
}
541+
542+
void Engine::RemoveUIChildren(UI::GenericElement *element) {
543+
for (UI::GenericElement *child : element->GetChildren()) {
544+
RemoveUIGenericElement(child);
545+
}
546+
}
547+
536548
void Engine::AddUIGenericElement(UI::GenericElement *element) {
537549
switch (element->type) {
538550
case UI::PANEL:
@@ -552,6 +564,25 @@ void Engine::AddUIGenericElement(UI::GenericElement *element) {
552564
}
553565
}
554566

567+
void Engine::RemoveUIGenericElement(UI::GenericElement *element) {
568+
switch (element->type) {
569+
case UI::PANEL:
570+
RemoveUIPanel(reinterpret_cast<UI::Panel *>(element));
571+
break;
572+
case UI::LABEL:
573+
RemoveUILabel(reinterpret_cast<UI::Label *>(element));
574+
break;
575+
case UI::BUTTON:
576+
RemoveUIButton(reinterpret_cast<UI::Button *>(element));
577+
break;
578+
case UI::UNKNOWN:
579+
case UI::ARROWS:
580+
case UI::SCALABLE:
581+
case UI::WAYPOINT:
582+
break;
583+
}
584+
}
585+
555586
void Engine::AddUIWaypoint(UI::Waypoint *waypoint) {
556587
EngineSharedContext sharedContext = GetSharedContext();
557588

@@ -659,6 +690,8 @@ void Engine::AddUIArrows(UI::Arrows *arrows) {
659690
}
660691

661692
void Engine::RemoveUIArrows(UI::Arrows *arrows) {
693+
RemoveUIChildren(arrows);
694+
662695
for (size_t i = 0; i < m_RenderUIArrows.size(); i++) {
663696
if (m_RenderUIArrows[i].arrows != arrows)
664697
continue;
@@ -686,6 +719,8 @@ void Engine::RemoveUIArrows(UI::Arrows *arrows) {
686719
}
687720

688721
void Engine::RemoveUIWaypoint(UI::Waypoint *waypoint) {
722+
RemoveUIChildren(waypoint);
723+
689724
for (size_t i = 0; i < m_RenderUIWaypoints.size(); i++) {
690725
if (m_RenderUIWaypoints[i].waypoint != waypoint)
691726
continue;
@@ -711,6 +746,8 @@ void Engine::RemoveUIWaypoint(UI::Waypoint *waypoint) {
711746

712747
vkFreeDescriptorSets(m_EngineDevice, m_UIWaypointDescriptorPool, 1, &renderUIWaypoint.descriptorSet);
713748
}
749+
750+
AddUIChildren(waypoint);
714751
}
715752

716753
void Engine::AddUIPanel(UI::Panel *panel) {
@@ -729,9 +766,13 @@ void Engine::AddUIPanel(UI::Panel *panel) {
729766
vkMapMemory(m_EngineDevice, renderUIPanel.uboBuffer.memory, 0, sizeof(renderUIPanel.ubo), 0, &(renderUIPanel.uboBuffer.mappedData));
730767

731768
m_UIPanels.push_back(renderUIPanel);
769+
770+
AddUIChildren(panel);
732771
}
733772

734773
void Engine::RemoveUIPanel(UI::Panel *panel) {
774+
RemoveUIChildren(panel);
775+
735776
for (size_t i = 0; i < m_UIPanels.size(); i++) {
736777
RenderUIPanel renderUIPanel = m_UIPanels[i];
737778

@@ -778,9 +819,13 @@ void Engine::AddUILabel(UI::Label *label) {
778819
vkMapMemory(m_EngineDevice, renderUILabel.uboBuffer.memory, 0, sizeof(renderUILabel.ubo), 0, &(renderUILabel.uboBuffer.mappedData));
779820

780821
m_UILabels.push_back(renderUILabel);
822+
823+
AddUIChildren(label);
781824
}
782825

783826
void Engine::RemoveUILabel(UI::Label *label) {
827+
RemoveUIChildren(label);
828+
784829
for (size_t i = 0; i < m_UILabels.size(); i++) {
785830
RenderUILabel renderUILabel = m_UILabels[i];
786831

@@ -807,13 +852,11 @@ void Engine::RemoveUILabel(UI::Label *label) {
807852
}
808853

809854
void Engine::AddUIButton(UI::Button *button) {
810-
AddUIPanel(button->bgPanel);
811-
AddUILabel(button->fgLabel);
855+
AddUIChildren(button);
812856
}
813857

814858
void Engine::RemoveUIButton(UI::Button *button) {
815-
RemoveUIPanel(button->bgPanel);
816-
RemoveUILabel(button->fgLabel);
859+
AddUIChildren(button);
817860
}
818861

819862
void Engine::RegisterUpdateFunction(const std::function<void()> &func) {
File renamed without changes.

src/ui.cpp

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,66 @@ inline glm::vec2 Scalable::GetUnfitScale() {
248248
return scale;
249249
}
250250

251-
GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxml::xml_node<char> *node) {
251+
GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxml::xml_node<char> *node, UI::GenericElement *parent = nullptr) {
252252
using namespace rapidxml;
253253
std::string nodeName = node->name();
254254

255255
GenericElement *element;
256256

257-
if (nodeName == "Panel") {
258-
xml_node<char> *fitTypeNode = node->first_node("FitType");
257+
if (nodeName == "Group") {
258+
xml_node<char> *propertiesNode = node->first_node("Properties");
259+
UTILASSERT(propertiesNode);
260+
261+
xml_node<char> *fitTypeNode = propertiesNode->first_node("FitType");
262+
263+
xml_node<char> *positionNode = propertiesNode->first_node("Position");
264+
UTILASSERT(positionNode);
265+
266+
xml_node<char> *positionXNode = positionNode->first_node("X");
267+
UTILASSERT(positionXNode);
268+
float positionX = std::stof(positionXNode->value());
269+
270+
xml_node<char> *positionYNode = positionNode->first_node("Y");
271+
UTILASSERT(positionYNode);
272+
float positionY = std::stof(positionYNode->value());
273+
274+
275+
xml_node<char> *scaleNode = propertiesNode->first_node("Scale");
276+
UTILASSERT(scaleNode);
277+
278+
xml_node<char> *scaleXNode = scaleNode->first_node("X");
279+
UTILASSERT(scaleXNode);
280+
float scaleX = std::stof(scaleXNode->value());
281+
282+
xml_node<char> *scaleYNode = scaleNode->first_node("Y");
283+
UTILASSERT(scaleYNode);
284+
float scaleY = std::stof(scaleYNode->value());
285+
286+
xml_node<char> *zDepthNode = propertiesNode->first_node("ZDepth");
287+
float zDepth = 1.0f;
288+
289+
if (zDepthNode) {
290+
zDepth = std::stof(zDepthNode->value());
291+
}
292+
293+
element = new UI::Scalable();
294+
element->SetPosition(glm::vec2(positionX, positionY));
295+
element->SetDepth(zDepth);
296+
297+
reinterpret_cast<UI::Scalable *>(element)->SetScale(glm::vec2(scaleX, scaleY));
298+
299+
if (fitTypeNode && std::string(fitTypeNode->value()) == "FIT_CHILDREN") {
300+
reinterpret_cast<UI::Scalable *>(element)->fitType = UI::FIT_CHILDREN;
301+
} else if (fitTypeNode && std::string(fitTypeNode->value()) == "NONE") {
302+
reinterpret_cast<UI::Scalable *>(element)->fitType = UI::NONE;
303+
}
304+
} else if (nodeName == "Panel") {
305+
xml_node<char> *propertiesNode = node->first_node("Properties");
306+
UTILASSERT(propertiesNode);
307+
308+
xml_node<char> *fitTypeNode = propertiesNode->first_node("FitType");
259309

260-
xml_node<char> *colorNode = node->first_node("Color");
310+
xml_node<char> *colorNode = propertiesNode->first_node("Color");
261311
UTILASSERT(colorNode);
262312

263313
xml_node<char> *colorRNode = colorNode->first_node("R");
@@ -273,7 +323,7 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
273323
float colorB = std::stof(colorBNode->value());
274324

275325

276-
xml_node<char> *positionNode = node->first_node("Position");
326+
xml_node<char> *positionNode = propertiesNode->first_node("Position");
277327
UTILASSERT(positionNode);
278328

279329
xml_node<char> *positionXNode = positionNode->first_node("X");
@@ -285,7 +335,7 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
285335
float positionY = std::stof(positionYNode->value());
286336

287337

288-
xml_node<char> *scaleNode = node->first_node("Scale");
338+
xml_node<char> *scaleNode = propertiesNode->first_node("Scale");
289339
UTILASSERT(scaleNode);
290340

291341
xml_node<char> *scaleXNode = scaleNode->first_node("X");
@@ -296,7 +346,7 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
296346
UTILASSERT(scaleYNode);
297347
float scaleY = std::stof(scaleYNode->value());
298348

299-
xml_node<char> *zDepthNode = node->first_node("ZDepth");
349+
xml_node<char> *zDepthNode = propertiesNode->first_node("ZDepth");
300350
float zDepth = 1.0f;
301351

302352
if (zDepthNode) {
@@ -311,11 +361,14 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
311361
reinterpret_cast<UI::Scalable *>(element)->fitType = UI::NONE;
312362
}
313363
} else if (nodeName == "Label") {
314-
xml_node<char> *textNode = node->first_node("Text");
364+
xml_node<char> *propertiesNode = node->first_node("Properties");
365+
UTILASSERT(propertiesNode);
366+
367+
xml_node<char> *textNode = propertiesNode->first_node("Text");
315368
UTILASSERT(textNode);
316369
std::string text = textNode->value();
317370

318-
xml_node<char> *positionNode = node->first_node("Position");
371+
xml_node<char> *positionNode = propertiesNode->first_node("Position");
319372
UTILASSERT(positionNode);
320373

321374
xml_node<char> *positionXNode = positionNode->first_node("X");
@@ -327,11 +380,11 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
327380
float positionY = std::stof(positionYNode->value());
328381

329382

330-
xml_node<char> *fontNode = node->first_node("Font");
383+
xml_node<char> *fontNode = propertiesNode->first_node("Font");
331384
UTILASSERT(fontNode);
332385
std::string fontPath = fontNode->value();
333386

334-
xml_node<char> *zDepthNode = node->first_node("ZDepth");
387+
xml_node<char> *zDepthNode = propertiesNode->first_node("ZDepth");
335388
float zDepth = 1.0f;
336389

337390
if (zDepthNode) {
@@ -340,9 +393,12 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
340393

341394
element = new UI::Label(sharedContext, text, fontPath, glm::vec2(positionX, positionY), zDepth);
342395
} else if (nodeName == "Button") {
343-
xml_node<char> *fitTypeNode = node->first_node("FitType");
396+
xml_node<char> *propertiesNode = node->first_node("Properties");
397+
UTILASSERT(propertiesNode);
344398

345-
xml_node<char> *positionNode = node->first_node("Position");
399+
xml_node<char> *fitTypeNode = propertiesNode->first_node("FitType");
400+
401+
xml_node<char> *positionNode = propertiesNode->first_node("Position");
346402
UTILASSERT(positionNode);
347403

348404
xml_node<char> *positionXNode = positionNode->first_node("X");
@@ -354,7 +410,7 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
354410
float positionY = std::stof(positionYNode->value());
355411

356412

357-
xml_node<char> *scaleNode = node->first_node("Scale");
413+
xml_node<char> *scaleNode = propertiesNode->first_node("Scale");
358414
UTILASSERT(scaleNode);
359415

360416
xml_node<char> *scaleXNode = scaleNode->first_node("X");
@@ -366,15 +422,15 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
366422
float scaleY = std::stof(scaleYNode->value());
367423

368424
/* bgPanel and fgLabel */
369-
xml_node<char> *bgPanelNode = node->first_node("BgPanel");
425+
xml_node<char> *bgPanelNode = propertiesNode->first_node("BgPanel");
370426
UTILASSERT(bgPanelNode);
371427
xml_node<char> *panelNode = bgPanelNode->first_node("Panel");
372428

373429
GenericElement *bgPanel = DeserializeUIElement(sharedContext, panelNode);
374430
UTILASSERT(bgPanel->type == UI::PANEL);
375431

376432

377-
xml_node<char> *fgLabelNode = node->first_node("FgLabel");
433+
xml_node<char> *fgLabelNode = propertiesNode->first_node("FgLabel");
378434
UTILASSERT(fgLabelNode);
379435
xml_node<char> *labelNode = fgLabelNode->first_node("Label");
380436

@@ -390,9 +446,17 @@ GenericElement *DeserializeUIElement(EngineSharedContext &sharedContext, rapidxm
390446
reinterpret_cast<UI::Scalable *>(element)->fitType = UI::NONE;
391447
}
392448
} else {
393-
throw std::runtime_error(fmt::format("Unknown UI Serialized Object Type: {}", nodeName));
449+
fmt::println("WARN: Unknown UI Serialized Object Type: {}", nodeName);
450+
451+
return nullptr;
394452
}
395453

454+
for (xml_node<char> *childElement = node->first_node("Properties")->next_sibling(); childElement; childElement = childElement->next_sibling()) {
455+
DeserializeUIElement(sharedContext, childElement, element);
456+
}
457+
458+
element->SetParent(parent);
459+
396460
return element;
397461
}
398462

@@ -417,7 +481,12 @@ std::vector<GenericElement *> UI::LoadUIFile(EngineSharedContext &sharedContext,
417481

418482
xml_node<char> *uiSceneNode = uiSceneXML.first_node("UIScene");
419483
for (xml_node<char> *uiElement = uiSceneNode->first_node(); uiElement; uiElement = uiElement->next_sibling()) {
420-
elements.push_back(DeserializeUIElement(sharedContext, uiElement));
484+
GenericElement *element = DeserializeUIElement(sharedContext, uiElement);
485+
486+
if (!element)
487+
continue;
488+
489+
elements.push_back(element);
421490
}
422491

423492
return elements;

0 commit comments

Comments
 (0)