From bea10a621e9787600c9c212672563dcfcc78a092 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 18 Apr 2023 16:23:36 +0200 Subject: [PATCH] Core Compiler: Fix type incomplete polymorph, work on RTS --- base.cpp | 11 ++++++++++- build.bat | 2 +- build/rtsgame/array.core | 32 ++++++++++++++++++++++++++++++-- build/rtsgame/main.core | 39 +++++++-------------------------------- build/rtsgame/map.core | 24 ++++++++++++++++++++++++ core_polymorph.cpp | 1 + 6 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 build/rtsgame/map.core diff --git a/base.cpp b/base.cpp index 3949b44..a01cff2 100644 --- a/base.cpp +++ b/base.cpp @@ -119,6 +119,8 @@ typedef double F64; #define F64MAX DBL_MAX #define F64MIN DBL_MIN +#include + #define api #define CORE_Static static #define global static @@ -126,7 +128,14 @@ typedef double F64; do { \ if (!(x)) Breakpoint; \ } while (0) -#define assert_message(x, ...) assert(x) +#define assert_message(x, ...) \ + do { \ + if (!(x)) { \ + printf(__VA_ARGS__); \ + printf("\n"); \ + Breakpoint; \ + } \ + } while (0) #define invalid_codepath assert_message(0, "Invalid codepath") #define invalid_return \ do { \ diff --git a/build.bat b/build.bat index 690841b..8e18089 100644 --- a/build.bat +++ b/build.bat @@ -1,7 +1,7 @@ @echo off call "..\misc\compile_setup.bat" -rem bld --dont_compile_core +bld --dont_compile_core cd build core_main.exe rtsgame/main.core bld --dont_compile_core --link=vendor/raylib/windows/raylibdll.lib diff --git a/build/rtsgame/array.core b/build/rtsgame/array.core index 8803097..27bb719 100644 --- a/build/rtsgame/array.core +++ b/build/rtsgame/array.core @@ -26,12 +26,27 @@ Free :: (a: *Array($T)) Reset :: (a: *Array($T)) a.len = 0 +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) // @reproduction: compiler hangs // index := (item - a.data)->*void->size_t + index := (item - a.data)->int Assert(index >= 0 && index < a.len) return index @@ -52,7 +67,6 @@ OrderedRemove :: (a: *Array($T), item: *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 @@ -61,11 +75,25 @@ TryGrowing :: (a: *Array($T)) 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(a, item, i) + break + + if insert_index == -1 + Add(a, item) + Reserve :: (a: *Array($T), size: int) Assert(size > a.cap) a.cap = size diff --git a/build/rtsgame/main.core b/build/rtsgame/main.core index c397045..6268ea1 100644 --- a/build/rtsgame/main.core +++ b/build/rtsgame/main.core @@ -71,51 +71,24 @@ Add(&guys, {100, 100}) #import "raylib.core" -MA :: #import "Arena.core" #load "array.core" +#load "map.core" V2I :: struct x: int y: int -Map_Tile :: int -Map :: struct - data: *Map_Tile - x: int - y: int - -Actor :: struct - p: V2I - target_p: V2I - WinX := 1280 WinY := 720 - MouseX := 0 MouseY := 0 MouseP: Vector2 - Mode := 0 RectX :: 16 RectY :: 16 -GuyP: V2I - main :: (): int - - - 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} - - 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) + MAP_Init() // InitAudioDevice() // sound := LoadSound("catune - Pass the town, and to the C.mp3") @@ -138,6 +111,8 @@ main :: (): int BeginDrawing() ClearBackground(RAYWHITE) + + map := &MAP_CurrentMap for x := 0, x < map.x, x += 1 for y := 0, y < map.y, y += 1 it := map.data + (x + y*map.x) @@ -147,7 +122,7 @@ main :: (): int 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} + // if Mode == 0 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; GuyP = V2I{x,y} color := GREEN if *it == 1 ;; color = RED @@ -155,8 +130,8 @@ main :: (): int DrawRectangleRec(r2, color) - for i := 0, i < actors.len, i += 1 - it := actors.data + i + for i := 0, i < map.actors.len, i += 1 + it := map.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} diff --git a/build/rtsgame/map.core b/build/rtsgame/map.core new file mode 100644 index 0000000..0a669c3 --- /dev/null +++ b/build/rtsgame/map.core @@ -0,0 +1,24 @@ + +MAP_CurrentMap: MAP_Map + + +MAP_Tile :: int +MAP_Map :: struct + data: *MAP_Tile + x: int + y: int + actors: Array(Actor) + +Actor :: struct + p: V2I + target_p: V2I + + +MAP_Init :: () + MAP_X :: 60 + MAP_Y :: 40 + + map_data: [MAP_X*MAP_Y]int + MAP_CurrentMap = MAP_Map{data = &map_data[0], x = MAP_X, y = MAP_Y} + Add(&MAP_CurrentMap.actors, {{4, 4}, {8, 8}}) + diff --git a/core_polymorph.cpp b/core_polymorph.cpp index cefc23e..1a0d144 100644 --- a/core_polymorph.cpp +++ b/core_polymorph.cpp @@ -205,6 +205,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array *repl) dst = (Ast_Atom *)ast_copy(it.ast, parent_scope, 0); } else if (it.kind == POLY_TYPE_REPLACEMENT) { + type_complete(it.type); dst = (Ast_Atom *)create_typespec_from_type(dst->pos, parent_scope, it.type); } else invalid_codepath;