From 6b9d0baeb630d679cd4f5ddf7a97e104f361282c Mon Sep 17 00:00:00 2001 From: kayomn Date: Thu, 22 Sep 2022 22:03:25 +0100 Subject: [PATCH] Temporary disable building against Lua --- build.zig | 80 +++++++++++++++++++++--------------------- src/main.zig | 99 +++++++++++++++++----------------------------------- 2 files changed, 72 insertions(+), 107 deletions(-) diff --git a/build.zig b/build.zig index a1b0b70..4206f31 100644 --- a/build.zig +++ b/build.zig @@ -15,50 +15,50 @@ pub fn build(builder: *std.build.Builder) void { ona_exe.linkSystemLibrary("SDL2"); // Lua dependency. - ona_exe.linkLibrary(link_lua: { - const lua_lib = builder.addStaticLibrary("lua", null); + // ona_exe.linkLibrary(link_lua: { + // const lua_lib = builder.addStaticLibrary("lua", null); - lua_lib.addIncludeDir("./ext/lua"); - lua_lib.linkLibC(); + // lua_lib.addIncludeDir("./ext/lua"); + // lua_lib.linkLibC(); - // TODO: Implement support for more than Linux. - lua_lib.addCSourceFiles(&.{ - "./ext/lua/lapi.c", - "./ext/lua/lauxlib.c", - "./ext/lua/lbaselib.c", - "./ext/lua/lcode.c", - "./ext/lua/lcorolib.c", - "./ext/lua/lctype.c", - "./ext/lua/ldblib.c", - "./ext/lua/ldebug.c", - "./ext/lua/ldo.c", - "./ext/lua/ldump.c", - "./ext/lua/lfunc.c", - "./ext/lua/lgc.c", - "./ext/lua/llex.c", - "./ext/lua/lmathlib.c", - "./ext/lua/lmem.c", - "./ext/lua/loadlib.c", - "./ext/lua/lobject.c", - "./ext/lua/lopcodes.c", - "./ext/lua/loslib.c", - "./ext/lua/lparser.c", - "./ext/lua/lstate.c", - "./ext/lua/lstring.c", - "./ext/lua/lstrlib.c", - "./ext/lua/ltablib.c", - "./ext/lua/lundump.c", - "./ext/lua/lutf8lib.c", - "./ext/lua/lvm.c", - "./ext/lua/lzio.c", - "./ext/lua/ltable.c", - "./ext/lua/ltm.c", - }, &.{"-DLUA_USE_LINUX", "-Wl"}); + // // TODO: Implement support for more than Linux. + // lua_lib.addCSourceFiles(&.{ + // "./ext/lua/lapi.c", + // "./ext/lua/lauxlib.c", + // "./ext/lua/lbaselib.c", + // "./ext/lua/lcode.c", + // "./ext/lua/lcorolib.c", + // "./ext/lua/lctype.c", + // "./ext/lua/ldblib.c", + // "./ext/lua/ldebug.c", + // "./ext/lua/ldo.c", + // "./ext/lua/ldump.c", + // "./ext/lua/lfunc.c", + // "./ext/lua/lgc.c", + // "./ext/lua/llex.c", + // "./ext/lua/lmathlib.c", + // "./ext/lua/lmem.c", + // "./ext/lua/loadlib.c", + // "./ext/lua/lobject.c", + // "./ext/lua/lopcodes.c", + // "./ext/lua/loslib.c", + // "./ext/lua/lparser.c", + // "./ext/lua/lstate.c", + // "./ext/lua/lstring.c", + // "./ext/lua/lstrlib.c", + // "./ext/lua/ltablib.c", + // "./ext/lua/lundump.c", + // "./ext/lua/lutf8lib.c", + // "./ext/lua/lvm.c", + // "./ext/lua/lzio.c", + // "./ext/lua/ltable.c", + // "./ext/lua/ltm.c", + // }, &.{"-DLUA_USE_LINUX", "-Wl"}); - lua_lib.install(); + // lua_lib.install(); - break: link_lua lua_lib; - }); + // break: link_lua lua_lib; + // }); const run_cmd = ona_exe.run(); diff --git a/src/main.zig b/src/main.zig index 1a6d95f..38d81ed 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,4 @@ -const c = @cImport({ +const ext = @cImport({ @cInclude("SDL2/SDL.h"); @cInclude("lua/lua.h"); @cInclude("lua/lualib.h"); @@ -16,55 +16,32 @@ const Request = struct { message: union(enum) { close: struct { - file: *c.SDL_RWops, + file: *ext.SDL_RWops, is_closed: *bool, }, open_readable: struct { uri: *const io.Uri, - file: ?*c.SDL_RWops, + file: ?*ext.SDL_RWops, }, }, }; -fn luaAlloc(userdata: ?*anyopaque, ptr: ?*anyopaque, original_size: usize, - updated_size: usize) callconv(.C) ?*anyopaque { - // Implementation derived from - // https://github.com/natecraddock/ziglua/blob/master/src/ziglua.zig. - const alignment = @alignOf(std.c.max_align_t); - const Allocator = std.mem.Allocator; - const allocator = @ptrCast(*Allocator, @alignCast(@alignOf(Allocator), userdata.?)); - if (@ptrCast(?[*]align(alignment) u8, @alignCast(alignment, ptr))) |prev_ptr| { - // Allocator is working with an existing pointer. - const prev_slice = prev_ptr[0 .. original_size]; - if (updated_size == 0) { - // Updated size of `0` to free the existing memory block. - allocator.free(prev_slice); - return null; - } - // Resize the existing memory block. - return (allocator.reallocAdvanced(prev_slice, alignment, - updated_size, .exact) catch return null).ptr; - } - // No existing pointer, allocate a new block of memory. - return (allocator.alignedAlloc(u8, alignment, updated_size) catch return null).ptr; -} - /// /// Entry point. /// pub fn main() anyerror!void { - if (c.SDL_Init(c.SDL_INIT_EVERYTHING) != 0) { - c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime"); + if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { + ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime"); return error.InitFailure; } - defer c.SDL_Quit(); + defer ext.SDL_Quit(); const pref_path = create_pref_path: { - const path = c.SDL_GetPrefPath("ona", "ona") orelse { - c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path"); + const path = ext.SDL_GetPrefPath("ona", "ona") orelse { + ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path"); return error.InitFailure; }; @@ -72,69 +49,57 @@ pub fn main() anyerror!void { break: create_pref_path path[0 .. std.mem.len(path)]; }; - defer c.SDL_free(pref_path.ptr); + defer ext.SDL_free(pref_path.ptr); const window = create_window: { - const pos = c.SDL_WINDOWPOS_UNDEFINED; + const pos = ext.SDL_WINDOWPOS_UNDEFINED; var flags = @as(u32, 0); - break: create_window c.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse { - c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window"); + break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse { + ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window"); return error.InitFailure; }; }; - defer c.SDL_DestroyWindow(window); + defer ext.SDL_DestroyWindow(window); const renderer = create_renderer: { var flags = @as(u32, 0); - break: create_renderer c.SDL_CreateRenderer(window, -1, flags) orelse { - c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer"); + break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse { + ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer"); return error.InitFailure; }; }; - defer c.SDL_DestroyRenderer(renderer); - - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - var lua_allocator = gpa.allocator(); - - const lua_state = c.lua_newstate(luaAlloc, @ptrCast(*anyopaque, &lua_allocator)) orelse { - c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, - "Failed to initialize Lua virtual machine"); - - return error.InitFailure; - }; - - defer c.lua_close(lua_state); + defer ext.SDL_DestroyRenderer(renderer); var request_chain = @as(?*Request, null); var is_running = true; while (is_running) { - var event = std.mem.zeroes(c.SDL_Event); + var event = std.mem.zeroes(ext.SDL_Event); - while (c.SDL_PollEvent(&event) != 0) { + while (ext.SDL_PollEvent(&event) != 0) { switch (event.type) { - c.SDL_QUIT => is_running = false, + ext.SDL_QUIT => is_running = false, else => {}, } } - if (c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) { - c.SDL_LogError(c.SDL_LOG_CATEGORY_VIDEO, c.SDL_GetError()); - c.SDL_ClearError(); + if (ext.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) { + ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError()); + ext.SDL_ClearError(); } - if (c.SDL_RenderClear(renderer) != 0) { - c.SDL_LogError(c.SDL_LOG_CATEGORY_VIDEO, c.SDL_GetError()); - c.SDL_ClearError(); + if (ext.SDL_RenderClear(renderer) != 0) { + ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError()); + ext.SDL_ClearError(); } - c.SDL_RenderPresent(renderer); + ext.SDL_RenderPresent(renderer); while (request_chain) |request_head| { const request = request_head; @@ -142,20 +107,20 @@ pub fn main() anyerror!void { request_chain = request_head.next; switch (request.message) { - .close => |*close| close.is_closed.* = (c.SDL_RWclose(close.file) == 0), + .close => |*close| close.is_closed.* = (ext.SDL_RWclose(close.file) == 0), .open_readable => |*open_readable| { if (open_readable.uri.isScheme("data")) { - var path = stack.Fixed(u8, 4096){}; + var path = stack.Fixed(u8, 4096).init(); // These can never fail as the sum of the potential bytes written will // always be less than 4096. path.pushAll("./") catch unreachable; std.debug.assert(open_readable.uri.writePath(path.asWriter())); - open_readable.file = c.SDL_RWFromFile(&path.buffer, "r"); + open_readable.file = ext.SDL_RWFromFile(&path.buffer, "r"); } else if (open_readable.uri.isScheme("user")) { - var path = stack.Fixed(u8, 4096){}; + var path = stack.Fixed(u8, 4096).init(); const isOk = errors.isOk; // Cannot guarantee that the sum of potential bytes written will always be @@ -163,7 +128,7 @@ pub fn main() anyerror!void { if (isOk(stack.FinitePushError, path.pushAll(pref_path)) and open_readable.uri.writePath(path.asWriter())) { - open_readable.file = c.SDL_RWFromFile(&path.buffer, "r"); + open_readable.file = ext.SDL_RWFromFile(&path.buffer, "r"); } } }, @@ -172,6 +137,6 @@ pub fn main() anyerror!void { resume request.frame; } - c.SDL_Delay(1); + ext.SDL_Delay(1); } }