canvas system

This commit is contained in:
Safariminer 2025-06-07 18:39:43 -04:00
parent 66956de068
commit 15ec5f71d6
6 changed files with 176 additions and 3 deletions

View File

@ -0,0 +1,99 @@
#include "Morault_Canvas.h"
#include "Morault_Resources.h"
#include <raymath.h>
#include "pugixml.hpp"
#include <regex>
void Morault::UI::Image::Draw(Morault::Gameplay::Player player)
{
using namespace Morault::Resources;
Vector2 anchorPosition;
if (anchor == BL) anchorPosition = { 0, (float)GetScreenHeight() };
if (anchor == BR) anchorPosition = { (float)GetScreenWidth(), (float)GetScreenHeight() };
if (anchor == TL) anchorPosition = { 0, 0 };
if (anchor == TR) anchorPosition = { (float)GetScreenWidth(), 0};
if (anchor == CN) anchorPosition = { (float)GetScreenWidth() / 2, (float)GetScreenHeight() / 2 };
Vector2 finalPosition = anchorPosition + Vector2((float)posx, (float)posy);
DrawTextureEx(std::get<Texture2D>(Textures(get(src))), finalPosition, 0, scale, WHITE);
}
void Morault::UI::Text::Draw(Morault::Gameplay::Player player)
{
using namespace Morault::Resources;
Vector2 anchorPosition;
if (anchor == BL) anchorPosition = { 0, (float)GetScreenHeight() };
if (anchor == BR) anchorPosition = { (float)GetScreenWidth(), (float)GetScreenHeight() };
if (anchor == TL) anchorPosition = { 0, 0 };
if (anchor == TR) anchorPosition = { (float)GetScreenWidth(), 0 };
if (anchor == CN) anchorPosition = { (float)GetScreenWidth() / 2, (float)GetScreenHeight() / 2 };
Vector2 finalPosition = anchorPosition + Vector2((float)posx, (float)posy);
std::string temp = text;
temp.replace(temp.find("[PLAYERHP]"), std::string("[PLAYERHP]").size(), std::to_string(player.data.hp));
DrawText(temp.c_str(), finalPosition.x, finalPosition.y, size, color);
}
Morault::UI::Canvas::Canvas()
{
}
Morault::UI::Canvas::Canvas(std::string canvasfile)
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(canvasfile.c_str());
if (!result) throw;
pugi::xml_node root = doc.child("canvas");
for (const auto& img : root.children("img")) {
UI::Image newimg;
newimg.anchor = (std::string)img.attribute("anchor").as_string() == "bl" ? BL :
(std::string)img.attribute("anchor").as_string() == "br" ? BR :
(std::string)img.attribute("anchor").as_string() == "tl" ? TL :
(std::string)img.attribute("anchor").as_string() == "tr" ? TR :
CN;
newimg.posx = img.attribute("posx").as_int();
newimg.posy = img.attribute("posy").as_int();
newimg.scale = img.attribute("scale").as_float();
newimg.src = (std::string)img.attribute("src").as_string();
elements.push_back(std::make_shared<UI::Image>(newimg));
}
for (const auto& txt : root.children("text")) {
UI::Text newtxt;
newtxt.anchor = (std::string)txt.attribute("anchor").as_string() == "bl" ? BL :
(std::string)txt.attribute("anchor").as_string() == "br" ? BR :
(std::string)txt.attribute("anchor").as_string() == "tl" ? TL :
(std::string)txt.attribute("anchor").as_string() == "tr" ? TR :
CN;
newtxt.posx = txt.attribute("posx").as_int();
newtxt.posy = txt.attribute("posy").as_int();
newtxt.color.r = (unsigned char)std::stoi(txt.attribute("colr").as_string());
newtxt.color.g = (unsigned char)std::stoi(txt.attribute("colg").as_string());
newtxt.color.b = (unsigned char)std::stoi(txt.attribute("colb").as_string());
newtxt.color.a = (unsigned char)std::stoi(txt.attribute("cola").as_string());
newtxt.size = txt.attribute("size").as_int();
newtxt.text = txt.child_value();
elements.push_back(std::make_shared<UI::Text>(newtxt));
}
}
void Morault::UI::Canvas::DrawCanvas(Morault::Gameplay::Player player)
{
for (int i = 0; i < elements.size(); i++) elements[i]->Draw(player);
}
Morault::UI::Canvas::~Canvas()
{
elements.clear();
}

View File

