From 43a1c20d73ef67dae7763da1f1027ab806342049 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 5 Mar 2024 10:40:28 -0500 Subject: [PATCH] General refactors to various files * Moved all mapped procedure overloads to grime.odin * Removed extraneous entity_box related procedures * Major refactor to grime_array its header is now allocated as part of its initialization * --- code/collision.odin | 36 -------------- code/grime.odin | 54 +++++++++++++++++++-- code/grime_array.odin | 96 ++++++++++++++++++++++++++----------- code/grime_hashmap_zpl.odin | 10 ++-- code/input.odin | 8 ---- code/space.odin | 15 ------ code/text.odin | 3 +- code/ui.odin | 13 ++--- 8 files changed, 129 insertions(+), 106 deletions(-) diff --git a/code/collision.odin b/code/collision.odin index 90b6bc9..cde9c2c 100644 --- a/code/collision.odin +++ b/code/collision.odin @@ -8,42 +8,6 @@ pos_within_range2 :: proc( pos : Vec2, range : Range2 ) -> b32 { return b32(within_x && within_y) } -box_is_within :: proc( box : ^ Box2, pos : Vec2 ) -> b32 { - bounds := box_get_bounds( box ) - within_x_bounds : b32 = pos.x >= bounds.top_left.x && pos.x <= bounds.bottom_right.x - within_y_bounds : b32 = pos.y >= bounds.bottom_right.y && pos.y <= bounds.top_left.y - return within_x_bounds && within_y_bounds -} - -// Not sure if I should in the future not do the radius check, -// As it maybe be better off as a general proc used in an iteration... -box_is_within_view :: proc( box : ^ Box2 ) -> b32 -{ - state := get_state(); using state - screen_extent := app_window.extent - - screen_bounds_radius := max(screen_extent.x, screen_extent.y) - box_bounds_radius := max(box.extent.x, box.extent.y) - - cam := project.workspace.cam - cam_box_distance := linalg.distance(cam.target, box.position) - acceptable_distance := box_bounds_radius + screen_bounds_radius - - if cam_box_distance > acceptable_distance { - return false - } - - screen_bounds := view_get_bounds() - bounds := box_get_bounds( box ) - - within_bounds : b32 = false - - // within_x_bounds : b32 = pos.x >= bounds.top_left.x && pos.x <= bounds.bottom_right.x - // within_y_bounds : b32 = pos.y >= bounds.bottom_right.y && pos.y <= bounds.top_left.y - - return within_bounds -} - is_within_screenspace :: proc( pos : Vec2 ) -> b32 { state := get_state(); using state screen_extent := state.app_window.extent diff --git a/code/grime.odin b/code/grime.odin index 2aa80dd..80c9af2 100644 --- a/code/grime.odin +++ b/code/grime.odin @@ -29,18 +29,24 @@ import fmt_io "core:fmt" import "core:mem" Allocator :: mem.Allocator AllocatorError :: mem.Allocator_Error + AllocatorMode :: mem.Allocator_Mode + AllocatorModeSet :: mem.Allocator_Mode_Set alloc :: mem.alloc alloc_bytes :: mem.alloc_bytes Arena :: mem.Arena arena_allocator :: mem.arena_allocator arena_init :: mem.arena_init + byte_slice :: mem.byte_slice free :: mem.free ptr_offset :: mem.ptr_offset + resize :: mem.resize slice_ptr :: mem.slice_ptr TrackingAllocator :: mem.Tracking_Allocator tracking_allocator :: mem.tracking_allocator tracking_allocator_init :: mem.tracking_allocator_init import "core:mem/virtual" +import "core:odin" + SourceCodeLocation :: runtime.Source_Code_Location import "core:os" FileFlag_Create :: os.O_CREATE FileFlag_ReadWrite :: os.O_RDWR @@ -67,10 +73,20 @@ import "core:unicode/utf8" OS_Type :: type_of(ODIN_OS) -// Alias Tables +context_ext :: proc( $ Type : typeid ) -> (^Type) { + return cast(^Type) context.user_ptr +} + +// Proc Name Overloads Alias table +// This has to be done on a per-module basis. Most likely can be automated + +cm_to_pixels :: proc { + f32_cm_to_pixels, + vec2_cm_to_pixels, + range2_cm_to_pixels, +} get_bounds :: proc { - box_get_bounds, view_get_bounds, } @@ -78,6 +94,24 @@ is_power_of_two :: proc { is_power_of_two_u32, } +pop :: proc { + stack_pop, + stack_allocator_pop, +} + +pressed :: proc { + btn_pressed, +} + +push :: proc { + stack_push, + stack_allocator_push, +} + +released :: proc { + btn_released, +} + to_runes :: proc { string_to_runes, } @@ -87,6 +121,18 @@ to_string :: proc { str_builder_to_string, } -context_ext :: proc( $ Type : typeid ) -> (^Type) { - return cast(^Type) context.user_ptr +pixels_to_cm :: proc { + f32_pixels_to_cm, + vec2_pixels_to_cm, + range2_pixels_to_cm, +} + +points_to_pixels :: proc { + f32_points_to_pixels, + vec2_points_to_pixels, +} + +ui_set_layout :: proc { + ui_style_set_layout, + ui_style_theme_set_layout, } diff --git a/code/grime_array.odin b/code/grime_array.odin index f89338c..59ce953 100644 --- a/code/grime_array.odin +++ b/code/grime_array.odin @@ -8,14 +8,26 @@ import "core:c/libc" import "core:mem" import "core:slice" -Array :: struct ( $ Type : typeid ) { +// Array :: struct ( $ Type : typeid ) { +// allocator : Allocator, +// capacity : u64, +// num : u64, +// data : [^]Type, +// } + +ArrayHeader :: struct ( $ Type : typeid ) { allocator : Allocator, capacity : u64, num : u64, data : [^]Type, } -array_underlying_slice :: proc(slice: []($ Type)) -> Array(Type) { +Array :: struct ( $ Type : typeid ) { + using header : ^ArrayHeader(Type), +} + +array_underlying_slice :: proc(slice: []($ Type)) -> Array(Type) +{ if len(slice) == 0 { return nil } @@ -41,17 +53,23 @@ array_init :: proc( $ Type : typeid, allocator : Allocator ) -> ( Array(Type), A return array_init_reserve( Type, allocator, array_grow_formula(0) ) } -array_init_reserve :: proc( $ Type : typeid, allocator : Allocator, capacity : u64 ) -> ( Array(Type), AllocatorError ) +array_init_reserve :: proc +( $ Type : typeid, allocator : Allocator, capacity : u64 ) -> ( result : Array(Type), alloc_error : AllocatorError ) { - raw_data, result_code := alloc( size_of(Array) + int(capacity) * size_of(Type), allocator = allocator ) - result := cast(^Array(Type)) raw_data; - result.data = cast( [^]Type ) (cast( [^]Array(Type)) result)[ 1:] + header_size :: size_of(ArrayHeader) + + raw_mem : rawptr + raw_mem, alloc_error = alloc( header_size + int(capacity) * size_of(Type), allocator = allocator ) + if alloc_error != AllocatorError.None do return + + result.header = cast( ^ArrayHeader(Type)) raw_mem; result.allocator = allocator result.capacity = capacity - return (result ^), result_code + result.data = cast( [^]Type ) (cast( [^]ArrayHeader(Type)) result.header)[ 1:] + return } -array_append :: proc( using self : ^ Array( $ Type), value : Type ) -> AllocatorError +array_append :: proc( using self : ^Array( $ Type), value : Type ) -> AllocatorError { if num == capacity { @@ -66,7 +84,7 @@ array_append :: proc( using self : ^ Array( $ Type), value : Type ) -> Allocator return AllocatorError.None } -array_append_slice :: proc( using self : ^ Array( $ Type ), items : []Type ) -> AllocatorError +array_append_slice :: proc( using self : ^Array( $ Type ), items : []Type ) -> AllocatorError { if num + len(items) > capacity { @@ -87,7 +105,7 @@ array_append_slice :: proc( using self : ^ Array( $ Type ), items : []Type ) -> return AllocatorError.None } -array_append_at :: proc( using self : ^ Array( $ Type ), item : Type, id : u64 ) -> AllocatorError +array_append_at :: proc( using self : ^Array( $ Type ), item : Type, id : u64 ) -> AllocatorError { id := id if id >= num { @@ -119,7 +137,7 @@ array_append_at :: proc( using self : ^ Array( $ Type ), item : Type, id : u64 ) return AllocatorError.None } -array_append_at_slice :: proc( using self : ^ Array( $ Type ), items : []Type, id : u64 ) -> AllocatorError +array_append_at_slice :: proc( using self : ^Array( $ Type ), items : []Type, id : u64 ) -> AllocatorError { id := id if id >= num { @@ -150,18 +168,28 @@ array_append_at_slice :: proc( using self : ^ Array( $ Type ), items : []Type, i return AllocatorError.None } -array_back :: proc( using self : ^ Array( $ Type ) ) -> ^ Type { +array_push_back :: proc( using self : Array( $ Type)) -> b32 { + if num == capacity { + return false + } + + data[ num ] = value + num += 1 + return true +} + +array_back :: proc( using self : Array( $ Type ) ) -> ( ^Type) { return & data[ num - 1 ] } -array_clear :: proc( using self : ^ Array( $ Type ), zero_data : b32 ) { +array_clear :: proc( using self : Array( $ Type ), zero_data : b32 ) { if zero_data { mem.set( raw_data( data ), 0, num ) } num = 0 } -array_fill :: proc( using self : ^ Array( $ Type ), begin, end : u64, value : Type ) -> b32 +array_fill :: proc( using self : Array( $ Type ), begin, end : u64, value : Type ) -> b32 { if begin < 0 || end >= num { return false @@ -177,12 +205,12 @@ array_fill :: proc( using self : ^ Array( $ Type ), begin, end : u64, value : Ty return true } -array_free :: proc( using self : ^ Array( $ Type ) ) { +array_free :: proc( using self : Array( $ Type ) ) { free( data, allocator ) - data = nil + self.data = nil } -array_grow :: proc( using self : ^ Array( $ Type ), min_capacity : u64 ) -> AllocatorError +array_grow :: proc( using self : ^Array( $ Type ), min_capacity : u64 ) -> AllocatorError { new_capacity := array_grow_formula( capacity ) @@ -192,12 +220,12 @@ array_grow :: proc( using self : ^ Array( $ Type ), min_capacity : u64 ) -> Allo return array_set_capacity( self, new_capacity ) } -array_pop :: proc( using self : ^ Array( $ Type ) ) { +array_pop :: proc( using self : Array( $ Type ) ) { verify( num != 0, "Attempted to pop an array with no elements" ) num -= 1 } -array_remove_at :: proc( using self : ^ Array( $ Type ), id : u64 ) +array_remove_at :: proc( using self : Array( $ Type ), id : u64 ) { verify( id >= num, "Attempted to remove from an index larger than the array" ) @@ -208,7 +236,7 @@ array_remove_at :: proc( using self : ^ Array( $ Type ), id : u64 ) num -= 1 } -array_reserve :: proc( using self : ^ Array( $ Type ), new_capacity : u64 ) -> AllocatorError +array_reserve :: proc( using self : ^Array( $ Type ), new_capacity : u64 ) -> AllocatorError { if capacity < new_capacity { return array_set_capacity( self, new_capacity ) @@ -216,7 +244,7 @@ array_reserve :: proc( using self : ^ Array( $ Type ), new_capacity : u64 ) -> A return AllocatorError.None } -array_resize :: proc( array : ^ Array( $ Type ), num : u64 ) -> AllocatorError +array_resize :: proc( array : ^Array( $ Type ), num : u64 ) -> AllocatorError { if array.capacity < num { @@ -230,23 +258,33 @@ array_resize :: proc( array : ^ Array( $ Type ), num : u64 ) -> AllocatorError return AllocatorError.None } -array_set_capacity :: proc( using self : ^ Array( $ Type ), new_capacity : u64 ) -> AllocatorError +array_set_capacity :: proc( self : ^Array( $ Type ), new_capacity : u64 ) -> AllocatorError { - if new_capacity == capacity { + if new_capacity == self.capacity { return AllocatorError.None } - if new_capacity < num { - num = new_capacity + if new_capacity < self.num { + self.num = new_capacity return AllocatorError.None } - new_data, result_code := alloc( cast(int) new_capacity * size_of(Type), allocator = allocator ) + header_size :: size_of(ArrayHeader(Type)) + + new_size := header_size + cast(int) new_capacity * size_of(Type) + old_size := header_size + cast(int) self.capacity * size_of(Type) + + new_mem, result_code := resize( self.header, old_size, new_size, allocator = self.allocator ) if result_code != AllocatorError.None { ensure( false, "Failed to allocate for new array capacity" ) return result_code } - free( data ) - data = cast( [^] Type ) new_data - capacity = new_capacity + + using new_self : Array(Type) + header = cast( ^ArrayHeader(Type)) new_mem; + data = cast( [^]Type ) (cast( [^]ArrayHeader(Type)) header)[ 1:] + capacity = new_capacity + num = self.num + + (self ^) = new_self return result_code } diff --git a/code/grime_hashmap_zpl.odin b/code/grime_hashmap_zpl.odin index b4de906..8099c50 100644 --- a/code/grime_hashmap_zpl.odin +++ b/code/grime_hashmap_zpl.odin @@ -4,7 +4,8 @@ // This implementation uses two ZPL-Based Arrays to hold entires and the actual hash table. // Its algorithim isn't that great, removal of elements is very expensive. -// To the point where if thats done quite a bit another implementation should be looked at. +// Growing the hashtable doesn't do a resize on the original arrays properly, leading to completely discarded memory. +// Its recommended to use something closer to raddbg's implementation for greater flexibility. package sectr import "core:slice" @@ -40,7 +41,8 @@ zpl_hmap_init :: proc( $ Type : typeid, allocator : Allocator ) -> ( HMapZPL( Ty return zpl_hmap_init_reserve( Type, allocator ) } -zpl_hmap_init_reserve :: proc( $ Type : typeid, allocator : Allocator, num : u64 ) -> ( HMapZPL( Type), AllocatorError ) +zpl_hmap_init_reserve :: proc +( $ Type : typeid, allocator : Allocator, num : u64 ) -> ( HMapZPL( Type), AllocatorError ) { result : HMapZPL(Type) hashes_result, entries_result : AllocatorError @@ -72,8 +74,8 @@ zpl_hmap_clear :: proc( using self : ^ HMapZPL( $ Type ) ) { zpl_hmap_destroy :: proc( using self : ^ HMapZPL( $ Type ) ) { if hashes.data != nil && hashes.capacity > 0 { - array_free( & hashes ) - array_free( & entries ) + array_free( hashes ) + array_free( entries ) } } diff --git a/code/input.odin b/code/input.odin index da9b595..d49c015 100644 --- a/code/input.odin +++ b/code/input.odin @@ -21,14 +21,6 @@ btn_released :: proc ( btn : DigitalBtn ) -> b32 { return btn.ended_down == false && btn.half_transitions > 0 } -pressed :: proc { - btn_pressed, -} - -released :: proc { - btn_released, -} - MaxKeyboardKeys :: 256 KeyboardKey :: enum u32 { null = 0x00, diff --git a/code/space.odin b/code/space.odin index ee849ff..3a46d12 100644 --- a/code/space.odin +++ b/code/space.odin @@ -21,22 +21,7 @@ when ODIN_OS == OS_Type.Windows { // 1 inch = 2.54 cm, 96 inch * 2.54 = 243.84 DPCM } -cm_to_pixels :: proc { - f32_cm_to_pixels, - vec2_cm_to_pixels, - range2_cm_to_pixels, -} -pixels_to_cm :: proc { - f32_pixels_to_cm, - vec2_pixels_to_cm, - range2_pixels_to_cm, -} - -points_to_pixels :: proc { - f32_points_to_pixels, - vec2_points_to_pixels, -} //region Unit Conversion Impl diff --git a/code/text.odin b/code/text.odin index 399a7de..ee44cd1 100644 --- a/code/text.odin +++ b/code/text.odin @@ -12,7 +12,8 @@ debug_draw_text :: proc( content : string, pos : Vec2, size : f32, color : rl.Co return } runes, alloc_error := to_runes( content, context.temp_allocator ) - verify( alloc_error == AllocatorError.None, "Failed to temp allocate runes" ) + // runes, alloc_error := to_runes( content, context.temp_allocator ) + // verify( alloc_error == AllocatorError.None, "Failed to temp allocate runes" ) font := font if font.key == Font_Default.key { diff --git a/code/ui.odin b/code/ui.odin index 78b9d3b..3d6739e 100644 --- a/code/ui.odin +++ b/code/ui.odin @@ -268,8 +268,8 @@ UI_State :: struct { layout_dirty : b32, // TODO(Ed) : Look into using a build arena like Ryan does for these possibly (and thus have a linked-list stack) - theme_stack : Stack( UI_StyleTheme, UI_Style_Stack_Size ), - parent_stack : Stack( ^ UI_Box, UI_Parent_Stack_Size ), + theme_stack : StackFixed( UI_StyleTheme, UI_Style_Stack_Size ), + parent_stack : StackFixed( ^ UI_Box, UI_Parent_Stack_Size ), // flag_stack : Stack( UI_BoxFlags, UI_BoxFlags_Stack_Size ), hot : UI_Key, @@ -558,11 +558,11 @@ ui_style_set_layout :: proc ( layout : UI_Layout, preset : UI_StylePreset ) { } ui_style_theme_push :: proc( preset : UI_StyleTheme ) { - stack_push( & get_state().ui_context.theme_stack, preset ) + push( & get_state().ui_context.theme_stack, preset ) } ui_style_theme_pop :: proc() { - stack_pop( & get_state().ui_context.theme_stack ) + pop( & get_state().ui_context.theme_stack ) } @(deferred_none = ui_style_theme_pop) @@ -575,8 +575,3 @@ ui_style_theme_set_layout :: proc ( layout : UI_Layout ) { preset.layout = layout } } - -ui_set_layout :: proc { - ui_style_set_layout, - ui_style_theme_set_layout, -}