Browse Source

Added extra tools for UI making as well as a TODO list

main
Ludovic 'Archivist' Lagouardette 4 months ago
parent
commit
79ec561f38
18 changed files with 195 additions and 57 deletions
  1. +2
    -1
      .gitignore
  2. +0
    -8
      .idea/.gitignore
  3. +0
    -4
      .idea/misc.xml
  4. +0
    -8
      .idea/modules.xml
  5. +0
    -6
      .idea/vcs.xml
  6. +4
    -2
      Engine/CMakeLists.txt
  7. +17
    -0
      Engine/README.md
  8. +1
    -0
      Engine/include/component.h
  9. +28
    -0
      Engine/include/components/panel_component.h
  10. +6
    -1
      Engine/include/components/sprite_component.h
  11. +32
    -14
      Engine/include/entity.h
  12. +2
    -2
      Engine/include/render_state.h
  13. +9
    -5
      Engine/include/transform.h
  14. +1
    -1
      Engine/src/background_entity.cpp
  15. +29
    -2
      Engine/src/main.cpp
  16. +61
    -0
      Engine/src/panel_component.cpp
  17. +2
    -2
      Engine/src/render_state.cpp
  18. +1
    -1
      Engine/src/ui_fps_entity.cpp

+ 2
- 1
.gitignore View File

@ -1 +1,2 @@
cmake-build-*/
cmake-build-*/
.idea/

+ 0
- 8
.idea/.gitignore View File

@ -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

+ 0
- 4
.idea/misc.xml View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

+ 0
- 8
.idea/modules.xml View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ProcurementOS.iml" filepath="$PROJECT_DIR$/.idea/ProcurementOS.iml" />
</modules>
</component>
</project>

+ 0
- 6
.idea/vcs.xml View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/cmake-build-debug/_deps/raylib-src" vcs="Git" />
</component>
</project>

+ 4
- 2
Engine/CMakeLists.txt View File

@ -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)

+ 17
- 0
Engine/README.md View File

@ -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
- [ ]
- [ ]
- [ ]
- [ ]

+ 1
- 0
Engine/include/component.h View File

@ -1,6 +1,7 @@
#pragma once
#include <chrono>
#include <memory>
namespace ant::core {
struct entity;

+ 28
- 0
Engine/include/components/panel_component.h View File

@ -0,0 +1,28 @@
#pragma once
#include "component.h"
#include <string>
#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<double>) 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<panel_component> m_parent_panel = std::shared_ptr<panel_component>(nullptr);
};
}

+ 6
- 1
Engine/include/components/sprite_component.h View File

@ -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:

+ 32
- 14
Engine/include/entity.h View File

@ -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<entity>& elem){return elem->visibility_flag();})) {
@ -109,9 +113,11 @@ namespace ant::core {
template<std::derived_from<entity> EntityType>
friend auto make_entity(const std::shared_ptr<entity>& parent, auto&&... ARGS) -> std::shared_ptr<EntityType>;
template<std::derived_from<entity> EntityType>
friend auto make_entity(auto&&... ARGS) -> std::shared_ptr<EntityType>;
template<std::derived_from<entity> EntityType>
friend auto make_entity(auto&&... ARGS) -> std::shared_ptr<EntityType>;
friend auto make_entity(const std::shared_ptr<entity>& parent) -> std::shared_ptr<entity>;
};
template<std::derived_from<entity> EntityType>
@ -126,14 +132,26 @@ namespace ant::core {
object->initialize();
return object;
}
template<std::derived_from<entity> EntityType>
[[nodiscard]] auto make_entity(auto&&... ARGS) -> std::shared_ptr<EntityType> {
auto object = std::make_shared<EntityType>(std::shared_ptr<entity>(nullptr), std::forward<decltype(ARGS)>(ARGS)...);
const std::shared_ptr<entity>& entity_router = object;
entity_router->m_parent = std::shared_ptr<entity>(nullptr);
entity_router->m_self = object;
object->initialize();
return object;
}
template<std::derived_from<entity> EntityType>
[[nodiscard]] auto make_entity(auto&&... ARGS) -> std::shared_ptr<EntityType> {
auto object = std::make_shared<EntityType>(std::shared_ptr<entity>(nullptr), std::forward<decltype(ARGS)>(ARGS)...);
const std::shared_ptr<entity>& entity_router = object;
entity_router->m_parent = std::shared_ptr<entity>(nullptr);
entity_router->m_self = object;
object->initialize();
return object;
}
[[nodiscard]] inline auto make_entity(const std::shared_ptr<entity>& parent) -> std::shared_ptr<entity> {
auto object = std::make_shared<entity>(parent);
const std::shared_ptr<entity>& 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;
}
}

