Temporary disable building against Lua
This commit is contained in:
parent
26ccb4cbe0
commit
6b9d0baeb6
80
build.zig
80
build.zig
|
@ -15,50 +15,50 @@ pub fn build(builder: *std.build.Builder) void {
|
||||||
ona_exe.linkSystemLibrary("SDL2");
|
ona_exe.linkSystemLibrary("SDL2");
|
||||||
|
|
||||||
// Lua dependency.
|
// Lua dependency.
|
||||||
ona_exe.linkLibrary(link_lua: {
|
// ona_exe.linkLibrary(link_lua: {
|
||||||
const lua_lib = builder.addStaticLibrary("lua", null);
|
// const lua_lib = builder.addStaticLibrary("lua", null);
|
||||||
|
|
||||||
lua_lib.addIncludeDir("./ext/lua");
|
// lua_lib.addIncludeDir("./ext/lua");
|
||||||
lua_lib.linkLibC();
|
// lua_lib.linkLibC();
|
||||||
|
|
||||||
// TODO: Implement support for more than Linux.
|
// // TODO: Implement support for more than Linux.
|
||||||
lua_lib.addCSourceFiles(&.{
|
// lua_lib.addCSourceFiles(&.{
|
||||||
"./ext/lua/lapi.c",
|
// "./ext/lua/lapi.c",
|
||||||
"./ext/lua/lauxlib.c",
|
// "./ext/lua/lauxlib.c",
|
||||||
"./ext/lua/lbaselib.c",
|
// "./ext/lua/lbaselib.c",
|
||||||
"./ext/lua/lcode.c",
|
// "./ext/lua/lcode.c",
|
||||||
"./ext/lua/lcorolib.c",
|
// "./ext/lua/lcorolib.c",
|
||||||
"./ext/lua/lctype.c",
|
// "./ext/lua/lctype.c",
|
||||||
"./ext/lua/ldblib.c",
|
// "./ext/lua/ldblib.c",
|
||||||
"./ext/lua/ldebug.c",
|
// "./ext/lua/ldebug.c",
|
||||||
"./ext/lua/ldo.c",
|
// "./ext/lua/ldo.c",
|
||||||
"./ext/lua/ldump.c",
|
// "./ext/lua/ldump.c",
|
||||||
"./ext/lua/lfunc.c",
|
// "./ext/lua/lfunc.c",
|
||||||
"./ext/lua/lgc.c",
|
// "./ext/lua/lgc.c",
|
||||||
"./ext/lua/llex.c",
|
// "./ext/lua/llex.c",
|
||||||
"./ext/lua/lmathlib.c",
|
// "./ext/lua/lmathlib.c",
|
||||||
"./ext/lua/lmem.c",
|
// "./ext/lua/lmem.c",
|
||||||
"./ext/lua/loadlib.c",
|
// "./ext/lua/loadlib.c",
|
||||||
"./ext/lua/lobject.c",
|
// "./ext/lua/lobject.c",
|
||||||
"./ext/lua/lopcodes.c",
|
// "./ext/lua/lopcodes.c",
|
||||||
"./ext/lua/loslib.c",
|
// "./ext/lua/loslib.c",
|
||||||
"./ext/lua/lparser.c",
|
// "./ext/lua/lparser.c",
|
||||||
"./ext/lua/lstate.c",
|
// "./ext/lua/lstate.c",
|
||||||
"./ext/lua/lstring.c",
|
// "./ext/lua/lstring.c",
|
||||||
"./ext/lua/lstrlib.c",
|
// "./ext/lua/lstrlib.c",
|
||||||
"./ext/lua/ltablib.c",
|
// "./ext/lua/ltablib.c",
|
||||||
"./ext/lua/lundump.c",
|
// "./ext/lua/lundump.c",
|
||||||
"./ext/lua/lutf8lib.c",
|
// "./ext/lua/lutf8lib.c",
|
||||||
"./ext/lua/lvm.c",
|
// "./ext/lua/lvm.c",
|
||||||
"./ext/lua/lzio.c",
|
// "./ext/lua/lzio.c",
|
||||||
"./ext/lua/ltable.c",
|
// "./ext/lua/ltable.c",
|
||||||
"./ext/lua/ltm.c",
|
// "./ext/lua/ltm.c",
|
||||||
}, &.{"-DLUA_USE_LINUX", "-Wl"});
|
// }, &.{"-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();
|
const run_cmd = ona_exe.run();
|
||||||
|
|
||||||
|
|
99
src/main.zig
99
src/main.zig
|
@ -1,4 +1,4 @@
|
||||||
const c = @cImport({
|
const ext = @cImport({
|
||||||
@cInclude("SDL2/SDL.h");
|
@cInclude("SDL2/SDL.h");
|
||||||
@cInclude("lua/lua.h");
|
@cInclude("lua/lua.h");
|
||||||
@cInclude("lua/lualib.h");
|
@cInclude("lua/lualib.h");
|
||||||
|
@ -16,55 +16,32 @@ const Request = struct {
|
||||||
|
|
||||||
message: union(enum) {
|
message: union(enum) {
|
||||||
close: struct {
|
close: struct {
|
||||||
file: *c.SDL_RWops,
|
file: *ext.SDL_RWops,
|
||||||
is_closed: *bool,
|
is_closed: *bool,
|
||||||
},
|
},
|
||||||
|
|
||||||
open_readable: struct {
|
open_readable: struct {
|
||||||
uri: *const io.Uri,
|
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.
|
/// Entry point.
|
||||||
///
|
///
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
if (c.SDL_Init(c.SDL_INIT_EVERYTHING) != 0) {
|
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
|
||||||
c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime");
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime");
|
||||||
|
|
||||||
return error.InitFailure;
|
return error.InitFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
defer c.SDL_Quit();
|
defer ext.SDL_Quit();
|
||||||
|
|
||||||
const pref_path = create_pref_path: {
|
const pref_path = create_pref_path: {
|
||||||
const path = c.SDL_GetPrefPath("ona", "ona") orelse {
|
const path = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
||||||
c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
|
||||||
|
|
||||||
return error.InitFailure;
|
return error.InitFailure;
|
||||||
};
|
};
|
||||||
|
@ -72,69 +49,57 @@ pub fn main() anyerror!void {
|
||||||
break: create_pref_path path[0 .. std.mem.len(path)];
|
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 window = create_window: {
|
||||||
const pos = c.SDL_WINDOWPOS_UNDEFINED;
|
const pos = ext.SDL_WINDOWPOS_UNDEFINED;
|
||||||
var flags = @as(u32, 0);
|
var flags = @as(u32, 0);
|
||||||
|
|
||||||
break: create_window c.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse {
|
break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse {
|
||||||
c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window");
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window");
|
||||||
|
|
||||||
return error.InitFailure;
|
return error.InitFailure;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
defer c.SDL_DestroyWindow(window);
|
defer ext.SDL_DestroyWindow(window);
|
||||||
|
|
||||||
const renderer = create_renderer: {
|
const renderer = create_renderer: {
|
||||||
var flags = @as(u32, 0);
|
var flags = @as(u32, 0);
|
||||||
|
|
||||||
break: create_renderer c.SDL_CreateRenderer(window, -1, flags) orelse {
|
break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse {
|
||||||
c.SDL_LogCritical(c.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer");
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer");
|
||||||
|
|
||||||
return error.InitFailure;
|
return error.InitFailure;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
defer c.SDL_DestroyRenderer(renderer);
|
defer ext.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);
|
|
||||||
|
|
||||||
var request_chain = @as(?*Request, null);
|
var request_chain = @as(?*Request, null);
|
||||||
var is_running = true;
|
var is_running = true;
|
||||||
|
|
||||||
while (is_running) {
|
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) {
|
switch (event.type) {
|
||||||
c.SDL_QUIT => is_running = false,
|
ext.SDL_QUIT => is_running = false,
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) {
|
if (ext.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) {
|
||||||
c.SDL_LogError(c.SDL_LOG_CATEGORY_VIDEO, c.SDL_GetError());
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
||||||
c.SDL_ClearError();
|
ext.SDL_ClearError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c.SDL_RenderClear(renderer) != 0) {
|
if (ext.SDL_RenderClear(renderer) != 0) {
|
||||||
c.SDL_LogError(c.SDL_LOG_CATEGORY_VIDEO, c.SDL_GetError());
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
||||||
c.SDL_ClearError();
|
ext.SDL_ClearError();
|
||||||
}
|
}
|
||||||
|
|
||||||
c.SDL_RenderPresent(renderer);
|
ext.SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
while (request_chain) |request_head| {
|
while (request_chain) |request_head| {
|
||||||
const request = request_head;
|
const request = request_head;
|
||||||
|
@ -142,20 +107,20 @@ pub fn main() anyerror!void {
|
||||||
request_chain = request_head.next;
|
request_chain = request_head.next;
|
||||||
|
|
||||||
switch (request.message) {
|
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| {
|
.open_readable => |*open_readable| {
|
||||||
if (open_readable.uri.isScheme("data")) {
|
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
|
// These can never fail as the sum of the potential bytes written will
|
||||||
// always be less than 4096.
|
// always be less than 4096.
|
||||||
path.pushAll("./") catch unreachable;
|
path.pushAll("./") catch unreachable;
|
||||||
std.debug.assert(open_readable.uri.writePath(path.asWriter()));
|
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")) {
|
} else if (open_readable.uri.isScheme("user")) {
|
||||||
var path = stack.Fixed(u8, 4096){};
|
var path = stack.Fixed(u8, 4096).init();
|
||||||
const isOk = errors.isOk;
|
const isOk = errors.isOk;
|
||||||
|
|
||||||
// Cannot guarantee that the sum of potential bytes written will always be
|
// 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
|
if (isOk(stack.FinitePushError, path.pushAll(pref_path)) and
|
||||||
open_readable.uri.writePath(path.asWriter())) {
|
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;
|
resume request.frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
c.SDL_Delay(1);
|
ext.SDL_Delay(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue