Core: Array is now module
This commit is contained in:
@@ -1,101 +0,0 @@
|
||||
#import "LibC.core"
|
||||
|
||||
Array :: struct($T: Type)
|
||||
data: *T
|
||||
len: int
|
||||
cap: int
|
||||
|
||||
Pop :: (a: *Array($T)): T
|
||||
if a.len > 0
|
||||
a.len -= 1
|
||||
return a.data[a.len]
|
||||
return {}
|
||||
|
||||
Contains :: (a: *Array($T), item: *T): bool
|
||||
result := item >= a.data && item < a.data + a.len
|
||||
return result
|
||||
|
||||
Free :: (a: *Array($T))
|
||||
free(a.data)
|
||||
a.cap = 0; a.len = 0; a.data = 0
|
||||
|
||||
Reset :: (a: *Array($T))
|
||||
a.len = 0
|
||||
|
||||
GetLast :: (a: *Array($T)): *T
|
||||
Assert(a.len > 0)
|
||||
result := a.data + a.len-1
|
||||
return result
|
||||
|
||||
Insert :: (a: *Array($T), item: T, index: int)
|
||||
if index == a.len
|
||||
Add(a, item)
|
||||
return
|
||||
|
||||
Assert(index < a.len)
|
||||
Assert(index >= 0)
|
||||
|
||||
TryGrowing(a)
|
||||
right_len := (a.len - index)->size_t
|
||||
memmove(a.data + index + 1, a.data + index, sizeof(T) * right_len)
|
||||
a.data[index] = item
|
||||
a.len += 1
|
||||
|
||||
GetIndex :: (a: *Array($T), item: *T): int
|
||||
Assert(a.len > 0)
|
||||
Assert(item >= a.data && item < a.data + a.len)
|
||||
|
||||
index := (item - a.data)->int
|
||||
Assert(index >= 0 && index < a.len)
|
||||
return index
|
||||
|
||||
UnorderedRemove :: (a: *Array($T), item: *T)
|
||||
Assert(a.len > 0)
|
||||
Assert(item >= a.data && item < a.data + a.len)
|
||||
|
||||
*item = a.data[--a.len]
|
||||
|
||||
OrderedRemove :: (a: *Array($T), item: *T)
|
||||
index := GetIndex(a, item)
|
||||
if index == a.len - 1
|
||||
Pop(a)
|
||||
return
|
||||
|
||||
length_right_of_item := (a.len - index - 1)->size_t
|
||||
memmove(a.data + index, a.data + index + 1, length_right_of_item * sizeof(T))
|
||||
a.len -= 1
|
||||
|
||||
TryGrowing :: (a: *Array($T))
|
||||
if a.cap == 0
|
||||
a.cap = 16
|
||||
a.data = malloc(sizeof(T) * a.cap->size_t)
|
||||
if a.len + 1 > a.cap
|
||||
a.cap *= 2
|
||||
a.data = realloc(a.data, sizeof(T) * a.cap->size_t)
|
||||
|
||||
Add :: (a: *Array($T), item: T)
|
||||
TryGrowing(a)
|
||||
a.data[a.len++] = item
|
||||
|
||||
BoundedAdd :: (a: *Array($T), item: T)
|
||||
Assert(a.len + 1 <= a.cap)
|
||||
Add(a, item)
|
||||
|
||||
InsertSortedDecreasing :: (a: *Array($T), item: T)
|
||||
insert_index := -1
|
||||
for i := 0, i < a.len, i += 1
|
||||
it := a.data + i
|
||||
if it.value_to_sort_by <= item.value_to_sort_by
|
||||
insert_index = i
|
||||
Insert(a, item, i)
|
||||
break
|
||||
|
||||
if insert_index == -1
|
||||
Add(a, item)
|
||||
|
||||
Reserve :: (a: *Array($T), size: int)
|
||||
Assert(size > a.cap)
|
||||
a.cap = size
|
||||
p := realloc(a.data, sizeof(T) * a.cap->size_t)
|
||||
Assert(p != 0)
|
||||
a.data = p
|
||||
@@ -1,5 +1,6 @@
|
||||
#import "raylib.core"
|
||||
A :: #load "array.core"
|
||||
#import "LibC.core"
|
||||
A :: #import "array.core"
|
||||
MAP :: #load "map.core"
|
||||
sqrtf :: #foreign (value: F32): F32
|
||||
|
||||
@@ -54,6 +55,11 @@ main :: (): int
|
||||
target_color := RED
|
||||
target_color.a = 255/2
|
||||
|
||||
COLOR_SelectionBox := GREEN
|
||||
COLOR_SelectionBox.a = 255/2
|
||||
|
||||
COLOR_Selected := COLOR_SelectionBox
|
||||
|
||||
testing := 0
|
||||
for !WindowShouldClose()
|
||||
defer ;; testing += 1
|
||||
@@ -200,7 +206,7 @@ main :: (): int
|
||||
for actor_i := 0, actor_i < MouseSelectedActors.len, actor_i += 1
|
||||
actor_it := MouseSelectedActors.data[actor_i]
|
||||
actor_box := MAP.Rect(actor_it.p)
|
||||
DrawRectangleRec(actor_box, GREEN)
|
||||
DrawRectangleRec(actor_box, COLOR_Selected)
|
||||
|
||||
if IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)
|
||||
p := MAP.ScreenToMap(MouseP)
|
||||
@@ -208,8 +214,6 @@ main :: (): int
|
||||
|
||||
if MouseSelecting
|
||||
A.Reset(&MouseSelectedActors)
|
||||
COLOR_SelectionBox := GREEN
|
||||
COLOR_SelectionBox.a = 255/2
|
||||
|
||||
for actor_i := 0, actor_i < map.actors.len, actor_i += 1
|
||||
actor_it := map.actors.data + actor_i
|
||||
|
||||
Reference in New Issue
Block a user