summaryrefslogtreecommitdiff
path: root/include/SSVOpenHexagon/Utils/FastVertexVector.hpp
blob: f53874ef52578ac62f807bf220336f73b4f46c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#pragma once

#include "SSVOpenHexagon/Global/Assert.hpp"

#include "SSVOpenHexagon/Utils/UniquePtrArray.hpp"

#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/PrimitiveType.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Vertex.hpp>

#include <SSVUtils/Core/Common/LikelyUnlikely.hpp>

#include <cstddef>
#include <cstring>

namespace hg::Utils {

template <sf::PrimitiveType TPrimitive>
struct FastVertexVector : public sf::Drawable
{
private:
    union VertexUnion
    {
        // To avoid invoking the default constructor of `sf::Vertex` when
        // resizing the dynamic array.
        sf::Vertex _v;

        VertexUnion()
        {}
    };

    static_assert(sizeof(VertexUnion) == sizeof(sf::Vertex));
    static_assert(alignof(VertexUnion) == alignof(sf::Vertex));

    Utils::UniquePtrArray<VertexUnion> _data{nullptr};
    std::size_t _size{};
    std::size_t _capacity{};

public:
    [[gnu::always_inline]] void reserve_more(const std::size_t n)
    {
        reserve(_size * 2 + n);
    }

    void reserve(const std::size_t n)
    {
        if(SSVU_LIKELY(_capacity >= n))
        {
            return;
        }

        auto new_data = Utils::makeUniqueArray<VertexUnion>(n);

        if(SSVU_UNLIKELY(_data != nullptr))
        {
            std::memcpy(
                new_data.get(), _data.get(), sizeof(sf::Vertex) * _size);
        }
        else
        {
            SSVOH_ASSERT(_size == 0);
            SSVOH_ASSERT(_capacity == 0);
        }

        _data = std::move(new_data);
        _capacity = n;
    }

    [[gnu::always_inline]] void unsafe_emplace_other(
        const FastVertexVector& rhs) noexcept
    {
        SSVOH_ASSERT(_size + rhs._size <= _capacity);

        if(SSVU_UNLIKELY(rhs.size() == 0))
        {
            return;
        }

        SSVOH_ASSERT(_data != nullptr);

        std::memcpy(_data.get() + _size, rhs._data.get(),
            sizeof(sf::Vertex) * rhs._size);

        _size += rhs._size;
    }

    [[gnu::always_inline]] void clear() noexcept
    {
        _size = 0;
    }

    [[nodiscard, gnu::always_inline]] std::size_t size() const noexcept
    {
        return _size;
    }

    template <typename... Ts>
    [[gnu::always_inline]] void unsafe_emplace_back(Ts&&... xs)
    {
        SSVOH_ASSERT(_size <= _capacity);
        SSVOH_ASSERT(_data != nullptr);

        new(&_data[_size++]._v) sf::Vertex{std::forward<Ts>(xs)...};
    }

    template <typename... Ts>
    [[gnu::always_inline]] void batch_unsafe_emplace_back(
        const sf::Color& color, Ts&&... positions)
    {
        SSVOH_ASSERT(_size + sizeof...(positions) <= _capacity);
        SSVOH_ASSERT(_data != nullptr);

        ((new(&_data[_size++]._v) sf::Vertex{positions, color}), ...);
    }

    void draw(sf::RenderTarget& mRenderTarget,
        sf::RenderStates mRenderStates) const override
    {
        if(SSVU_UNLIKELY(_data == nullptr))
        {
            SSVOH_ASSERT(_size == 0);
            SSVOH_ASSERT(_capacity == 0);
            return;
        }

        // UB:
        mRenderTarget.draw(reinterpret_cast<const sf::Vertex*>(_data.get()),
            _size, TPrimitive, mRenderStates);
    }

    [[nodiscard, gnu::always_inline]] sf::Vertex& operator[](
        const std::size_t i) noexcept
    {
        SSVOH_ASSERT(i < _size);
        SSVOH_ASSERT(_data != nullptr);

        return _data[i]._v;
    }

    [[nodiscard, gnu::always_inline]] const sf::Vertex& operator[](
        const std::size_t i) const noexcept
    {
        SSVOH_ASSERT(i < _size);
        SSVOH_ASSERT(_data != nullptr);

        return _data[i]._v;
    }
};

class FastVertexVectorTris
    : public FastVertexVector<sf::PrimitiveType::Triangles>
{};

class FastVertexVectorQuads : public FastVertexVector<sf::PrimitiveType::Quads>
{};

} // namespace hg::Utils