twn_rendering -> twn_draw

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

View File

@ -99,7 +99,7 @@ set(TWN_SOURCE_FILES
src/twn_camera.c include/twn_camera.h
src/twn_textures.c src/twn_textures_c.h
src/rendering/twn_rendering.c src/rendering/twn_rendering_c.h
src/rendering/twn_draw.c src/rendering/twn_draw_c.h
src/rendering/twn_sprites.c
src/rendering/twn_text.c
src/rendering/twn_triangles.c

View File

@ -252,7 +252,7 @@ static void drawdef(Player *player) {
.h = player->sprite_h,
});
push_circle((Vec2) { 256, 128 },
draw_circle((Vec2) { 256, 128 },
24,
(Color) { 255, 0, 0, 255 });
}
@ -268,10 +268,10 @@ static void drawdef_debug(Player *player) {
/* .r = 0, .g = 0, .b = 0, .a = 255, */
/* }; */
push_rectangle(player->collider_x,
draw_rectangle(player->collider_x,
(Color){ 0, 0, 255, 128 });
push_rectangle(player->collider_y,
draw_rectangle(player->collider_y,
(Color){ 0, 0, 255, 128 });
}

View File

@ -30,9 +30,9 @@ static void title_tick(State *state) {
const char *font = "fonts/kenney-pixel.ttf";
int text_h = 32;
int text_w = text_get_width(text_str, text_h, font);
int text_w = draw_text_width(text_str, text_h, font);
push_rectangle(
draw_rectangle(
(Rect) {
.x = 0,
.y = 0,
@ -41,7 +41,7 @@ static void title_tick(State *state) {
},
(Color) { 0, 0, 0, 255 }
);
push_text(
draw_text(
text_str,
(Vec2){ 0, 0 },
text_h,

View File

@ -39,7 +39,7 @@ static void drawdef_debug(struct World *world) {
for (size_t i = 0; i < world->tilemap_height * world->tilemap_width; ++i) {
if (world->tiles[i].type == TILE_TYPE_VOID) continue;
push_rectangle(to_frect(world->tiles[i].rect),
draw_rectangle(to_frect(world->tiles[i].rect),
(Color) { 255, 0, 255, 128 });
}
}

View File

@ -55,7 +55,7 @@ static void ingame_tick(State *state) {
input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input));
}
set_camera(&scn->cam);
draw_camera(&scn->cam);
#define TERRAIN_FREQUENCY 0.1f
@ -69,7 +69,7 @@ static void ingame_tick(State *state) {
float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
unfurl_triangle("/assets/grass.png",
draw_triangle("/assets/grass.png",
(Vec3){ (float)x, d0, (float)y },
(Vec3){ (float)x + 1, d1, (float)y },
(Vec3){ (float)x, d3, (float)y - 1 },
@ -77,7 +77,7 @@ static void ingame_tick(State *state) {
(Vec2){ 128, 0 },
(Vec2){ 0, 128 });
unfurl_triangle("/assets/grass.png",
draw_triangle("/assets/grass.png",
(Vec3){ (float)x + 1, d1, (float)y },
(Vec3){ (float)x + 1, d2, (float)y - 1 },
(Vec3){ (float)x, d3, (float)y - 1 },
@ -87,8 +87,8 @@ static void ingame_tick(State *state) {
}
}
push_skybox("/assets/miramar/miramar_*.tga");
push_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 });
draw_skybox("/assets/miramar/miramar_*.tga");
draw_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 });
}

View File

@ -27,9 +27,9 @@ static void title_tick(State *state) {
const char *font = "/fonts/kenney-pixel.ttf";
int text_h = 32;
int text_w = text_get_width(text_str, text_h, font);
int text_w = draw_text_width(text_str, text_h, font);
push_rectangle(
draw_rectangle(
(Rect) {
.x = 0,
.y = 0,
@ -38,7 +38,7 @@ static void title_tick(State *state) {
},
(Color) { 0, 0, 0, 255 }
);
push_text(text_str, (Vec2){ 0, 0 }, text_h, (Color) { 255, 255, 255, 255 }, font);
draw_text(text_str, (Vec2){ 0, 0 }, text_h, (Color) { 255, 255, 255, 255 }, font);
free(text_str);
}

View File

@ -108,7 +108,7 @@ static Color table_to_color(lua_State *L, int idx) {
static int b_sprite(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
PushSpriteArgs args = { 0 };
DrawSpriteArgs args = { 0 };
lua_getfield(L, 1, "path");
const char *field_path = lua_tostring(L, -1);
@ -157,7 +157,7 @@ static int b_sprite(lua_State *L) {
args.stretch_opt_set = true;
}
push_sprite(args);
draw_sprite_args(args);
return 0;
}
@ -181,7 +181,7 @@ static int b_rectangle(lua_State *L) {
if (lua_istable(L, -1))
color = table_to_color(L, -1);
push_rectangle(rect, color);
draw_rectangle(rect, color);
return 0;
}
@ -227,7 +227,7 @@ static int b_text(lua_State *L) {
if (font == NULL)
luaL_error(L, "bad field 'font' in 'data' (string expected, got %s)", luaL_typename(L, -1));
push_text(string, (Vec2) { x, y }, height_px, color, font);
draw_text(string, (Vec2) { x, y }, height_px, color, font);
return 0;
}

109
include/twn_draw.h Normal file
View File

@ -0,0 +1,109 @@
#ifndef TWN_DRAW_H
#define TWN_DRAW_H
#include "twn_util.h"
#include "twn_option.h"
#include "twn_camera.h"
#include "twn_engine_api.h"
#include <stdbool.h>
/* pushes a sprite onto the sprite render queue */
TWN_API 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); /* optional, default: false */
/* pushes a filled rectangle onto the rectangle render queue */
TWN_API void draw_rectangle(Rect rect, Color color);
/* pushes a filled circle onto the circle render queue */
TWN_API void draw_circle(Vec2 position, float radius, Color color);
/* TODO: have font optional, with something minimal coming embedded */
TWN_API void draw_text(char const *string,
Vec2 position,
int height_px, /* optional, default: 22 */
Color color, /* optional, default: all 0 */
char const *font);
TWN_API int draw_text_width(char const *string,
int height_px, /* TODO: make optional */
char const *font);
TWN_API void draw_9slice(char const *texture_path,
int texture_w,
int texture_h,
int border_thickness,
Rect rect,
Color color); /* TODO: make optional */
/* pushes a textured 3d triangle onto the render queue */
/* vertices are in absolute coordinates, relative to world origin */
/* texture coordinates are in pixels */
TWN_API void draw_triangle(char const *path,
Vec3 v0,
Vec3 v1,
Vec3 v2,
Vec2 uv0,
Vec2 uv1,
Vec2 uv2);
// TODO: decide whether it's needed to begin with?
// intended usage for it is baked lighting, i would think.
/* pushes a colored textured 3d triangle onto the render queue */
// void unfurl_colored_triangle(const char *path,
// Vec3 v0,
// Vec3 v1,
// Vec3 v2,
// Vec2sh uv0,
// Vec2sh uv1,
// Vec2sh uv2,
// Color c0,
// Color c1,
// Color c2);
// TODO:
// http://www.lighthouse3d.com/opengl/billboarding/index.php?billCheat2
// void unfurl_billboard(const char *path,
// Vec2 position,
// Vec2 scaling,
// Rect uvs);
/* pushes a camera state to be used for all future unfurl_* commands */
TWN_API void draw_camera(const Camera *camera);
/* expects '*' masks that will be expanded to 6 names: 'up', 'down', 'east', 'west', 'north' and 'south' */
TWN_API void draw_skybox(const char *paths);
TWN_API void draw_fog(float start, float end, float density, Color color);
#ifndef TWN_NOT_C
typedef struct DrawSpriteArgs {
char const *path;
Rect rect;
m_option_list(
Rect, texture_region,
Color, color,
float, rotation,
bool, flip_x,
bool, flip_y,
bool, stretch )
} DrawSpriteArgs;
TWN_API void draw_sprite_args(DrawSpriteArgs args);
#define m_sprite(...) (draw_sprite_args((DrawSpriteArgs){__VA_ARGS__}))
/* TODO: define more */
#endif /* TWN_NOT_C */
#endif

View File

@ -4,7 +4,7 @@
#include "twn_context.h"
#include "twn_rendering.h"
#include "twn_draw.h"
#include "twn_audio.h"
#include "twn_util.h"
#include "twn_input.h"

View File

@ -1,81 +0,0 @@
#ifndef TWN_RENDERING_H
#define TWN_RENDERING_H
#include "twn_util.h"
#include "twn_option.h"
#include "twn_camera.h"
#include "twn_engine_api.h"
#include <stdbool.h>
typedef struct PushSpriteArgs {
char *path;
Rect rect;
m_option_list(
Rect, texture_region,
Color, color,
float, rotation,
bool, flip_x,
bool, flip_y,
bool, stretch )
} PushSpriteArgs;
/* pushes a sprite onto the sprite render queue */
/* this is a simplified version of push_sprite_ex for the most common case. */
/* it assumes you want no color modulation, no rotation, no flip */
TWN_API void push_sprite(PushSpriteArgs args);
#define m_sprite(...) (push_sprite((PushSpriteArgs){__VA_ARGS__}))
/* pushes a filled rectangle onto the rectangle render queue */
TWN_API void push_rectangle(Rect rect, Color color);
/* pushes a filled circle onto the circle render queue */
TWN_API void push_circle(Vec2 position, float radius, Color color);
TWN_API void push_text(char *string, Vec2 position, int height_px, Color color, const char *font);
TWN_API int text_get_width(char *string, int height_px, const char *font);
TWN_API void push_9slice(char *texture_path, int texture_w, int texture_h, int border_thickness, Rect rect, Color color);
/* pushes a textured 3d triangle onto the render queue */
/* vertices are in absolute coordinates, relative to world origin */
/* texture coordinates are in pixels */
TWN_API void unfurl_triangle(const char *path,
Vec3 v0,
Vec3 v1,
Vec3 v2,
Vec2 uv0,
Vec2 uv1,
Vec2 uv2);
// TODO: decide whether it's needed to begin with?
// intended usage for it is baked lighting, i would think.
/* pushes a colored textured 3d triangle onto the render queue */
// void unfurl_colored_triangle(const char *path,
// Vec3 v0,
// Vec3 v1,
// Vec3 v2,
// Vec2sh uv0,
// Vec2sh uv1,
// Vec2sh uv2,
// Color c0,
// Color c1,
// Color c2);
// TODO:
// http://www.lighthouse3d.com/opengl/billboarding/index.php?billCheat2
// void unfurl_billboard(const char *path,
// Vec2 position,
// Vec2 scaling,
// Rect uvs);
/* pushes a camera state to be used for all future unfurl_* commands */
TWN_API void set_camera(const Camera *camera);
/* expects '*' masks that will be expanded to 6 names: 'up', 'down', 'east', 'west', 'north' and 'south' */
TWN_API void push_skybox(const char *paths);
TWN_API void push_fog(float start, float end, float density, Color color);
#endif

View File

@ -5,40 +5,6 @@
/* plain data aggregates that are accepted between public procedure boundaries */
/* 32-bit color data */
typedef struct Color {
_Alignas(4)
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} Color;
/* a rectangle with the origin at the upper left (integer) */
typedef struct Recti {
_Alignas(16)
int32_t x;
int32_t y;
int32_t w;
int32_t h;
} Recti;
/* a rectangle with the origin at the upper left (floating point) */
typedef struct Rect {
_Alignas(16)
float x;
float y;
float w;
float h;
} Rect;
typedef struct Matrix4 {
Vec4 row[4];
} Matrix4;
/* a point in some space (integer) */
typedef struct Vec2i {
@ -77,4 +43,39 @@ _Alignas(16)
} Vec4;
/* 32-bit color data */
typedef struct Color {
_Alignas(4)
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} Color;
/* a rectangle with the origin at the upper left (integer) */
typedef struct Recti {
_Alignas(16)
int32_t x;
int32_t y;
int32_t w;
int32_t h;
} Recti;
/* a rectangle with the origin at the upper left (floating point) */
typedef struct Rect {
_Alignas(16)
float x;
float y;
float w;
float h;
} Rect;
typedef struct Matrix4 {
Vec4 row[4];
} Matrix4;
#endif

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,

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,14 +1,15 @@
#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,
void draw_triangle(const char *path,
Vec3 v0,
Vec3 v1,
Vec3 v2,

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>