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 { 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); } }