update to latest zig
This commit is contained in:
parent
ccb18ace37
commit
09ef4b6106
|
@ -10,10 +10,7 @@ pub fn build(b: *Builder) void {
|
|||
b.default_step.dependOn(&exe.step);
|
||||
b.installArtifact(exe);
|
||||
|
||||
|
||||
const run = b.step("run", "Run the demo");
|
||||
const run_cmd = b.addCommand(".", b.env_map,
|
||||
[][]const u8{exe.getOutputPath(), });
|
||||
const run_cmd = exe.run();
|
||||
run.dependOn(&run_cmd.step);
|
||||
run_cmd.step.dependOn(&exe.step);
|
||||
}
|
||||
|
|
38
src/main.zig
38
src/main.zig
|
@ -3,42 +3,22 @@ const c = @cImport({
|
|||
});
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
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;
|
||||
};
|
||||
defer c.SDL_DestroyWindow(screen);
|
||||
|
||||
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;
|
||||
};
|
||||
defer c.SDL_DestroyRenderer(renderer);
|
||||
|
@ -48,19 +28,19 @@ pub fn main() !void {
|
|||
@ptrCast(*const c_void, &zig_bmp[0]),
|
||||
@intCast(c_int, zig_bmp.len),
|
||||
) 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;
|
||||
};
|
||||
defer assert(SDL_RWclose(rw) == 0);
|
||||
defer assert(c.SDL_RWclose(rw) == 0);
|
||||
|
||||
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;
|
||||
};
|
||||
defer c.SDL_FreeSurface(zig_surface);
|
||||
|
||||
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;
|
||||
};
|
||||
defer c.SDL_DestroyTexture(zig_texture);
|
||||
|
@ -68,7 +48,7 @@ pub fn main() !void {
|
|||
var quit = false;
|
||||
while (!quit) {
|
||||
var event: c.SDL_Event = undefined;
|
||||
while (SDL_PollEvent(&event) != 0) {
|
||||
while (c.SDL_PollEvent(&event) != 0) {
|
||||
switch (event.@"type") {
|
||||
c.SDL_QUIT => {
|
||||
quit = true;
|
||||
|
|
Loading…
Reference in New Issue
Block a user