From 0c09982fbbc554188ad4a048a136dced6981bacf Mon Sep 17 00:00:00 2001 From: San Jacobs Date: Tue, 23 Sep 2025 13:23:55 +0200 Subject: Scissoring --- src/main.odin | 1 + src/tafl/tafl.odin | 76 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main.odin b/src/main.odin index caae860..f28fab4 100644 --- a/src/main.odin +++ b/src/main.odin @@ -103,6 +103,7 @@ main :: proc() { } t.render() + free_all(context.temp_allocator) } } diff --git a/src/tafl/tafl.odin b/src/tafl/tafl.odin index 474639d..369485b 100644 --- a/src/tafl/tafl.odin +++ b/src/tafl/tafl.odin @@ -4,21 +4,24 @@ import "core:fmt" import "core:strconv" import "core:strings" import rl "vendor:raylib" +import "core:c" __DEFAULT_FONT_FILE :: #load("../../res/Inter_24pt-Medium.ttf") tafl_elements : [4096]Tafl tafl_elements_count : int -tafl_stack : [4096]int +tafl_stack : [4096]int tafl_stack_depth : int child_index_buffer : [4096]int child_index_buffer_len : int -temp_child_buffer : [4096]int +temp_child_buffer : [1024]int temp_child_buffer_len : int // May be inferred by child count, and not be needed +scissor_stack : [1024]Box + DEFAULT_FONT : rl.Font FONT_SIZE : int : 24 @@ -87,7 +90,7 @@ tafl_open :: proc( } if text != "" { - measurement := rl.MeasureTextEx(DEFAULT_FONT, strings.clone_to_cstring(text), f32(FONT_SIZE), 0) + measurement := rl.MeasureTextEx(DEFAULT_FONT, strings.clone_to_cstring(text, allocator=context.temp_allocator), f32(FONT_SIZE), 0) sizing_width = {.FIXED, int(measurement.x), int(measurement.x)} sizing_height = {.FIXED, FONT_SIZE, FONT_SIZE} } @@ -332,22 +335,15 @@ render :: proc() { position_pass() rl.BeginDrawing() - rl.ClearBackground({255,0,0,255}) + rl.ClearBackground({0,0,0,255}) //fmt.println("\n") - #reverse for tafl_index in child_index_buffer[:child_index_buffer_len] { - tafl := tafl_elements[tafl_index] - color : rl.Color = {u8(tafl.color.r*255.0), - u8(tafl.color.g*255.0), - u8(tafl.color.b*255.0), - u8(tafl.color.a*255.0),} - //fmt.printf("%#v\n\n", tafl) - rl.DrawRectangleRec({f32(tafl.x), f32(tafl.y), f32(tafl.width), f32(tafl.height),}, color) - if(tafl.text != "") { - rl.DrawTextEx(DEFAULT_FONT, strings.clone_to_cstring(tafl.text, allocator=context.allocator), {f32(tafl.x), f32(tafl.y)}, f32(FONT_SIZE), 0, rl.WHITE) - } - } + + recursive_draw(0) + + + rl.EndScissorMode() rl.DrawFPS(5, 5) @@ -355,6 +351,40 @@ render :: proc() { clear_layout() } +recursive_draw :: proc(index : int) { + tafl := tafl_elements[index] + + current_scissor : Box + if index > 0 { + parent_scissor := scissor_stack[tafl.own_depth-1] + if parent_scissor.size.x <= 0 || parent_scissor.size.y <= 0 { + return + } + own_scissor : Box = {position={tafl.x, tafl.y}, size={tafl.width, tafl.height}} + current_scissor = box_clamp(parent_scissor, own_scissor) + } else { + current_scissor = {position={tafl.x, tafl.y}, size={tafl.width, tafl.height}} + } + scissor_stack[tafl.own_depth] = current_scissor + rl.BeginScissorMode(c.int(current_scissor.position.x), c.int(current_scissor.position.y), c.int(current_scissor.size.x), c.int(current_scissor.size.y),) + /* + indent(tafl.own_depth) + fmt.printfln("%02d Drawing %02d\t\t{}", tafl.own_depth, tafl.own_index, scissor_stack[tafl.own_depth]) + */ + color : rl.Color = {u8(tafl.color.r*255.0), + u8(tafl.color.g*255.0), + u8(tafl.color.b*255.0), + u8(tafl.color.a*255.0),} + rl.DrawRectangleRec({f32(tafl.x), f32(tafl.y), f32(tafl.width), f32(tafl.height),}, color) + if(tafl.text != "") { + rl.DrawTextEx(DEFAULT_FONT, strings.clone_to_cstring(tafl.text, allocator=context.temp_allocator), {f32(tafl.x), f32(tafl.y)}, f32(FONT_SIZE), 0, rl.WHITE) + } + + for child_index in child_index_buffer[tafl.children.index:tafl.children.index+tafl.children.len] { + recursive_draw(child_index) + } +} + grow_pass :: proc() { #reverse for tafl_index in child_index_buffer[:child_index_buffer_len] { tafl := &tafl_elements[tafl_index] @@ -425,7 +455,7 @@ Tafl :: struct { x, y : int, own_index : int, // Don't ship this - own_depth : int, // Don't ship this + own_depth : int, // Maybe don't ship this parent : ^Tafl, children : struct { @@ -447,6 +477,18 @@ Tafl :: struct { text : string, } +Box :: struct { + position : [2]int, + size : [2]int, +} +box_clamp :: proc(parent, child : Box) -> (output : Box) { + output.position.x = clamp(child.position.x, parent.position.x, parent.position.x+parent.size.x) + output.position.y = clamp(child.position.y, parent.position.y, parent.position.y+parent.size.y) + output.size.x = clamp(child.size.x, 0, parent.position.x+parent.size.x-output.position.x) + output.size.y = clamp(child.size.y, 0, parent.position.y+parent.size.y-output.position.y) + return +} + __child_iterator_index : int = 0 // TODO: NOT THIS children_of :: proc(tafl: ^Tafl) -> (child: ^Tafl, ok: bool) { if __child_iterator_index >= tafl.children.len { -- cgit v1.2.1