Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
primitives.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include "flint/core/api.h"
5#include "flint/graphics/drawable.h"
6#include "flint/graphics/renderer.h"
7#include "flint/graphics/types/color.h"
8#include "flint/graphics/types/primitive-types.h"
9#include "flint/math/types.h"
10
11#include <vector>
12
13namespace Flint {
14
23 class PrimitiveArray : public Drawable {
24 public:
25 PrimitiveArray() = default;
26
32 PrimitiveArray(PrimitiveType type)
33 : m_type(type) {
34 }
35
36 FLINT_API ~PrimitiveArray() override;
37
43 void setPrimitiveType(PrimitiveType type) {
44 m_type = type;
45 }
46
52 void init(SDL_GPUDevice *device) {
53 m_device = device;
54 }
55
63 void resize(std::size_t count) {
64 m_vertices.resize(count);
65 }
66
70 std::size_t size() const {
71 return m_vertices.size();
72 }
73
75 PrimitiveVertex &operator[](std::size_t i) {
76 return m_vertices[i];
77 }
78
80 const PrimitiveVertex &operator[](std::size_t i) const {
81 return m_vertices[i];
82 }
83
89 FLINT_API SDL_GPUPrimitiveType getSDLPrimitiveType() const;
90
95 FLINT_API void emit(RenderQueue &queue) const override;
96
102 const void *getTransformedData() const {
103 return m_transformedVerts.data();
104 }
105
109 size_t getTransformedSize() const {
110 return m_transformedVerts.size() * sizeof(GPULineVertex);
111 }
112
116 size_t getVertexCount() const {
117 return m_transformedVerts.size();
118 }
119
120 private:
124 struct GPULineVertex {
125 float pos[2];
126 float color[4];
127 };
128
129 PrimitiveType m_type;
130 std::vector<PrimitiveVertex> m_vertices;
131 SDL_GPUDevice *m_device = nullptr;
132
133 mutable std::vector<GPULineVertex> m_transformedVerts;
134
135 mutable SDL_GPUBuffer *m_vertexBuffer = nullptr;
136 mutable Uint32 m_vertexBufferSize = 0;
137 };
138}
Interface for objects that contribute ordered work to a rendered frame.
Definition drawable.h:21
A CPU-side collection of colored 2-D vertices that can be drawn as primitives.
Definition primitives.h:23
void setPrimitiveType(PrimitiveType type)
Change the primitive topology.
Definition primitives.h:43
size_t getVertexCount() const
Return the number of transformed vertices ready for upload.
Definition primitives.h:116
FLINT_API SDL_GPUPrimitiveType getSDLPrimitiveType() const
Map the engine PrimitiveType to the corresponding SDL GPU primitive type.
FLINT_API void emit(RenderQueue &queue) const override
Emit this drawable's rendering work for the current frame.
const PrimitiveVertex & operator[](std::size_t i) const
Const element access.
Definition primitives.h:80
std::size_t size() const
Return the current number of vertices.
Definition primitives.h:70
PrimitiveArray(PrimitiveType type)
Construct a PrimitiveArray with the given topology.
Definition primitives.h:32
PrimitiveVertex & operator[](std::size_t i)
Non-const element access.
Definition primitives.h:75
void resize(std::size_t count)
Resize the vertex array to count entries.
Definition primitives.h:63
size_t getTransformedSize() const
Return the byte size of the transformed vertex buffer.
Definition primitives.h:109
void init(SDL_GPUDevice *device)
Associate a GPU device (required before draw() is called by the renderer).
Definition primitives.h:52
const void * getTransformedData() const
Return a pointer to the most recently transformed vertex data.
Definition primitives.h:102
Batches drawables into sorted geometry, primitive, and custom execution passes for GPURenderer.
Internal vertex structure sent directly to line/point primitive shaders.
Public user-facing vertex representation for point and line primitives.