2024-10-07 14:53:09 +00:00
|
|
|
#include "twn_draw.h"
|
|
|
|
#include "twn_draw_c.h"
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_engine_context_c.h"
|
|
|
|
#include "twn_util.h"
|
2024-10-07 07:44:18 +00:00
|
|
|
#include "twn_util_c.h"
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_textures_c.h"
|
2024-10-07 14:53:09 +00:00
|
|
|
#include "twn_option.h"
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
#include <stb_ds.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* an implementation note:
|
|
|
|
* try to avoid doing expensive work in the push functions,
|
|
|
|
* because they will be called multiple times in the main loop
|
|
|
|
* before anything is really rendered
|
|
|
|
*/
|
2024-10-07 14:53:09 +00:00
|
|
|
void draw_sprite(char const *path,
|
|
|
|
Rect rect,
|
|
|
|
Rect const *texture_region, /* optional, default: NULL */
|
|
|
|
Color color, /* optional, default: all 255 */
|
|
|
|
float rotation, /* optional, default: 0 */
|
|
|
|
bool flip_x, /* optional, default: false */
|
|
|
|
bool flip_y, /* optional, default: false */
|
|
|
|
bool stretch)
|
|
|
|
{
|
2024-09-23 17:43:16 +00:00
|
|
|
SpritePrimitive sprite = {
|
2024-10-07 14:53:09 +00:00
|
|
|
.rect = rect,
|
|
|
|
.color = color,
|
|
|
|
.rotation = rotation,
|
|
|
|
.texture_key = textures_get_key(&ctx.texture_cache, path),
|
|
|
|
.flip_x = flip_x,
|
|
|
|
.flip_y = flip_y,
|
|
|
|
.repeat = !stretch,
|
|
|
|
.texture_region_opt_set = texture_region != NULL,
|
2024-07-27 12:10:19 +00:00
|
|
|
};
|
|
|
|
|
2024-10-07 14:53:09 +00:00
|
|
|
if (texture_region)
|
|
|
|
sprite.texture_region_opt = *texture_region;
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Primitive2D primitive = {
|
2024-07-27 12:10:19 +00:00
|
|
|
.type = PRIMITIVE_2D_SPRITE,
|
|
|
|
.sprite = sprite,
|
|
|
|
};
|
|
|
|
|
|
|
|
arrput(ctx.render_queue_2d, primitive);
|
|
|
|
}
|
|
|
|
|
2024-10-07 14:53:09 +00:00
|
|
|
void draw_sprite_args(const DrawSpriteArgs args) {
|
|
|
|
Color const color = m_or(args, color, ((Color) { 255, 255, 255, 255 }));
|
|
|
|
float const rotation = m_or(args, rotation, 0.0f);
|
|
|
|
bool const flip_x = m_or(args, flip_x, false);
|
|
|
|
bool const flip_y = m_or(args, flip_y, false);
|
|
|
|
bool const stretch = m_or(args, stretch, false);
|
|
|
|
Rect const *texture_region = m_is_set(args, texture_region) ? &args.texture_region_opt : NULL;
|
|
|
|
|
|
|
|
draw_sprite(args.path, args.rect, texture_region, color, rotation, flip_x, flip_y, stretch);
|
|
|
|
}
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
struct SpriteBatch collect_sprite_batch(const Primitive2D primitives[], size_t len) {
|
2024-07-27 12:10:19 +00:00
|
|
|
/* assumes that first primitive is already a sprite */
|
2024-07-31 21:23:32 +00:00
|
|
|
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);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
struct SpriteBatch batch = {
|
2024-07-28 20:59:23 +00:00
|
|
|
.mode = textures_get_mode(&ctx.texture_cache, primitives[0].sprite.texture_key),
|
2024-07-28 13:25:25 +00:00
|
|
|
.constant_colored = true,
|
2024-07-31 21:23:32 +00:00
|
|
|
.repeat = primitives[0].sprite.repeat,
|
2024-07-27 12:10:19 +00:00
|
|
|
};
|
|
|
|
|
2024-07-28 19:23:28 +00:00
|
|
|
const uint32_t uniform_color = *(const uint32_t *)&primitives[0].sprite.color;
|
2024-07-28 13:25:25 +00:00
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
/* batch size is clamped so that reallocated short indices could be used */
|
|
|
|
if (len >= QUAD_ELEMENT_BUFFER_LENGTH)
|
|
|
|
len = QUAD_ELEMENT_BUFFER_LENGTH;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2024-09-23 17:43:16 +00:00
|
|
|
const Primitive2D *const current = &primitives[i];
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
/* don't touch things other than sprites */
|
|
|
|
if (current->type != PRIMITIVE_2D_SPRITE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* only collect the same blend modes */
|
2024-09-23 17:43:16 +00:00
|
|
|
const TextureMode mode = textures_get_mode(&ctx.texture_cache, current->sprite.texture_key);
|
2024-07-28 20:59:23 +00:00
|
|
|
if (mode != batch.mode)
|
2024-07-27 12:10:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* only collect the same texture atlases */
|
2024-07-31 21:23:32 +00:00
|
|
|
if (textures_get_atlas_id(&ctx.texture_cache, current->sprite.texture_key) != atlas_id)
|
2024-07-27 12:10:19 +00:00
|
|
|
break;
|
|
|
|
|
2024-07-31 21:23:32 +00:00
|
|
|
/* repeated textures require separate handling */
|
|
|
|
if (batch.repeat) {
|
|
|
|
/* all must be repeated */
|
|
|
|
if (!current->sprite.repeat)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* all must be of same texture id, not just atlas id */
|
|
|
|
if (current->sprite.texture_key.id != texture_key_id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
/* if all are modulated the same we can skip sending the color data */
|
2024-07-31 21:23:32 +00:00
|
|
|
if (*(const uint32_t *)¤t->sprite.color != uniform_color)
|
2024-07-28 13:25:25 +00:00
|
|
|
batch.constant_colored = false;
|
2024-07-28 13:06:47 +00:00
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
++batch.size;
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
return batch;
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* assumes that orthogonal matrix setup is done already */
|
2024-09-23 17:43:16 +00:00
|
|
|
void render_sprites(const Primitive2D primitives[],
|
|
|
|
const struct SpriteBatch batch)
|
2024-07-27 12:10:19 +00:00
|
|
|
{
|
2024-07-28 11:39:23 +00:00
|
|
|
/* single vertex array is used for every batch with NULL glBufferData() trick at the end */
|
2024-09-23 17:43:16 +00:00
|
|
|
static VertexBuffer vertex_array = 0;
|
2024-07-27 12:10:19 +00:00
|
|
|
if (vertex_array == 0)
|
2024-09-16 06:07:01 +00:00
|
|
|
vertex_array = create_vertex_buffer();
|
2024-07-28 13:06:47 +00:00
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
use_texture_mode(batch.mode);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const Rect dims =
|
2024-07-27 12:10:19 +00:00
|
|
|
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
|
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
/* vertex population over a vertex buffer builder interface */
|
2024-07-27 12:10:19 +00:00
|
|
|
{
|
2024-09-23 17:43:16 +00:00
|
|
|
VertexBufferBuilder payload = build_vertex_buffer(vertex_array, get_sprite_payload_size(batch) * batch.size);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
for (size_t i = 0; i < batch.size; ++i) {
|
2024-07-28 20:59:23 +00:00
|
|
|
/* render opaques front to back */
|
|
|
|
const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
|
2024-09-23 17:43:16 +00:00
|
|
|
const SpritePrimitive sprite = primitives[cur].sprite;
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-10-07 22:20:42 +00:00
|
|
|
/* TODO: try caching it */
|
2024-09-23 17:43:16 +00:00
|
|
|
const Rect srcrect =
|
2024-07-28 11:39:23 +00:00
|
|
|
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Vec2 uv0, uv1, uv2, uv3;
|
2024-07-31 21:23:32 +00:00
|
|
|
|
|
|
|
if (!sprite.repeat) {
|
|
|
|
const float wr = srcrect.w / dims.w;
|
|
|
|
const float hr = srcrect.h / dims.h;
|
|
|
|
const float xr = srcrect.x / dims.w;
|
|
|
|
const float yr = srcrect.y / dims.h;
|
2024-07-28 11:39:23 +00:00
|
|
|
|
2024-09-21 17:07:05 +00:00
|
|
|
if (!m_is_set(sprite, texture_region)) {
|
2024-09-23 17:43:16 +00:00
|
|
|
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 };
|
2024-09-21 17:07:05 +00:00
|
|
|
} else {
|
2024-10-02 16:39:27 +00:00
|
|
|
/* TODO: support for flipping */
|
|
|
|
uv0 = (Vec2){ (srcrect.x + sprite.texture_region_opt.x) / dims.w,
|
|
|
|
(srcrect.y + sprite.texture_region_opt.y) / dims.h };
|
|
|
|
uv1 = (Vec2){ (srcrect.x + sprite.texture_region_opt.x) / dims.w,
|
|
|
|
(srcrect.y + sprite.texture_region_opt.y + sprite.texture_region_opt.h) / dims.h };
|
|
|
|
uv2 = (Vec2){ (srcrect.x + sprite.texture_region_opt.x + sprite.texture_region_opt.w) / dims.w,
|
|
|
|
(srcrect.y + sprite.texture_region_opt.y + sprite.texture_region_opt.h) / dims.h };
|
|
|
|
uv3 = (Vec2){ (srcrect.x + sprite.texture_region_opt.x + sprite.texture_region_opt.w) / dims.w,
|
|
|
|
(srcrect.y + sprite.texture_region_opt.y) / dims.h };
|
2024-09-21 17:07:05 +00:00
|
|
|
}
|
2024-07-31 21:23:32 +00:00
|
|
|
} else {
|
|
|
|
/* try fitting texture into supplied destination rectangle */
|
|
|
|
|
|
|
|
const float rx = sprite.rect.w / srcrect.w;
|
|
|
|
const float ry = sprite.rect.h / srcrect.h;
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
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 };
|
2024-07-31 21:52:15 +00:00
|
|
|
|
2024-09-21 17:07:05 +00:00
|
|
|
if (m_is_set(sprite, texture_region)) {
|
2024-07-31 21:52:15 +00:00
|
|
|
/* displace origin */
|
|
|
|
|
2024-09-21 17:07:05 +00:00
|
|
|
const float ax = sprite.texture_region_opt.x / srcrect.w;
|
|
|
|
const float ay = sprite.texture_region_opt.y / srcrect.h;
|
2024-07-31 21:52:15 +00:00
|
|
|
|
|
|
|
uv0.x += ax; uv1.x += ax; uv2.x += ax; uv3.x += ax;
|
|
|
|
uv0.y += ay; uv1.y += ay; uv2.y += ay; uv3.y += ay;
|
|
|
|
}
|
2024-07-31 21:23:32 +00:00
|
|
|
}
|
2024-07-28 13:06:47 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Vec2 v0, v1, v2, v3;
|
2024-07-28 13:06:47 +00:00
|
|
|
|
|
|
|
/* todo: fast PI/2 degree divisible rotations? */
|
2024-07-28 11:39:23 +00:00
|
|
|
if (sprite.rotation == 0.0f) {
|
2024-07-28 13:06:47 +00:00
|
|
|
/* non-rotated case */
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
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 };
|
2024-07-28 13:06:47 +00:00
|
|
|
|
2024-10-13 18:32:31 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
} else if (sprite.rect.w == sprite.rect.h) {
|
|
|
|
/* rotated square case */
|
2024-07-28 11:39:23 +00:00
|
|
|
|
2024-10-13 18:32:31 +00:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const Vec2 c = frect_center(sprite.rect);
|
|
|
|
const Vec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
|
|
|
|
const Vec2 d = {
|
2024-07-28 11:39:23 +00:00
|
|
|
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
|
|
|
|
.y = t.y * sprite.rect.h * (float)M_SQRT1_2,
|
|
|
|
};
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
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 };
|
2024-07-28 13:06:47 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* rotated non-square case*/
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const Vec2 c = frect_center(sprite.rect);
|
|
|
|
const Vec2 t = fast_cossine(sprite.rotation);
|
2024-07-29 09:43:46 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const Vec2 h = { sprite.rect.w / 2, sprite.rect.h / 2 };
|
2024-07-29 09:43:46 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
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 };
|
2024-07-28 13:06:47 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
push_sprite_payload_to_vertex_buffer_builder(batch, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color);
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
finally_render_sprites(primitives, batch, vertex_array);
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|