+ 2
- 2
Engine/include/render_state.h View File

@ -8,8 +8,8 @@ namespace ant::render {
any,
end
};
void schedule_in_frame(int layer, std::function<void()> 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<void()> 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<void()>{std::move(fn)}, pos);
}
void render_all();

+ 9
- 5
Engine/include/transform.h View File

@ -8,11 +8,15 @@ namespace ant::core {
T X;
T Y;
};
template<typename T>
auto operator+(const Vec2<T>& lhs, const Vec2<T>& rhs) -> Vec2<T>{
return {.X = lhs.X + rhs.X, .Y = lhs.Y + rhs.Y};
}
template<typename T>
auto operator+(const Vec2<T>& lhs, const Vec2<T>& rhs) -> Vec2<T>{
return {.X = lhs.X + rhs.X, .Y = lhs.Y + rhs.Y};
}
template<typename T>
auto operator*(const Vec2<T>& lhs, const Vec2<T>& rhs) -> Vec2<T>{
return {.X = lhs.X * rhs.X, .Y = lhs.Y * rhs.Y};
}
template<typename T = float>
struct transform {

+ 1
- 1
Engine/src/background_entity.cpp View File

@ -3,5 +3,5 @@
#include "raylib.h"
void ant::entities::utilities::background_entity::render() {
ant::render::schedule_in_frame(o">-255, [color = m_color](){ ClearBackground(color);});
ant::render::schedule_in_frame(255, [color = m_color](){ ClearBackground(color);});
}

+ 29
- 2
Engine/src/main.cpp View File

@ -1,16 +1,21 @@
#include <iostream>
#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>();
ant::core::scene::current->root_nodes.push_back(ant::core::make_entity<ant::entities::utilities::ui_fps_entity>());
ant::core::scene::current->root_nodes.push_back(ant::core::make_entity<ant::entities::utilities::ui_fps_entity>());
ant::core::scene::current->root_nodes.push_back(ant::core::make_entity<ant::entities::utilities::background_entity>(BLACK));
ant::core::scene::current->root_nodes.push_back(ant::core::make_entity<ant::core::entity>());
ant::core::scene::current->root_nodes.back()
->add_component<ant::components::graphical::sprite_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<ant::entities::utilities::background_entity>(BLACK));
ant::core::scene::current->root_nodes.push_back(ant::core::make_entity<ant::core::entity>());
ant::core::scene::current->root_nodes.back()
->add_component<ant::components::ui::panel_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<ant::components::ui::panel_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<double> 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();

+ 61
- 0
Engine/src/panel_component.cpp View File

@ -0,0 +1,61 @@
#include <iostream>
#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<panel_component>(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<float>(std::floor(parent_layer));
auto climb = static_cast<float>(parent_layer) - bottom;
return -(bottom + 1 - (1 - climb) / 2);
}
return static_cast<float>(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<double>) {
}

+ 2
- 2
Engine/src/render_state.cpp View File

@ -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<void()> op;
};
@ -19,7 +19,7 @@ static bool operator<(const render_thunk& lhs, const render_thunk& rhs) {
}
static std::priority_queue<render_thunk> renders;
void ant::render::schedule_in_frame(int layer, std::function<void()> fn, ant::render::layer_pos pos) {
void ant::render::schedule_in_frame(float layer, std::function<void()> fn, ant::render::layer_pos pos) {
renders.push({layer, pos, std::move(fn)});
}

+ 1
- 1
Engine/src/ui_fps_entity.cpp View File

@ -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(o">-255, [coords = this->transform().position](){
DrawFPS(static_cast<int>(coords.X),static_cast<int>(coords.Y));
});
}

Loading…
Cancel
Save