summaryrefslogtreecommitdiff
path: root/include/SSVOpenHexagon/Utils/FastVertexVector.hpp
blob: 33545bee7575e2a536e1277b4d9a82ed9fb1a146 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// 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;
    }

    [[nodiscard, gnu::always_inline]] sf::Vertex* begin() noexcept
    {
        SSVOH_ASSERT(_data != nullptr);
        return &(_data[0]._v);
    }

    [[nodiscard, gnu::always_inline]] const sf::Vertex* begin() const noexcept
    {
        SSVOH_ASSERT(_data != nullptr);
        return &(_data[0]._v);
    }

    [[nodiscard, gnu::always_inline]] sf::Vertex* end() noexcept
    {
        return begin() + _size;
    }

    [[nodiscard, gnu::always_inline]] const sf::Vertex* end() const noexcept
    {
        return begin() + _size;
    }
};

class FastVertexVectorTris
    : public FastVertexVector<sf::PrimitiveType::Triangles>
{
public:
    [[gnu::always_inline]] void batch_unsafe_emplace_back_quad(
        const sf::Color& color, const sf::Vector2f& nw, const sf::Vector2f& sw,
        const sf::Vector2f& se, const sf::Vector2f& ne)
    {
        batch_unsafe_emplace_back(color, //
            nw, sw, se,                  //
            nw, se, ne);
    }

    [[gnu::always_inline]] void unsafe_emplace_back_quad( //
        const sf::Vector2f& nw, const sf::Color& colorNW, //
        const sf::Vector2f& sw, const sf::Color& colorSW, //
        const sf::Vector2f& se, const sf::Color& colorSE, //
        const sf::Vector2f& ne, const sf::Color& colorNE)
    {
        unsafe_emplace_back(nw, colorNW);
        unsafe_emplace_back(sw, colorSW);
        unsafe_emplace_back(se, colorSE);
        unsafe_emplace_back(nw, colorNW);
        unsafe_emplace_back(se, colorSE);
        unsafe_emplace_back(ne, colorNE);
    }

    [[gnu::always_inline]] void reserve_more_quad(const std::size_t n)
    {
        reserve_more(n * 6);
    }

    [[gnu::always_inline]] void reserve_quad(const std::size_t n)
    {
        reserve(n * 6);
    }
};

} // namespace hg::Utils