Remove pointer index, fix memoery leak in glyph

This commit is contained in:
SamratGhale
2026-03-29 06:34:00 +05:45
parent c1b64e9066
commit 06664f9f67
5 changed files with 47 additions and 57 deletions
+27 -8
View File
@@ -12,7 +12,6 @@ import "shared:odin-imgui/imgui_impl_glfw"
import "shared:odin-imgui/imgui_impl_opengl3"
**/
import "core:fmt"
import gl "vendor:OpenGL"
import "vendor:glfw"
import e2_draw "shared:Edit2D/draw"
@@ -28,6 +27,10 @@ __e2d_interal : struct
}
/*
This engine is meant to be used by the game
All the stuffs internal should be stored in __e2d_internal
*/
Engine_State :: struct
{
@@ -41,13 +44,14 @@ Engine_State :: struct
//Must be set before calling ion_init
width, height : i32,
title : cstring,
time : f32,
tex_line : u32,
drop_callback : glfw.DropProc,
input : Input_State,
mu_ctx : mu.Context,
/* The opengl texture id created using mu.default_atlas_alpha */
mu_tex : u32,
}
@@ -55,7 +59,6 @@ MAX_KEYS :: 512
Input_State :: struct
{
mouse_wheel : [2]f64,
mouse : [2]f64,
mouse_prev : [2]f64,
@@ -105,7 +108,7 @@ engine_check_types :: proc($Game: typeid)
assert(entity_engine.is_using, "Should be using")
assert(defs_engine.is_using, "Should be using")
assert(entity_engine.type.id == typeid_of(engine_entity))
assert(entity_engine.type.id == typeid_of(Engine_Entity))
assert(defs_engine.type.id == typeid_of(Engine_Entity_Def))
}
@@ -162,13 +165,23 @@ engine_init :: proc($GameType : typeid, state: ^Engine_State, font_path : strin
draw_configure_box2d(state)
cbor.tag_register_type({
marshal = proc(_: ^cbor.Tag_Implementation, e: cbor.Encoder, v: any) -> cbor.Marshal_Error {
marshal = proc(
_: ^cbor.Tag_Implementation,
e: cbor.Encoder, v: any) -> cbor.Marshal_Error
{
cbor._encode_u8(e.writer, 201, .Tag) or_return
return nil;
},
unmarshal = proc(_: ^cbor.Tag_Implementation, d: cbor.Decoder, _: cbor.Tag_Number, v: any) -> (cbor.Unmarshal_Error) {
unmarshal = proc(
_: ^cbor.Tag_Implementation,
d: cbor.Decoder, _: cbor.Tag_Number,
v: any) -> (cbor.Unmarshal_Error)
{
return nil
},
}, 201, rawptr)
@@ -208,7 +221,6 @@ normalize_rect :: proc(rect : mu.Rect, size : i32) -> e2_draw.Rect
update_frame :: proc(state: ^Engine_State)
{
state.input.mouse_wheel = {}
__e2d_interal.mouse_scroll = {}
glfw.PollEvents()
@@ -258,6 +270,7 @@ end_frame :: proc(state: ^Engine_State)
cmd: ^mu.Command
draw := &state.draw
for mu.next_command(&state.mu_ctx, &cmd)
{
#partial switch c in cmd.variant{
@@ -360,10 +373,16 @@ draw_flush :: proc(state: ^Engine_State)
d.glyph.width = d.cam.width
d.glyph.height = d.cam.height
width, height := glfw.GetFramebufferSize(state.window)
state.width = width
state.height = height
e2_glyph.glyph_init(&d.glyph, d.font_path)
}
e2_draw.draw_flush(d, __e2d_interal.viewport_changed)
glfw.SwapBuffers(state.window)
__e2d_interal.viewport_changed = false
}
+5 -5
View File
@@ -114,7 +114,7 @@ compare_engine_entity_def :: proc(a, b : Engine_Entity_Def) -> bool
engine_entity :: struct {
Engine_Entity :: struct {
body_id : b2.BodyId,
shape_id : b2.ShapeId,
@@ -129,12 +129,12 @@ engine_entity :: struct {
engine_entity_single_body :: proc(def : ^Engine_Entity_Def, world_id: b2.WorldId, index : i32) -> engine_entity
engine_entity_single_body :: proc(def : ^Engine_Entity_Def, world_id: b2.WorldId, index : i32) -> Engine_Entity
{
def := def
new_entity : engine_entity
new_entity : Engine_Entity
if def.index != 0
@@ -270,7 +270,7 @@ engine_create_chain_shape :: proc(
def : ^Engine_Entity_Def,
world_id : b2.WorldId,
index : i32
) -> engine_entity
) -> Engine_Entity
{
joint_def := def.rev_joint
orig_pos := def.body_def.position
@@ -311,7 +311,7 @@ engine_create_entity :: proc(
def : ^Engine_Entity_Def,
world_id : b2.WorldId,
index : i32
) -> engine_entity
) -> Engine_Entity
{
if .CHAIN not_in def.entity_flags
+2 -2
View File
@@ -126,7 +126,7 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
text_color: [4]f32 = {f32( color.r)/ 255.0 ,f32(color.g)/255.0, f32(color.b)/255.0, f32(color.a)/255.0}
glyph_state.rect_buffer = make([dynamic]GlyphRectInstance, 0, 1000)
//glyph_state.rect_buffer = make([dynamic]GlyphRectInstance, 0, 1000)
{
//put every glyph in text into rect_buffer
@@ -147,7 +147,6 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
prev_codepoint: rune = 0
delete(glyph_state.rect_buffer)
glyph_state.rect_buffer = make([dynamic]GlyphRectInstance, 0, 1000)
glyph_state.curr = pos
glyph_state.curr.y += math.round(baseline)
@@ -329,4 +328,5 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
gl.InvalidateBufferData(glyph_state.rect_instances_vbo)
}
delete(glyph_state.rect_buffer)
}
+2 -2
View File
@@ -143,9 +143,9 @@ handle_entity_mode :: proc(
def.body_def.position = mpos
ret = true
}
else if state.input.mouse_wheel.y != 0
else if __e2d_interal.mouse_scroll.y != 0
{
def.scale += f32(state.input.mouse_prev.y / 5)
def.scale += f32( __e2d_interal.mouse_scroll.y / 5)
ret = true
}
}
+10 -39
View File
@@ -33,13 +33,13 @@ Edit_Mode :: enum
{
entity_defs : [dynamic]^Engine_Entity_Def,
entities : [dynamic]^Engine_Entity,
selected_entity : i32,
selected_entity : i32,
world : ^Engine_World,
state : ^Engine_State,
vertex_index : i32,
chain_index : ^i32,
chain_index : i32,
edit_mode : Edit_Mode,
curr_joint_index : i32,
@@ -97,38 +97,14 @@ mui_text_height :: proc(font: mu.Font) -> i32
{
interface.selected_entity = 0
interface.vertex_index = 0
interface.vertex_index^ = 0
interface.chain_index = new(i32)
interface.chain_index^ = 0
}
interface.chain_index = 0
}
mu_interface_game_mode :: proc(state: ^Engine_State, interface: ^Interface_State)
interface_draw_options :: proc(state: ^engine_state)
{
debug_draw := &state.debug_draw
im.SliderFloat("Zoom", &state.draw.cam.zoom, 0, 100)
im.Checkbox("Shapes", &debug_draw.drawShapes)
im.Checkbox("Joints", &debug_draw.drawJoints)
im.Checkbox("Joint Extras", &debug_draw.drawJointExtras)
im.Checkbox("Bounds", &debug_draw.drawBounds)
im.Checkbox("Contact Points", &debug_draw.drawContacts)
im.Checkbox("Contact Normals", &debug_draw.drawContactNormals)
im.Checkbox("Contact Inpulses", &debug_draw.drawContactImpulses)
im.Checkbox("Contact Features", &debug_draw.drawContactFeatures)
im.Checkbox("Friction Inpulses", &debug_draw.drawFrictionImpulses)
im.Checkbox("Mass ", &debug_draw.drawMass)
im.Checkbox("Body Names", &debug_draw.drawBodyNames)
im.Checkbox("Graph Colors", &debug_draw.drawGraphColors)
im.Checkbox("Islands ", &debug_draw.drawIslands)
im.SliderFloat("Rotation", &state.draw.cam.rotation, 0, 360)
}
*/
mu_interface_game_mode :: proc(state: ^Engine_State, interface: ^Interface_State)
{
width :i32 = 250
{
if mu.begin_window(&state.mu_ctx, "Edit Mode", {state.width - width, 0, width, 170})
{
for type in Edit_Mode
{
b : bool = interface.edit_mode == type
@@ -141,7 +117,7 @@ mu_interface_game_mode :: proc(state: ^Engine_State, interface: ^Interface_State
mu_interface_draw_options :: proc(state: ^Engine_State)
{
if mu.begin_window(&state.mu_ctx, "Options", {0, 150, 200, 400}){
debug_draw := &state.debug_draw
debug_draw := &state.debug_draw
mu.label(&state.mu_ctx, "Zoom")
mu.slider(&state.mu_ctx, &state.draw.cam.zoom,0, 100)
@@ -177,18 +153,15 @@ interface_all :: proc($E: typeid, interface: ^Interface_State, game_data : $G) -
}
state := interface.state
//if handle_input(E, interface.state, game_data) do return true
state := interface.state
mu.begin(&state.mu_ctx)
mu_interface_draw_options(interface.state)
mu_interface_draw_options(interface.state)
mu_interface_game_mode(state, interface)
width : i32 = 250
{
if mu.begin_window(&state.mu_ctx, "B2d Interface", {state.width - width, 170, width, 450}, { .NO_TITLE})
{
if interface.edit_mode == .ENTITY
{
@@ -203,9 +176,7 @@ interface_all :: proc($E: typeid, interface: ^Interface_State, game_data : $G) -
//if interface_joints(interface) do ret = true
mu.end(&state.mu_ctx)
mu.end(&state.mu_ctx)
return ret
return ret
}