hot reload working
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
fn rn_cmd_t *rn_get_cmd(rn_cmd_kind_t kind) {
|
||||
b32 alloc_new = false;
|
||||
if (rn_state.last_cmd == NULL) {
|
||||
if (rn->last_cmd == NULL) {
|
||||
alloc_new = true;
|
||||
} else if (rn_state.last_cmd->kind != kind) {
|
||||
} else if (rn->last_cmd->kind != kind) {
|
||||
alloc_new = true;
|
||||
}
|
||||
|
||||
rn_cmd_t *result = rn_state.last_cmd;
|
||||
rn_cmd_t *result = rn->last_cmd;
|
||||
if (alloc_new) {
|
||||
result = ma_push_type(tcx.temp, rn_cmd_t);
|
||||
result = ma_push_type(tcx->temp, rn_cmd_t);
|
||||
result->kind = kind;
|
||||
SLLQ_APPEND(rn_state.first_cmd, rn_state.last_cmd, result);
|
||||
SLLQ_APPEND(rn->first_cmd, rn->last_cmd, result);
|
||||
|
||||
if (rn_cmd_kind_quad) {
|
||||
result->vertex = rn_state.vertices + rn_state.len;
|
||||
result->vertex = rn->vertices + rn->len;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ fn rn_cmd_t *rn_get_cmd(rn_cmd_kind_t kind) {
|
||||
|
||||
fn rn_vertex_t *rn_push_vertex(rn_cmd_t *cmd, u32 count) {
|
||||
rn_vertex_t *result = cmd->vertex + cmd->len;
|
||||
rn_state.len += count;
|
||||
rn->len += count;
|
||||
cmd->len += count;
|
||||
return result;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ fn void rn_push_quad(r2f32_t rect, r2f32_t tex, v4f32_t color) {
|
||||
}
|
||||
|
||||
fn void rn_draw_rect(r2f32_t rect, v4f32_t color) {
|
||||
rn_push_quad(rect, rn_state.main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(rect, rn->main_font->white_texture_bounding_box, color);
|
||||
}
|
||||
|
||||
fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) {
|
||||
@@ -71,10 +71,10 @@ fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) {
|
||||
r2f32_t right = r2f32_cut_right(&rect, border_thickness);
|
||||
r2f32_t top = r2f32_cut_top(&rect, border_thickness);
|
||||
r2f32_t bottom = r2f32_cut_bottom(&rect, border_thickness);
|
||||
rn_push_quad(left, rn_state.main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(right, rn_state.main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(top, rn_state.main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(bottom, rn_state.main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(left, rn->main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(right, rn->main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(top, rn->main_font->white_texture_bounding_box, color);
|
||||
rn_push_quad(bottom, rn->main_font->white_texture_bounding_box, color);
|
||||
}
|
||||
|
||||
fn i64 rn_get_char_spacing(rn_font_t *font, u32 codepoint) {
|
||||
@@ -113,7 +113,7 @@ fn v2f32_t rn_draw_string(rn_font_t *font, v2f32_t pos, v4f32_t color, s8_t stri
|
||||
}
|
||||
|
||||
fn v2f32_t rn_draw_stringf(rn_font_t *font, v2f32_t pos, v4f32_t color, char *str, ...) {
|
||||
S8_FMT(tcx.temp, str, result);
|
||||
S8_FMT(tcx->temp, str, result);
|
||||
return rn_draw_string(font, pos, color, result);
|
||||
}
|
||||
|
||||
|
||||
@@ -83,4 +83,4 @@ struct rn_state_t {
|
||||
u32 vao;
|
||||
u32 vbo;
|
||||
};
|
||||
gb rn_state_t rn_state;
|
||||
gb rn_state_t *rn;
|
||||
@@ -11,7 +11,7 @@
|
||||
#define STBTT_acos(x) (f64_acos(x))
|
||||
#define STBTT_fabs(x) (f64_abs(x))
|
||||
#define STBTT_assert(x) (assert(x))
|
||||
#define STBTT_malloc(x,u) (ma_push_size(tcx.temp, x))
|
||||
#define STBTT_malloc(x,u) (ma_push_size(tcx->temp, x))
|
||||
#define STBTT_free(x,u)
|
||||
#define STBTT_strlen(x) (str_len(x))
|
||||
#define STBTT_memcpy memory_copy
|
||||
|
||||
@@ -33,13 +33,13 @@ fn void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity
|
||||
}
|
||||
|
||||
fn void rn_reload_font(f32 font_size, f32 dpr) {
|
||||
if (rn_state.main_font->texture_id) {
|
||||
glDeleteTextures(1, &rn_state.main_font->texture_id);
|
||||
if (rn->main_font->texture_id) {
|
||||
glDeleteTextures(1, &rn->main_font->texture_id);
|
||||
}
|
||||
|
||||
ma_temp_t scratch = ma_begin_scratch();
|
||||
rn_atlas_t *atlas = rn_create_atlas(scratch.arena, (v2i32_t){2048, 2048});
|
||||
rn_reload_font_atlas(rn_state.main_font, main_font, atlas, (i32)(font_size * dpr));
|
||||
rn_reload_font_atlas(rn->main_font, main_font, atlas, (i32)(font_size * dpr));
|
||||
|
||||
GLint filter = GL_NEAREST;
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &atlas->texture_id);
|
||||
@@ -49,42 +49,44 @@ fn void rn_reload_font(f32 font_size, f32 dpr) {
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTextureStorage2D(atlas->texture_id, 1, GL_R8, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y);
|
||||
glTextureSubImage2D(atlas->texture_id, 0, 0, 0, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y, GL_RED, GL_UNSIGNED_BYTE, atlas->bitmap);
|
||||
rn_state.main_font->texture_id = atlas->texture_id;
|
||||
rn->main_font->texture_id = atlas->texture_id;
|
||||
ma_end_scratch(scratch);
|
||||
}
|
||||
|
||||
fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) {
|
||||
rn_state.cap = 1024*256;
|
||||
rn_state.vertices = ma_push_array(perm, rn_vertex_t, rn_state.cap);
|
||||
tcx->data[0] = ma_push_type(perm, rn_state_t);
|
||||
rn = tcx->data[0];
|
||||
rn->cap = 1024*256;
|
||||
rn->vertices = ma_push_array(perm, rn_vertex_t, rn->cap);
|
||||
|
||||
rn_state.main_font = rn_create_font(perm);
|
||||
rn->main_font = rn_create_font(perm);
|
||||
rn_reload_font(font_size, dpr);
|
||||
|
||||
glDebugMessageCallback(&gl_debug_callback, NULL);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
|
||||
glCreateBuffers(1, &rn_state.vbo);
|
||||
glNamedBufferStorage(rn_state.vbo, rn_state.cap * sizeof(rn_vertex_t), 0, GL_DYNAMIC_STORAGE_BIT);
|
||||
glCreateBuffers(1, &rn->vbo);
|
||||
glNamedBufferStorage(rn->vbo, rn->cap * sizeof(rn_vertex_t), 0, GL_DYNAMIC_STORAGE_BIT);
|
||||
|
||||
glCreateVertexArrays(1, &rn_state.vao);
|
||||
glCreateVertexArrays(1, &rn->vao);
|
||||
|
||||
GLint vbuf_index = 0;
|
||||
glVertexArrayVertexBuffer(rn_state.vao, vbuf_index, rn_state.vbo, 0, sizeof(struct rn_vertex_t));
|
||||
glVertexArrayVertexBuffer(rn->vao, vbuf_index, rn->vbo, 0, sizeof(struct rn_vertex_t));
|
||||
|
||||
GLint a_pos = 0;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_pos, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, pos));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_pos, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_pos);
|
||||
glVertexArrayAttribFormat(rn->vao, a_pos, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, pos));
|
||||
glVertexArrayAttribBinding(rn->vao, a_pos, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn->vao, a_pos);
|
||||
|
||||
GLint a_tex = 1;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_tex, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, tex));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_tex, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_tex);
|
||||
glVertexArrayAttribFormat(rn->vao, a_tex, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, tex));
|
||||
glVertexArrayAttribBinding(rn->vao, a_tex, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn->vao, a_tex);
|
||||
|
||||
GLint a_color = 2;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_color, 4, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, color));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_color, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_color);
|
||||
glVertexArrayAttribFormat(rn->vao, a_color, 4, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, color));
|
||||
glVertexArrayAttribBinding(rn->vao, a_color, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn->vao, a_color);
|
||||
|
||||
char *glsl_vshader =
|
||||
"#version 450 core\n"
|
||||
@@ -123,17 +125,21 @@ fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) {
|
||||
" OutColor = c;\n"
|
||||
"}\n";
|
||||
|
||||
rn_state.shader2d = rn_create_shader(glsl_vshader, glsl_fshader);
|
||||
rn->shader2d = rn_create_shader(glsl_vshader, glsl_fshader);
|
||||
}
|
||||
|
||||
void rn_begin(app_frame_t *frame, v4f32_t clear_color) {
|
||||
rn_state.frame = frame;
|
||||
rn_state.clear_color = clear_color;
|
||||
void rn_begin_frame(rn_state_t *rn_ctx, app_frame_t *frame) {
|
||||
rn = rn_ctx;
|
||||
rn->frame = frame;
|
||||
}
|
||||
|
||||
void rn_begin(v4f32_t clear_color) {
|
||||
rn->clear_color = clear_color;
|
||||
}
|
||||
|
||||
void rn_end(void) {
|
||||
f32 wx = rn_state.frame->window_size.x;
|
||||
f32 wy = rn_state.frame->window_size.y;
|
||||
f32 wx = rn->frame->window_size.x;
|
||||
f32 wy = rn->frame->window_size.y;
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
@@ -142,21 +148,21 @@ void rn_end(void) {
|
||||
|
||||
glViewport(0, 0, (GLsizei)wx, (GLsizei)wy);
|
||||
glScissor(0, 0, (GLsizei)wx, (GLsizei)wy);
|
||||
glClearColor(rn_state.clear_color.r, rn_state.clear_color.g, rn_state.clear_color.b, rn_state.clear_color.a);
|
||||
glClearColor(rn->clear_color.r, rn->clear_color.g, rn->clear_color.b, rn->clear_color.a);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
|
||||
// Default draw using the font texture
|
||||
glBindProgramPipeline(rn_state.shader2d.pipeline);
|
||||
glBindProgramPipeline(rn->shader2d.pipeline);
|
||||
float xinverse = 1.f / (wx / 2.f);
|
||||
float yinverse = 1.f / (wy / 2.f);
|
||||
glProgramUniform2f(rn_state.shader2d.vshader, 0, xinverse, yinverse);
|
||||
glProgramUniform2f(rn->shader2d.vshader, 0, xinverse, yinverse);
|
||||
|
||||
for (rn_cmd_t *it = rn_state.first_cmd; it; it = it->next) {
|
||||
for (rn_cmd_t *it = rn->first_cmd; it; it = it->next) {
|
||||
if (it->kind == rn_cmd_kind_quad) {
|
||||
glNamedBufferSubData(rn_state.vbo, 0, it->len * sizeof(rn_vertex_t), it->vertex);
|
||||
glBindVertexArray(rn_state.vao);
|
||||
glNamedBufferSubData(rn->vbo, 0, it->len * sizeof(rn_vertex_t), it->vertex);
|
||||
glBindVertexArray(rn->vao);
|
||||
GLint s_texture = 0; // texture unit that sampler2D will use in GLSL code
|
||||
glBindTextureUnit(s_texture, rn_state.main_font->texture_id);
|
||||
glBindTextureUnit(s_texture, rn->main_font->texture_id);
|
||||
glDrawArrays(GL_TRIANGLES, 0, it->len);
|
||||
} else if (it->kind == rn_cmd_kind_set_clip) {
|
||||
GLint x = (GLint)it->rect.min.x;
|
||||
@@ -169,7 +175,7 @@ void rn_end(void) {
|
||||
} else_is_invalid;
|
||||
}
|
||||
|
||||
rn_state.first_cmd = NULL;
|
||||
rn_state.last_cmd = NULL;
|
||||
rn_state.len = 0;
|
||||
}
|
||||
rn->first_cmd = NULL;
|
||||
rn->last_cmd = NULL;
|
||||
rn->len = 0;
|
||||
}//
|
||||
|
||||
@@ -19,11 +19,11 @@ struct rn_state_t {
|
||||
rn_state_t rn_state;
|
||||
|
||||
fn void rn_reload_font(f32 font_size, f32 dpr) {
|
||||
rn_state.main_font->size = font_size * dpr;
|
||||
rn->main_font->size = font_size * dpr;
|
||||
}
|
||||
|
||||
fn void rn_init(ma_arena_t *perm, f32 font_size, f32 dpr) {
|
||||
rn_state.main_font = ma_push_type(perm, rn_font_t);
|
||||
rn->main_font = ma_push_type(perm, rn_font_t);
|
||||
rn_reload_font(font_size, dpr);
|
||||
}
|
||||
|
||||
@@ -43,12 +43,12 @@ fn void rn_draw_rect_border(r2f32_t rect, v4f32_t color, f32 border_thickness) {
|
||||
}
|
||||
|
||||
fn v2f32_t rn_measure_string(rn_font_t *font, s8_t string) {
|
||||
f32 x = wasm_measure_text((isize)string.str, string.len, (isize) font_face.str, font_face.len, rn_state.main_font->size);
|
||||
return (v2f32_t){x, rn_state.main_font->size};
|
||||
f32 x = wasm_measure_text((isize)string.str, string.len, (isize) font_face.str, font_face.len, rn->main_font->size);
|
||||
return (v2f32_t){x, rn->main_font->size};
|
||||
}
|
||||
|
||||
fn v2f32_t rn_draw_string(rn_font_t *font, v2f32_t pos, v4f32_t color, s8_t string) {
|
||||
wasm_draw_text((isize)string.str, string.len, pos.x, pos.y, (isize) font_face.str, font_face.len, rn_state.main_font->size, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a);
|
||||
wasm_draw_text((isize)string.str, string.len, pos.x, pos.y, (isize) font_face.str, font_face.len, rn->main_font->size, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a);
|
||||
v2f32_t size = rn_measure_string(font, string);
|
||||
return size;
|
||||
}
|
||||
@@ -63,8 +63,8 @@ fn void rn_set_clip(r2f32_t rect) {
|
||||
}
|
||||
|
||||
fn void rn_begin(app_frame_t *frame, v4f32_t clear_color) {
|
||||
rn_state.frame = frame;
|
||||
r2f32_t window_rect = r2f32(0, 0, rn_state.frame->window_size.x, rn_state.frame->window_size.y);
|
||||
rn->frame = frame;
|
||||
r2f32_t window_rect = r2f32(0, 0, rn->frame->window_size.x, rn->frame->window_size.y);
|
||||
wasm_clear();
|
||||
rn_draw_rect(window_rect, clear_color);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user