Major Restructuring

N0ffie switched to mingw (fixing linux warnings)
This commit is contained in:
n0ffie 2025-04-12 18:09:16 +02:00
parent 384c758295
commit 8e0e9ceb7c
26 changed files with 1016 additions and 2921 deletions

18
include/cigui/cigui.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <cigui/config.h>
#include <cigui/core/Renderer.hpp>
#include <cigui/core/View.hpp>
#include <cigui/views/views.hpp>
#include <stdint.h>
namespace cig {
constexpr unsigned int VERSION_MAJOR = CIGUI_VERSION_MAJOR;
constexpr unsigned int VERSION_MINOR = CIGUI_VERSION_MINOR;
constexpr unsigned int VERSION_PATCH = CIGUI_VERSION_PATCH;
// Version string
constexpr const char *VERSION = CIGUI_VERSION;
}

37
include/cigui/config.h.in Normal file
View file

@ -0,0 +1,37 @@
#pragma once
// Version information
#define CIGUI_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define CIGUI_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define CIGUI_VERSION_PATCH @PROJECT_VERSION_PATCH@
#define CIGUI_VERSION "@PROJECT_VERSION@"
// Export macros for DLL/shared library
#if defined(_MSC_VER)
#if defined(CIGUI_DLL)
#if defined(cigui_EXPORTS) // Set by CMake automatically
#define CIGUI_API __declspec(dllexport)
#define CIGUI_TEMPLATE
#else
#define CIGUI_API __declspec(dllimport)
#define CIGUI_TEMPLATE extern
#endif
#else
#define CIGUI_API
#define CIGUI_TEMPLATE
#endif
#else
#if defined(CIGUI_DLL) && defined(__GNUC__) && __GNUC__ >= 4
#define CIGUI_API __attribute__ ((visibility ("default")))
#define CIGUI_TEMPLATE
#else
#define CIGUI_API
#define CIGUI_TEMPLATE
#endif
#endif
// For template classes, we use inline in headers
#define CIGUI_TEMPLATE_API
// Special macro for template instantiations
#define CIGUI_TEMPLATE_INST extern template class CIGUI_API

View file

@ -0,0 +1,64 @@
#pragma once
#include <cigui/utils/Vectors.hpp>
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 LayoutDirection : uint8_t {
None = 0,
LeftToRight,
RightToLeft,
TopToBottom,
BottomToTop,
};
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 {
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 {
LayoutPosition rule = LayoutPosition::None;
vec2f position = {0.f, 0.f};
} position;
};
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <sfml/Graphics.hpp>
#include <memory>
#include <stdexcept>
namespace cig {
class RenderCall {
std::shared_ptr<sf::Drawable> drawable;
sf::RenderStates states;
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");
}
}
void draw(sf::RenderTarget &target, const sf::RenderStates &rstates) const {
target.draw(*drawable, rstates);
}
};
}

View file

@ -0,0 +1,29 @@
#pragma once
#pragma once
#include <SFML/Graphics.hpp>
#include <memory>
#include <cigui/core/View.hpp>
#include <cigui/core/RenderCall.hpp>
namespace cig {
class Renderer {
public:
explicit Renderer(View *_view) : view(_view) { view->draw(); }
std::unique_ptr<View> view;
void update() const {
if (view->update())
view->draw();
}
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);
}
};
}

View file

@ -0,0 +1,44 @@
#pragma once
#include <cigui/utils/List.hpp>
#include <cigui/core/Layout.hpp>
#include <cigui/core/RenderCall.hpp>
#include <memory>
namespace cig
{
class View {
protected:
List<RenderCall> m_RenderCalls;
Layout m_Layout;
public:
virtual ~View() = default;
[[nodiscard]] const List<RenderCall> &renderCalls() const { return m_RenderCalls; }
std::unique_ptr<View> content;
virtual bool update() {
if (content)
return content->update();
return false;
}
void draw() {
if (!m_RenderCalls.empty()) {
m_RenderCalls.clear();
}
content = std::unique_ptr<View>(body());
if (!content) {
return;
}
content->draw();
auto &contentRenderCalls = content->renderCalls();
m_RenderCalls.expand(contentRenderCalls);
}
virtual View *body() = 0;
};
}

View file