@ -0,0 +1,48 @@
#pragma once
#include <raylib.h>
#include <iostream>
#include "Morault_Player.h"
#include <vector>
namespace Morault {
namespace UI {
typedef enum {
BL, // bottom left
BR, // bottom right
TL, // top left
TR, // top right
CN // center
} Anchor;
class CanvasElement {
public:
int posx, posy;
Anchor anchor;
virtual void Draw(Morault::Gameplay::Player player) = 0;
};
class Image : public CanvasElement {
public:
int posx, posy;
Anchor anchor;
std::string src;
float scale;
void Draw(Morault::Gameplay::Player player);
};
class Text : public CanvasElement {
public:
int posx, posy;
Anchor anchor;
std::string text;
int size;
Color color;
void Draw(Morault::Gameplay::Player player);
};
class Canvas {
std::vector<std::shared_ptr<CanvasElement>> elements;
public:
Canvas();
Canvas(std::string canvasfile);
void DrawCanvas(Morault::Gameplay::Player player);
~Canvas();
};
}
}

View File

@ -137,6 +137,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="..\deps\include\pugixml.cpp" /> <ClCompile Include="..\deps\include\pugixml.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="Morault_Canvas.cpp" />
<ClCompile Include="Morault_Map.cpp" /> <ClCompile Include="Morault_Map.cpp" />
<ClCompile Include="Morault_Player.cpp" /> <ClCompile Include="Morault_Player.cpp" />
<ClCompile Include="Morault_Rendering.cpp" /> <ClCompile Include="Morault_Rendering.cpp" />
@ -146,6 +147,7 @@
<ClCompile Include="Morault_Utils.cpp" /> <ClCompile Include="Morault_Utils.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Morault_Canvas.h" />
<ClInclude Include="Morault_EngineInfo.h" /> <ClInclude Include="Morault_EngineInfo.h" />
<ClInclude Include="Morault_Map.h" /> <ClInclude Include="Morault_Map.h" />
<ClInclude Include="Morault_Player.h" /> <ClInclude Include="Morault_Player.h" />

View File

@ -45,6 +45,9 @@
<ClCompile Include="Morault_Safety.cpp"> <ClCompile Include="Morault_Safety.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Morault_Canvas.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Morault_Resources.h"> <ClInclude Include="Morault_Resources.h">
@ -71,5 +74,8 @@
<ClInclude Include="Morault_Safety.h"> <ClInclude Include="Morault_Safety.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Morault_Canvas.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,13 @@
<canvas>
<canvasinfo>
<name>Game UI Canvas</name>
<author>Safariminer</author>
</canvasinfo>
<!-- different possible anchors: bl, br, tl, tr, cn -->
<img src="data/ui/health.png" posx="0" posy="-64" anchor="bl" scale="1" />
<text posx="66" posy="-62" size="64" anchor="bl" colr="0" colg="0" colb="0" cola="255">[PLAYERHP] HP</text>
<text posx="64" posy="-64" size="64" anchor="bl" colr="255" colg="255" colb="255" cola="255">[PLAYERHP] HP</text>
</canvas>

View File

@ -9,6 +9,8 @@
#include "Morault_Rendering.h" #include "Morault_Rendering.h"
#include "Morault_Safety.h" #include "Morault_Safety.h"
#include "Morault_Canvas.h"
#define MORAULT_MAP_RENDER_SCALE 0.1f #define MORAULT_MAP_RENDER_SCALE 0.1f
typedef enum { typedef enum {
@ -57,9 +59,11 @@ int main(int argc, char** argv) {
Textures(load("data/textures/test.png")); Textures(load("data/textures/test.png"));
} }
Morault::UI::Canvas *canvas = new Morault::UI::Canvas("data/ui/gameui.canvas");
Vector2 editorOffset = { 0,0 }; Vector2 editorOffset = { 0,0 };
while (running) { while (running) {
if (WindowShouldClose()) { if (WindowShouldClose()) {
@ -126,9 +130,10 @@ int main(int argc, char** argv) {
EndMode3D(); EndMode3D();
if (map->MapTriangles.size() == 0) DrawText("Map is empty, press E to open the map editor!", 0, GetScreenHeight()-94, 30, WHITE); if (map->MapTriangles.size() == 0) DrawText("Map is empty, press E to open the map editor!", 0, GetScreenHeight()-94, 30, WHITE);
DrawTexture(std::get<Texture2D>(Morault::Resources::Textures(Morault::Resources::get("data/ui/health.png"))), 0, GetScreenHeight() - 64, WHITE); // DrawTexture(std::get<Texture2D>(Morault::Resources::Textures(Morault::Resources::get("data/ui/health.png"))), 0, GetScreenHeight() - 64, WHITE);
DrawText(TextFormat("%i", player.data.hp), 66, GetScreenHeight() - 62, 64, BLACK); // DrawText(TextFormat("%i", player.data.hp), 66, GetScreenHeight() - 62, 64, BLACK);
DrawText(TextFormat("%i", player.data.hp), 64, GetScreenHeight() - 64, 64, WHITE); // DrawText(TextFormat("%i", player.data.hp), 64, GetScreenHeight() - 64, 64, WHITE);
canvas->DrawCanvas(player);
DrawFPS(0, 0); DrawFPS(0, 0);
EndDrawing(); EndDrawing();
} break; } break;