Created
June 30, 2019 14:17
-
-
Save leroycep/ac3e7089f3540895c8d5998bdfbf2a7f to your computer and use it in GitHub Desktop.
Kind of working Raylib in zig example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const Builder = std.build.Builder; | |
pub fn build(b: *Builder) void { | |
const mode = b.standardReleaseOptions(); | |
var exe = b.addExecutable("game", "src/main.zig"); | |
exe.setBuildMode(mode); | |
exe.addLibPath("lib"); | |
exe.addIncludeDir("include"); | |
exe.linkSystemLibrary("raylib"); | |
exe.linkSystemLibrary("c"); | |
const assets_path = std.fs.path.join(b.allocator, [_][]const u8{b.build_root, "assets"}); | |
exe.addBuildOption([]const u8, "assets_path", b.fmt("\"{}\"", assets_path)); | |
b.default_step.dependOn(&exe.step); | |
b.installArtifact(exe); | |
const play = b.step("play", "Play the game"); | |
const run = exe.run(); | |
play.dependOn(&run.step); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/main.zig | |
const build_options = @import("build_options"); | |
const std = @import("std"); | |
const c = @cImport({ | |
@cInclude("raylib.h"); | |
}); | |
const SCREEN_WIDTH = 640; | |
const SCREEN_HEIGHT = 480; | |
const MAX_COLUMNS = 20; | |
const IMG_PATH = build_options.assets_path ++ "/ahri.png"; | |
pub fn main() !void { | |
var alloc = std.heap.c_allocator; | |
const c_img_path = try std.cstr.addNullByte(alloc, IMG_PATH); | |
defer alloc.free(c_img_path); | |
c.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, c"Simple SIGIL"); | |
defer c.CloseWindow(); | |
var camera = c.Camera{ | |
.position = c.Vector3{ .x = 4.0, .y = 2.0, .z = 4.0 }, | |
.target = c.Vector3{ .x = 0.0, .y = 1.8, .z = 0.0 }, | |
.up = c.Vector3{ .x = 0.0, .y = 1.0, .z = 0.0 }, | |
.fovy = 60.0, | |
.@"type" = c.CAMERA_PERSPECTIVE, | |
}; | |
var columns: [MAX_COLUMNS]Column = undefined; | |
var i: usize = 0; | |
while (i < MAX_COLUMNS) : (i += 1) { | |
const height = @intToFloat(f32, c.GetRandomValue(1, 12)); | |
columns[i] = Column{ | |
.height = height, | |
.position = c.Vector3{ .x = floatRand(f32, -15, 15), .y = height / 2, .z = floatRand(f32, -15, 15) }, | |
.color = c.Color{ .r = intRand(u8, 20, 255), .g = intRand(u8, 10, 55), .b = 30, .a = 255 }, | |
}; | |
} | |
c.SetCameraMode(camera, c.CAMERA_FIRST_PERSON); | |
const cat = c.LoadImage(&c_img_path[0]); | |
const texture = c.LoadTextureFromImage(cat); | |
defer c.UnloadTexture(texture); | |
c.UnloadImage(cat); | |
var cat_pos = c.Vector2{ .x = SCREEN_WIDTH / 2, .y = SCREEN_HEIGHT / 2 }; | |
c.SetTargetFPS(60); | |
while (!c.WindowShouldClose()) { | |
c.UpdateCamera(&camera); | |
if (c.IsKeyDown(c.KEY_RIGHT)) cat_pos.x += 2.0; | |
if (c.IsKeyDown(c.KEY_LEFT)) cat_pos.x -= 2.0; | |
if (c.IsKeyDown(c.KEY_DOWN)) cat_pos.y += 2.0; | |
if (c.IsKeyDown(c.KEY_UP)) cat_pos.y -= 2.0; | |
{ | |
c.BeginDrawing(); | |
defer c.EndDrawing(); | |
const WHITE = c.Color{ .r = 0xFF, .g = 0xFF, .b = 0xFF, .a = 0xFF }; | |
const LIGHTGRAY = c.Color{ .r = 0x9F, .g = 0x9F, .b = 0x9F, .a = 0xFF }; | |
c.ClearBackground(WHITE); | |
{ | |
c.BeginMode3D(camera); | |
defer c.EndMode3D(); | |
c.DrawPlane(c.Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }, c.Vector2{ .x = 32.0, .y = 32.0 }, LIGHTGRAY); | |
for (columns) |col| { | |
c.DrawCube(col.position, 2.0, col.height, 2.0, col.color); | |
} | |
} | |
c.DrawTexture(texture, @floatToInt(c_int, cat_pos.x), @floatToInt(c_int, cat_pos.y), WHITE); | |
c.DrawText(c"First window achievement get!", 180, 200, 20, LIGHTGRAY); | |
} | |
} | |
} | |
fn floatRand(comptime T: type, min: c_int, max: c_int) T { | |
return @intToFloat(T, c.GetRandomValue(min, max)); | |
} | |
fn intRand(comptime T: type, min: c_int, max: c_int) T { | |
return @intCast(T, c.GetRandomValue(min, max)); | |
} | |
const Column = struct { | |
height: f32, | |
position: c.Vector3, | |
color: c.Color, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ exa -a -I "target|.git|zig-cache|assets|sdl-main.zig" --tree | |
. | |
├── build.zig | |
├── include | |
│ └── raylib.h | |
├── lib | |
│ ├── cmake | |
│ │ └── raylib | |
│ │ ├── raylib-config-version.cmake | |
│ │ └── raylib-config.cmake | |
│ ├── libraylib.a | |
│ ├── libraylib.so -> libraylib.so.2 | |
│ ├── libraylib.so.2 -> libraylib.so.2.5.0 | |
│ ├── libraylib.so.2.5.0 | |
│ └── pkgconfig | |
│ └── raylib.pc | |
├── shell.nix | |
└── src | |
└── main.zig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment