2024-10-07 14:53:09 +00:00
|
|
|
#ifndef TWN_DRAW_C_H
|
|
|
|
#define TWN_DRAW_C_H
|
2024-07-30 15:31:38 +00:00
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_textures_c.h"
|
2024-09-23 17:43:16 +00:00
|
|
|
#include "twn_text_c.h"
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_util.h"
|
|
|
|
#include "twn_option.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
2024-09-16 13:17:00 +00:00
|
|
|
#include <stb_truetype.h>
|
|
|
|
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#else
|
|
|
|
#include <glad/glad.h>
|
|
|
|
#endif
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
extern Matrix4 camera_projection_matrix;
|
|
|
|
extern Matrix4 camera_look_at_matrix;
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
#define QUAD_ELEMENT_BUFFER_LENGTH (65536 / 6)
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef GLuint VertexBuffer;
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct VertexBufferBuilder {
|
2024-09-16 13:17:00 +00:00
|
|
|
size_t bytes_left;
|
|
|
|
void *mapping;
|
2024-09-23 17:43:16 +00:00
|
|
|
} VertexBufferBuilder;
|
2024-09-16 13:17:00 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct SpritePrimitive {
|
|
|
|
Rect rect;
|
|
|
|
Color color;
|
2024-07-27 12:10:19 +00:00
|
|
|
float rotation;
|
2024-09-23 17:43:16 +00:00
|
|
|
TextureKey texture_key;
|
2024-07-08 00:44:20 +00:00
|
|
|
bool flip_x;
|
|
|
|
bool flip_y;
|
2024-07-31 21:23:32 +00:00
|
|
|
bool repeat;
|
2024-07-31 21:52:15 +00:00
|
|
|
|
|
|
|
m_option_list(
|
2024-09-23 17:43:16 +00:00
|
|
|
Rect, texture_region )
|
|
|
|
} SpritePrimitive;
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct RectPrimitive {
|
|
|
|
Rect rect;
|
|
|
|
Color color;
|
|
|
|
} RectPrimitive;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct CirclePrimitive {
|
2024-07-08 00:44:20 +00:00
|
|
|
float radius;
|
2024-09-23 17:43:16 +00:00
|
|
|
Color color;
|
|
|
|
Vec2 position;
|
|
|
|
} CirclePrimitive;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct TextPrimitive {
|
|
|
|
Color color;
|
|
|
|
Vec2 position;
|
2024-08-23 02:41:52 +00:00
|
|
|
char *text;
|
|
|
|
const char *font;
|
|
|
|
int height_px;
|
2024-09-23 17:43:16 +00:00
|
|
|
} TextPrimitive;
|
2024-08-23 02:41:52 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef enum Primitive2DType {
|
2024-07-16 02:31:54 +00:00
|
|
|
PRIMITIVE_2D_SPRITE,
|
|
|
|
PRIMITIVE_2D_RECT,
|
|
|
|
PRIMITIVE_2D_CIRCLE,
|
2024-08-23 02:41:52 +00:00
|
|
|
PRIMITIVE_2D_TEXT,
|
2024-09-23 17:43:16 +00:00
|
|
|
} Primitive2DType;
|
2024-07-16 02:31:54 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct Primitive2D {
|
|
|
|
Primitive2DType type;
|
2024-07-16 02:31:54 +00:00
|
|
|
|
|
|
|
union {
|
2024-09-23 17:43:16 +00:00
|
|
|
SpritePrimitive sprite;
|
|
|
|
RectPrimitive rect;
|
|
|
|
CirclePrimitive circle;
|
|
|
|
TextPrimitive text;
|
2024-07-16 02:31:54 +00:00
|
|
|
};
|
2024-09-23 17:43:16 +00:00
|
|
|
} Primitive2D;
|
2024-07-16 02:31:54 +00:00
|
|
|
|
2024-07-14 13:04:12 +00:00
|
|
|
/* union for in-place recalculation of texture coordinates */
|
2024-09-23 17:43:16 +00:00
|
|
|
union UncoloredSpaceTriangle {
|
2024-07-14 13:04:12 +00:00
|
|
|
/* pending for sending, uvs are not final as texture atlases could update */
|
2024-09-23 17:43:16 +00:00
|
|
|
struct UncoloredSpaceTrianglePrimitive {
|
|
|
|
Vec3 v0;
|
|
|
|
Vec2 uv0; /* in pixels */
|
|
|
|
Vec3 v1;
|
|
|
|
Vec2 uv1; /* in pixels */
|
|
|
|
Vec3 v2;
|
|
|
|
Vec2 uv2; /* in pixels */
|
2024-07-14 13:04:12 +00:00
|
|
|
} primitive;
|
|
|
|
|
2024-07-30 22:12:45 +00:00
|
|
|
/* TODO: have it packed? */
|
2024-07-14 13:04:12 +00:00
|
|
|
/* structure that is passed in opengl vertex array */
|
2024-09-23 17:43:16 +00:00
|
|
|
struct UncoloredSpaceTrianglePayload {
|
|
|
|
Vec3 v0;
|
|
|
|
Vec2 uv0;
|
|
|
|
Vec3 v1;
|
|
|
|
Vec2 uv1;
|
|
|
|
Vec3 v2;
|
|
|
|
Vec2 uv2;
|
2024-07-14 13:04:12 +00:00
|
|
|
} payload;
|
|
|
|
};
|
|
|
|
|
2024-07-10 16:15:28 +00:00
|
|
|
/* batch of primitives with overlapping properties */
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct MeshBatch {
|
2024-07-14 13:04:12 +00:00
|
|
|
uint8_t *primitives;
|
2024-09-23 17:43:16 +00:00
|
|
|
} MeshBatch;
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct MeshBatchItem {
|
|
|
|
TextureKey key;
|
|
|
|
struct MeshBatch value;
|
|
|
|
} MeshBatchItem;
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct TextCache {
|
|
|
|
struct FontData **data;
|
|
|
|
} TextCache;
|
2024-08-23 02:41:52 +00:00
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-07-30 15:31:38 +00:00
|
|
|
/* renders the background, then the primitives in all render queues */
|
|
|
|
void render(void);
|
|
|
|
|
|
|
|
/* clears all render queues */
|
|
|
|
void render_queue_clear(void);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void create_circle_geometry(Vec2 position,
|
|
|
|
Color color,
|
2024-09-16 06:07:01 +00:00
|
|
|
float radius,
|
|
|
|
size_t num_vertices,
|
|
|
|
SDL_Vertex **vertices_out,
|
|
|
|
int **indices_out);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
struct SpriteBatch {
|
2024-09-16 06:07:01 +00:00
|
|
|
size_t size; /* how many primitives are in current batch */
|
2024-09-23 17:43:16 +00:00
|
|
|
TextureMode mode;
|
2024-09-16 06:07:01 +00:00
|
|
|
bool constant_colored; /* whether colored batch is uniformly colored */
|
|
|
|
bool repeat; /* whether repeat is needed */
|
2024-09-23 17:43:16 +00:00
|
|
|
} collect_sprite_batch(const Primitive2D primitives[], size_t len);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void render_sprites(const Primitive2D primitives[],
|
|
|
|
const struct SpriteBatch batch);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void draw_uncolored_space_traingle_batch(MeshBatch *batch,
|
|
|
|
TextureKey texture_key);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
/* text */
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void render_text(const TextPrimitive *text);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void text_cache_init(TextCache *cache);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void text_cache_deinit(TextCache *cache);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-27 18:02:24 +00:00
|
|
|
void text_cache_reset_arena(TextCache *cache);
|
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
/* vertex buffer */
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
VertexBuffer create_vertex_buffer(void);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void delete_vertex_buffer(VertexBuffer buffer);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void specify_vertex_buffer(VertexBuffer buffer, void *data, size_t bytes);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
|
|
|
/* uses present in 1.5 buffer mapping feature */
|
2024-09-23 17:43:16 +00:00
|
|
|
VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
|
|
|
/* collects bytes for sending to the gpu until all is pushed, which is when false is returned */
|
2024-09-23 17:43:16 +00:00
|
|
|
bool push_to_vertex_buffer_builder(VertexBufferBuilder *builder,
|
2024-09-16 13:17:00 +00:00
|
|
|
void *bytes,
|
|
|
|
size_t size);
|
|
|
|
|
|
|
|
/* state */
|
|
|
|
|
|
|
|
void setup_viewport(int x, int y, int width, int height);
|
|
|
|
|
|
|
|
void clear_draw_buffer(void);
|
|
|
|
|
|
|
|
void swap_buffers(void);
|
|
|
|
|
|
|
|
void set_depth_range(double low, double high);
|
|
|
|
|
|
|
|
void bind_quad_element_buffer(void);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void render_circle(const CirclePrimitive *circle);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void render_rectangle(const RectPrimitive *rectangle);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
|
|
|
void use_space_pipeline(void);
|
|
|
|
|
|
|
|
void use_2d_pipeline(void);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void use_texture_mode(TextureMode mode);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void finally_render_sprites(Primitive2D const primitives[],
|
|
|
|
struct SpriteBatch batch,
|
|
|
|
VertexBuffer buffer);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
size_t get_sprite_payload_size(struct SpriteBatch batch);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
bool push_sprite_payload_to_vertex_buffer_builder(struct SpriteBatch batch,
|
|
|
|
VertexBufferBuilder *builder,
|
|
|
|
Vec2 v0, Vec2 v1, Vec2 v2, Vec2 v3,
|
|
|
|
Vec2 uv0, Vec2 uv1, Vec2 uv2, Vec2 uv3,
|
|
|
|
Color color);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void finally_draw_uncolored_space_traingle_batch(MeshBatch const *batch,
|
|
|
|
TextureKey texture_key,
|
|
|
|
VertexBuffer buffer);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
|
|
|
size_t get_text_payload_size(void);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
bool push_text_payload_to_vertex_buffer_builder(FontData const *font_data,
|
|
|
|
VertexBufferBuilder *builder,
|
2024-09-16 13:17:00 +00:00
|
|
|
stbtt_aligned_quad quad);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void finally_draw_text(FontData const *font_data,
|
2024-09-16 13:17:00 +00:00
|
|
|
size_t len,
|
2024-09-23 17:43:16 +00:00
|
|
|
Color color,
|
|
|
|
VertexBuffer buffer);
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-26 18:02:56 +00:00
|
|
|
void render_skybox(void);
|
|
|
|
|
|
|
|
void finally_render_skybox(char *paths_in_use);
|
|
|
|
|
2024-10-01 13:10:36 +00:00
|
|
|
void apply_fog(void);
|
|
|
|
|
|
|
|
void finally_apply_fog(float start, float end, float density, Color color);
|
|
|
|
|
|
|
|
void pop_fog(void);
|
|
|
|
|
|
|
|
void finally_pop_fog(void);
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
#endif
|