Skip to content

Commit

Permalink
Actually destroy graphics stuff
Browse files Browse the repository at this point in the history
Implemented `kr_g2_destroy` to actually do something

This should fix #3
  • Loading branch information
tcdude committed Apr 21, 2023
1 parent bb9918f commit 93cf4a1
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 16 deletions.
16 changes: 16 additions & 0 deletions Sources/krink/graphics2/coloredpainter.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "coloredpainter.h"
#include <assert.h>
#include <kinc/graphics4/graphics.h>
#include <kinc/graphics4/indexbuffer.h>
#include <kinc/graphics4/pipeline.h>
Expand Down Expand Up @@ -28,11 +29,13 @@ static int rect_buffer_index = 0;
static int rect_buffer_start = 0;
static int tris_buffer_index = 0;
static int tris_buffer_start = 0;
static bool csp_initialized = false;

static void csp_rect_end(bool tris_done);
static void csp_tris_end(bool rects_done);

void kr_csp_init(void) {
assert(!csp_initialized);
{
kinc_file_reader_t reader;
kinc_file_reader_open(&reader, "kr-painter-colored.vert", KINC_FILE_TYPE_ASSET);
Expand Down Expand Up @@ -102,6 +105,19 @@ void kr_csp_init(void) {
indices[i * 3 + 2] = i * 3 + 2;
}
kinc_g4_index_buffer_unlock_all(&tris_index_buffer);
csp_initialized = true;
}

void kr_csp_destroy(void) {
assert(csp_initialized);
kinc_g4_index_buffer_destroy(&tris_index_buffer);
kinc_g4_index_buffer_destroy(&rect_index_buffer);
kinc_g4_vertex_buffer_destroy(&tris_vertex_buffer);
kinc_g4_vertex_buffer_destroy(&rect_vertex_buffer);
kinc_g4_pipeline_destroy(&pipeline);
kinc_g4_shader_destroy(&vert_shader);
kinc_g4_shader_destroy(&frag_shader);
csp_initialized = false;
}

// Rect Impl
Expand Down
1 change: 1 addition & 0 deletions Sources/krink/graphics2/coloredpainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
#endif

void kr_csp_init(void);
void kr_csp_destroy(void);
void kr_csp_set_projection_matrix(kinc_matrix4x4_t mat);
void kr_csp_fill_rect(float x, float y, float width, float height, uint32_t color, float opacity,
kr_matrix3x3_t transformation);
Expand Down
35 changes: 20 additions & 15 deletions Sources/krink/graphics2/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ static int g2_last_width = -1;
static int g2_last_height = -1;

void kr_g2_init(void) {
if (!g2_painters_initialized) {
kr_isp_init();
kr_csp_init();
kr_tsp_init();
kr_sdf_init();
g2_transformation = kr_matrix3x3_identity();
}
assert(!g2_painters_initialized);
kr_isp_init();
kr_csp_init();
kr_tsp_init();
kr_sdf_init();
g2_transformation = kr_matrix3x3_identity();
g2_painters_initialized = true;
}

static inline void internal_set_projection_matrix(int iwidth, int iheight) {
Expand Down Expand Up @@ -83,11 +83,16 @@ static void internal_update_projection_matrix(int window) {
}

void kr_g2_destroy(void) {
// Maybe not needed?
assert(g2_painters_initialized);
kr_isp_destroy();
kr_csp_destroy();
kr_tsp_destroy();
kr_sdf_destroy();
g2_painters_initialized = false;
}

void kr_g2_begin(int window) {
assert(!begin && g2_active_window == -1);
assert(!begin && g2_active_window == -1 && g2_painters_initialized);
g2_active_window = window;
internal_update_projection_matrix(window);
begin = true;
Expand Down Expand Up @@ -343,14 +348,14 @@ void kr_g2_scissor(float x, float y, float w, float h) {
kr_tsp_end();
kr_csp_end();
kr_sdf_end();
int xi = (int)(x + 0.5f);
int yi = (int)(y + 0.5f);
int wi = (int)(w + 0.5f);
int hi = (int)(h + 0.5f);
int xi = (int)(x + 0.5f);
int yi = (int)(y + 0.5f);
int wi = (int)(w + 0.5f);
int hi = (int)(h + 0.5f);
#ifdef KINC_METAL
// Crude metal fix! TODO: Improve bound check
if (xi + wi > g2_last_width) wi = g2_last_width - xi;
if (yi + hi > g2_last_height) hi = g2_last_height - yi;
if (xi + wi > g2_last_width) wi = g2_last_width - xi;
if (yi + hi > g2_last_height) hi = g2_last_height - yi;
#endif
kinc_g4_scissor(xi, yi, wi, hi);
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/krink/graphics2/imagepainter.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <krink/math/vector.h>
#include <krink/memory.h>

#include <assert.h>
#include <stdbool.h>

static kinc_g4_vertex_buffer_t vertex_buffer;
static kinc_g4_index_buffer_t index_buffer;
static kinc_g4_shader_t vert_shader;
Expand All @@ -26,6 +29,7 @@ static kinc_matrix4x4_t projection_matrix;
static float *rect_verts = NULL;
static int buffer_index = 0;
static int buffer_start = 0;
static bool isp_initialized = false;

static bool bilinear_filter = false;

Expand All @@ -36,6 +40,7 @@ static int font_size = 0;
#endif

void kr_isp_init(void) {
assert(!isp_initialized);
{
kinc_file_reader_t reader;
kinc_file_reader_open(&reader, "kr-painter-image.vert", KINC_FILE_TYPE_ASSET);
Expand Down Expand Up @@ -93,6 +98,17 @@ void kr_isp_init(void) {
indices[i * 3 * 2 + 5] = i * 4 + 3;
}
kinc_g4_index_buffer_unlock_all(&index_buffer);
isp_initialized = true;
}

void kr_isp_destroy(void) {
assert(isp_initialized);
kinc_g4_index_buffer_destroy(&index_buffer);
kinc_g4_vertex_buffer_destroy(&vertex_buffer);
kinc_g4_pipeline_destroy(&pipeline);
kinc_g4_shader_destroy(&vert_shader);
kinc_g4_shader_destroy(&frag_shader);
isp_initialized = false;
}

void kr_isp_set_rect_verts(float btlx, float btly, float tplx, float tply, float tprx, float tpry,
Expand Down
1 change: 1 addition & 0 deletions Sources/krink/graphics2/imagepainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
#endif

void kr_isp_init(void);
void kr_isp_destroy(void);
void kr_isp_set_bilinear_filter(bool bilinear);
void kr_isp_set_projection_matrix(kinc_matrix4x4_t mat);
void kr_isp_draw_scaled_sub_image(kr_image_t *img, float sx, float sy, float sw, float sh, float dx,
Expand Down
28 changes: 28 additions & 0 deletions Sources/krink/graphics2/sdfpainter.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <krink/math/vector.h>
#include <krink/memory.h>

#include <assert.h>
#include <stdbool.h>

static kinc_g4_vertex_buffer_t rect_vertex_buffer;
static kinc_g4_index_buffer_t rect_index_buffer;
static kinc_g4_vertex_buffer_t circle_vertex_buffer;
Expand Down Expand Up @@ -41,6 +44,7 @@ static int circle_buffer_index = 0;
static int circle_buffer_start = 0;
static int line_buffer_index = 0;
static int line_buffer_start = 0;
static bool sdf_initialized = false;

static void sdf_rect_end(void);
static void sdf_circle_end(void);
Expand Down Expand Up @@ -241,9 +245,33 @@ static void build_line_pipeline(void) {
}

void kr_sdf_init(void) {
assert(!sdf_initialized);
build_rect_pipeline();
build_circle_pipeline();
build_line_pipeline();
sdf_initialized = true;
}

void kr_sdf_destroy(void) {
assert(sdf_initialized);
kinc_g4_index_buffer_destroy(&rect_index_buffer);
kinc_g4_vertex_buffer_destroy(&rect_vertex_buffer);
kinc_g4_pipeline_destroy(&rect_pipeline);
kinc_g4_shader_destroy(&rect_vert_shader);
kinc_g4_shader_destroy(&rect_frag_shader);

kinc_g4_index_buffer_destroy(&circle_index_buffer);
kinc_g4_vertex_buffer_destroy(&circle_vertex_buffer);
kinc_g4_pipeline_destroy(&circle_pipeline);
kinc_g4_shader_destroy(&circle_vert_shader);
kinc_g4_shader_destroy(&circle_frag_shader);

kinc_g4_index_buffer_destroy(&line_index_buffer);
kinc_g4_vertex_buffer_destroy(&line_vertex_buffer);
kinc_g4_pipeline_destroy(&line_pipeline);
kinc_g4_shader_destroy(&line_vert_shader);
kinc_g4_shader_destroy(&line_frag_shader);
sdf_initialized = false;
}

// SDF Rect Impl
Expand Down
1 change: 1 addition & 0 deletions Sources/krink/graphics2/sdfpainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct kr_sdf_corner_radius {
} kr_sdf_corner_radius_t;

void kr_sdf_init(void);
void kr_sdf_destroy(void);
void kr_sdf_set_projection_matrix(kinc_matrix4x4_t mat);
void kr_sdf_draw_rect(float x, float y, float width, float height, kr_sdf_corner_radius_t corner,
float border, float smooth, uint32_t color, uint32_t border_color,
Expand Down
17 changes: 16 additions & 1 deletion Sources/krink/graphics2/textpainter.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef KR_FULL_RGBA_FONTS
#include "textpainter.h"

#include <assert.h>
#include <kinc/graphics4/graphics.h>
#include <kinc/graphics4/indexbuffer.h>
#include <kinc/graphics4/pipeline.h>
Expand All @@ -16,6 +15,9 @@
#include <krink/math/vector.h>
#include <krink/memory.h>

#include <assert.h>
#include <stdbool.h>

static kinc_g4_vertex_buffer_t vertex_buffer;
static kinc_g4_index_buffer_t index_buffer;
static kinc_g4_shader_t vert_shader;
Expand All @@ -28,12 +30,14 @@ static kinc_matrix4x4_t projection_matrix;
static float *rect_verts = NULL;
static int buffer_index = 0;
static int buffer_start = 0;
static bool tsp_initialized = false;

static kr_ttf_font_t *active_font = NULL;
static bool bilinear_filter = false;
static int font_size = 0;

void kr_tsp_init(void) {
assert(!tsp_initialized);
{
kinc_file_reader_t reader;
kinc_file_reader_open(&reader, "kr-painter-text.vert", KINC_FILE_TYPE_ASSET);
Expand Down Expand Up @@ -91,6 +95,17 @@ void kr_tsp_init(void) {
indices[i * 3 * 2 + 5] = i * 4 + 3;
}
kinc_g4_index_buffer_unlock_all(&index_buffer);
tsp_initialized = true;
}

void kr_tsp_destroy(void) {
assert(tsp_initialized);
kinc_g4_index_buffer_destroy(&index_buffer);
kinc_g4_vertex_buffer_destroy(&vertex_buffer);
kinc_g4_pipeline_destroy(&pipeline);
kinc_g4_shader_destroy(&vert_shader);
kinc_g4_shader_destroy(&frag_shader);
tsp_initialized = false;
}

void kr_tsp_set_rect_verts(float btlx, float btly, float tplx, float tply, float tprx, float tpry,
Expand Down
1 change: 1 addition & 0 deletions Sources/krink/graphics2/textpainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {
#endif

void kr_tsp_init(void);
void kr_tsp_destroy(void);
void kr_tsp_draw_buffer(bool end);
void kr_tsp_set_bilinear_filter(bool bilinear);
void kr_tsp_set_projection_matrix(kinc_matrix4x4_t mat);
Expand Down

0 comments on commit 93cf4a1

Please sign in to comment.