nngn
Loading...
Searching...
No Matches
renderers.h
Go to the documentation of this file.
1#ifndef NNGN_RENDER_RENDERERS_H
2#define NNGN_RENDER_RENDERERS_H
3
4#include "lua/table.h"
5#include "math/vec2.h"
6#include "math/vec3.h"
7#include "math/vec4.h"
8#include "utils/def.h"
9#include "utils/flags.h"
10
11struct Entity;
12
13namespace nngn {
14
15struct Renderer {
16 enum Type : u8 {
17 SPRITE = 1,
19 };
20 enum Flag : u8 { UPDATED = 1u << 0 };
21 Entity *entity = nullptr;
22 vec3 pos = {};
23 float z_off = 0;
25 bool updated() const { return this->flags.is_set(Flag::UPDATED); }
26 void set_pos(vec3 p) { this->pos = p; this->flags |= Flag::UPDATED; }
27};
28
30 vec2 size = {};
31 std::array<vec2, 2> uv = {{{0, 1}, {1, 0}}};
32 u32 tex = 0;
33 static void uv_coords(
34 uvec2 uv0, uvec2 uv1, uvec2 scale, std::span<float> s);
35 template<typename T, std::size_t N>
36 static std::span<float> uv_span(std::array<T, N> *a);
38};
39
40template<typename T, std::size_t N>
41std::span<float> SpriteRenderer::uv_span(std::array<T, N> *a) {
42 constexpr auto n = N * T::n_dim;
43 static_assert(sizeof(*a) == n * sizeof(float));
44 return {&(*a)[0][0], n};
45}
46
48 uvec2 uv0, uvec2 uv1, uvec2 scale, std::span<float> s)
49{
50 assert(3 <= s.size());
51 const auto fs = static_cast<vec2>(scale);
52 const auto fc0 = static_cast<vec2>(uv0) / fs;
53 const auto fc1 = static_cast<vec2>(uv1) / fs;
54 s[0] = fc0.x, s[1] = 1 - fc0.y;
55 s[2] = fc1.x, s[3] = 1 - fc1.y;
56}
57
58}
59
60#endif
precision highp float
Definition: common.h:14
assert
Definition: debug.lua:3
Definition: debug.h:45
std::uint8_t u8
Definition: def.h:12
std::uint32_t u32
Definition: def.h:14
load
Definition: entity.lua:5
Definition: entity.h:19
Wrapper for a small unsigned integral representing flags.
Definition: flags.h:18
constexpr bool is_set(AT a) const
Definition: flags.h:45
Definition: renderers.h:15
float z_off
Definition: renderers.h:23
Flags< Flag > flags
Definition: renderers.h:24
void set_pos(vec3 p)
Definition: renderers.h:26
Flag
Definition: renderers.h:20
@ UPDATED
Definition: renderers.h:20
bool updated() const
Definition: renderers.h:25
Type
Definition: renderers.h:16
@ SPRITE
Definition: renderers.h:17
@ N_TYPES
Definition: renderers.h:18
Entity * entity
Definition: renderers.h:21
vec3 pos
Definition: renderers.h:22
Definition: renderers.h:29
std::array< vec2, 2 > uv
Definition: renderers.h:31
vec2 size
Definition: renderers.h:30
u32 tex
Definition: renderers.h:32
static std::span< float > uv_span(std::array< T, N > *a)
Definition: renderers.h:41
static void uv_coords(uvec2 uv0, uvec2 uv1, uvec2 scale, std::span< float > s)
Definition: renderers.h:47
Non-owning reference to a table on the stack.
Definition: table.h:166
T x
Definition: vec2.h:12
Operations on table values.
std::chrono::seconds s
Definition: timing.cpp:6