Changes in respect to the style guide

WIP
This commit is contained in:
noffie 2025-04-12 21:31:00 +02:00
parent 4a004dcb8b
commit 002d97ee27
11 changed files with 319 additions and 189 deletions

View file

@ -3,62 +3,72 @@
#include <cigui/utils/Vectors.hpp>
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;
};
}
struct
{
LayoutPosition rule = LayoutPosition::None;
vec2f position = {0.f, 0.f};
} position;
};
}

View file

@ -1,23 +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;
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<sf::Drawable> drawable;
sf::RenderStates states;
void draw(sf::RenderTarget &target, const sf::RenderStates &rstates) const {
target.draw(*drawable, rstates);
}
};
}
public:
explicit RenderCall(const sf::RenderStates& rstates, sf::Drawable* ptr);
void draw(sf::RenderTarget& target, const sf::RenderStates& rstates) const;
};
}

View file

@ -1,29 +1,24 @@
#pragma once
#pragma once
#include <SFML/Graphics.hpp>
#include <memory>
#include <cigui/core/View.hpp>
#include <cigui/core/RenderCall.hpp>
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> 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;
};
}

View file

@ -1,14 +1,14 @@
#pragma once
#include <cigui/utils/List.hpp>
#include <cigui/core/Layout.hpp>
#include <cigui/core/RenderCall.hpp>
#include <memory>
#include <cigui/utils/List.hpp>
#include <memory>
namespace cig
{
class RenderCall;
class View {
protected:
List<RenderCall> m_RenderCalls;
@ -17,27 +17,12 @@ namespace cig
public:
virtual ~View() = default;
[[nodiscard]] const List<RenderCall> &renderCalls() const { return m_RenderCalls; }
[[nodiscard]] const List<RenderCall>& renderCalls() const;
std::unique_ptr<View> 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<View>(body());
if (!content) {
return;
}
content->draw();
auto &contentRenderCalls = content->renderCalls();
m_RenderCalls.expand(contentRenderCalls);
}
void draw();
virtual View *body() = 0;
};

View file

@ -2,6 +2,7 @@
#include <functional>
#include <memory>
namespace cig
{
template <typename T, size_t growth_scalar = 1, size_t growth_summand = 3>
@ -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<T[]>(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 <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 emplace_back(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();
}
void expand(const List<T>& other);
template <typename Lambda>
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 <cigui/utils/List.inl>

View file

@ -1,12 +1,13 @@
#include <cigui/utils/List.hpp>
#define __LIST_FUNC_DEFINE__(rtt) \
#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)
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<T[]>(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 <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)...);
}
LIST_FUNC_DEFINE(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();
}
LIST_FUNC_DEFINE(template <typename Lambda> void)::iterate(Lambda&& lambda) const
{
for (size_t i = 0; i < m_Size; i++)
lambda(m_Data.get()[i]);
}
}
#undef LIST_FUNC_DEFINE

View file

@ -1,7 +1,10 @@
#pragma once
#include <cigui/config.h>
#include <stdint.h>
#define TYPEDEF_VECTOR(NAME, T, N, s) typedef NAME<T> 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<T>; \
namespace cig {
template <typename T>
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 <typename T>
union Vector3 {
UNNAMED_STRUCT {
T x, y, z;
};
UNNAMED_STRUCT {
T r, g, b;
};
};
TYPEDEF_VECTORS(Vector3, 3)
namespace cig
{
template <typename T>
union Vector2
{
UNNAMED_STRUCT
{
T x, y;
};
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)
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

@ -1,6 +1,11 @@
#pragma once
#include <cigui/core/View.hpp>
#include <SFML/Graphics/Color.hpp>
namespace sf {
class RectangleShape;
}
namespace cig {
struct Rectangle : View {

View file

@ -1 +1,14 @@

#include <cigui/core/RenderCall.hpp>
#include <sfml/Graphics.hpp>
#include <memory>
#include <stdexcept>
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);
}
}

19
src/core/Renderer.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <cigui/core/Renderer.hpp>
#include <cigui/core/View.hpp>
#include <cigui/core/RenderCall.hpp>
#include <sfml/Graphics.hpp>
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);
}
}

29
src/core/View.cpp Normal file
View file

@ -0,0 +1,29 @@
#include <cigui/core/View.hpp>
#include <cigui/core/RenderCall.hpp>
namespace cig {
const List<RenderCall>& View::renderCalls() const
{
return m_RenderCalls;
}
void View::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);
}
bool View::update() {
if (content)
return content->update();
return false;
}
}