add minimal SDL skeleton

master
Fabien Freling 2020-08-30 20:05:00 +02:00
parent bf927efb97
commit 9083e0e7f1
2 changed files with 71 additions and 1 deletions

View File

@ -1,4 +1,5 @@
const Builder = @import("std").build.Builder;
const builtin = @import("std").builtin;
pub fn build(b: *Builder) void {
// Standard target options allows the person running `zig build` to choose
@ -14,6 +15,10 @@ pub fn build(b: *Builder) void {
const exe = b.addExecutable("in_one_weekend", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.linkSystemLibrary("SDL2");
exe.linkSystemLibrary("c");
exe.install();
const run_cmd = exe.run();

View File

@ -1,5 +1,70 @@
const std = @import("std");
const sdl = @cImport({
@cInclude("SDL.h");
});
// From: https://github.com/Nelarius/weekend-raytracer-zig/blob/master/src/main.zig
// 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, sdl.SDL_WINDOWPOS_UNDEFINED_MASK);
const win_width = 640;
const win_height = 320;
const fps = 60;
pub fn main() anyerror!void {
std.log.info("All your codebase are belong to us.", .{});
if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO) != 0) {
std.log.err("Unable to initialize SDL: {}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
}
defer sdl.SDL_Quit();
const window = sdl.SDL_CreateWindow("Raytracing in One Weekend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, win_width, win_height, sdl.SDL_WINDOW_OPENGL) orelse {
std.log.err("Unable to create window: {}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
};
const surface = sdl.SDL_GetWindowSurface(window) orelse {
std.log.err("Unable to get window surface: {}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
};
{
_ = sdl.SDL_LockSurface(surface);
var h: usize = 0;
while (h < win_height) {
var w: usize = 0;
while (w < win_width) {
// std.log.info("w = {}, h = {}", .{ w, h });
const index = w * 4 + h * @intCast(usize, surface.*.pitch);
const target_pixel = @ptrToInt(surface.*.pixels) + index;
@intToPtr(*u32, target_pixel).* = 0xffffffff;
w += 1;
}
h += 1;
}
defer sdl.SDL_UnlockSurface(surface);
}
if (sdl.SDL_UpdateWindowSurface(window) != 0) {
std.log.err("Error updating window surface: {}", .{sdl.SDL_GetError()});
return error.SDLUpdateWindowFailed;
}
var running = true;
while (running) {
var event: sdl.SDL_Event = undefined;
while (sdl.SDL_PollEvent(&event) != 0) {
switch (event.@"type") {
sdl.SDL_QUIT => {
running = false;
},
else => {},
}
}
sdl.SDL_Delay(1000 / fps);
}
}