Recalculate Font on window resize

This commit is contained in:
SamratGhale
2026-03-27 20:40:35 +05:45
parent 00ae6cbc4e
commit f49651384c
7 changed files with 188 additions and 148 deletions
+103 -101
View File
@@ -1,20 +1,30 @@
package edit2d
import "core:fmt"
import "base:intrinsics"
import "core:encoding/cbor"
import b2 "vendor:box2d"
import "base:runtime"
/*
import im "shared:odin-imgui"
import "shared:odin-imgui/imgui_impl_glfw"
import "shared:odin-imgui/imgui_impl_opengl3"
**/
import gl "vendor:OpenGL"
import "vendor:glfw"
import e2_draw "shared:Edit2D/draw"
import e2_draw "shared:Edit2D/draw"
import e2_glyph "shared:Edit2D/glyph"
import "core:reflect"
import mu "vendor:microui"
//Only to be used inside the libarary
__e2d_interal : struct
{
viewport_changed : bool
}
engine_state :: struct
@@ -98,6 +108,12 @@ engine_check_types :: proc($Game: typeid)
}
size_callback :: proc "c" (window: glfw.WindowHandle, width, height : i32)
{
__e2d_interal.viewport_changed = true
}
/*
This will only be called once to initilize the engine
initilize graphics library, glfw, callbacks
@@ -126,32 +142,8 @@ engine_init :: proc($GameType : typeid, state: ^engine_state, font_path : strin
glfw.MakeContextCurrent(state.window)
glfw.SwapInterval(1)
gl.load_up_to(4, 5, glfw.gl_set_proc_address)
im.CHECKVERSION()
im.CreateContext()
io := im.GetIO()
io.ConfigFlags += {
.NavEnableKeyboard,
.NavEnableGamepad,
.DpiEnableScaleFonts,
}
im.StyleColorsClassic()
style := im.GetStyle()
style.ChildBorderSize = 0.
style.ChildRounding = 6
style.TabRounding = 6
style.FrameRounding = 6
style.GrabRounding = 6
style.WindowRounding = 6
style.PopupRounding = 6
imgui_impl_glfw.InitForOpenGL(state.window, true)
imgui_impl_opengl3.Init("#version 150")
glfw.SetWindowSizeCallback(state.window, size_callback)
state.draw.cam = e2_draw.camera_init()
@@ -175,33 +167,26 @@ engine_init :: proc($GameType : typeid, state: ^engine_state, font_path : strin
}, 201, rawptr)
//Glyph and microui
mu.init(&state.mu_ctx)
state.mu_ctx.style.font = cast(mu.Font)&state.draw.glyph
state.mu_ctx.text_width = mui_text_width
state.mu_ctx.text_height = mui_text_height
texture_data := make([]u8, mu.DEFAULT_ATLAS_WIDTH* mu.DEFAULT_ATLAS_HEIGHT * 4)
idx :i32 = 0
for y in 0..<mu.DEFAULT_ATLAS_HEIGHT
{
//Create index
mu.init(&state.mu_ctx)
state.mu_ctx.style.font = cast(mu.Font)&state.draw.glyph
state.mu_ctx.text_width = mui_text_width
state.mu_ctx.text_height = mui_text_height
texture_data := make([]u8, mu.DEFAULT_ATLAS_WIDTH* mu.DEFAULT_ATLAS_HEIGHT * 4)
idx :i32 = 0
for y in 0..<mu.DEFAULT_ATLAS_HEIGHT
for x in 0..<mu.DEFAULT_ATLAS_WIDTH
{
for x in 0..<mu.DEFAULT_ATLAS_WIDTH
{
texture_data[idx + 0] = 255
texture_data[idx + 1] = 255
texture_data[idx + 2] = 255
texture_data[idx + 3] = mu.default_atlas_alpha[idx/4]
idx += 4
}
texture_data[idx + 0] = 255
texture_data[idx + 1] = 255
texture_data[idx + 2] = 255
texture_data[idx + 3] = mu.default_atlas_alpha[idx/4]
idx += 4
}
state.mu_tex = e2_draw.create_texture(texture_data, mu.DEFAULT_ATLAS_WIDTH, mu.DEFAULT_ATLAS_HEIGHT)
}
state.mu_tex = e2_draw.create_texture(texture_data, mu.DEFAULT_ATLAS_WIDTH, mu.DEFAULT_ATLAS_HEIGHT)
}
normalize_rect :: proc(rect : mu.Rect, size : i32) -> e2_draw.Rect
@@ -251,78 +236,84 @@ update_frame :: proc(state: ^engine_state)
state.width , state.height = glfw.GetFramebufferSize(state.window)
gl.Viewport(0, 0, state.width, state.height)
/*
imgui_impl_opengl3.NewFrame()
imgui_impl_glfw.NewFrame()
im.NewFrame()
*/
}
end_frame :: proc(state: ^engine_state)
{
/*
im.Render()
imgui_impl_opengl3.RenderDrawData(im.GetDrawData())
*/
//Microui
fb_x, fb_y := glfw.GetFramebufferSize(state.window)
state.draw.glyph.atlas_width = fb_x
state.draw.glyph.atlas_height = fb_y
cmd: ^mu.Command
draw := &state.draw
for mu.next_command(&state.mu_ctx, &cmd)
{
cmd: ^mu.Command
draw := &state.draw
for mu.next_command(&state.mu_ctx, &cmd)
{
#partial switch c in cmd.variant{
case ^mu.Command_Text:
{
color := c.color
append(&draw.texts, e2_draw.TextItem{c.str, {f32(c.pos.x), f32(c.pos.y)}, {c.color.r, color.g, color.b, color.a}})
}
case ^mu.Command_Rect:
{
rect := c.rect
pos :[2]f32= {f32(rect.x), f32(rect.y)}
wh :[2]f32= {f32(rect.w), f32(rect.h)}
w, h := wh.x, wh.y
points : [4][2]f32 = {
{pos.x + w, pos.y},
{pos.x, pos.y},
{pos.x, pos.y + h},
{pos.x + w, pos.y + h},
}
for &p, i in &points do points[i] = e2_draw.camera_convert_screen_to_world(&draw.cam, p)
color := c.color
//hex_color := e2_draw.make_hex_color({color.r, color.g, color.b, color.a})
e2_draw.solid_polygon_add(&draw.polygons, {q = e2_draw.Rot{c = 1}}, &points[0], 4, 0.01, transmute([4]u8)color)
}
case ^mu.Command_Clip:
{
rect := c.rect
gl.Scissor(rect.x, state.height - (rect.y + rect.h), rect.w, rect.h)
}
case ^mu.Command_Icon:{
rect := mu.default_atlas[c.id]
append(&draw.textures.textures, e2_draw.TextureData{
texture_id = state.mu_tex,
src_rect = normalize_rect(rect, mu.DEFAULT_ATLAS_WIDTH),
dst_rect = e2_draw.Rect{f32(c.rect.x), f32(c.rect.y), f32(rect.w), f32(rect.h)},
})
}
#partial switch c in cmd.variant{
case ^mu.Command_Text:
{
color := c.color
append(&draw.texts, e2_draw.TextItem{c.str, {f32(c.pos.x), f32(c.pos.y)}, {c.color.r, color.g, color.b, color.a}})
}
case ^mu.Command_Rect:
{
rect := c.rect
pos :[2]f32= {f32(rect.x), f32(rect.y)}
wh :[2]f32= {f32(rect.w), f32(rect.h)}
w, h := wh.x, wh.y
points : [4][2]f32 = {
{pos.x + w, pos.y},
{pos.x, pos.y},
{pos.x, pos.y + h},
{pos.x + w, pos.y + h},
}
for &p, i in &points do points[i] = e2_draw.camera_convert_screen_to_world(&draw.cam, p)
color := c.color
e2_draw.solid_polygon_add(&draw.polygons, {q = e2_draw.Rot{c = 1}}, &points[0], 4, 0.01, transmute([4]u8)color)
}
case ^mu.Command_Clip:
{
rect := c.rect
gl.Scissor(rect.x, state.height - (rect.y + rect.h), rect.w, rect.h)
}
case ^mu.Command_Icon:{
rect := mu.default_atlas[c.id]
append(&draw.textures.textures, e2_draw.TextureData{
texture_id = state.mu_tex,
src_rect = normalize_rect(rect, mu.DEFAULT_ATLAS_WIDTH),
dst_rect = e2_draw.Rect{f32(c.rect.x), f32(c.rect.y), f32(rect.w), f32(rect.h)},
})
}
}
}
glfw.SwapBuffers(state.window)
__e2d_interal.viewport_changed = false
}
cleanup :: proc(state: ^engine_state)
{
imgui_impl_opengl3.Shutdown()
imgui_impl_glfw.Shutdown()
//imgui_impl_opengl3.Shutdown()
//imgui_impl_glfw.Shutdown()
}
engine_should_close :: proc(state : ^engine_state) -> b32
@@ -367,6 +358,17 @@ is_key_released :: #force_inline proc(state: ^engine_state, key : i32) -> bool{
draw_flush :: proc(d: ^e2_draw.Draw)
{
e2_draw.draw_flush(d)
if __e2d_interal.viewport_changed
{
d.glyph.atlas_width = d.cam.width
d.glyph.atlas_height = d.cam.height
d.glyph.font_size_pt = 9
d.glyph.width = d.cam.width
d.glyph.height = d.cam.height
e2_glyph.glyph_init(&d.glyph, d.font_path)
}
e2_draw.draw_flush(d, __e2d_interal.viewport_changed)
}