From 15ec5f71d68870f95531328299c32f70345e493a Mon Sep 17 00:00:00 2001 From: Safariminer Date: Sat, 7 Jun 2025 18:39:43 -0400 Subject: [PATCH] canvas system --- ProjectMorault/Morault_Canvas.cpp | 99 +++++++++++++++++++ ProjectMorault/Morault_Canvas.h | 48 +++++++++ ProjectMorault/ProjectMorault.vcxproj | 2 + ProjectMorault/ProjectMorault.vcxproj.filters | 6 ++ ProjectMorault/data/ui/gameui.canvas | 13 +++ ProjectMorault/main.cpp | 11 ++- 6 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 ProjectMorault/Morault_Canvas.cpp create mode 100644 ProjectMorault/Morault_Canvas.h create mode 100644 ProjectMorault/data/ui/gameui.canvas diff --git a/ProjectMorault/Morault_Canvas.cpp b/ProjectMorault/Morault_Canvas.cpp new file mode 100644 index 0000000..ea4aaf4 --- /dev/null +++ b/ProjectMorault/Morault_Canvas.cpp @@ -0,0 +1,99 @@ +#include "Morault_Canvas.h" +#include "Morault_Resources.h" +#include +#include "pugixml.hpp" +#include + +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(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(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(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(); +} diff --git a/ProjectMorault/Morault_Canvas.h b/ProjectMorault/Morault_Canvas.h new file mode 100644 index 0000000..a17e3d0 --- /dev/null +++ b/ProjectMorault/Morault_Canvas.h @@ -0,0 +1,48 @@ +#pragma once +#include +#include +#include "Morault_Player.h" +#include +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> elements; + public: + Canvas(); + Canvas(std::string canvasfile); + void DrawCanvas(Morault::Gameplay::Player player); + ~Canvas(); + }; + } +} \ No newline at end of file diff --git a/ProjectMorault/ProjectMorault.vcxproj b/ProjectMorault/ProjectMorault.vcxproj index e7200b5..a6efe1f 100644 --- a/ProjectMorault/ProjectMorault.vcxproj +++ b/ProjectMorault/ProjectMorault.vcxproj @@ -137,6 +137,7 @@ + @@ -146,6 +147,7 @@ + diff --git a/ProjectMorault/ProjectMorault.vcxproj.filters b/ProjectMorault/ProjectMorault.vcxproj.filters index ef94c02..78127ae 100644 --- a/ProjectMorault/ProjectMorault.vcxproj.filters +++ b/ProjectMorault/ProjectMorault.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + @@ -71,5 +74,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/ProjectMorault/data/ui/gameui.canvas b/ProjectMorault/data/ui/gameui.canvas new file mode 100644 index 0000000..789874e --- /dev/null +++ b/ProjectMorault/data/ui/gameui.canvas @@ -0,0 +1,13 @@ + + + + Game UI Canvas + Safariminer + + + + + [PLAYERHP] HP + [PLAYERHP] HP + + \ No newline at end of file diff --git a/ProjectMorault/main.cpp b/ProjectMorault/main.cpp index 70e6cfe..26a3a70 100644 --- a/ProjectMorault/main.cpp +++ b/ProjectMorault/main.cpp @@ -9,6 +9,8 @@ #include "Morault_Rendering.h" #include "Morault_Safety.h" +#include "Morault_Canvas.h" + #define MORAULT_MAP_RENDER_SCALE 0.1f typedef enum { @@ -57,8 +59,10 @@ int main(int argc, char** argv) { Textures(load("data/textures/test.png")); } + Morault::UI::Canvas *canvas = new Morault::UI::Canvas("data/ui/gameui.canvas"); Vector2 editorOffset = { 0,0 }; + while (running) { @@ -126,9 +130,10 @@ int main(int argc, char** argv) { EndMode3D(); if (map->MapTriangles.size() == 0) DrawText("Map is empty, press E to open the map editor!", 0, GetScreenHeight()-94, 30, WHITE); - DrawTexture(std::get(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), 64, GetScreenHeight() - 64, 64, WHITE); + // DrawTexture(std::get(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), 64, GetScreenHeight() - 64, 64, WHITE); + canvas->DrawCanvas(player); DrawFPS(0, 0); EndDrawing(); } break;