From 9083e0e7f1d96db1a198761557c81d65bd2c5629 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Sun, 30 Aug 2020 20:05:00 +0200 Subject: [PATCH] add minimal SDL skeleton --- in_one_weekend/build.zig | 5 +++ in_one_weekend/src/main.zig | 67 ++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/in_one_weekend/build.zig b/in_one_weekend/build.zig index e0cdcf0..7cc7523 100644 --- a/in_one_weekend/build.zig +++ b/in_one_weekend/build.zig @@ -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(); diff --git a/in_one_weekend/src/main.zig b/in_one_weekend/src/main.zig index d29869f..03a7d3a 100644 --- a/in_one_weekend/src/main.zig +++ b/in_one_weekend/src/main.zig @@ -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); + } }