twn_rendering -> twn_draw

This commit is contained in:
2024-10-07 17:53:09 +03:00
parent 9353999a30
commit ade1af12ca
22 changed files with 238 additions and 186 deletions

View File

@ -1,9 +1,10 @@
#include "twn_rendering.h"
#include "twn_rendering_c.h"
#include "twn_draw.h"
#include "twn_draw_c.h"
#include "twn_engine_context_c.h"
#include "twn_util.h"
#include "twn_util_c.h"
#include "twn_textures_c.h"
#include "twn_option.h"
#include <stb_ds.h>
@ -16,20 +17,29 @@
* because they will be called multiple times in the main loop
* before anything is really rendered
*/
/* 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 PushSpriteArgs args) {
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)
{
SpritePrimitive sprite = {
.rect = args.rect,
.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),
.flip_y = m_or(args, flip_y, false),
.repeat = !m_or(args, stretch, true),
m_opt_from(texture_region, args, texture_region)
.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,
};
if (texture_region)
sprite.texture_region_opt = *texture_region;
Primitive2D primitive = {
.type = PRIMITIVE_2D_SPRITE,
.sprite = sprite,
@ -38,6 +48,17 @@ void push_sprite(const PushSpriteArgs args) {
arrput(ctx.render_queue_2d, primitive);
}
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);
}
struct SpriteBatch collect_sprite_batch(const Primitive2D primitives[], size_t len) {
/* assumes that first primitive is already a sprite */