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,13 +1,13 @@
#include "twn_util.h"
#include "twn_engine_context_c.h"
#include "twn_rendering_c.h"
#include "twn_rendering.h"
#include "twn_draw_c.h"
#include "twn_draw.h"
#include <SDL2/SDL.h>
#include <stb_ds.h>
void push_circle(Vec2 position, float radius, Color color) {
void draw_circle(Vec2 position, float radius, Color color) {
CirclePrimitive circle = {
.radius = radius,
.color = color,
@ -25,11 +25,11 @@ void push_circle(Vec2 position, float radius, Color color) {
/* TODO: caching and reuse scheme */
/* vertices_out and indices_out MUST BE FREED */
void create_circle_geometry(Vec2 position,
Color color,
float radius,
size_t num_vertices,
SDL_Vertex **vertices_out,
int **indices_out)
Color color,
float radius,
size_t num_vertices,
SDL_Vertex **vertices_out,
int **indices_out)
{
SDL_Vertex *vertices = cmalloc(sizeof *vertices * (num_vertices + 1));
int *indices = cmalloc(sizeof *indices * (num_vertices * 3));

View File

@ -1,7 +1,8 @@
#include "twn_rendering_c.h"
#include "twn_rendering.h"
#include "twn_draw_c.h"
#include "twn_draw.h"
#include "twn_engine_context_c.h"
#include "twn_camera.h"
#include "twn_types.h"
#include <SDL2/SDL.h>
#include <stb_ds.h>
@ -35,7 +36,7 @@ void render_queue_clear(void) {
/* rectangle */
void push_rectangle(Rect rect, Color color) {
void draw_rectangle(Rect rect, Color color) {
RectPrimitive rectangle = {
.rect = rect,
.color = color,
@ -50,7 +51,7 @@ void push_rectangle(Rect rect, Color color) {
}
void push_9slice(char *texture_path, int texture_w, int texture_h, int border_thickness, Rect rect, Color color) {
void draw_9slice(const char *texture_path, int texture_w, int texture_h, int border_thickness, Rect rect, Color color) {
const float bt = (float)border_thickness; /* i know! */
const float bt2 = bt * 2; /* combined size of the two borders in an axis */
@ -279,7 +280,7 @@ void render(void) {
}
void set_camera(const Camera *const camera) {
void draw_camera(const Camera *const camera) {
/* TODO: skip recaulculating if it's the same? */
camera_projection_matrix = camera_perspective(camera);
camera_look_at_matrix = camera_look_at(camera);

View File

@ -1,5 +1,5 @@
#ifndef TWN_RENDERING_C_H
#define TWN_RENDERING_C_H
#ifndef TWN_DRAW_C_H
#define TWN_DRAW_C_H
#include "twn_textures_c.h"
#include "twn_text_c.h"

View File

@ -1,5 +1,5 @@
#include "twn_rendering.h"
#include "twn_rendering_c.h"
#include "twn_draw.h"
#include "twn_draw_c.h"
#include "twn_util.h"
#include <stdbool.h>
@ -9,7 +9,7 @@ static Color color_cache;
static bool fog_used = false;
void push_fog(float start, float end, float density, Color color) {
void draw_fog(float start, float end, float density, Color color) {
start_cache = start;
end_cache = end;
density_cache = density;

View File

@ -1,4 +1,4 @@
#include "twn_rendering_c.h"
#include "twn_draw_c.h"
#include "twn_util.h"
#include "twn_util_c.h"
#include "twn_config.h"

View File

@ -1,4 +1,4 @@
#include "twn_rendering_c.h"
#include "twn_draw_c.h"
#include "twn_engine_context_c.h"
#include "twn_util.h"

View File

@ -1,11 +1,11 @@
#include "twn_rendering.h"
#include "twn_rendering_c.h"
#include "twn_draw.h"
#include "twn_draw_c.h"
#include <SDL2/SDL.h>
char *paths_in_use;
void push_skybox(const char *paths) {
void draw_skybox(const char *paths) {
if (paths_in_use && SDL_strcmp(paths, paths_in_use) == 0)
return;

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 */

View File

@ -1,5 +1,5 @@
#include "twn_rendering_c.h"
#include "twn_rendering.h"
#include "twn_draw_c.h"
#include "twn_draw.h"
#include "twn_util.h"
#include "twn_config.h"
#include "twn_engine_context_c.h"
@ -248,7 +248,7 @@ void text_cache_reset_arena(TextCache *cache) {
}
void push_text(char *string, Vec2 position, int height_px, Color color, const char *font_path) {
void draw_text(const char *string, Vec2 position, int height_px, Color color, const char *font_path) {
ensure_font_cache(font_path, height_px);
/* the original string might not be around by the time it's used, so copy it */
@ -273,7 +273,7 @@ void push_text(char *string, Vec2 position, int height_px, Color color, const ch
}
int text_get_width(char *string, int height_px, const char *font_path) {
int draw_text_width(const char *string, int height_px, const char *font_path) {
ensure_font_cache(font_path, height_px);
FontData *font_data = get_font_data(font_path, height_px);

View File

@ -1,20 +1,21 @@
#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_textures_c.h"
#include "twn_types.h"
#include <stb_ds.h>
/* TODO: automatic handling of repeating textures */
/* for that we could allocate a loner texture */
void unfurl_triangle(const char *path,
Vec3 v0,
Vec3 v1,
Vec3 v2,
Vec2 uv0,
Vec2 uv1,
Vec2 uv2)
void draw_triangle(const char *path,
Vec3 v0,
Vec3 v1,
Vec3 v2,
Vec2 uv0,
Vec2 uv1,
Vec2 uv2)
{
const TextureKey texture_key = textures_get_key(&ctx.texture_cache, path);

View File

@ -5,7 +5,7 @@
#include "twn_textures_c.h"
#include "twn_audio_c.h"
#include "twn_engine_api.h"
#include "rendering/twn_rendering_c.h"
#include "rendering/twn_draw_c.h"
#include <SDL2/SDL.h>
#include <toml.h>