diff --git a/include/cigui/core/Layout.hpp b/include/cigui/core/Layout.hpp index e35fde0..d031e4a 100644 --- a/include/cigui/core/Layout.hpp +++ b/include/cigui/core/Layout.hpp @@ -3,62 +3,72 @@ #include -namespace cig { -enum class LayoutSizes : uint8_t { - None = 0, - Min, - Max, - Fixed, -}; +namespace cig +{ + enum class LayoutSizes : uint8_t + { + None = 0, + Min, + Max, + Fixed, + }; -enum class LayoutAlignment : uint8_t { - None = 0, - Left, - Right, - Top, - Bottom, - Center, -}; + enum class LayoutAlignment : uint8_t + { + None = 0, + Left, + Right, + Top, + Bottom, + Center, + }; -enum class LayoutDirection : uint8_t { - None = 0, - LeftToRight, - RightToLeft, - TopToBottom, - BottomToTop, -}; + enum class LayoutDirection : uint8_t + { + None = 0, + LeftToRight, + RightToLeft, + TopToBottom, + BottomToTop, + }; -enum class LayoutPosition : uint8_t { - None = 0, - Absolute, - Left, - Right, - Top, - Bottom, - Center, -}; + enum class LayoutPosition : uint8_t + { + None = 0, + Absolute, + Left, + Right, + Top, + Bottom, + Center, + }; -struct Layout { - struct { - LayoutSizes rule = LayoutSizes::None; - vec2f minSize = {0.f, 0.f}; - vec2f maxSize = {0.f, 0.f}; - } size; + struct Layout + { + struct + { + LayoutSizes rule = LayoutSizes::None; + vec2f minSize = {0.f, 0.f}; + vec2f maxSize = {0.f, 0.f}; + } size; - struct { - LayoutAlignment rule = LayoutAlignment::None; - vec4f padding = {0.f, 0.f, 0.f, 0.f}; - vec4f margin = {0.f, 0.f, 0.f, 0.f}; - } alignment; + struct + { + LayoutAlignment rule = LayoutAlignment::None; + vec4f padding = {0.f, 0.f, 0.f, 0.f}; + vec4f margin = {0.f, 0.f, 0.f, 0.f}; + } alignment; - struct { - LayoutDirection rule = LayoutDirection::None; - vec2f spacing = {0.f, 0.f}; - } direction; + struct + { + LayoutDirection rule = LayoutDirection::None; + vec2f spacing = {0.f, 0.f}; + } direction; - struct { - LayoutPosition rule = LayoutPosition::None; - vec2f position = {0.f, 0.f}; - } position; -}; -} \ No newline at end of file + struct + { + LayoutPosition rule = LayoutPosition::None; + vec2f position = {0.f, 0.f}; + } position; + }; +} diff --git a/include/cigui/core/RenderCall.hpp b/include/cigui/core/RenderCall.hpp index f697cd4..9904344 100644 --- a/include/cigui/core/RenderCall.hpp +++ b/include/cigui/core/RenderCall.hpp @@ -1,23 +1,23 @@ #pragma once -#include #include -#include -namespace cig { -class RenderCall { - std::shared_ptr drawable; - sf::RenderStates states; +namespace sf { + class Drawable; + class RenderTarget; + struct RenderStates; +} -public: - explicit RenderCall(const sf::RenderStates &rstates, sf::Drawable *ptr) : drawable(ptr), states(rstates) { - if (!drawable) { - throw std::runtime_error("RenderCall::RenderCall(): Drawable is null"); - } - } +namespace cig +{ + class RenderCall + { + std::shared_ptr drawable; + sf::RenderStates states; - void draw(sf::RenderTarget &target, const sf::RenderStates &rstates) const { - target.draw(*drawable, rstates); - } -}; -} \ No newline at end of file + public: + explicit RenderCall(const sf::RenderStates& rstates, sf::Drawable* ptr); + + void draw(sf::RenderTarget& target, const sf::RenderStates& rstates) const; + }; +} diff --git a/include/cigui/core/Renderer.hpp b/include/cigui/core/Renderer.hpp index 7597f1a..100f22a 100644 --- a/include/cigui/core/Renderer.hpp +++ b/include/cigui/core/Renderer.hpp @@ -1,29 +1,24 @@ #pragma once -#pragma once - -#include #include -#include -#include +namespace sf { + class RenderTarget; + class RenderStates; +} namespace cig { + class View; + class RenderCall; class Renderer { public: - explicit Renderer(View *_view) : view(_view) { view->draw(); } + explicit Renderer(View *_view); std::unique_ptr view; - void update() const { - if (view->update()) - view->draw(); - } + void update() const; - void render(sf::RenderTarget &target, const sf::RenderStates &states) const { - auto lambda = [&target, &states](const RenderCall& renderCall) { renderCall.draw(target, states); }; - view->renderCalls().iterate(lambda); - } + void render(sf::RenderTarget &target, const sf::RenderStates &states) const; }; } diff --git a/include/cigui/core/View.hpp b/include/cigui/core/View.hpp index 5617ebf..f307a28 100644 --- a/include/cigui/core/View.hpp +++ b/include/cigui/core/View.hpp @@ -1,14 +1,14 @@ #pragma once - -#include #include -#include -#include +#include +#include namespace cig { + class RenderCall; + class View { protected: List m_RenderCalls; @@ -17,27 +17,12 @@ namespace cig public: virtual ~View() = default; - [[nodiscard]] const List &renderCalls() const { return m_RenderCalls; } + [[nodiscard]] const List& renderCalls() const; std::unique_ptr content; - virtual bool update() { - if (content) - return content->update(); - return false; - } + virtual bool update(); - void draw() { - if (!m_RenderCalls.empty()) { - m_RenderCalls.clear(); - } - content = std::unique_ptr(body()); - if (!content) { - return; - } - content->draw(); - auto &contentRenderCalls = content->renderCalls(); - m_RenderCalls.expand(contentRenderCalls); - } + void draw(); virtual View *body() = 0; }; diff --git a/include/cigui/utils/List.hpp b/include/cigui/utils/List.hpp index 6a086d5..2bde179 100644 --- a/include/cigui/utils/List.hpp +++ b/include/cigui/utils/List.hpp @@ -2,6 +2,7 @@ #include #include + namespace cig { template @@ -12,68 +13,36 @@ namespace cig size_t m_Capacity; protected: - void reserve(const size_t capacity); + void reserve(size_t capacity); public: - explicit List(const size_t capacity = 3) : m_Capacity(capacity) { reserve(capacity); } + explicit List(size_t capacity = 3); - void own(T* data, const size_t size) - { - m_Data = data; - m_Size = size; - } + void own(T* data, size_t size); - void copy(T* data, const size_t size) - { - m_Data = std::make_unique(size); - std::copy(data, data + size, m_Data.get()); - m_Size = size; - } + void copy(T* data, size_t size); - [[nodiscard]] size_t size() const { return m_Size; } + [[nodiscard]] size_t size() const; - T& operator[](size_t index) { return m_Data.get()[index]; } + T& operator[](size_t index); - const T& operator[](size_t index) const { return m_Data.get()[index]; } + const T& operator[](size_t index) const; - void need(const size_t additional_size) - { - if (m_Size + additional_size > m_Capacity) - reserve(m_Capacity + additional_size); - } + void need(size_t additional_size); - [[nodiscard]] bool empty() const { return m_Size == 0; } + [[nodiscard]] bool empty() const; - void clear() { m_Size = 0; } + void clear(); - void push_back(const T& value) - { - if (m_Size >= m_Capacity) - reserve(m_Capacity * growth_scalar + growth_summand); - m_Data.get()[m_Size++] = value; - } + void push_back(const T& value); template - void emplace_back(Args&&... args) - { - if (m_Size >= m_Capacity) - reserve(m_Capacity * growth_scalar + growth_summand); - m_Data.get()[m_Size++] = T(std::forward(args)...); - } + void emplace_back(Args&&... args); - void expand(const List& other) - { - need(other.size()); - std::copy(other.m_Data.get(), other.m_Data.get() + other.size(), m_Data.get() + m_Size); - m_Size += other.size(); - } + void expand(const List& other); template - void iterate(Lambda&& lambda) const - { - for (size_t i = 0; i < m_Size; i++) - lambda(m_Data.get()[i]); - } + void iterate(Lambda&& lambda) const; }; } #include \ No newline at end of file diff --git a/include/cigui/utils/List.inl b/include/cigui/utils/List.inl index bf937bf..4cc57f4 100644 --- a/include/cigui/utils/List.inl +++ b/include/cigui/utils/List.inl @@ -1,12 +1,13 @@ +#include -#define __LIST_FUNC_DEFINE__(rtt) \ +#define LIST_FUNC_DEFINE(rtt) \ template \ rtt List namespace cig { - __LIST_FUNC_DEFINE__(void)::reserve(const size_t capacity) + LIST_FUNC_DEFINE(void)::reserve(const size_t capacity) { if (!m_Data) { @@ -19,4 +20,68 @@ namespace cig m_Data = std::move(newData); m_Capacity = capacity; } + + LIST_FUNC_DEFINE()::List(const size_t capacity) : m_Capacity(capacity) { reserve(capacity); } + + LIST_FUNC_DEFINE(void)::own(T* data, const size_t size) { + m_Data = data; + m_Size = size; + } + + LIST_FUNC_DEFINE(void)::copy(T* data, const size_t size) { + m_Data = std::make_unique(size); + std::copy(data, data + size, m_Data.get()); + m_Size = size; + } + + LIST_FUNC_DEFINE(size_t)::size() const { + return m_Size; + } + + LIST_FUNC_DEFINE(T &)::operator[](const size_t index) { + return m_Data.get()[index]; + } + LIST_FUNC_DEFINE(const T &)::operator[](const size_t index) const { + return m_Data.get()[index]; + } + + LIST_FUNC_DEFINE(void)::need(const size_t additional_size) { + if (m_Size + additional_size > m_Capacity) + reserve(m_Capacity + additional_size); + } + + LIST_FUNC_DEFINE(bool)::empty() const { + return m_Size == 0; + } + + LIST_FUNC_DEFINE(void)::clear() { + m_Size = 0; + } + + LIST_FUNC_DEFINE(void)::push_back(const T& value) { + if (m_Size >= m_Capacity) + reserve(m_Capacity * growth_scalar + growth_summand); + m_Data.get()[m_Size++] = value; + } + + + LIST_FUNC_DEFINE(template void)::emplace_back(Args&&... args) { + if (m_Size >= m_Capacity) + reserve(m_Capacity * growth_scalar + growth_summand); + m_Data.get()[m_Size++] = T(std::forward(args)...); + } + + LIST_FUNC_DEFINE(void)::expand(const List& other) { + need(other.size()); + std::copy(other.m_Data.get(), other.m_Data.get() + other.size(), m_Data.get() + m_Size); + m_Size += other.size(); + } + + LIST_FUNC_DEFINE(template void)::iterate(Lambda&& lambda) const + { + for (size_t i = 0; i < m_Size; i++) + lambda(m_Data.get()[i]); + } } + +#undef LIST_FUNC_DEFINE \ No newline at end of file diff --git a/include/cigui/utils/Vectors.hpp b/include/cigui/utils/Vectors.hpp index c44d461..4035f18 100644 --- a/include/cigui/utils/Vectors.hpp +++ b/include/cigui/utils/Vectors.hpp @@ -1,7 +1,10 @@ #pragma once +#include + #include + #define TYPEDEF_VECTOR(NAME, T, N, s) typedef NAME vec##N##s; #define TYPEDEF_VECTORS(NAME, N) \ @@ -20,47 +23,84 @@ TYPEDEF_VECTOR(NAME, unsigned long long, N, ull) #if defined(__GNUC__) || defined(__clang__) - #define UNNAMED_STRUCT __extension__ struct +#define UNNAMED_STRUCT __extension__ struct #else #defien UNNAMED_STRUCT struct #endif +#if CIGUI_DLL +#define VECTOR_TEMPLATE_INST(N, T) CIGUI_TEMPLATE_INST Vector##N; \ -namespace cig { -template -union Vector2 { - UNNAMED_STRUCT { - T x, y; - }; - UNNAMED_STRUCT { - T a, b; - }; -}; +#define VECTOR_INSTANTIATION(N) \ + VECTOR_TEMPLATE_INST(N, float) \ + VECTOR_TEMPLATE_INST(N, double)\ + VECTOR_TEMPLATE_INST(N, long double) \ + VECTOR_TEMPLATE_INST(N, size_t) \ + VECTOR_TEMPLATE_INST(N, int) \ + VECTOR_TEMPLATE_INST(N, short) \ + VECTOR_TEMPLATE_INST(N, long) \ + VECTOR_TEMPLATE_INST(N, long long) \ + VECTOR_TEMPLATE_INST(N, unsigned int) \ + VECTOR_TEMPLATE_INST(N, unsigned short) \ + VECTOR_TEMPLATE_INST(N, unsigned long) \ + VECTOR_TEMPLATE_INST(N, unsigned long long) +#else +#define VECTOR_INSTANTIATION(N) +#endif -TYPEDEF_VECTORS(Vector2, 2) -template -union Vector3 { - UNNAMED_STRUCT { - T x, y, z; - }; - UNNAMED_STRUCT { - T r, g, b; - }; -}; -TYPEDEF_VECTORS(Vector3, 3) +namespace cig +{ + template + union Vector2 + { + UNNAMED_STRUCT + { + T x, y; + }; -template -union Vector4 { - UNNAMED_STRUCT { - T x, y, z, w; - }; - UNNAMED_STRUCT { - T r, g, b, a; - }; - UNNAMED_STRUCT { - T left, top, right, bottom; - }; -}; -TYPEDEF_VECTORS(Vector4, 4) + UNNAMED_STRUCT + { + T a, b; + }; + }; + + TYPEDEF_VECTORS(Vector2, 2) + + template + union Vector3 + { + UNNAMED_STRUCT + { + T x, y, z; + }; + + UNNAMED_STRUCT + { + T r, g, b; + }; + }; + + TYPEDEF_VECTORS(Vector3, 3) + + template + union Vector4 + { + UNNAMED_STRUCT + { + T x, y, z, w; + }; + + UNNAMED_STRUCT + { + T r, g, b, a; + }; + + UNNAMED_STRUCT + { + T left, top, right, bottom; + }; + }; + + TYPEDEF_VECTORS(Vector4, 4) } diff --git a/include/cigui/views/Rectangle.hpp b/include/cigui/views/Rectangle.hpp index c21e9eb..1d62bde 100644 --- a/include/cigui/views/Rectangle.hpp +++ b/include/cigui/views/Rectangle.hpp @@ -1,6 +1,11 @@ #pragma once #include +#include + +namespace sf { + class RectangleShape; +} namespace cig { struct Rectangle : View { diff --git a/src/core/RenderCall.cpp b/src/core/RenderCall.cpp index 5f28270..9873f90 100644 --- a/src/core/RenderCall.cpp +++ b/src/core/RenderCall.cpp @@ -1 +1,14 @@ - \ No newline at end of file +#include +#include +#include +#include + +namespace cig { + RenderCall::RenderCall(const sf::RenderStates& rstates, sf::Drawable* ptr) : drawable(ptr), states(rstates) { + if (!drawable) { throw std::runtime_error("RenderCall::RenderCall(): Drawable is null"); } + } + + void RenderCall::draw(sf::RenderTarget& target, const sf::RenderStates& rstates) const { + target.draw(*drawable, rstates); + } +} \ No newline at end of file diff --git a/src/core/Renderer.cpp b/src/core/Renderer.cpp new file mode 100644 index 0000000..2e852ca --- /dev/null +++ b/src/core/Renderer.cpp @@ -0,0 +1,19 @@ +#include + +#include +#include +#include + +namespace cig { + Renderer::Renderer(View *_view) : view(_view) { view->draw(); } + + void Renderer::update() const { + if (view->update()) + view->draw(); + } + + void Renderer::render(sf::RenderTarget &target, const sf::RenderStates &states) const { + auto lambda = [&target, &states](const RenderCall &renderCall) { renderCall.draw(target, states); }; + view->renderCalls().iterate(lambda); + } +} \ No newline at end of file diff --git a/src/core/View.cpp b/src/core/View.cpp new file mode 100644 index 0000000..f390243 --- /dev/null +++ b/src/core/View.cpp @@ -0,0 +1,29 @@ +#include + +#include + +namespace cig { + const List& View::renderCalls() const + { + return m_RenderCalls; + } + + void View::draw() { + if (!m_RenderCalls.empty()) { + m_RenderCalls.clear(); + } + content = std::unique_ptr(body()); + if (!content) { + return; + } + content->draw(); + auto &contentRenderCalls = content->renderCalls(); + m_RenderCalls.expand(contentRenderCalls); + } + + bool View::update() { + if (content) + return content->update(); + return false; + } +} \ No newline at end of file