Compiler game work + enable cast from pointer to int

This commit is contained in:
Krzosa Karol
2023-04-18 13:50:57 +02:00
parent 013db0f985
commit 4ecb329033
7 changed files with 153 additions and 45 deletions

View File

@@ -5,11 +5,70 @@ Array :: struct($T: Type)
len: int
cap: int
Add :: (a: *Array($T), item: T)
Pop :: (a: *Array($T)): T
if a.len > 0
a.len -= 1
return a.data[a.len]
// @reproduction: c compile error, probably because of compound expression (Array){}
// return {}
result: T
return result
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
GetIndex :: (a: *Array($T), item: *T): int
Assert(a.len > 0)
Assert(item >= a.data && item < a.data + a.len)
// @reproduction: compiler hangs
// index := (item - a.data)->*void->size_t
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->U64)
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->U64)
a.data = realloc(a.data, SizeOf(T) * a.cap->size_t)
Add :: (a: *Array($T), item: T)
TryGrowing(a)
a.data[a.len++] = 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

View File

@@ -1,6 +1,3 @@
#import "raylib.core"
#load "array.core"
/*@feature_idea: labeled block
@@ -63,9 +60,23 @@ Add(&guys, {100, 100})
//
// Map map = (Map ){.data = (&(map_data[0])), .data = 0x10, .x = 16};
// @reproduction
// if Mode == 0 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; GuyP = {x,y}
//
// Error! Couldn't infer type of compound expression
//
// Couldn't figure out type of compound expression when variable got declared and separetly
// got assigned a value
//
Guy :: struct
pos: Vector2
#import "raylib.core"
MA :: #import "Arena.core"
#load "array.core"
V2I :: struct
x: int
y: int
Map_Tile :: int
Map :: struct
@@ -73,54 +84,85 @@ Map :: struct
x: int
y: int
SCR_X := 1280
SCR_Y := 720
Actor :: struct
p: V2I
target_p: V2I
M_X := 0
M_Y := 0
M_P: Vector2
WinX := 1280
WinY := 720
MouseX := 0
MouseY := 0
MouseP: Vector2
Mode := 0
RectX :: 16
RectY :: 16
GuyP: V2I
main :: (): int
guys: Array(Guy)
Add(&guys, {pos = {100, 100}})
map_data: [16*16]int
map: Map = {data = &map_data[0], x = 16, y = 16}
MAP_X :: 60
MAP_Y :: 40
map_data: [MAP_X*MAP_Y]int
map: Map = {data = &map_data[0], x = MAP_X, y = MAP_Y}
InitWindow(SCR_X, SCR_Y, "Testing")
SetTargetFPS(60)
actors: Array(Actor)
Reserve(&actors, 4)
Add(&actors, {{4, 4}, {8, 8}})
Assert(Contains(&actors, actors.data) == true)
OrderedRemove(&actors, actors.data)
Assert(Contains(&actors, actors.data) == false)
// InitAudioDevice()
// sound := LoadSound("catune - Pass the town, and to the C.mp3")
// SetMasterVolume(0.01)
// PlaySound(sound)
InitWindow(WinX, WinY, "Testing")
SetTargetFPS(60)
for !WindowShouldClose()
SCR_X = GetScreenWidth()
SCR_Y = GetScreenHeight()
M_X = GetMouseX()
M_Y = GetMouseY()
M_P = GetMousePosition()
WinX = GetScreenWidth()
WinY = GetScreenHeight()
MouseX = GetMouseX()
MouseY = GetMouseY()
MouseP = GetMousePosition()
if IsKeyPressed(KEY_F1) ;; Mode := 0
if IsKeyPressed(KEY_F2) ;; Mode := 1
BeginDrawing()
ClearBackground(RAYWHITE)
RX := 16
RY := 16
for x := 0, x < 16, x += 1
for y := 0, y < 16, y += 1
it := map.data[x + y*map.x]
r: Rectangle = {(x * RX)->F32, (y * RY)->F32, RX->F32, RY->F32}
for x := 0, x < map.x, x += 1
for y := 0, y < map.y, y += 1
it := map.data + (x + y*map.x)
r: Rectangle = {x->F32 * RectX, y->F32 * RectY, RectX, RectY}
r2: Rectangle = {r.x + 1, r.y + 1, r.width - 2, r.height - 2}
color := RED
if it == 1 ;; color = GREEN
if CheckCollisionPointRec(M_P, r) ;; color = BLUE
colliding := CheckCollisionPointRec(MouseP, r)
if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) ;; *it = 1
if Mode == 0 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; GuyP = V2I{x,y}
color := GREEN
if *it == 1 ;; color = RED
if colliding == true ;; color = MAROON
DrawRectangleRec(r2, color)
for i := 0, i < actors.len, i += 1
it := actors.data + i
r := Rectangle{it.p.x->F32 * RectX, it.p.y->F32 * RectY, RectX, RectY}
target_r := Rectangle{it.target_p.x->F32 * RectX, it.target_p.y->F32 * RectY, RectX, RectY}
DrawRectangleRec(r, YELLOW)
DrawRectangleRec(target_r, PURPLE)
// DrawFPS(0, 0)
// DrawText(TextFormat("Testing %d", 32), 100, 100, 20, MAROON)