update to latest zig

This commit is contained in:
Andrew Kelley 2021-03-29 12:18:07 -07:00
parent ccb18ace37
commit 09ef4b6106
2 changed files with 10 additions and 33 deletions

View File

@ -10,10 +10,7 @@ pub fn build(b: *Builder) void {
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
b.installArtifact(exe); b.installArtifact(exe);
const run = b.step("run", "Run the demo"); const run = b.step("run", "Run the demo");
const run_cmd = b.addCommand(".", b.env_map, const run_cmd = exe.run();
[][]const u8{exe.getOutputPath(), });
run.dependOn(&run_cmd.step); run.dependOn(&run_cmd.step);
run_cmd.step.dependOn(&exe.step);
} }

View File

@ -3,42 +3,22 @@ const c = @cImport({
}); });
const assert = @import("std").debug.assert; const assert = @import("std").debug.assert;
// See https://github.com/zig-lang/zig/issues/565
// SDL_video.h:#define SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED_DISPLAY(0)
// SDL_video.h:#define SDL_WINDOWPOS_UNDEFINED_DISPLAY(X) (SDL_WINDOWPOS_UNDEFINED_MASK|(X))
// SDL_video.h:#define SDL_WINDOWPOS_UNDEFINED_MASK 0x1FFF0000u
const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, c.SDL_WINDOWPOS_UNDEFINED_MASK);
extern fn SDL_PollEvent(event: *c.SDL_Event) c_int;
// SDL_RWclose is fundamentally unrepresentable in Zig, because `ctx` is
// evaluated twice. One could make the case that this is a bug in SDL,
// especially since the docs list a real function prototype that would not
// have this double-evaluation of the parameter.
// If SDL would instead of a macro use a static inline function,
// it would resolve the SDL bug as well as make the function visible to Zig
// and to debuggers.
// SDL_rwops.h:#define SDL_RWclose(ctx) (ctx)->close(ctx)
inline fn SDL_RWclose(ctx: [*]c.SDL_RWops) c_int {
return ctx[0].close.?(ctx);
}
pub fn main() !void { pub fn main() !void {
if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) { if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
c.SDL_Log(c"Unable to initialize SDL: %s", c.SDL_GetError()); c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
return error.SDLInitializationFailed; return error.SDLInitializationFailed;
} }
defer c.SDL_Quit(); defer c.SDL_Quit();
const screen = c.SDL_CreateWindow(c"My Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 300, 73, c.SDL_WINDOW_OPENGL) orelse const screen = c.SDL_CreateWindow("My Game Window", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, 300, 73, c.SDL_WINDOW_OPENGL) orelse
{ {
c.SDL_Log(c"Unable to create window: %s", c.SDL_GetError()); c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
return error.SDLInitializationFailed; return error.SDLInitializationFailed;
}; };
defer c.SDL_DestroyWindow(screen); defer c.SDL_DestroyWindow(screen);
const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse { const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse {
c.SDL_Log(c"Unable to create renderer: %s", c.SDL_GetError()); c.SDL_Log("Unable to create renderer: %s", c.SDL_GetError());
return error.SDLInitializationFailed; return error.SDLInitializationFailed;
}; };
defer c.SDL_DestroyRenderer(renderer); defer c.SDL_DestroyRenderer(renderer);
@ -48,19 +28,19 @@ pub fn main() !void {
@ptrCast(*const c_void, &zig_bmp[0]), @ptrCast(*const c_void, &zig_bmp[0]),
@intCast(c_int, zig_bmp.len), @intCast(c_int, zig_bmp.len),
) orelse { ) orelse {
c.SDL_Log(c"Unable to get RWFromConstMem: %s", c.SDL_GetError()); c.SDL_Log("Unable to get RWFromConstMem: %s", c.SDL_GetError());
return error.SDLInitializationFailed; return error.SDLInitializationFailed;
}; };
defer assert(SDL_RWclose(rw) == 0); defer assert(c.SDL_RWclose(rw) == 0);
const zig_surface = c.SDL_LoadBMP_RW(rw, 0) orelse { const zig_surface = c.SDL_LoadBMP_RW(rw, 0) orelse {
c.SDL_Log(c"Unable to load bmp: %s", c.SDL_GetError()); c.SDL_Log("Unable to load bmp: %s", c.SDL_GetError());
return error.SDLInitializationFailed; return error.SDLInitializationFailed;
}; };
defer c.SDL_FreeSurface(zig_surface); defer c.SDL_FreeSurface(zig_surface);
const zig_texture = c.SDL_CreateTextureFromSurface(renderer, zig_surface) orelse { const zig_texture = c.SDL_CreateTextureFromSurface(renderer, zig_surface) orelse {
c.SDL_Log(c"Unable to create texture from surface: %s", c.SDL_GetError()); c.SDL_Log("Unable to create texture from surface: %s", c.SDL_GetError());
return error.SDLInitializationFailed; return error.SDLInitializationFailed;
}; };
defer c.SDL_DestroyTexture(zig_texture); defer c.SDL_DestroyTexture(zig_texture);
@ -68,7 +48,7 @@ pub fn main() !void {
var quit = false; var quit = false;
while (!quit) { while (!quit) {
var event: c.SDL_Event = undefined; var event: c.SDL_Event = undefined;
while (SDL_PollEvent(&event) != 0) { while (c.SDL_PollEvent(&event) != 0) {
switch (event.@"type") { switch (event.@"type") {
c.SDL_QUIT => { c.SDL_QUIT => {
quit = true; quit = true;