From 79ec561f383e22e7fe95856253781e0b4499997b Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Fri, 29 Dec 2023 16:29:12 +0100 Subject: [PATCH] Added extra tools for UI making as well as a TODO list --- .gitignore | 3 +- .idea/.gitignore | 8 --- .idea/misc.xml | 4 -- .idea/modules.xml | 8 --- .idea/vcs.xml | 6 -- Engine/CMakeLists.txt | 6 +- Engine/README.md | 17 ++++++ Engine/include/component.h | 1 + Engine/include/components/panel_component.h | 28 +++++++++ Engine/include/components/sprite_component.h | 7 ++- Engine/include/entity.h | 46 ++++++++++----- Engine/include/render_state.h | 4 +- Engine/include/transform.h | 14 +++-- Engine/src/background_entity.cpp | 2 +- Engine/src/main.cpp | 31 +++++++++- Engine/src/panel_component.cpp | 61 ++++++++++++++++++++ Engine/src/render_state.cpp | 4 +- Engine/src/ui_fps_entity.cpp | 2 +- 18 files changed, 195 insertions(+), 57 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml create mode 100644 Engine/README.md create mode 100644 Engine/include/components/panel_component.h create mode 100644 Engine/src/panel_component.cpp diff --git a/.gitignore b/.gitignore index 72b18fd..e3fcfb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -cmake-build-*/ \ No newline at end of file +cmake-build-*/ +.idea/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 79b3c94..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 34368da..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 5fca962..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Engine/CMakeLists.txt b/Engine/CMakeLists.txt index 8be8fcc..ab9d608 100644 --- a/Engine/CMakeLists.txt +++ b/Engine/CMakeLists.txt @@ -2,9 +2,11 @@ Include(FetchContent) FetchContent_Declare( RayLib - GIT_REPOSITORY D:/AntiJeu/cmake-build-debug/_deps/raylib-src + GIT_REPOSITORY https://github.com/raysan5/raylib GIT_TAG 4.5.0 GIT_PROGRESS TRUE + USES_TERMINAL_DOWNLOAD TRUE + GIT_SHALLOW TRUE ) FetchContent_MakeAvailable(RayLib) @@ -21,7 +23,7 @@ add_executable(Engine src/ui_fps_entity.cpp include/entities/background_entity.h src/background_entity.cpp - include/components/sprite_component.h src/sprite_component.cpp) + include/components/sprite_component.h src/sprite_component.cpp include/components/panel_component.h src/panel_component.cpp) target_include_directories(Engine PUBLIC include) target_link_libraries(Engine PUBLIC raylib) file(COPY assets/apple.png DESTINATION assets/apple.png) \ No newline at end of file diff --git a/Engine/README.md b/Engine/README.md new file mode 100644 index 0000000..e754734 --- /dev/null +++ b/Engine/README.md @@ -0,0 +1,17 @@ +# Ant minimalist game engine + +## TODO: + +- [ ] scripting +- [ ] rendering subroutines +- [ ] viewport logic +- [ ] particles +- [ ] collisions and collider components +- [ ] `panel_component` + - [ ] focus logic + - [ ] input logic + - [ ] controller tooltips +- [ ] +- [ ] +- [ ] +- [ ] \ No newline at end of file diff --git a/Engine/include/component.h b/Engine/include/component.h index 82615e4..e88befa 100644 --- a/Engine/include/component.h +++ b/Engine/include/component.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace ant::core { struct entity; diff --git a/Engine/include/components/panel_component.h b/Engine/include/components/panel_component.h new file mode 100644 index 0000000..8a3d4a5 --- /dev/null +++ b/Engine/include/components/panel_component.h @@ -0,0 +1,28 @@ +#pragma once +#include "component.h" +#include +#include "raylib.h" +#include "transform.h" + +namespace ant::components::ui { + struct panel_component : public ant::core::component { + panel_component() {} + panel_component( + int layer = 0, + Color color = BLUE, + ant::core::Vec2<> size = {100,100}, + ant::core::Vec2<> scale = {1,1} + ); + std::string name() override {return "panel_component";} + void initialize() override; + void render() override; + void update(std::chrono::duration) override; + ant::core::Vec2<> scale(); + private: + ant::core::Vec2<> m_size; + ant::core::Vec2<> m_scale; + int m_layer; //< This is ignored if a parent panel is found, panels bleed up to one layer below their current layer + Color m_color; + std::weak_ptr m_parent_panel = std::shared_ptr(nullptr); + }; +} \ No newline at end of file diff --git a/Engine/include/components/sprite_component.h b/Engine/include/components/sprite_component.h index e43aec6..89581ba 100644 --- a/Engine/include/components/sprite_component.h +++ b/Engine/include/components/sprite_component.h @@ -7,7 +7,12 @@ namespace ant::components::graphical { struct sprite_component : public ant::core::component { sprite_component() {} - sprite_component(const std::string& str, int layer = 0, ant::core::transform<> reposition = {{0,0},0}, ant::core::Vec2<> scale = {1,1}); + sprite_component( + const std::string& texture_path, + int layer = 0, + ant::core::transform<> reposition = {{0,0},0}, + ant::core::Vec2<> scale = {1,1} + ); std::string name() override {return "sprite_component";} void render() override; private: diff --git a/Engine/include/entity.h b/Engine/include/entity.h index a6926f0..415f093 100644 --- a/Engine/include/entity.h +++ b/Engine/include/entity.h @@ -65,7 +65,11 @@ namespace ant::core { auto set_visible(bool visible) -> bool { return std::exchange(m_is_visible, visible); } - virtual void initialize() {} + virtual void initialize() { + for (auto& comp : m_components) { + comp->initialize(); + } + } virtual void render() { if(not visibility_flag()) return; for(auto& child : m_children | std::views::filter([](const std::shared_ptr& elem){return elem->visibility_flag();})) { @@ -109,9 +113,11 @@ namespace ant::core { template EntityType> friend auto make_entity(const std::shared_ptr& parent, auto&&... ARGS) -> std::shared_ptr; - - template EntityType> - friend auto make_entity(auto&&... ARGS) -> std::shared_ptr; + + template EntityType> + friend auto make_entity(auto&&... ARGS) -> std::shared_ptr; + + friend auto make_entity(const std::shared_ptr& parent) -> std::shared_ptr; }; template EntityType> @@ -126,14 +132,26 @@ namespace ant::core { object->initialize(); return object; } - - template EntityType> - [[nodiscard]] auto make_entity(auto&&... ARGS) -> std::shared_ptr { - auto object = std::make_shared(std::shared_ptr(nullptr), std::forward(ARGS)...); - const std::shared_ptr& entity_router = object; - entity_router->m_parent = std::shared_ptr(nullptr); - entity_router->m_self = object; - object->initialize(); - return object; - } + + template EntityType> + [[nodiscard]] auto make_entity(auto&&... ARGS) -> std::shared_ptr { + auto object = std::make_shared(std::shared_ptr(nullptr), std::forward(ARGS)...); + const std::shared_ptr& entity_router = object; + entity_router->m_parent = std::shared_ptr(nullptr); + entity_router->m_self = object; + object->initialize(); + return object; + } + + [[nodiscard]] inline auto make_entity(const std::shared_ptr& parent) -> std::shared_ptr { + auto object = std::make_shared(parent); + const std::shared_ptr& entity_router = object; + entity_router->m_parent = parent; + entity_router->m_self = object; + if(parent) { + parent->m_children.push_back(object); + } + object->initialize(); + return object; + } } \ No newline at end of file diff --git a/Engine/include/render_state.h b/Engine/include/render_state.h index ff19375..34c1028 100644 --- a/Engine/include/render_state.h +++ b/Engine/include/render_state.h @@ -8,8 +8,8 @@ namespace ant::render { any, end }; - void schedule_in_frame(int layer, std::function fn, layer_pos pos = layer_pos::any); - void schedule_in_frame(int layer, auto fn, layer_pos pos = layer_pos::any) { + void schedule_in_frame(float layer, std::function fn, layer_pos pos = layer_pos::any); + void schedule_in_frame(float layer, auto fn, layer_pos pos = layer_pos::any) { schedule_in_frame(layer, std::function{std::move(fn)}, pos); } void render_all(); diff --git a/Engine/include/transform.h b/Engine/include/transform.h index ac8ec6f..890d794 100644 --- a/Engine/include/transform.h +++ b/Engine/include/transform.h @@ -8,11 +8,15 @@ namespace ant::core { T X; T Y; }; - - template - auto operator+(const Vec2& lhs, const Vec2& rhs) -> Vec2{ - return {.X = lhs.X + rhs.X, .Y = lhs.Y + rhs.Y}; - } + + template + auto operator+(const Vec2& lhs, const Vec2& rhs) -> Vec2{ + return {.X = lhs.X + rhs.X, .Y = lhs.Y + rhs.Y}; + } + template + auto operator*(const Vec2& lhs, const Vec2& rhs) -> Vec2{ + return {.X = lhs.X * rhs.X, .Y = lhs.Y * rhs.Y}; + } template struct transform { diff --git a/Engine/src/background_entity.cpp b/Engine/src/background_entity.cpp index b811390..7dc2add 100644 --- a/Engine/src/background_entity.cpp +++ b/Engine/src/background_entity.cpp @@ -3,5 +3,5 @@ #include "raylib.h" void ant::entities::utilities::background_entity::render() { - ant::render::schedule_in_frame(-255, [color = m_color](){ ClearBackground(color);}); + ant::render::schedule_in_frame(255, [color = m_color](){ ClearBackground(color);}); } \ No newline at end of file diff --git a/Engine/src/main.cpp b/Engine/src/main.cpp index ac43445..7c867aa 100644 --- a/Engine/src/main.cpp +++ b/Engine/src/main.cpp @@ -1,16 +1,21 @@ +#include #include "raylib.h" #include "scene.h" #include "render_state.h" #include "entities/ui_fps_entity.h" #include "entities/background_entity.h" #include "components/sprite_component.h" +#include "components/panel_component.h" using namespace std::string_literals; int main() { InitWindow(800, 600, "Example"); ant::core::scene::current = std::make_shared(); - ant::core::scene::current->root_nodes.push_back(ant::core::make_entity()); + + ant::core::scene::current->root_nodes.push_back(ant::core::make_entity()); + ant::core::scene::current->root_nodes.push_back(ant::core::make_entity(BLACK)); + ant::core::scene::current->root_nodes.push_back(ant::core::make_entity()); ant::core::scene::current->root_nodes.back() ->add_component( @@ -21,10 +26,32 @@ int main() { ); ant::core::scene::current->root_nodes.back()->transform().position = {250,250}; ant::core::scene::current->root_nodes.back()->transform().angle = 120; - ant::core::scene::current->root_nodes.push_back(ant::core::make_entity(BLACK)); + + ant::core::scene::current->root_nodes.push_back(ant::core::make_entity()); + ant::core::scene::current->root_nodes.back() + ->add_component( + 100, + BLUE, + ant::core::Vec2<>{400, 400}, + ant::core::Vec2<>{1,1} + ); + ant::core::scene::current->root_nodes.back()->transform().position = {390, 10}; + + auto child = ant::core::make_entity(ant::core::scene::current->root_nodes.back()); + child->transform().position = {150,150}; + child + ->add_component( + 12, + RED, + ant::core::Vec2<>{100, 100}, + ant::core::Vec2<>{1,1} + )->initialize(); + // TODO: investigate the rendering of subpanels + auto ref = std::chrono::steady_clock::now(); std::chrono::duration frame_time = ref - ref; while(not WindowShouldClose()) { + std::cout << "Frame start\n"; ant::core::scene::current->update(frame_time); ant::core::scene::current->render(); BeginDrawing(); diff --git a/Engine/src/panel_component.cpp b/Engine/src/panel_component.cpp new file mode 100644 index 0000000..b044db1 --- /dev/null +++ b/Engine/src/panel_component.cpp @@ -0,0 +1,61 @@ +#include +#include "components/panel_component.h" +#include "entity.h" +#include "raylib.h" +#include "render_state.h" + +ant::components::ui::panel_component::panel_component( + int layer, + Color color, + ant::core::Vec2<> size, + ant::core::Vec2<> scale +) +: m_layer(layer) +, m_size(size) +, m_scale(scale) +, m_color(color) +{} + +void ant::components::ui::panel_component::initialize() { + auto parent = owner()->parent(); + while(parent) { + if(auto panels = parent->find_components("panel_component"); not panels.empty()) { + m_parent_panel = dynamic_pointer_cast(panels.front()); + break; + } + parent = parent->parent(); + } +} + +ant::core::Vec2<> ant::components::ui::panel_component::scale() { + if(auto parent = m_parent_panel.lock(); parent) { + return parent->scale()*m_scale; + } + return m_scale; +} + + + + +void ant::components::ui::panel_component::render() { + auto pos = (*owner()).world_transform().position; + auto size = m_size*scale(); + auto layer = [&]()-> float { + if(auto panel = m_parent_panel.lock()) { + auto parent_layer = -panel->m_layer; + auto bottom = static_cast(std::floor(parent_layer)); + auto climb = static_cast(parent_layer) - bottom; + return -(bottom + 1 - (1 - climb) / 2); + } + return static_cast(m_layer); + }(); + auto color = m_color; + // TODO: add a warning for drawing a big panel in a smaller panel + ant::render::schedule_in_frame(layer, [pos, size, color] () { + std::cout << pos.X << " " << pos.Y << " " << size.X << " " << size.Y << " | " << (int)color.r << " " << (int)color.g << " " << (int)color.b << std::endl; + DrawRectangle(pos.X, pos.Y, size.X, size.Y, color); + }); +} +void ant::components::ui::panel_component::update(std::chrono::duration) { + +} \ No newline at end of file diff --git a/Engine/src/render_state.cpp b/Engine/src/render_state.cpp index 46dc3f6..ec38b02 100644 --- a/Engine/src/render_state.cpp +++ b/Engine/src/render_state.cpp @@ -2,7 +2,7 @@ #include "render_state.h" struct render_thunk { - int prio; + float prio; ant::render::layer_pos pos = ant::render::layer_pos::any; std::function op; }; @@ -19,7 +19,7 @@ static bool operator<(const render_thunk& lhs, const render_thunk& rhs) { } static std::priority_queue renders; -void ant::render::schedule_in_frame(int layer, std::function fn, ant::render::layer_pos pos) { +void ant::render::schedule_in_frame(float layer, std::function fn, ant::render::layer_pos pos) { renders.push({layer, pos, std::move(fn)}); } diff --git a/Engine/src/ui_fps_entity.cpp b/Engine/src/ui_fps_entity.cpp index bbae939..0cd01b2 100644 --- a/Engine/src/ui_fps_entity.cpp +++ b/Engine/src/ui_fps_entity.cpp @@ -4,7 +4,7 @@ void ant::entities::utilities::ui_fps_entity::render() { entity::render(); - ant::render::schedule_in_frame(255, [coords = this->transform().position](){ + ant::render::schedule_in_frame(-255, [coords = this->transform().position](){ DrawFPS(static_cast(coords.X),static_cast(coords.Y)); }); } \ No newline at end of file