commit cfd9fc0e73658c09a218feb21b8281fe367647b6 Author: Waozi Date: Sat Sep 26 23:50:24 2026 -0300 Create Game2D package with raylib adapter diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9560f56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/ +/ziran.local.toml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9eeb030 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2026, Waozi + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eb4827b --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +.PHONY: test +test: + sh tests/test.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..88bba17 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Game2D + +Game2D is an optional Ziran package for 2D games that use Kryon. The `Game2D` +module holds backend-independent 2D values and collision helpers. The `Raylib` +module provides the existing raylib game API as a separate adapter. Kryon keeps +its UI geometry, drawing primitives, widgets, and raylib UI host. + +```toml +[dependencies.Game2D] +git = "https://github.com/kryonlabs/game2d.git" +ref = "master" +``` + +Import the shared module with `using Game :: #import "Game2D";`. A native C or +C++ game that uses raylib can additionally import `Raylib`. The raylib adapter +uses raylib's C ABI and does not support native Go. Select and link a platform +host in the application; a Ziran package does not install a renderer by itself. + +For local development, add an ignored `ziran.local.toml` with overrides for +`Game2D`, `Kryon`, and `ziran` in the application. Commit `ziran.lock` for +reproducible builds. diff --git a/src/Game2D/module.zi b/src/Game2D/module.zi new file mode 100644 index 0000000..a391c2b --- /dev/null +++ b/src/Game2D/module.zi @@ -0,0 +1,35 @@ +// Backend-independent 2D geometry. Games use Kryon's public geometry values +// while the selected host owns drawing and input effects. +using Geometry :: #import "geometry"; + +Circle :: struct { + center: Vector2 + radius: float32 +} + +#program_export +CircleContains :: (circle: Circle, point: Vector2) -> bool { + if circle.radius < 0.0 { return false } + dx: float32 = point.x - circle.center.x + dy: float32 = point.y - circle.center.y + return dx * dx + dy * dy <= circle.radius * circle.radius +} + +#program_export +CirclesOverlap :: (first: Circle, second: Circle) -> bool { + if first.radius < 0.0 || second.radius < 0.0 { return false } + dx: float32 = first.center.x - second.center.x + dy: float32 = first.center.y - second.center.y + reach: float32 = first.radius + second.radius + return dx * dx + dy * dy <= reach * reach +} + +#program_export +RectanglesOverlap :: (first: Rectangle, second: Rectangle) -> bool { + return first.width > 0.0 && first.height > 0.0 && + second.width > 0.0 && second.height > 0.0 && + first.x < second.x + second.width && + second.x < first.x + first.width && + first.y < second.y + second.height && + second.y < first.y + first.height +} diff --git a/src/Raylib/module.zi b/src/Raylib/module.zi new file mode 100644 index 0000000..67b285b --- /dev/null +++ b/src/Raylib/module.zi @@ -0,0 +1,524 @@ +// Raylib game surface for Ziran applications. Games import this module to +// open a window, read keyboard/mouse/touch input, load textures and audio, +// draw shapes and text, and use raylib's timing and random values with +// raylib's own names. Color, Texture2D, Vector2, Rectangle, and Camera come +// from the shared drawing and geometry modules and match the raylib C ABI, +// so every declaration here passes those values to raylib by value. Foreign +// declarations stay private to this module; games call the plain wrappers, +// and headless builds link C stubs under the raylib names instead of +// libraylib. +#import "c_string" +using Geometry :: #import "geometry"; +using Drawing :: #import "drawing_props"; + +raylib :: #system_library "raylib"; +libm :: #system_library "m"; +libc :: #system_library "libc"; + +AudioStream :: struct { + buffer: *void + processor: *void + sample_rate: u32 + sample_size: u32 + channels: u32 +} + +Wave :: struct { + frame_count: u32 + sample_rate: u32 + sample_size: u32 + channels: u32 + data: *void +} + +Sound :: struct { + stream: AudioStream + frame_count: u32 +} + +Music :: struct { + stream: AudioStream + frame_count: u32 + looping: bool + ctx_type: s32 + ctx_data: *void +} + +AudioCallback :: #type (buffer: *void, frames: u32) -> void #c_call; + +RenderTexture2D :: struct { + id: u32 + texture: Texture2D + depth: Texture2D +} + +// The raylib C image value is distinct from Kryon's Image widget. +RaylibImage :: struct { + data: *void + width: s32 + height: s32 + mipmaps: s32 + format: s32 +} + +// Keyboard keys, mouse buttons, texture filters, blend modes, window flags, +// and palette colors the games use, with raylib's values. +KEY_NULL :: 0; +KEY_SPACE :: 32; +KEY_KB_MENU :: 348; +KEY_ESCAPE :: 256; +KEY_ENTER :: 257; +KEY_TAB :: 258; +KEY_BACKSPACE :: 259; +KEY_RIGHT :: 262; +KEY_LEFT :: 263; +KEY_DOWN :: 264; +KEY_UP :: 265; +KEY_A :: 65; +KEY_D :: 68; +KEY_E :: 69; +KEY_G :: 71; +KEY_M :: 77; +KEY_S :: 83; +KEY_W :: 87; +KEY_X :: 88; +KEY_BACK :: 4; +MOUSE_BUTTON_LEFT :: 0; +MOUSE_BUTTON_RIGHT :: 1; +MOUSE_LEFT_BUTTON :: 0; +TEXTURE_FILTER_POINT :: 0; +BLEND_ALPHA :: 0; +BLEND_ADDITIVE :: 1; +FLAG_WINDOW_RESIZABLE :: 4; +FLAG_WINDOW_HIDDEN :: 128; +FLAG_WINDOW_ALWAYS_RUN :: 256; + +WHITE :: Color.{255, 255, 255, 255}; +BLACK :: Color.{0, 0, 0, 255}; + +// Window and frame loop. +RaylibInitWindow :: (width: s32, height: s32, title: *u8) #foreign raylib "InitWindow"; +RaylibCloseWindow :: () #foreign raylib "CloseWindow"; +RaylibIsWindowReady :: () -> bool #foreign raylib "IsWindowReady"; +RaylibWindowShouldClose :: () -> bool #foreign raylib "WindowShouldClose"; +RaylibIsWindowHidden :: () -> bool #foreign raylib "IsWindowHidden"; +RaylibIsWindowMinimized :: () -> bool #foreign raylib "IsWindowMinimized"; +RaylibIsWindowFocused :: () -> bool #foreign raylib "IsWindowFocused"; +RaylibSetConfigFlags :: (flags: u32) #foreign raylib "SetConfigFlags"; +RaylibSetWindowState :: (flags: u32) #foreign raylib "SetWindowState"; +RaylibClearWindowState :: (flags: u32) #foreign raylib "ClearWindowState"; +RaylibRestoreWindow :: () #foreign raylib "RestoreWindow"; +RaylibSetWindowFocused :: () #foreign raylib "SetWindowFocused"; +RaylibSetTargetFPS :: (fps: s32) #foreign raylib "SetTargetFPS"; +RaylibSetExitKey :: (key: s32) #foreign raylib "SetExitKey"; +RaylibGetScreenWidth :: () -> s32 #foreign raylib "GetScreenWidth"; +RaylibGetScreenHeight :: () -> s32 #foreign raylib "GetScreenHeight"; +RaylibGetRenderWidth :: () -> s32 #foreign raylib "GetRenderWidth"; +RaylibGetRenderHeight :: () -> s32 #foreign raylib "GetRenderHeight"; +RaylibGetWindowScaleDPI :: () -> Vector2 #foreign raylib "GetWindowScaleDPI"; +RaylibGetCurrentMonitor :: () -> s32 #foreign raylib "GetCurrentMonitor"; +RaylibGetMonitorWidth :: (monitor: s32) -> s32 #foreign raylib "GetMonitorWidth"; +RaylibGetMonitorHeight :: (monitor: s32) -> s32 #foreign raylib "GetMonitorHeight"; +RaylibBeginDrawing :: () #foreign raylib "BeginDrawing"; +RaylibEndDrawing :: () #foreign raylib "EndDrawing"; + +InitWindow :: (width: s32, height: s32, title: string) { + bytes: [256]u8 + if !CopyCString(title, bytes[:]) { return } + RaylibInitWindow(width, height, *bytes[0]) +} + +CloseWindow :: () { RaylibCloseWindow() } +IsWindowReady :: () -> bool { return RaylibIsWindowReady() } +WindowShouldClose :: () -> bool { return RaylibWindowShouldClose() } +IsWindowHidden :: () -> bool { return RaylibIsWindowHidden() } +IsWindowMinimized :: () -> bool { return RaylibIsWindowMinimized() } +IsWindowFocused :: () -> bool { return RaylibIsWindowFocused() } +SetConfigFlags :: (flags: u32) { RaylibSetConfigFlags(flags) } +SetWindowState :: (flags: u32) { RaylibSetWindowState(flags) } +ClearWindowState :: (flags: u32) { RaylibClearWindowState(flags) } +RestoreWindow :: () { RaylibRestoreWindow() } +SetWindowFocused :: () { RaylibSetWindowFocused() } +SetTargetFPS :: (fps: s32) { RaylibSetTargetFPS(fps) } +SetExitKey :: (key: s32) { RaylibSetExitKey(key) } +GetScreenWidth :: () -> s32 { return RaylibGetScreenWidth() } +GetScreenHeight :: () -> s32 { return RaylibGetScreenHeight() } +GetRenderWidth :: () -> s32 { return RaylibGetRenderWidth() } +GetRenderHeight :: () -> s32 { return RaylibGetRenderHeight() } +GetWindowScaleDPI :: () -> Vector2 { return RaylibGetWindowScaleDPI() } +GetCurrentMonitor :: () -> s32 { return RaylibGetCurrentMonitor() } +GetMonitorWidth :: (monitor: s32) -> s32 { return RaylibGetMonitorWidth(monitor) } +GetMonitorHeight :: (monitor: s32) -> s32 { return RaylibGetMonitorHeight(monitor) } +BeginDrawing :: () { RaylibBeginDrawing() } +EndDrawing :: () { RaylibEndDrawing() } + +// Timing and random values. +RaylibGetTime :: () -> float64 #foreign raylib "GetTime"; +RaylibGetFrameTime :: () -> float32 #foreign raylib "GetFrameTime"; +RaylibSetRandomSeed :: (seed: u32) #foreign raylib "SetRandomSeed"; +RaylibGetRandomValue :: (minimum: s32, maximum: s32) -> s32 #foreign raylib "GetRandomValue"; + +GetTime :: () -> float64 { return RaylibGetTime() } +GetFrameTime :: () -> float32 { return RaylibGetFrameTime() } +SetRandomSeed :: (seed: u32) { RaylibSetRandomSeed(seed) } +GetRandomValue :: (minimum: s32, maximum: s32) -> s32 { + return RaylibGetRandomValue(minimum, maximum) +} + +// Input. +RaylibIsKeyPressed :: (key: s32) -> bool #foreign raylib "IsKeyPressed"; +RaylibIsKeyDown :: (key: s32) -> bool #foreign raylib "IsKeyDown"; +RaylibIsMouseButtonPressed :: (button: s32) -> bool #foreign raylib "IsMouseButtonPressed"; +RaylibIsMouseButtonDown :: (button: s32) -> bool #foreign raylib "IsMouseButtonDown"; +RaylibIsMouseButtonReleased :: (button: s32) -> bool #foreign raylib "IsMouseButtonReleased"; +RaylibGetMousePosition :: () -> Vector2 #foreign raylib "GetMousePosition"; +RaylibCheckCollisionPointRec :: (point: Vector2, + rectangle: Rectangle) -> bool #foreign raylib "CheckCollisionPointRec"; +RaylibGetMouseDelta :: () -> Vector2 #foreign raylib "GetMouseDelta"; +RaylibGetMouseWheelMove :: () -> float32 #foreign raylib "GetMouseWheelMove"; +RaylibGetTouchPointCount :: () -> s32 #foreign raylib "GetTouchPointCount"; +RaylibGetTouchPosition :: (index: s32) -> Vector2 #foreign raylib "GetTouchPosition"; + +IsKeyPressed :: (key: s32) -> bool { return RaylibIsKeyPressed(key) } +IsKeyDown :: (key: s32) -> bool { return RaylibIsKeyDown(key) } +IsMouseButtonPressed :: (button: s32) -> bool { + return RaylibIsMouseButtonPressed(button) +} +IsMouseButtonDown :: (button: s32) -> bool { + return RaylibIsMouseButtonDown(button) +} +IsMouseButtonReleased :: (button: s32) -> bool { + return RaylibIsMouseButtonReleased(button) +} +GetMousePosition :: () -> Vector2 { return RaylibGetMousePosition() } +CheckCollisionPointRec :: (point: Vector2, rectangle: Rectangle) -> bool { + return RaylibCheckCollisionPointRec(point, rectangle) +} +GetMouseDelta :: () -> Vector2 { return RaylibGetMouseDelta() } +GetMouseWheelMove :: () -> float32 { return RaylibGetMouseWheelMove() } +GetTouchPointCount :: () -> s32 { return RaylibGetTouchPointCount() } +GetTouchPosition :: (index: s32) -> Vector2 { return RaylibGetTouchPosition(index) } + +// Drawing. +RaylibClearBackground :: (color: Color) #foreign raylib "ClearBackground"; +RaylibDrawRectangle :: (pos_x: s32, pos_y: s32, width: s32, height: s32, + color: Color) #foreign raylib "DrawRectangle"; +RaylibDrawCircle :: (center_x: s32, center_y: s32, radius: float32, + color: Color) #foreign raylib "DrawCircle"; +RaylibDrawCircleV :: (center: Vector2, radius: float32, + color: Color) #foreign raylib "DrawCircleV"; +RaylibDrawCircleGradient :: (center: Vector2, radius: float32, inner: Color, + outer: Color) #foreign raylib "DrawCircleGradient"; +RaylibDrawEllipse :: (center_x: s32, center_y: s32, radius_h: float32, + radius_v: float32, color: Color) #foreign raylib "DrawEllipse"; +RaylibDrawRing :: (center: Vector2, inner_radius: float32, outer_radius: float32, + start_angle: float32, end_angle: float32, segments: s32, + color: Color) #foreign raylib "DrawRing"; +RaylibDrawTriangle :: (v1: Vector2, v2: Vector2, v3: Vector2, + color: Color) #foreign raylib "DrawTriangle"; +RaylibDrawLineEx :: (start: Vector2, end: Vector2, thickness: float32, + color: Color) #foreign raylib "DrawLineEx"; +RaylibBeginMode2D :: (camera: Camera) #foreign raylib "BeginMode2D"; +RaylibEndMode2D :: () #foreign raylib "EndMode2D"; +RaylibBeginBlendMode :: (mode: s32) #foreign raylib "BeginBlendMode"; +RaylibEndBlendMode :: () #foreign raylib "EndBlendMode"; +RaylibBeginScissorMode :: (x: s32, y: s32, width: s32, + height: s32) #foreign raylib "BeginScissorMode"; +RaylibEndScissorMode :: () #foreign raylib "EndScissorMode"; + +ClearBackground :: (color: Color) { RaylibClearBackground(color) } +DrawRectangle :: (pos_x: s32, pos_y: s32, width: s32, height: s32, + color: Color) { + RaylibDrawRectangle(pos_x, pos_y, width, height, color) +} +DrawCircle :: (center_x: s32, center_y: s32, radius: float32, color: Color) { + RaylibDrawCircle(center_x, center_y, radius, color) +} +DrawCircleV :: (center: Vector2, radius: float32, color: Color) { + RaylibDrawCircleV(center, radius, color) +} +DrawCircleGradient :: (center: Vector2, radius: float32, inner: Color, + outer: Color) { + RaylibDrawCircleGradient(center, radius, inner, outer) +} +DrawEllipse :: (center_x: s32, center_y: s32, radius_h: float32, + radius_v: float32, color: Color) { + RaylibDrawEllipse(center_x, center_y, radius_h, radius_v, color) +} +DrawRing :: (center: Vector2, inner_radius: float32, outer_radius: float32, + start_angle: float32, end_angle: float32, segments: s32, color: Color) { + RaylibDrawRing(center, inner_radius, outer_radius, start_angle, end_angle, + segments, color) +} +DrawTriangle :: (v1: Vector2, v2: Vector2, v3: Vector2, color: Color) { + RaylibDrawTriangle(v1, v2, v3, color) +} +DrawLineEx :: (start: Vector2, end: Vector2, thickness: float32, color: Color) { + RaylibDrawLineEx(start, end, thickness, color) +} +BeginMode2D :: (camera: Camera) { RaylibBeginMode2D(camera) } +EndMode2D :: () { RaylibEndMode2D() } +BeginBlendMode :: (mode: s32) { RaylibBeginBlendMode(mode) } +EndBlendMode :: () { RaylibEndBlendMode() } +BeginScissorMode :: (x: s32, y: s32, width: s32, height: s32) { + RaylibBeginScissorMode(x, y, width, height) +} +EndScissorMode :: () { RaylibEndScissorMode() } + +// Text with raylib's default font. +RaylibDrawText :: (value: *u8, pos_x: s32, pos_y: s32, font_size: s32, + color: Color) #foreign raylib "DrawText"; +RaylibMeasureText :: (value: *u8, font_size: s32) -> s32 #foreign raylib "MeasureText"; + +DrawText :: (value: string, pos_x: s32, pos_y: s32, font_size: s32, + color: Color) { + bytes: [4096]u8 + if !CopyCString(value, bytes[:]) { return } + RaylibDrawText(*bytes[0], pos_x, pos_y, font_size, color) +} + +MeasureText :: (value: string, font_size: s32) -> s32 { + bytes: [4096]u8 + if !CopyCString(value, bytes[:]) { return 0 } + return RaylibMeasureText(*bytes[0], font_size) +} + +// Textures and render targets. +RaylibLoadImageFromScreen :: () -> RaylibImage #foreign raylib "LoadImageFromScreen"; +RaylibUnloadImage :: (image: RaylibImage) #foreign raylib "UnloadImage"; +RaylibExportImage :: (image: RaylibImage, path: *u8) -> bool #foreign raylib "ExportImage"; +RaylibLoadTexture :: (path: *u8) -> Texture2D #foreign raylib "LoadTexture"; +RaylibUnloadTexture :: (texture: Texture2D) #foreign raylib "UnloadTexture"; +RaylibSetTextureFilter :: (texture: Texture2D, + filter: s32) #foreign raylib "SetTextureFilter"; +RaylibDrawTexturePro :: (texture: Texture2D, source: Rectangle, + destination: Rectangle, origin: Vector2, rotation: float32, + tint: Color) #foreign raylib "DrawTexturePro"; +RaylibDrawTexture :: (texture: Texture2D, pos_x: s32, pos_y: s32, + tint: Color) #foreign raylib "DrawTexture"; +RaylibLoadRenderTexture :: (width: s32, + height: s32) -> RenderTexture2D #foreign raylib "LoadRenderTexture"; +RaylibUnloadRenderTexture :: (target: RenderTexture2D) #foreign raylib "UnloadRenderTexture"; +RaylibBeginTextureMode :: (target: RenderTexture2D) #foreign raylib "BeginTextureMode"; +RaylibEndTextureMode :: () #foreign raylib "EndTextureMode"; +RaylibIsRenderTextureValid :: (target: RenderTexture2D) -> bool #foreign raylib "IsRenderTextureValid"; + +EmptyTexture :: () -> Texture2D { + empty: Texture2D + empty.id = 0 + empty.width = 0 + empty.height = 0 + empty.mipmaps = 0 + empty.format = 0 + return empty +} + +LoadImageFromScreen :: () -> RaylibImage { return RaylibLoadImageFromScreen() } +UnloadImage :: (image: RaylibImage) { RaylibUnloadImage(image) } +ExportImage :: (image: RaylibImage, path: string) -> bool { + bytes: [1024]u8 + if !CopyCString(path, bytes[:]) { return false } + return RaylibExportImage(image, *bytes[0]) +} + +EmptyWave :: () -> Wave { + empty: Wave + empty.frame_count = 0 + empty.sample_rate = 0 + empty.sample_size = 0 + empty.channels = 0 + empty.data = null + return empty +} + +EmptySound :: () -> Sound { + empty: Sound + empty.stream.buffer = null + empty.stream.processor = null + empty.stream.sample_rate = 0 + empty.stream.sample_size = 0 + empty.stream.channels = 0 + empty.frame_count = 0 + return empty +} + +EmptyMusic :: () -> Music { + empty: Music + empty.stream.buffer = null + empty.stream.processor = null + empty.stream.sample_rate = 0 + empty.stream.sample_size = 0 + empty.stream.channels = 0 + empty.frame_count = 0 + empty.looping = false + empty.ctx_type = 0 + empty.ctx_data = null + return empty +} + +EmptyRenderTexture :: () -> RenderTexture2D { + empty: RenderTexture2D + empty.id = 0 + empty.texture = EmptyTexture() + empty.depth = EmptyTexture() + return empty +} + +LoadTexture :: (path: string) -> Texture2D { + bytes: [1024]u8 + if !CopyCString(path, bytes[:]) { return EmptyTexture() } + return RaylibLoadTexture(*bytes[0]) +} + +UnloadTexture :: (texture: Texture2D) { RaylibUnloadTexture(texture) } +SetTextureFilter :: (texture: Texture2D, filter: s32) { + RaylibSetTextureFilter(texture, filter) +} +DrawTexturePro :: (texture: Texture2D, source: Rectangle, destination: Rectangle, + origin: Vector2, rotation: float32, tint: Color) { + RaylibDrawTexturePro(texture, source, destination, origin, rotation, tint) +} +DrawTexture :: (texture: Texture2D, pos_x: s32, pos_y: s32, tint: Color) { + RaylibDrawTexture(texture, pos_x, pos_y, tint) +} +LoadRenderTexture :: (width: s32, height: s32) -> RenderTexture2D { + return RaylibLoadRenderTexture(width, height) +} +UnloadRenderTexture :: (target: RenderTexture2D) { + RaylibUnloadRenderTexture(target) +} +BeginTextureMode :: (target: RenderTexture2D) { RaylibBeginTextureMode(target) } +EndTextureMode :: () { RaylibEndTextureMode() } +IsRenderTextureValid :: (target: RenderTexture2D) -> bool { + return RaylibIsRenderTextureValid(target) +} + +// Audio. +RaylibInitAudioDevice :: () #foreign raylib "InitAudioDevice"; +RaylibCloseAudioDevice :: () #foreign raylib "CloseAudioDevice"; +RaylibIsAudioDeviceReady :: () -> bool #foreign raylib "IsAudioDeviceReady"; +RaylibSetMasterVolume :: (volume: float32) #foreign raylib "SetMasterVolume"; +RaylibPlaySound :: (sound: Sound) #foreign raylib "PlaySound"; +RaylibStopSound :: (sound: Sound) #foreign raylib "StopSound"; +RaylibPauseSound :: (sound: Sound) #foreign raylib "PauseSound"; +RaylibIsSoundPlaying :: (sound: Sound) -> bool #foreign raylib "IsSoundPlaying"; +RaylibUnloadSound :: (sound: Sound) #foreign raylib "UnloadSound"; +RaylibSetSoundVolume :: (sound: Sound, volume: float32) #foreign raylib "SetSoundVolume"; +RaylibSetSoundPitch :: (sound: Sound, pitch: float32) #foreign raylib "SetSoundPitch"; +RaylibIsSoundValid :: (sound: Sound) -> bool #foreign raylib "IsSoundValid"; +RaylibLoadWave :: (path: *u8) -> Wave #foreign raylib "LoadWave"; +RaylibLoadWaveFromMemory :: (file_type: *u8, data: *u8, + size: s32) -> Wave #foreign raylib "LoadWaveFromMemory"; +RaylibUnloadWave :: (wave: Wave) #foreign raylib "UnloadWave"; +RaylibWaveFormat :: (wave: *Wave, sample_rate: s32, + sample_size: s32, channels: s32) #foreign raylib "WaveFormat"; +RaylibLoadSoundFromWave :: (wave: Wave) -> Sound #foreign raylib "LoadSoundFromWave"; +RaylibPlayMusicStream :: (music: Music) #foreign raylib "PlayMusicStream"; +RaylibUpdateMusicStream :: (music: Music) #foreign raylib "UpdateMusicStream"; +RaylibStopMusicStream :: (music: Music) #foreign raylib "StopMusicStream"; +RaylibPauseMusicStream :: (music: Music) #foreign raylib "PauseMusicStream"; +RaylibResumeMusicStream :: (music: Music) #foreign raylib "ResumeMusicStream"; +RaylibIsMusicStreamPlaying :: (music: Music) -> bool #foreign raylib "IsMusicStreamPlaying"; +RaylibSeekMusicStream :: (music: Music, position: float32) #foreign raylib "SeekMusicStream"; +RaylibSetMusicVolume :: (music: Music, volume: float32) #foreign raylib "SetMusicVolume"; +RaylibUnloadMusicStream :: (music: Music) #foreign raylib "UnloadMusicStream"; +RaylibIsMusicValid :: (music: Music) -> bool #foreign raylib "IsMusicValid"; +RaylibAttachAudioMixedProcessor :: (callback: AudioCallback) #foreign raylib "AttachAudioMixedProcessor"; +RaylibDetachAudioMixedProcessor :: (callback: AudioCallback) #foreign raylib "DetachAudioMixedProcessor"; +RaylibLoadSound :: (path: *u8) -> Sound #foreign raylib "LoadSound"; +RaylibLoadMusicStream :: (path: *u8) -> Music #foreign raylib "LoadMusicStream"; + +InitAudioDevice :: () { RaylibInitAudioDevice() } +CloseAudioDevice :: () { RaylibCloseAudioDevice() } +IsAudioDeviceReady :: () -> bool { return RaylibIsAudioDeviceReady() } +SetMasterVolume :: (volume: float32) { RaylibSetMasterVolume(volume) } +PlaySound :: (sound: Sound) { RaylibPlaySound(sound) } +StopSound :: (sound: Sound) { RaylibStopSound(sound) } +PauseSound :: (sound: Sound) { RaylibPauseSound(sound) } +IsSoundPlaying :: (sound: Sound) -> bool { + return RaylibIsSoundPlaying(sound) +} +UnloadSound :: (sound: Sound) { RaylibUnloadSound(sound) } +SetSoundVolume :: (sound: Sound, volume: float32) { + RaylibSetSoundVolume(sound, volume) +} +SetSoundPitch :: (sound: Sound, pitch: float32) { + RaylibSetSoundPitch(sound, pitch) +} +IsSoundValid :: (sound: Sound) -> bool { return RaylibIsSoundValid(sound) } +LoadWave :: (path: string) -> Wave { + bytes: [1024]u8 + if !CopyCString(path, bytes[:]) { return EmptyWave() } + return RaylibLoadWave(*bytes[0]) +} +LoadWaveFromMemory :: (file_type: string, data: *u8, + size: s32) -> Wave { + extension: [32]u8 + if data == null || size <= 0 || + !CopyCString(file_type, extension[:]) { return EmptyWave() } + return RaylibLoadWaveFromMemory(*extension[0], data, size) +} +UnloadWave :: (wave: Wave) { RaylibUnloadWave(wave) } +WaveFormat :: (wave: *Wave, sample_rate: s32, + sample_size: s32, channels: s32) { + if wave == null { return } + RaylibWaveFormat(wave, sample_rate, sample_size, channels) +} +LoadSoundFromWave :: (wave: Wave) -> Sound { + return RaylibLoadSoundFromWave(wave) +} +PlayMusicStream :: (music: Music) { RaylibPlayMusicStream(music) } +UpdateMusicStream :: (music: Music) { RaylibUpdateMusicStream(music) } +StopMusicStream :: (music: Music) { RaylibStopMusicStream(music) } +PauseMusicStream :: (music: Music) { RaylibPauseMusicStream(music) } +ResumeMusicStream :: (music: Music) { RaylibResumeMusicStream(music) } +IsMusicStreamPlaying :: (music: Music) -> bool { + return RaylibIsMusicStreamPlaying(music) +} +SeekMusicStream :: (music: Music, position: float32) { + RaylibSeekMusicStream(music, position) +} +SetMusicVolume :: (music: Music, volume: float32) { + RaylibSetMusicVolume(music, volume) +} +UnloadMusicStream :: (music: Music) { RaylibUnloadMusicStream(music) } +IsMusicValid :: (music: Music) -> bool { return RaylibIsMusicValid(music) } +AttachAudioMixedProcessor :: (callback: AudioCallback) { + RaylibAttachAudioMixedProcessor(callback) +} +DetachAudioMixedProcessor :: (callback: AudioCallback) { + RaylibDetachAudioMixedProcessor(callback) +} +LoadSound :: (path: string) -> Sound { + bytes: [1024]u8 + if !CopyCString(path, bytes[:]) { return EmptySound() } + return RaylibLoadSound(*bytes[0]) +} +LoadMusicStream :: (path: string) -> Music { + bytes: [1024]u8 + if !CopyCString(path, bytes[:]) { return EmptyMusic() } + return RaylibLoadMusicStream(*bytes[0]) +} + +// Math from libm and libc, with the C names the games call. +RaylibSinf :: (value: float32) -> float32 #foreign libm "sinf"; +RaylibCosf :: (value: float32) -> float32 #foreign libm "cosf"; +RaylibSqrtf :: (value: float32) -> float32 #foreign libm "sqrtf"; +RaylibFabsf :: (value: float32) -> float32 #foreign libm "fabsf"; +RaylibExpf :: (value: float32) -> float32 #foreign libm "expf"; +RaylibPowf :: (base: float32, exponent: float32) -> float32 #foreign libm "powf"; +RaylibFmodf :: (value: float32, divisor: float32) -> float32 #foreign libm "fmodf"; +RaylibAbs :: (value: s32) -> s32 #foreign libc "abs"; + +sinf :: (value: float32) -> float32 { return RaylibSinf(value) } +cosf :: (value: float32) -> float32 { return RaylibCosf(value) } +sqrtf :: (value: float32) -> float32 { return RaylibSqrtf(value) } +fabsf :: (value: float32) -> float32 { return RaylibFabsf(value) } +expf :: (value: float32) -> float32 { return RaylibExpf(value) } +powf :: (base: float32, exponent: float32) -> float32 { + return RaylibPowf(base, exponent) +} +fmodf :: (value: float32, divisor: float32) -> float32 { + return RaylibFmodf(value, divisor) +} +abs :: (value: s32) -> s32 { return RaylibAbs(value) } diff --git a/tests/game2d_behavior.zi b/tests/game2d_behavior.zi new file mode 100644 index 0000000..d5c5f4c --- /dev/null +++ b/tests/game2d_behavior.zi @@ -0,0 +1,15 @@ +using Game :: #import "Game2D"; + +#program_export +main :: () -> s32 { + center: Vector2 = Vector2.{10.0, 10.0} + circle: Circle = Circle.{center, 5.0} + if !CircleContains(circle, Vector2.{13.0, 14.0}) { return 1 } + if CircleContains(circle, Vector2.{16.0, 10.0}) { return 2 } + if !CirclesOverlap(circle, Circle.{Vector2.{20.0, 10.0}, 5.0}) { return 3 } + if !RectanglesOverlap(Rectangle.{0.0, 0.0, 10.0, 10.0}, + Rectangle.{9.0, 9.0, 10.0, 10.0}) { return 4 } + if RectanglesOverlap(Rectangle.{0.0, 0.0, 10.0, 10.0}, + Rectangle.{10.0, 10.0, 10.0, 10.0}) { return 5 } + return 0 +} diff --git a/tests/raylib_wave_abi.c b/tests/raylib_wave_abi.c new file mode 100644 index 0000000..e46fd21 --- /dev/null +++ b/tests/raylib_wave_abi.c @@ -0,0 +1,34 @@ +#include +#include + +#ifdef GENERATED +#include "Raylib.h" +#define IMAGE_TYPE RaylibImage +#define WAVE_FRAME frame_count +#define WAVE_RATE sample_rate +#define WAVE_SAMPLE sample_size +#define SOUND_FRAME frame_count +#define STREAM_RATE sample_rate +#else +#include "raylib.h" +#define IMAGE_TYPE Image +#define WAVE_FRAME frameCount +#define WAVE_RATE sampleRate +#define WAVE_SAMPLE sampleSize +#define SOUND_FRAME frameCount +#define STREAM_RATE sampleRate +#endif + +int main(void) +{ + printf("%zu %zu %zu %zu %zu %zu %zu %zu %zu %zu %zu %zu %zu %zu %zu %zu\n", + sizeof(Wave), offsetof(Wave, WAVE_FRAME), + offsetof(Wave, WAVE_RATE), offsetof(Wave, WAVE_SAMPLE), + offsetof(Wave, channels), offsetof(Wave, data), + sizeof(AudioStream), offsetof(AudioStream, STREAM_RATE), + sizeof(Sound), offsetof(Sound, SOUND_FRAME), + sizeof(IMAGE_TYPE), offsetof(IMAGE_TYPE, data), + offsetof(IMAGE_TYPE, width), offsetof(IMAGE_TYPE, height), + offsetof(IMAGE_TYPE, mipmaps), offsetof(IMAGE_TYPE, format)); + return 0; +} diff --git a/tests/test.sh b/tests/test.sh new file mode 100644 index 0000000..233c897 --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,43 @@ +#!/bin/sh +set -eu + +repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$repo" +export ZIRAN_TEST_ALLOW_LOCAL_GIT=1 +unset DISPLAY WAYLAND_DISPLAY + +ziran=${ZIRAN:-../ziran/build/bin/ziran} +compiler=${ZIRAN_DIR:-../ziran} +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT HUP INT TERM + +"$ziran" check --project src/Game2D/module.zi +"$ziran" check --project src/Raylib/module.zi +"$ziran" build --project --target=c \ + -o "$work/c" tests/game2d_behavior.zi +"${CC:-cc}" -std=c99 -I"$compiler/include" -I"$work/c" \ + "$work/c"/*.c -lm -o "$work/game2d-behavior" +"$work/game2d-behavior" + +"$ziran" bundle --project --entry game2d_behavior:main \ + -o build/game2d-behavior.zib tests/game2d_behavior.zi +"$ziran" run build/game2d-behavior.zib + +"$ziran" build --project --target=c -o build/raylib-c src/Raylib/module.zi +"$ziran" build --project --target=cpp -o build/raylib-cpp src/Raylib/module.zi +"${CC:-cc}" -std=c11 -I"$compiler/include" -Ibuild/raylib-c \ + -c build/raylib-c/Raylib.c -o build/Raylib.o +"${CXX:-c++}" -std=c++17 -I"$compiler/include" -Ibuild/raylib-cpp \ + -c build/raylib-cpp/Raylib.cpp -o build/Raylib-cpp.o + +raylib_header=${RAYLIB_HEADER_DIR:-../kryon/vendor/raylib/src} +if test -f "$raylib_header/raylib.h"; then + "${CC:-cc}" -std=c11 -DGENERATED -ffunction-sections \ + -Wl,--gc-sections -I"$compiler/include" -Ibuild/raylib-c \ + tests/raylib_wave_abi.c -o build/generated-abi + "${CC:-cc}" -std=c11 -I"$raylib_header" \ + tests/raylib_wave_abi.c -o build/raylib-abi + test "$(build/generated-abi)" = "$(build/raylib-abi)" +fi + +echo "Game2D source, native, portable, and raylib ABI checks passed" diff --git a/ziran.lock b/ziran.lock new file mode 100644 index 0000000..3daf6c2 --- /dev/null +++ b/ziran.lock @@ -0,0 +1,24 @@ +{ + "format": 1, + "packages": [ + { + "commit": "e7bd798600cea81a3983905ac7de8fd460877829", + "dependencies": {}, + "id": "pbfb0225fc208cf6f", + "name": "Kryon", + "ref": "master", + "url": "https://github.com/kryonlabs/kryon.git" + } + ], + "root": { + "dependencies": { + "Kryon": "pbfb0225fc208cf6f" + }, + "name": "Game2D" + }, + "toolchain": { + "commit": "d511c230dd8efdc8459b719b4d8e2214bab1747f", + "ref": "master", + "url": "https://github.com/ziranlang/ziran.git" + } +} diff --git a/ziran.toml b/ziran.toml new file mode 100644 index 0000000..6e450b2 --- /dev/null +++ b/ziran.toml @@ -0,0 +1,15 @@ +[package] +name = "Game2D" +module_roots = ["src", "tests"] + +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "master" + +[dependencies.Kryon] +git = "https://github.com/kryonlabs/kryon.git" +ref = "master" + +[exports] +Game2D = "src/Game2D/module.zi" +Raylib = "src/Raylib/module.zi"