Last active
June 30, 2020 09:50
-
-
Save peterhellberg/421735d78a9e01fcde245dc84f6f3ecc to your computer and use it in GitHub Desktop.
sdl-zig-demo (https://github.com/andrewrk/sdl-zig-demo) made to work with zig 0.6.0
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 Builder = @import("std").build.Builder; | |
pub fn build(b: *Builder) void { | |
const mode = b.standardReleaseOptions(); | |
const exe = b.addExecutable("sdl-zig-demo", "src/main.zig"); | |
exe.setBuildMode(mode); | |
exe.linkSystemLibrary("SDL2"); | |
exe.linkSystemLibrary("c"); | |
exe.install(); | |
const run_cmd = exe.run(); | |
run_cmd.step.dependOn(b.getInstallStep()); | |
const run_step = b.step("run", "Run the app"); | |
run_step.dependOn(&run_cmd.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
const c = @cImport({ | |
@cInclude("SDL2/SDL.h"); | |
}); | |
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("Unable to initialize SDL: %s", c.SDL_GetError()); | |
return error.SDLInitializationFailed; | |
} | |
defer c.SDL_Quit(); | |
const screen = c.SDL_CreateWindow("My Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 300, 73, c.SDL_WINDOW_OPENGL) orelse | |
{ | |
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("Unable to create renderer: %s", c.SDL_GetError()); | |
return error.SDLInitializationFailed; | |
}; | |
defer c.SDL_DestroyRenderer(renderer); | |
const zig_bmp = @embedFile("zig.bmp"); | |
const rw = c.SDL_RWFromConstMem( | |
@ptrCast(*const c_void, &zig_bmp[0]), | |
@intCast(c_int, zig_bmp.len), | |
) orelse { | |
c.SDL_Log("Unable to get RWFromConstMem: %s", c.SDL_GetError()); | |
return error.SDLInitializationFailed; | |
}; | |
defer assert(SDL_RWclose(rw) == 0); | |
const zig_surface = c.SDL_LoadBMP_RW(rw, 0) orelse { | |
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("Unable to create texture from surface: %s", c.SDL_GetError()); | |
return error.SDLInitializationFailed; | |
}; | |
defer c.SDL_DestroyTexture(zig_texture); | |
var quit = false; | |
while (!quit) { | |
var event: c.SDL_Event = undefined; | |
while (SDL_PollEvent(&event) != 0) { | |
switch (event.@"type") { | |
c.SDL_QUIT => { | |
quit = true; | |
}, | |
else => {}, | |
} | |
} | |
_ = c.SDL_RenderClear(renderer); | |
_ = c.SDL_RenderCopy(renderer, zig_texture, null, null); | |
c.SDL_RenderPresent(renderer); | |
c.SDL_Delay(17); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BMP
Window