From 022f874c32d02780b2e2991d52e3013d976f28d7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 13 Oct 2022 12:33:16 +0200 Subject: [PATCH] Working on math libraries --- build.bat | 2 +- core_main.cpp | 4 ++++ examples/raymarcher.core | 22 +++++++++--------- modules/MathF32.core | 34 ++++++++++++++++++++++++++++ modules/{Math.core => MathVec2.core} | 25 -------------------- modules/MathVec3.core | 8 +++---- modules/Multimedia.core | 3 ++- modules/win32_multimedia.core | 2 ++ 8 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 modules/MathF32.core rename modules/{Math.core => MathVec2.core} (72%) diff --git a/build.bat b/build.bat index 3a54032..e448fef 100644 --- a/build.bat +++ b/build.bat @@ -5,6 +5,6 @@ rem cl main.cpp -I.. user32.lib clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib rem ubuntu run clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o core.out -main examples/arms_race/arms_race.core +rem main examples/arms_race/arms_race.core popd diff --git a/core_main.cpp b/core_main.cpp index f2ec5a7..854e84a 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -5,6 +5,9 @@ Current: - [ ] Maybe wait to implement WASM / bytecode emitter before doing this? Probably need some UInt Int types and those should be default, target decides the size of these +- [ ] Fix invalid error message: AlmostLinearToSRGB :: (a: Color);; return {sqrtf(a.r), sqrtf(a.g), sqrtf(a.b), a.a} +- [ ] Fix untyped literal going to codegen stage, example in arms_race +- [ ] Fix and decide what to do when initializing global variable using not constants - [ ] Fix adressing void is possible, maybe make it possible to address void using bytes * a.void + 10 @@ -25,6 +28,7 @@ In the future - [ ] Add ability to do i: int = 0 inside for loops for i: int = 0, i < 10, i+=1 - [ ] Complicated c declaration generation +- [ ] Other kinds of casts, a cast from structs of same layout, a cast without conversion - [ ] Expand macros - [ ] Defer diff --git a/examples/raymarcher.core b/examples/raymarcher.core index 25a764f..f241272 100644 --- a/examples/raymarcher.core +++ b/examples/raymarcher.core @@ -1,6 +1,6 @@ -#import "Math.core" -V3 :: #import "MathVec3.core" -Vec3 :: V3.Vec3 +F :: #import "MathF32.core" +V3 :: #import "MathVec3.core"; Vec3 :: V3.Vec3 +V2 :: #import "MathVec2.core"; Vec2 :: V2.Vec2 Epsilon :: 0.00001 Screen : *U32 @@ -18,8 +18,8 @@ Raymarcher_Update :: () forward := Vec3{0, 0, -1} side := V3.Normalize(V3.Cross(forward, up)) - LightPos.x = cosf(TotalTime->F32)*4 - LightPos.y = sinf(TotalTime->F32)*4 + LightPos.x = F.Cos(TotalTime->F32)*4 + LightPos.y = F.Sin(TotalTime->F32)*4 ambient_color := Vec3{0.2,0.2,0.2} diffuse_color := Vec3{0.7,0.2,0.2} @@ -77,9 +77,9 @@ Raymarcher_Update :: () color = color * light_intensity // Gamma correction - color.x = sqrtf(color.x) - color.y = sqrtf(color.y) - color.z = sqrtf(color.z) + color.x = F.SquareRoot(color.x) + color.y = F.SquareRoot(color.y) + color.z = F.SquareRoot(color.z) Screen[x + y*X] = V3.ConvertToARGB(color) else;; Screen[x + y*X] = 0 @@ -126,7 +126,7 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS } Assert(RegisterClassW(&w) != 0) - screen_size: Vec2I = {1280, 720} + screen_size: V2.Vec2I = {1280, 720} window := CreateWindowExW( dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0, X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = screen_size.x->int, nHeight = screen_size.y->int, @@ -179,12 +179,12 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS Windows_Bitmap :: struct - size: Vec2I + size: V2.Vec2I data: *U32 hdc: HDC dib: HBITMAP -CreateBitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap +CreateBitmap :: (size: V2.Vec2I, bottom_up: Bool = true): Windows_Bitmap result: Windows_Bitmap = {size = size} if bottom_up == false result.size.y = -result.size.y diff --git a/modules/MathF32.core b/modules/MathF32.core new file mode 100644 index 0000000..ad3c075 --- /dev/null +++ b/modules/MathF32.core @@ -0,0 +1,34 @@ +sqrtf :: #foreign (value: F32): F32 +cosf :: #foreign (value: F32): F32 +sinf :: #foreign (value: F32): F32 +floorf :: #foreign (value: F32): F32 +roundf :: #foreign (value: F32): F32 +ceilf :: #foreign (value: F32): F32 + +Floor :: floorf +Round :: roundf +Ceil :: ceilf +SquareRoot :: sqrtf +Cos :: cosf +Sin :: sinf + +Clamp :: (min: F32, value: F32, max: F32): F32 + if value > max;; return max + if value < min;; return min + return value + +ClampBottom :: (min: F32, value: F32): F32 + if value < min;; return min + return value + +Absolute :: (val: F32): F32 + if val < 0;; return -val + return val + +Min :: (a: F32, b: F32): F32 + if a > b ;; return b + return a + +Max :: (a: F32, b: F32): F32 + if a > b ;; return a + return b diff --git a/modules/Math.core b/modules/MathVec2.core similarity index 72% rename from modules/Math.core rename to modules/MathVec2.core index 45704ce..730cefa 100644 --- a/modules/Math.core +++ b/modules/MathVec2.core @@ -1,10 +1,3 @@ -sqrtf :: #foreign (value: F32): F32 -cosf :: #foreign (value: F32): F32 -sinf :: #foreign (value: F32): F32 - -floorf :: #foreign (value: F32): F32 -roundf :: #foreign (value: F32): F32 -ceilf :: #foreign (value: F32): F32 Vec2I :: struct;; x: S64; y: S64 Vec2 :: struct;; x: F32; y: F32 @@ -37,21 +30,3 @@ Vec2 :: struct;; x: F32; y: F32 FloorVec2ToVec2I :: (a: Vec2): Vec2I ;; return {floorf(a.x)->S64, floorf(a.y)->S64} CastVec2ToVec2I :: (a: Vec2): Vec2I ;; return {a.x->S64, a.y->S64} - -F32_Clamp :: (min: F32, value: F32, max: F32): F32 - if value > max;; return max - if value < min;; return min - return value - -F32_ClampBottom :: (min: F32, value: F32): F32 - if value < min;; return min - return value - -F32_Absolute :: (val: F32): F32 - if val < 0;; return -val - return val - -F32_Min :: (a: F32, b: F32): F32 - if a > b ;; return b ; return a -F32_Max :: (a: F32, b: F32): F32 - if a > b ;; return a ; return b diff --git a/modules/MathVec3.core b/modules/MathVec3.core index 7bfb3f9..9ab9774 100644 --- a/modules/MathVec3.core +++ b/modules/MathVec3.core @@ -1,4 +1,4 @@ -#import "Math.core" +#import "MathF32.core" Vec3 :: struct ;; x: F32; y: F32; z: F32 Length :: (a: Vec3): F32 ;; return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z) @@ -36,9 +36,9 @@ Reflect :: (a: Vec3, normal: Vec3): Vec3 return result ConvertToARGB :: (a: Vec3): U32 - a.x = F32_Clamp(0, a.x, 1) - a.y = F32_Clamp(0, a.y, 1) - a.z = F32_Clamp(0, a.z, 1) + a.x = Clamp(0, a.x, 1) + a.y = Clamp(0, a.y, 1) + a.z = Clamp(0, a.z, 1) r := (a.x * 255)->U32 << 16 g := (a.y * 255)->U32 << 8 b := (a.z * 255)->U32 << 0 diff --git a/modules/Multimedia.core b/modules/Multimedia.core index c06e72c..4a48b78 100644 --- a/modules/Multimedia.core +++ b/modules/Multimedia.core @@ -58,6 +58,7 @@ Mouse :: struct wheel: S64 #import "Base.core" -#import "Math.core" +#import "MathF32.core" +#import "MathVec2.core" #import "Arena.core" #load "$os_multimedia.core" diff --git a/modules/win32_multimedia.core b/modules/win32_multimedia.core index 3fcc1a4..a7e74ec 100644 --- a/modules/win32_multimedia.core +++ b/modules/win32_multimedia.core @@ -163,6 +163,7 @@ UpdateMultimedia :: (): Bool Mu.window.y = size.y Mu.window.sizef.x = Mu.window.x->F32 Mu.window.sizef.y = Mu.window.y->F32 + Mu.window.size = size Mu.frame_count += 1 @@ -186,6 +187,7 @@ UpdateMultimedia :: (): Bool WindowProc :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT result: LRESULT if msg == WM_DESTROY + // @todo: Add destroy window PostQuitMessage(0) return 0 elif msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN