Created
May 16, 2023 00:55
-
-
Save akhenakh/bbeef782d45f9979bb9f368fb76b740d to your computer and use it in GitHub Desktop.
Setup Zig for Gamedev for OSX basd on https://zig.news/kristoff/how-to-release-your-zig-applications-2h90
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"); | |
pub fn build(b: *std.build.Builder) void { | |
const target = b.standardTargetOptions(.{ .default_target = .{ .abi = .gnu } }); | |
const mode = b.standardReleaseOptions(); | |
const exe = b.addExecutable("hello-gamedev", "src/main.zig"); | |
exe.setTarget(target); | |
exe.setBuildMode(mode); | |
exe.linkSystemLibrary("sdl2"); | |
exe.linkLibC(); | |
exe.install(); | |
} |
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 c = @cImport({ | |
@cInclude("SDL.h"); | |
}); | |
pub fn main() !void { | |
_ = c.SDL_Init(c.SDL_INIT_VIDEO); | |
defer c.SDL_Quit(); | |
var window = c.SDL_CreateWindow("hello gamedev", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 640, 400, 0); | |
defer c.SDL_DestroyWindow(window); | |
var renderer = c.SDL_CreateRenderer(window, 0, c.SDL_RENDERER_PRESENTVSYNC); | |
defer c.SDL_DestroyRenderer(renderer); | |
mainloop: while (true) { | |
var sdl_event: c.SDL_Event = undefined; | |
while (c.SDL_PollEvent(&sdl_event) != 0) { | |
switch (sdl_event.type) { | |
c.SDL_QUIT => break :mainloop, | |
else => {}, | |
} | |
} | |
_ = c.SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff); | |
_ = c.SDL_RenderClear(renderer); | |
var rect = c.SDL_Rect{ .x = 0, .y = 0, .w = 60, .h = 60 }; | |
const a = 0.001 * @intToFloat(f32, c.SDL_GetTicks()); | |
const t = 2 * std.math.pi / 3.0; | |
const r = 100 * @cos(0.1 * a); | |
rect.x = 290 + @floatToInt(i32, r * @cos(a)); | |
rect.y = 170 + @floatToInt(i32, r * @sin(a)); | |
_ = c.SDL_SetRenderDrawColor(renderer, 0xff, 0, 0, 0xff); | |
_ = c.SDL_RenderFillRect(renderer, &rect); | |
rect.x = 290 + @floatToInt(i32, r * @cos(a + t)); | |
rect.y = 170 + @floatToInt(i32, r * @sin(a + t)); | |
_ = c.SDL_SetRenderDrawColor(renderer, 0, 0xff, 0, 0xff); | |
_ = c.SDL_RenderFillRect(renderer, &rect); | |
rect.x = 290 + @floatToInt(i32, r * @cos(a + 2 * t)); | |
rect.y = 170 + @floatToInt(i32, r * @sin(a + 2 * t)); | |
_ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0xff, 0xff); | |
_ = c.SDL_RenderFillRect(renderer, &rect); | |
c.SDL_RenderPresent(renderer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build with
zig build -Dtarget=aarch64-macos