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 {
24 enum Flag : u8 { UPDATED = 1u << 0 };
25 Entity *entity = nullptr;
26 vec3 pos = {};
27 float z_off = 0;
29 bool updated() const { return this->flags.is_set(Flag::UPDATED); }
30 void set_pos(vec3 p) { this->pos = p; this->flags |= Flag::UPDATED; }
31};
32
34 vec2 size = {};
35 std::array<vec2, 2> uv = {{{0, 1}, {1, 0}}};
36 u32 tex = 0;
37 static void uv_coords(
38 uvec2 uv0, uvec2 uv1, uvec2 scale, std::span<float> s);
39 template<typename T, std::size_t N>
40 static std::span<float> uv_span(std::array<T, N> *a);
42};
43
45 vec3 color = {1, 1, 1};
46 float size = 0;
47 void load(const nngn::lua::table &t);
48};
49
51 std::array<vec4, 6> uv = {};
52 vec3 size = {};
53 u32 tex = 0;
54 void load(const nngn::lua::table &t);
55};
56
57template<typename T, std::size_t N>
58std::span<float> SpriteRenderer::uv_span(std::array<T, N> *a) {
59 constexpr auto n = N * T::n_dim;
60 static_assert(sizeof(*a) == n * sizeof(float));
61 return {&(*a)[0][0], n};
62}
63
65 uvec2 uv0, uvec2 uv1, uvec2 scale, std::span<float> s)
66{
67 assert(3 <= s.size());
68 const auto fs = static_cast<vec2>(scale);
69 const auto fc0 = static_cast<vec2>(uv0) / fs;
70 const auto fc1 = static_cast<vec2>(uv1) / fs;
71 s[0] = fc0.x, s[1] = 1 - fc0.y;
72 s[2] = fc1.x, s[3] = 1 - fc1.y;
73}
74
75}
76
77#endif
precision highp float
Definition common.h:15
assert
Definition debug.lua:3
local n
Definition dump_lights.lua:5
local N
Definition gamma.lua:5
pos
Definition lua_audio.cpp:17
Definition audio.cpp:7
std::uint32_t u32
Definition def.h:14
std::uint8_t u8
Definition def.h:12
load
Definition entity.lua:7
local a
Definition input.lua:35
Definition entity.h:24
Definition renderers.h:44
float size
Definition renderers.h:46
vec3 color
Definition renderers.h:45
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:27
Flags< Flag > flags
Definition renderers.h:28
void set_pos(vec3 p)
Definition renderers.h:30
Flag
Definition renderers.h:24
@ UPDATED
Definition renderers.h:24
bool updated() const
Definition renderers.h:29
Type
Definition renderers.h:16
@ CUBE
Definition renderers.h:20
@ SPRITE
Definition renderers.h:17
@ TRANSLUCENT
Definition renderers.h:19
@ VOXEL
Definition renderers.h:21
@ SCREEN_SPRITE
Definition renderers.h:18
@ N_TYPES
Definition renderers.h:22
Entity * entity
Definition renderers.h:25
Definition renderers.h:33
std::array< vec2, 2 > uv
Definition renderers.h:35
vec2 size
Definition renderers.h:34
u32 tex
Definition renderers.h:36
static std::span< float > uv_span(std::array< T, N > *a)
Definition renderers.h:58
static void uv_coords(uvec2 uv0, uvec2 uv1, uvec2 scale, std::span< float > s)
Definition renderers.h:64
Definition renderers.h:50
u32 tex
Definition renderers.h:53
std::array< vec4, 6 > uv
Definition renderers.h:51
vec3 size
Definition renderers.h:52
Non-owning reference to a table on the stack.
Definition table.h:166
Owning reference to a table on the stack, popped when destroyed.
Definition table.h:172
T x
Definition vec2.h:12
Operations on table values.
std::chrono::seconds s
Definition timing.cpp:6