diff options
Diffstat (limited to 'code')
-rw-r--r-- | code/basic.felan | 1 | ||||
-rw-r--r-- | code/lib/io.felan | 10 | ||||
-rw-r--r-- | code/lib/memory.felan | 26 | ||||
-rw-r--r-- | code/lib/operator.felan | 48 | ||||
-rw-r--r-- | code/lib/print.felan | 172 | ||||
-rw-r--r-- | code/lib/string.felan | 113 | ||||
-rw-r--r-- | code/lib/types.felan | 2 | ||||
-rw-r--r-- | code/lib/vector.felan | 47 | ||||
-rw-r--r-- | code/main.felan | 184 |
9 files changed, 374 insertions, 229 deletions
diff --git a/code/basic.felan b/code/basic.felan index 1e4ce2f..8f9adb0 100644 --- a/code/basic.felan +++ b/code/basic.felan @@ -3,3 +3,4 @@ @import("lib/io.felan"); @import("lib/memory.felan"); @import("lib/string.felan"); +@import("lib/vector.felan"); diff --git a/code/lib/io.felan b/code/lib/io.felan index c486902..d470331 100644 --- a/code/lib/io.felan +++ b/code/lib/io.felan @@ -14,14 +14,6 @@ println :: (value:anytype) -> void { print_char('\n'); }; -print :: (value:string) -> void { - i := 0; - while i < @cast(value.length,i64) { - print_char(value[i]); - i += 1; - } -}; - print_char :: (value:u8) -> void { putchar(@cast(value,i32)); }; @@ -80,7 +72,7 @@ _print_unsigned :: (comptime t:type, value:t) -> void { } {} while i < NUMBERS_SIZE - 1 { - @putc(numbers[i]); + print_char(numbers[i]); i += 1; } return; diff --git a/code/lib/memory.felan b/code/lib/memory.felan index f1f9898..0082a1e 100644 --- a/code/lib/memory.felan +++ b/code/lib/memory.felan @@ -1,11 +1,29 @@ +@import("operator.felan"); + libc :: @c_library("/usr/lib/libc.so.6"); malloc :: @c_function(libc,"malloc",(i64)->(*void)); +realloc :: @c_function(libc,"realloc",(*void,i64)->(*void)); free :: @c_function(libc,"free",(*void)->void); +memcpy :: @c_function(libc,"memcpy",(*void,*void,i64)->(*void)); + +malloc :: (size:u64,comptime t:type) -> (*t) { + return @cast(malloc(@cast(size*@size_of(t),i64)),*t); +}; + +realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) { + return @cast( + realloc( + @cast(ptr,*void), + @cast(size*@size_of(@type_of(ptr.*)),i64), + ), + @type_of(ptr) + ); +}; -malloc :: (size:i64,comptime t:type) -> (*t) { - return @cast(malloc(size*@cast(@size_of(t),i64)),*t); +free :: (ptr:*anytype) -> void { + free(@cast(ptr,*void)); }; -free :: (a:*anytype) -> void { - free(@cast(a,*void)); +memcpy :: (dest:*anytype,src:@type_of(dest),n:u64) -> void { + memcpy(@cast(dest,*void),@cast(src,*void),@cast(n,i64)); }; diff --git a/code/lib/operator.felan b/code/lib/operator.felan index b7ac63a..5536343 100644 --- a/code/lib/operator.felan +++ b/code/lib/operator.felan @@ -578,7 +578,7 @@ __logical_not__ :: (value:bool) -> bool { return value == false; }; -__logical_and__ :: (left:bool,lazy right:bool) -> bool { +__logical_and__ :: (left:bool,right:bool) macro -> bool { if left == false { return false; } else if right == false { @@ -588,7 +588,7 @@ __logical_and__ :: (left:bool,lazy right:bool) -> bool { } }; -__logical_or__ :: (left:bool,lazy right:bool) -> bool { +__logical_or__ :: (left:bool,right:bool) macro -> bool { if left == true { return true; } else if right == true { @@ -800,28 +800,48 @@ __sub__ :: (left:*anytype,right:i64) -> (@type_of(left)) { return @sub(left,right); }; -__get_item__ :: (left:*anytype,index:i64) -> (@type_of(left.*)) { - return (left + index).*; +__sum__ :: (left:*anytype,right:u64) -> (@type_of(left)) { + return @add(left,right); +}; + +__sub__ :: (left:*anytype,right:u64) -> (@type_of(left)) { + return @sub(left,right); +}; + +__get_item__ :: (left:**anytype,index:i64) -> (@type_of(left.*.*)) { + return (left.* + index).*; }; -__set_item__ :: (left:*anytype,index:i64,item:@type_of(left.*)) -> (@type_of(left.*)) { - return (left + index).* = item; +__set_item__ :: (left:**anytype,index:i64,item:@type_of(left.*.*)) -> (@type_of(left.*.*)) { + return (left.* + index).* = item; }; -__get_item_address__ :: (left:*anytype,index:i64) -> (@type_of(left)) { - return (left + index); +__get_item_address__ :: (left:**anytype,index:i64) -> (@type_of(left.*)) { + return (left.* + index); }; //---------------------- Array ------------------------ -__get_item__ :: (left:[]anytype,index:i64) -> (@type_of(left.ptr.*)) { - return (left.ptr + index).*; +__get_item__ :: (left:*[]anytype,index:i64) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*[]anytype,index:i64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*[]anytype,index:i64) -> (@type_of(left.*.ptr)) { + return (left.*.ptr + index); +}; + +__get_item__ :: (left:*[]anytype,index:u64) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).*; }; -__set_item__ :: (left:[]anytype,index:i64,item:@type_of(left.ptr.*)) -> (@type_of(left.ptr.*)) { - return (left.ptr + index).* = item; +__set_item__ :: (left:*[]anytype,index:u64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).* = item; }; -__get_item_address__ :: (left:[]anytype,index:i64) -> (@type_of(left.ptr)) { - return (left.ptr + index); +__get_item_address__ :: (left:*[]anytype,index:u64) -> (@type_of(left.*.ptr)) { + return (left.*.ptr + index); }; diff --git a/code/lib/print.felan b/code/lib/print.felan deleted file mode 100644 index 2680921..0000000 --- a/code/lib/print.felan +++ /dev/null @@ -1,172 +0,0 @@ -print :: (value:bool)->void{ - if value print("true"); - else print("false"); -}; - -print :: (value:u8)->void{ - putc value; -}; - -print :: (value:[]u8)->void{ - i :u64= 0; - while i < value.length { - putc value[i]; - i += 1; - } -}; - -print_reverse :: (value:[]u8, size:u8)->void{ - size := size; - while size != 0 { - size -= 1; - putc value[size]; - } -}; - -print :: (value:i8)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -128) { - print("128"); - return; - } - value = -value; - } - - output :[3]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i - 1); -}; - -print :: (value:i16)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -32768) { - print("32768"); - return; - } - value = -value; - } - - output :[5]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i); -}; - -print :: (value:i32)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -2147483648) { - print("2147483648"); - return; - } - value = -value; - } - - output :[10]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i - 1); -}; - -print :: (value:i64)->void { - value := value; - if (value < 0) { - putc '-'; - if (value == -9223372036854775808) { - print("9223372036854775808"); - return; - } - value = -value; - } - - output :[19]u8= undefined; - i :u8= 0; - while { - output[i] = '0' + @cast(value % 10, u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(output, i - 1); -}; - -print :: (value:u8)->void{ - value := value; - result :[3]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; - -print :: (value:u16)->void{ - value := value; - result :[5]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; - -print :: (value:u32)->void{ - value := value; - result :[10]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; - -print :: (value:u64)->void{ - value := value; - result :[20]u8 = undefined; - i :u8= 0; - while { - result[i] = '0' + @cast(value % 10,u8); - i += 1; - value /= 10; - value != 0; - } {} - - print_reverse(result, i - 1); -}; diff --git a/code/lib/string.felan b/code/lib/string.felan index c7047b3..789b0fb 100644 --- a/code/lib/string.felan +++ b/code/lib/string.felan @@ -1,9 +1,114 @@ @import("operator.felan"); +@import("io.felan"); @import("types.felan"); +@import("memory.felan"); + +String :: struct { + ptr : *u8; + size : u64; + capacity : u64; +}; + +string_new :: () -> String { + str : String = undefined; + + str.ptr = malloc(0u64,u8); + str.size = 0u64; + str.capacity = 0u64; + + return str; +}; + +string_from :: (str:[]u8) -> String { + result : String = undefined; + + result.ptr = malloc(str.length,u8); + result.size = str.length; + result.capacity = str.length; + + memcpy(result.ptr,str.ptr,str.length); + + return result; +}; + +append :: (this:*String, str:String) -> void { + _grow_if_needed(this, str.size); + + memcpy(this.*.ptr+this.*.size,str.ptr,str.size); + + this.*.size += str.size; +}; + +append :: (this:*String, str:[]u8) -> void { + _grow_if_needed(this, str.length); + + memcpy(this.*.ptr+this.*.size,str.ptr,str.length); + + this.*.size += str.length; +}; + +append :: (this:*String, char:u8) -> void { + _grow_if_needed(this, 1u64); + + this.*[this.*.size] = char; + + this.*.size += 1u64; +}; + +_grow_if_needed :: (this:*String, count:u64) -> void { + new_size := this.*.size + count; + if new_size >= this.*.capacity { + cap := this.*.capacity + this.*.capacity / 2u64 + 1u64; + if new_size >= cap { + cap = new_size; + } + this.*.capacity = cap; + this.*.ptr = realloc(this.*.ptr,this.*.capacity); + } +}; + +slice :: (this:String, begin:i64, end:i64) -> String { + result:String = undefined; + + result.ptr = this.ptr + begin; + result.size = @cast(end-begin,u64); + result.capacity = 0u64; -sub_string :: (str:string, begin:i64, end:i64) -> string { - result :[0]u8 = undefined; - result.ptr = str.ptr + begin; - result.length = @cast(end-begin,u64); return result; }; + +delete :: (this:String) -> void { + free(this.ptr); +}; + +print :: (this:String) -> void { + i := 0u64; + while i < this.size { + print_char(this[i]); + i += 1u64; + } +}; + +__get_item__ :: (left:*String,index:i64) -> u8 { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*String,index:i64,item:u8) -> u8 { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*String,index:i64) -> (*u8) { + return (left.*.ptr + index); +}; + +__get_item__ :: (left:*String,index:u64) -> u8 { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*String,index:u64,item:u8) -> u8 { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*String,index:u64) -> (*u8) { + return (left.*.ptr + index); +}; diff --git a/code/lib/types.felan b/code/lib/types.felan index df7f5c3..087f884 100644 --- a/code/lib/types.felan +++ b/code/lib/types.felan @@ -1,5 +1,3 @@ -string :: []u8; - array :: (comptime size:u64, comptime t:type) -> type { return struct { ptr : *t; diff --git a/code/lib/vector.felan b/code/lib/vector.felan new file mode 100644 index 0000000..61fe77c --- /dev/null +++ b/code/lib/vector.felan @@ -0,0 +1,47 @@ +@import("operator.felan"); +@import("memory.felan"); + +vector :: (comptime t:type)->type{ + return struct { + ptr : *t; + size : u64; + capacity : u64; + }; +}; + +vector_new :: (comptime t:type) -> (vector(t)) { + v : vector(t) = undefined; + v.ptr = malloc(0u64,@type_of(v.ptr.*)); + v.size = 0u64; + v.capacity = 0u64; + return v; +}; + +__get_item__ :: (left:*vector(anytype),index:i64) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).*; +}; + +__set_item__ :: (left:*vector(anytype),index:i64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.*.ptr.*)) { + return (left.*.ptr + index).* = item; +}; + +__get_item_address__ :: (left:*vector(anytype),index:i64) -> (@type_of(left.*.ptr)) { + return (left.*.ptr + index); +}; + +push_back :: (vec:*vector(anytype),value:@type_of(vec.*[0])) -> void { + _grow_if_needed(vec); + vec.*[@cast(vec.*.size,i64)] = value; + vec.*.size += 1u64; +}; + +_grow_if_needed :: (vec:*vector(anytype)) -> void { + if vec.*.size == vec.*.capacity { + vec.*.capacity = vec.*.capacity + vec.*.capacity/2u64 + 1u64; + vec.*.ptr = realloc(vec.*.ptr,vec.*.capacity); + } +}; + +delete :: (vec:vector(anytype)) -> void { + free(vec.ptr); +}; diff --git a/code/main.felan b/code/main.felan index e80b7fc..b8b41ec 100644 --- a/code/main.felan +++ b/code/main.felan @@ -1,33 +1,169 @@ @import("basic.felan"); -file :: @import("file.felan"); - -t :: (comptime formatter : string) macro -> string { - i := 0; - in := 0; - opening := 0; - while @cast(i,u64) < formatter.length { - c := formatter[i]; - if c == '{' { - if in == 0 { - opening = i+1; + +raylib :: @c_library("/usr/lib/libraylib.so.5.5.0"); +InitWindow :: @c_function(raylib,"InitWindow",(i32,i32,*u8)->void); +SetTargetFPS :: @c_function(raylib,"SetTargetFPS",(i32)->void); +WindowShouldClose :: @c_function(raylib,"WindowShouldClose",()->bool); +CloseWindow :: @c_function(raylib,"CloseWindow",()->void); +BeginDrawing :: @c_function(raylib,"BeginDrawing",()->void); +ClearBackground :: @c_function(raylib,"ClearBackground",(Color)->void); +EndDrawing :: @c_function(raylib,"EndDrawing",()->void); +IsKeyPressed :: @c_function(raylib,"IsKeyPressed",(i32)->bool); +IsGestureDetected :: @c_function(raylib,"IsGestureDetected",(u32)->bool); +DrawText :: @c_function(raylib,"DrawText",(*u8,i32,i32,i32,Color)->void); +DrawRectangle :: @c_function(raylib,"DrawRectangle",(i32,i32,i32,i32,Color)->void); +GetFPS :: @c_function(raylib,"GetFPS",()->i32); +DrawLine :: @c_function(raylib,"DrawLine",(startPosX:i32,startPosY:i32,endPosX:i32,endPosY:i32,color:Color)->void); + +Color :: struct { + r:u8; + g:u8; + b:u8; + a:u8; +}; + +KEY_ENTER :: 257i32; + +GESTURE_TAP :: 1u32; + +LOGO :: 0; +TITLE :: 1; +GAMEPLAY :: 2; +ENDING :: 3; + +SCREEN_WIDTH :: 800i32; +SCREEN_HEIGHT :: 450i32; + +RAYWHITE :: color(245u8,245u8,245u8,255u8); +LIGHTGRAY :: color(200u8,200u8,200u8,255u8); +GRAY :: color(130u8,130u8,130u8,255u8); +GREEN :: color(0u8,228u8,48u8,255u8); +DARKGREEN :: color(0u8,117u8,44u8,255u8); +BLUE :: color(0u8,121u8,241u8,255u8); +DARKBLUE :: color(0u8,82u8,172u8,255u8); +PURPLE :: color(200u8,122u8,255u8,255u8); +MAROON :: color(100u8,33u8,55u8,255u8); + +main :: () -> void { + title := "Hello\0"; + + InitWindow(SCREEN_WIDTH,SCREEN_HEIGHT,title.ptr); + + currentScreen := LOGO; + + framesCounter := 0i32; + + TARGET_FPS :: 144i32; + SetTargetFPS(TARGET_FPS); + + while(!WindowShouldClose()){ + if currentScreen == LOGO { + framesCounter += 1i32; + if framesCounter > TARGET_FPS * 2i32 { + currentScreen = TITLE; } - in += 1; - } else if c == '}' { - in -= 1; - if in == 0 { - return sub_string(formatter,opening,i); - } else if in < 0 { - in = 0; + } else if currentScreen == TITLE { + if IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP) { + currentScreen = GAMEPLAY; } + } else if currentScreen == GAMEPLAY { + if IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP) { + currentScreen = ENDING; + } + } else if currentScreen == ENDING { + if IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP) { + currentScreen = TITLE; + } + } + + BeginDrawing(); + + if currentScreen == LOGO { + drawLogo(); + } else if currentScreen == TITLE { + ClearBackground(GREEN); + title := "TITLE SCREEN\0"; + DrawText(title.ptr,20i32,20i32,40i32,DARKGREEN); + text := "PRESS ENTER or TAP to JUMP to GAMEPLAY SCREEN\0"; + DrawText(text.ptr,120i32,220i32,20i32,DARKGREEN); + } else if currentScreen == GAMEPLAY { + drawGamePlay(); + } else if currentScreen == ENDING { + ClearBackground(BLUE); + title := "ENDING SCREEN\0"; + DrawText(title.ptr,20i32,20i32,40i32,DARKBLUE); + text := "PRESS ENTER or TAP to RETURN to TITLE SCREEN\0"; + DrawText(text.ptr,120i32,220i32,20i32,DARKBLUE); + } else { + println(-1); + return; } - i += 1; + + EndDrawing(); } - return ""; + + CloseWindow(); }; -main :: () -> void { - arr :[10][]u8 = undefined; - arr[0] = "Hello"; - print(arr[0]); +drawLogo :: () -> void { + ClearBackground(RAYWHITE); + title := "LOGO SCREEN\0"; + DrawText(title.ptr,20i32,20i32,40i32,LIGHTGRAY); + text := "WAIT for 2 SECONDS...\0"; + DrawText(text.ptr,290i32,220i32,20i32,LIGHTGRAY); +}; + +x := 0i32; +y := 0i32; + +dx := 1i32; +dy := 1i32; + +RECT_SIZE :: 40i32; + +drawGamePlay :: () -> void { + ClearBackground(PURPLE); + title := "GAMEPLAY SCREEN\0"; + DrawText(title.ptr,20i32,20i32,40i32,MAROON); + text := "PRESS ENTER or TAP to JUMP to ENDING SCREEN\0"; + DrawText(text.ptr,130i32,220i32,20i32,MAROON); + DrawRectangle(x,y,RECT_SIZE,RECT_SIZE,MAROON); + DrawLine(SCREEN_WIDTH/2i32,0i32,SCREEN_WIDTH/2i32,SCREEN_HEIGHT,MAROON); + + x += dx; + y += dy; + if x <= 0i32 { + dx = 1i32; + } else if x+RECT_SIZE >= SCREEN_WIDTH { + dx = -1i32; + } + + if y <= 0i32 { + dy = 1i32; + } else if y+RECT_SIZE >= SCREEN_HEIGHT { + dy = -1i32; + } +}; + +color :: (r:u8,g:u8,b:u8,a:u8)->Color { + result:Color = undefined; + + result.r = r; + result.g = g; + result.b = b; + result.a = a; + + return result; }; + +color :: (value : u32) -> Color { + result:Color = undefined; + + result.r = @cast(value >> @cast(3*8,u32),u8); + result.g = @cast(value >> @cast(2*8,u32),u8); + result.b = @cast(value >> @cast(1*8,u32),u8); + result.a = @cast(value >> @cast(0*8,u32),u8); + + return result; +}; |