Refactor draw to be separate and handle ortho draw

This commit is contained in:
2026-03-23 13:32:46 +05:45
parent 157f04060e
commit 739a806493
6 changed files with 637 additions and 267 deletions
+112 -10
View File
@@ -1,23 +1,26 @@
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 draw "shared:Edit2D/draw"
import e2_draw "shared:Edit2D/draw"
import "core:reflect"
import mu "vendor:microui"
engine_state :: struct
{
window : glfw.WindowHandle,
draw : draw.Draw,
draw : e2_draw.Draw,
restart, pause : bool,
substep_count : u32,
@@ -29,7 +32,8 @@ engine_state :: struct
drop_callback : glfw.DropProc,
input : input_state,
mu_ctx : mu.Context,
}
MAX_KEYS :: 512
@@ -95,17 +99,24 @@ engine_check_types :: proc($Game: typeid)
This will only be called once to initilize the engine
initilize graphics library, glfw, callbacks
*/
engine_init :: proc($GameType : typeid, state: ^engine_state)
engine_init :: proc($GameType : typeid, state: ^engine_state, font_path : string="")
{
engine_check_types(GameType)
assert(glfw.Init() == true)
glfw.WindowHint(glfw.SCALE_TO_MONITOR, 1)
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 4)
glfw.WindowHint(glfw.OPENGL_DEBUG_CONTEXT, true)
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 5)
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
state.window = glfw.CreateWindow(state.width, state.height, state.title, nil, nil)
//gl.DebugMessageControl(gl.DONT_CARE, gl.DONT_CARE, gl.DONT_CARE, 0, nil, gl.TRUE);
assert(state.window != nil)
@@ -139,7 +150,7 @@ engine_init :: proc($GameType : typeid, state: ^engine_state)
imgui_impl_glfw.InitForOpenGL(state.window, true)
imgui_impl_opengl3.Init("#version 150")
state.draw.cam = draw.camera_init()
state.draw.cam = e2_draw.camera_init()
display_w, display_h := glfw.GetFramebufferSize(state.window)
state.draw.cam.width = display_w
@@ -147,7 +158,7 @@ engine_init :: proc($GameType : typeid, state: ^engine_state)
state.draw.cam.zoom = 15
state.draw.show_ui = true
draw.draw_create(&state.draw, &state.draw.cam)
e2_draw.draw_create(&state.draw, &state.draw.cam, font_path)
cbor.tag_register_type({
marshal = proc(_: ^cbor.Tag_Implementation, e: cbor.Encoder, v: any) -> cbor.Marshal_Error {
@@ -159,6 +170,16 @@ engine_init :: proc($GameType : typeid, state: ^engine_state)
},
}, 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
}
}
@@ -168,6 +189,25 @@ update_frame :: proc(state: ^engine_state)
glfw.PollEvents()
keyboard_update(state)
x, y:= glfw.GetCursorPos(state.window)
mu.input_mouse_move(&state.mu_ctx, i32(x), i32(y))
{
for key in key_map
{
if is_key_pressed(state, key) do mu.input_key_down(&state.mu_ctx, key_map[key])
if is_key_released(state, key) do mu.input_key_up(&state.mu_ctx, key_map[key])
}
for key in mouse_map
{
if is_key_pressed(state, key) do mu.input_mouse_down(&state.mu_ctx, i32(x), i32(y), mouse_map[key])
if is_key_released(state, key) do mu.input_mouse_up(&state.mu_ctx,i32(x), i32(y), mouse_map[key])
}
}
gl.ClearColor(0.4, 0.5, 0.6, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
@@ -187,6 +227,68 @@ end_frame :: proc(state: ^engine_state)
{
im.Render()
imgui_impl_opengl3.RenderDrawData(im.GetDrawData())
//Microui
{
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 = b2.Rot{c = 1}}, &points[0], 4, 0, hex_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:{
if c.id == .CHECK
{
pos :[2]f32= {f32(c.rect.x), f32(c.rect.y)}
pos.x += f32(c.rect.w)/2.0
pos.y += f32(c.rect.h)/2.0
pos = e2_draw.camera_convert_screen_to_world(&draw.cam, pos)
e2_draw.solid_circle_add(&draw.solid_circles,{p = pos, q = b2.Rot{c = 1}} , 0.1,{255, 255, 255, 255})
}else{
fmt.println(c.id)
}
}
}
}
}
glfw.SwapBuffers(state.window)
}
@@ -236,9 +338,9 @@ is_key_released :: #force_inline proc(state: ^engine_state, key : i32) -> bool{
}
draw_flush :: proc(d: ^draw.Draw)
draw_flush :: proc(d: ^e2_draw.Draw)
{
draw.draw_flush(d)
e2_draw.draw_flush(d)
}