@ -0,0 +1,79 @@
#pragma once
#include <functional>
#include <memory>
namespace cig
{
template <typename T, size_t growth_scalar = 1, size_t growth_summand = 3>
class List
{
std::unique_ptr<T> m_Data;
size_t m_Size = 0;
size_t m_Capacity;
protected:
void reserve(const size_t capacity);
public:
explicit List(const size_t capacity = 3) : m_Capacity(capacity) { reserve(capacity); }
void own(T* data, const size_t size)
{
m_Data = data;
m_Size = size;
}
void copy(T* data, const size_t size)
{
m_Data = std::make_unique<T[]>(size);
std::copy(data, data + size, m_Data.get());
m_Size = size;
}
[[nodiscard]] size_t size() const { return m_Size; }
T& operator[](size_t index) { return m_Data.get()[index]; }
const T& operator[](size_t index) const { return m_Data.get()[index]; }
void need(const size_t additional_size)
{
if (m_Size + additional_size > m_Capacity)
reserve(m_Capacity + additional_size);
}
[[nodiscard]] bool empty() const { return m_Size == 0; }
void clear() { m_Size = 0; }
void push_back(const T& value)
{
if (m_Size >= m_Capacity)
reserve(m_Capacity * growth_scalar + growth_summand);
m_Data.get()[m_Size++] = value;
}
template <typename... Args>
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>(args)...);
}
void expand(const List<T>& 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();
}
template <typename Lambda>
void iterate(Lambda&& lambda) const
{
for (size_t i = 0; i < m_Size; i++)
lambda(m_Data.get()[i]);
}
};
}
#include <cigui/utils/List.inl>

View file

@ -0,0 +1,22 @@
#define __LIST_FUNC_DEFINE__(rtt) \
template <typename T, size_t growth_scalar, size_t growth_summand> \
rtt List<T, growth_scalar, growth_summand>
namespace cig
{
__LIST_FUNC_DEFINE__(void)::reserve(const size_t capacity)
{
if (!m_Data)
{
m_Data = std::unique_ptr<T>(static_cast<T*>(calloc(capacity, sizeof(T))));
m_Capacity = capacity;
return;
}
std::unique_ptr<T> newData(static_cast<T*>(calloc(capacity, sizeof(T))));
std::copy(m_Data.get(), m_Data.get() + m_Size, newData.get());
m_Data = std::move(newData);
m_Capacity = capacity;
}
}

View file

@ -0,0 +1,66 @@
#pragma once
#include <stdint.h>
#define TYPEDEF_VECTOR(NAME, T, N, s) typedef NAME<T> vec##N##s;
#define TYPEDEF_VECTORS(NAME, N) \
typedef NAME<float> vec##N; \
TYPEDEF_VECTOR(NAME, float, N, f) \
TYPEDEF_VECTOR(NAME, double, N, d) \
TYPEDEF_VECTOR(NAME, long double, N, ld) \
TYPEDEF_VECTOR(NAME, size_t, N, sz) \
TYPEDEF_VECTOR(NAME, int, N, i) \
TYPEDEF_VECTOR(NAME, unsigned int, N, u) \
TYPEDEF_VECTOR(NAME, short, N, s) \
TYPEDEF_VECTOR(NAME, unsigned short, N, us) \
TYPEDEF_VECTOR(NAME, long, N, l) \
TYPEDEF_VECTOR(NAME, unsigned long, N, ul) \
TYPEDEF_VECTOR(NAME, long long, N, ll) \
TYPEDEF_VECTOR(NAME, unsigned long long, N, ull)
#if defined(__GNUC__) || defined(__clang__)
#define UNNAMED_STRUCT __extension__ struct
#else
#defien UNNAMED_STRUCT struct
#endif
namespace cig {
template <typename T>
union Vector2 {
UNNAMED_STRUCT {
T x, y;
};
UNNAMED_STRUCT {
T a, b;
};
};
TYPEDEF_VECTORS(Vector2, 2)
template <typename T>
union Vector3 {
UNNAMED_STRUCT {
T x, y, z;
};
UNNAMED_STRUCT {
T r, g, b;
};
};
TYPEDEF_VECTORS(Vector3, 3)
template <typename T>
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)
}

View file

@ -0,0 +1,43 @@
#pragma once
#include <cigui/core/View.hpp>
namespace cig {
struct Rectangle : View {
sf::Color m_Color;
sf::Color m_BorderColor;
float m_BorderThickness = 0;
Rectangle *setBorderColor(const sf::Color &color) {
m_BorderColor = color;
return this;
}
Rectangle *setBorderThickness(float thickness) {
m_BorderThickness = thickness;
return this;
}
Rectangle *setColor(const sf::Color &color) {
m_Color = color;
return this;
}
Rectangle *setSize(const vec2f &size) {
m_Layout.size.minSize = size;
return this;
}
View *body() override {
auto m_Shape = new sf::RectangleShape(sf::Vector2f{m_Layout.size.minSize.x, m_Layout.size.minSize.y});
m_Shape->setFillColor(m_Color);
if (m_BorderThickness > 0) {
m_Shape->setOutlineThickness(m_BorderThickness);
m_Shape->setOutlineColor(m_BorderColor);
}
m_RenderCalls.emplace_back(sf::RenderStates(), m_Shape);
return nullptr;
}
explicit Rectangle(const vec2f &size) {
m_Layout.size.minSize = size;
}
};
}

View file

@ -0,0 +1,3 @@
#pragma once
#include <cigui/views/Rectangle.hpp>