typedef & PascalCase for ALL structs and enums
This commit is contained in:
@ -17,10 +17,10 @@
|
||||
*/
|
||||
/* TODO: it might make sense to infer alpha channel presence / meaningfulness for textures in atlas */
|
||||
/* so that they are rendered with no blend / batched in a way to reduce overdraw automatically */
|
||||
void push_sprite(const t_push_sprite_args args) {
|
||||
struct sprite_primitive sprite = {
|
||||
void push_sprite(const PushSpriteArgs args) {
|
||||
SpritePrimitive sprite = {
|
||||
.rect = args.rect,
|
||||
.color = m_or(args, color, ((t_color) { 255, 255, 255, 255 })),
|
||||
.color = m_or(args, color, ((Color) { 255, 255, 255, 255 })),
|
||||
.rotation = m_or(args, rotation, 0.0f),
|
||||
.texture_key = textures_get_key(&ctx.texture_cache, args.path),
|
||||
.flip_x = m_or(args, flip_x, false),
|
||||
@ -29,7 +29,7 @@ void push_sprite(const t_push_sprite_args args) {
|
||||
m_opt_from(texture_region, args, texture_region)
|
||||
};
|
||||
|
||||
struct primitive_2d primitive = {
|
||||
Primitive2D primitive = {
|
||||
.type = PRIMITIVE_2D_SPRITE,
|
||||
.sprite = sprite,
|
||||
};
|
||||
@ -38,12 +38,12 @@ void push_sprite(const t_push_sprite_args args) {
|
||||
}
|
||||
|
||||
|
||||
struct sprite_batch collect_sprite_batch(const struct primitive_2d primitives[], size_t len) {
|
||||
struct SpriteBatch collect_sprite_batch(const Primitive2D primitives[], size_t len) {
|
||||
/* assumes that first primitive is already a sprite */
|
||||
const uint16_t texture_key_id = primitives[0].sprite.texture_key.id;
|
||||
const int atlas_id = textures_get_atlas_id(&ctx.texture_cache, primitives[0].sprite.texture_key);
|
||||
|
||||
struct sprite_batch batch = {
|
||||
struct SpriteBatch batch = {
|
||||
.mode = textures_get_mode(&ctx.texture_cache, primitives[0].sprite.texture_key),
|
||||
.constant_colored = true,
|
||||
.repeat = primitives[0].sprite.repeat,
|
||||
@ -56,14 +56,14 @@ struct sprite_batch collect_sprite_batch(const struct primitive_2d primitives[],
|
||||
len = QUAD_ELEMENT_BUFFER_LENGTH;
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
const struct primitive_2d *const current = &primitives[i];
|
||||
const Primitive2D *const current = &primitives[i];
|
||||
|
||||
/* don't touch things other than sprites */
|
||||
if (current->type != PRIMITIVE_2D_SPRITE)
|
||||
break;
|
||||
|
||||
/* only collect the same blend modes */
|
||||
const enum texture_mode mode = textures_get_mode(&ctx.texture_cache, current->sprite.texture_key);
|
||||
const TextureMode mode = textures_get_mode(&ctx.texture_cache, current->sprite.texture_key);
|
||||
if (mode != batch.mode)
|
||||
break;
|
||||
|
||||
@ -94,32 +94,32 @@ struct sprite_batch collect_sprite_batch(const struct primitive_2d primitives[],
|
||||
|
||||
|
||||
/* assumes that orthogonal matrix setup is done already */
|
||||
void render_sprites(const struct primitive_2d primitives[],
|
||||
const struct sprite_batch batch)
|
||||
void render_sprites(const Primitive2D primitives[],
|
||||
const struct SpriteBatch batch)
|
||||
{
|
||||
/* single vertex array is used for every batch with NULL glBufferData() trick at the end */
|
||||
static vertex_buffer vertex_array = 0;
|
||||
static VertexBuffer vertex_array = 0;
|
||||
if (vertex_array == 0)
|
||||
vertex_array = create_vertex_buffer();
|
||||
|
||||
use_texture_mode(batch.mode);
|
||||
|
||||
const t_frect dims =
|
||||
const Rect dims =
|
||||
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
|
||||
|
||||
/* vertex population over a vertex buffer builder interface */
|
||||
{
|
||||
vertex_buffer_builder payload = build_vertex_buffer(vertex_array, get_sprite_payload_size(batch) * batch.size);
|
||||
VertexBufferBuilder payload = build_vertex_buffer(vertex_array, get_sprite_payload_size(batch) * batch.size);
|
||||
|
||||
for (size_t i = 0; i < batch.size; ++i) {
|
||||
/* render opaques front to back */
|
||||
const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
|
||||
const struct sprite_primitive sprite = primitives[cur].sprite;
|
||||
const SpritePrimitive sprite = primitives[cur].sprite;
|
||||
|
||||
const t_frect srcrect =
|
||||
const Rect srcrect =
|
||||
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
|
||||
|
||||
t_fvec2 uv0, uv1, uv2, uv3;
|
||||
Vec2 uv0, uv1, uv2, uv3;
|
||||
|
||||
if (!sprite.repeat) {
|
||||
const float wr = srcrect.w / dims.w;
|
||||
@ -128,20 +128,20 @@ void render_sprites(const struct primitive_2d primitives[],
|
||||
const float yr = srcrect.y / dims.h;
|
||||
|
||||
if (!m_is_set(sprite, texture_region)) {
|
||||
uv0 = (t_fvec2){ xr + wr * sprite.flip_x, yr + hr * sprite.flip_y };
|
||||
uv1 = (t_fvec2){ xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y };
|
||||
uv2 = (t_fvec2){ xr + wr * !sprite.flip_x, yr + hr * !sprite.flip_y };
|
||||
uv3 = (t_fvec2){ xr + wr * !sprite.flip_x, yr + hr * sprite.flip_y };
|
||||
uv0 = (Vec2){ xr + wr * sprite.flip_x, yr + hr * sprite.flip_y };
|
||||
uv1 = (Vec2){ xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y };
|
||||
uv2 = (Vec2){ xr + wr * !sprite.flip_x, yr + hr * !sprite.flip_y };
|
||||
uv3 = (Vec2){ xr + wr * !sprite.flip_x, yr + hr * sprite.flip_y };
|
||||
} else {
|
||||
const float offx = (sprite.texture_region_opt.x / srcrect.w) * (srcrect.w / dims.w);
|
||||
const float offy = (sprite.texture_region_opt.y / srcrect.h) * (srcrect.h / dims.h);
|
||||
const float offw = (1.0f - (sprite.texture_region_opt.w / srcrect.w)) * (srcrect.w / dims.w);
|
||||
const float offh = (1.0f - (sprite.texture_region_opt.h / srcrect.h)) * (srcrect.h / dims.h);
|
||||
|
||||
uv0 = (t_fvec2){ xr + offx + wr * sprite.flip_x, yr + offy + hr * sprite.flip_y };
|
||||
uv1 = (t_fvec2){ xr + offx + wr * sprite.flip_x, yr - offh + hr * !sprite.flip_y };
|
||||
uv2 = (t_fvec2){ xr - offw + wr * !sprite.flip_x, yr - offh + hr * !sprite.flip_y };
|
||||
uv3 = (t_fvec2){ xr - offw + wr * !sprite.flip_x, yr + offy + hr * sprite.flip_y };
|
||||
uv0 = (Vec2){ xr + offx + wr * sprite.flip_x, yr + offy + hr * sprite.flip_y };
|
||||
uv1 = (Vec2){ xr + offx + wr * sprite.flip_x, yr - offh + hr * !sprite.flip_y };
|
||||
uv2 = (Vec2){ xr - offw + wr * !sprite.flip_x, yr - offh + hr * !sprite.flip_y };
|
||||
uv3 = (Vec2){ xr - offw + wr * !sprite.flip_x, yr + offy + hr * sprite.flip_y };
|
||||
}
|
||||
} else {
|
||||
/* try fitting texture into supplied destination rectangle */
|
||||
@ -149,10 +149,10 @@ void render_sprites(const struct primitive_2d primitives[],
|
||||
const float rx = sprite.rect.w / srcrect.w;
|
||||
const float ry = sprite.rect.h / srcrect.h;
|
||||
|
||||
uv0 = (t_fvec2){ rx * sprite.flip_x, ry * sprite.flip_y };
|
||||
uv1 = (t_fvec2){ rx * sprite.flip_x, ry * !sprite.flip_y };
|
||||
uv2 = (t_fvec2){ rx * !sprite.flip_x, ry * !sprite.flip_y };
|
||||
uv3 = (t_fvec2){ rx * !sprite.flip_x, ry * sprite.flip_y };
|
||||
uv0 = (Vec2){ rx * sprite.flip_x, ry * sprite.flip_y };
|
||||
uv1 = (Vec2){ rx * sprite.flip_x, ry * !sprite.flip_y };
|
||||
uv2 = (Vec2){ rx * !sprite.flip_x, ry * !sprite.flip_y };
|
||||
uv3 = (Vec2){ rx * !sprite.flip_x, ry * sprite.flip_y };
|
||||
|
||||
if (m_is_set(sprite, texture_region)) {
|
||||
/* displace origin */
|
||||
@ -165,44 +165,44 @@ void render_sprites(const struct primitive_2d primitives[],
|
||||
}
|
||||
}
|
||||
|
||||
t_fvec2 v0, v1, v2, v3;
|
||||
Vec2 v0, v1, v2, v3;
|
||||
|
||||
/* todo: fast PI/2 degree divisible rotations? */
|
||||
if (sprite.rotation == 0.0f) {
|
||||
/* non-rotated case */
|
||||
|
||||
v0 = (t_fvec2){ sprite.rect.x, sprite.rect.y };
|
||||
v1 = (t_fvec2){ sprite.rect.x, sprite.rect.y + sprite.rect.h };
|
||||
v2 = (t_fvec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y + sprite.rect.h };
|
||||
v3 = (t_fvec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y };
|
||||
v0 = (Vec2){ sprite.rect.x, sprite.rect.y };
|
||||
v1 = (Vec2){ sprite.rect.x, sprite.rect.y + sprite.rect.h };
|
||||
v2 = (Vec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y + sprite.rect.h };
|
||||
v3 = (Vec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y };
|
||||
|
||||
} else if (sprite.rect.w == sprite.rect.h) {
|
||||
/* rotated square case */
|
||||
|
||||
const t_fvec2 c = frect_center(sprite.rect);
|
||||
const t_fvec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
|
||||
const t_fvec2 d = {
|
||||
const Vec2 c = frect_center(sprite.rect);
|
||||
const Vec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
|
||||
const Vec2 d = {
|
||||
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
|
||||
.y = t.y * sprite.rect.h * (float)M_SQRT1_2,
|
||||
};
|
||||
|
||||
v0 = (t_fvec2){ c.x - d.x, c.y - d.y };
|
||||
v1 = (t_fvec2){ c.x - d.y, c.y + d.x };
|
||||
v2 = (t_fvec2){ c.x + d.x, c.y + d.y };
|
||||
v3 = (t_fvec2){ c.x + d.y, c.y - d.x };
|
||||
v0 = (Vec2){ c.x - d.x, c.y - d.y };
|
||||
v1 = (Vec2){ c.x - d.y, c.y + d.x };
|
||||
v2 = (Vec2){ c.x + d.x, c.y + d.y };
|
||||
v3 = (Vec2){ c.x + d.y, c.y - d.x };
|
||||
|
||||
} else {
|
||||
/* rotated non-square case*/
|
||||
|
||||
const t_fvec2 c = frect_center(sprite.rect);
|
||||
const t_fvec2 t = fast_cossine(sprite.rotation);
|
||||
const Vec2 c = frect_center(sprite.rect);
|
||||
const Vec2 t = fast_cossine(sprite.rotation);
|
||||
|
||||
const t_fvec2 h = { sprite.rect.w / 2, sprite.rect.h / 2 };
|
||||
const Vec2 h = { sprite.rect.w / 2, sprite.rect.h / 2 };
|
||||
|
||||
v0 = (t_fvec2){ c.x + t.x * -h.x - t.y * -h.y, c.y + t.y * -h.x + t.x * -h.y };
|
||||
v1 = (t_fvec2){ c.x + t.x * -h.x - t.y * +h.y, c.y + t.y * -h.x + t.x * +h.y };
|
||||
v2 = (t_fvec2){ c.x + t.x * +h.x - t.y * +h.y, c.y + t.y * +h.x + t.x * +h.y };
|
||||
v3 = (t_fvec2){ c.x + t.x * +h.x - t.y * -h.y, c.y + t.y * +h.x + t.x * -h.y };
|
||||
v0 = (Vec2){ c.x + t.x * -h.x - t.y * -h.y, c.y + t.y * -h.x + t.x * -h.y };
|
||||
v1 = (Vec2){ c.x + t.x * -h.x - t.y * +h.y, c.y + t.y * -h.x + t.x * +h.y };
|
||||
v2 = (Vec2){ c.x + t.x * +h.x - t.y * +h.y, c.y + t.y * +h.x + t.x * +h.y };
|
||||
v3 = (Vec2){ c.x + t.x * +h.x - t.y * -h.y, c.y + t.y * +h.x + t.x * -h.y };
|
||||
}
|
||||
|
||||
push_sprite_payload_to_vertex_buffer_builder(batch, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color);
|
||||
|
Reference in New Issue
Block a user