summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-05-31 13:56:00 +0200
committerkdx <kikoodx@paranoici.org>2023-05-31 13:56:00 +0200
commit3d6ba6848d84d58f3c591e6ed5a6f47af10ecead (patch)
treea4ac4d9446d8e1e56f673c445a326d85865ca167
downloadzxc-3d6ba6848d84d58f3c591e6ed5a6f47af10ecead.tar.gz
initial commit
-rw-r--r--.gitignore1
-rw-r--r--.gitmodules3
m---------SDL.zig0
-rw-r--r--build.zig76
-rw-r--r--src/main.zig11
-rw-r--r--src/zxc.zig70
6 files changed, 161 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..fe95f8d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/zig-*
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..fd59bb0
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "SDL.zig"]
+ path = SDL.zig
+ url = https://github.com/MasterQ32/SDL.zig
diff --git a/SDL.zig b/SDL.zig
new file mode 160000
+Subproject fbe5f599c65b7a5642990443b2a686d1c53a498
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..7ca8105
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,76 @@
+const std = @import("std");
+const Sdk = @import("SDL.zig/Sdk.zig");
+
+// Although this function looks imperative, note that its job is to
+// declaratively construct a build graph that will be executed by an external
+// runner.
+pub fn build(b: *std.Build) void {
+ // Standard target options allows the person running `zig build` to choose
+ // what target to build for. Here we do not override the defaults, which
+ // means any target is allowed, and the default is native. Other options
+ // for restricting supported target set are available.
+ const target = b.standardTargetOptions(.{});
+
+ const sdk = Sdk.init(b, null);
+
+ // Standard optimization options allow the person running `zig build` to select
+ // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
+ // set a preferred release mode, allowing the user to decide how to optimize.
+ const optimize = b.standardOptimizeOption(.{});
+
+ const exe = b.addExecutable(.{
+ .name = "zxc",
+ // In this case the main source file is merely a path, however, in more
+ // complicated build scripts, this could be a generated file.
+ .root_source_file = .{ .path = "src/main.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+ sdk.link(exe, .dynamic);
+
+ exe.addModule("sdl2", sdk.getWrapperModule());
+
+ // This declares intent for the executable to be installed into the
+ // standard location when the user invokes the "install" step (the default
+ // step when running `zig build`).
+ b.installArtifact(exe);
+
+ // This *creates* a Run step in the build graph, to be executed when another
+ // step is evaluated that depends on it. The next line below will establish
+ // such a dependency.
+ const run_cmd = b.addRunArtifact(exe);
+
+ // By making the run step depend on the install step, it will be run from the
+ // installation directory rather than directly from within the cache directory.
+ // This is not necessary, however, if the application depends on other installed
+ // files, this ensures they will be present and in the expected location.
+ run_cmd.step.dependOn(b.getInstallStep());
+
+ // This allows the user to pass arguments to the application in the build
+ // command itself, like this: `zig build run -- arg1 arg2 etc`
+ if (b.args) |args| {
+ run_cmd.addArgs(args);
+ }
+
+ // This creates a build step. It will be visible in the `zig build --help` menu,
+ // and can be selected like this: `zig build run`
+ // This will evaluate the `run` step rather than the default, which is "install".
+ const run_step = b.step("run", "Run the app");
+ run_step.dependOn(&run_cmd.step);
+
+ // Creates a step for unit testing. This only builds the test executable
+ // but does not run it.
+ const unit_tests = b.addTest(.{
+ .root_source_file = .{ .path = "src/main.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+
+ const run_unit_tests = b.addRunArtifact(unit_tests);
+
+ // Similar to creating the run step earlier, this exposes a `test` step to
+ // the `zig build --help` menu, providing a way for the user to request
+ // running the unit tests.
+ const test_step = b.step("test", "Run unit tests");
+ test_step.dependOn(&run_unit_tests.step);
+}
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..4a5c92c
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,11 @@
+const std = @import("std");
+const zxc = @import("zxc.zig");
+
+pub fn main() !void {
+ var z = try zxc.init(.{
+ .width = 400,
+ .height = 224,
+ .target_fps = 60,
+ });
+ defer z.deinit();
+}
diff --git a/src/zxc.zig b/src/zxc.zig
new file mode 100644
index 0000000..f394f7d
--- /dev/null
+++ b/src/zxc.zig
@@ -0,0 +1,70 @@
+const std = @import("std");
+const sdl = @import("sdl2");
+const Self = @This();
+
+const Config = struct {
+ const RenderMode = enum {
+ Pixel,
+ PixelPerfect,
+ HdRender,
+ };
+
+ width: usize,
+ height: usize,
+ scale: usize = 1,
+ target_fps: usize,
+ render: RenderMode = .Pixel,
+ title: [:0]const u8 = "ZXC",
+};
+
+config: Config,
+window: sdl.Window,
+renderer: sdl.Renderer,
+
+pub fn init(config: Config) !Self {
+ var self = Self{
+ .config = config,
+ .window = undefined,
+ .renderer = undefined,
+ };
+
+ try sdl.init(.{ .video = true });
+ errdefer sdl.quit();
+
+ const basepath = sdl.c.SDL_GetBasePath();
+ if (basepath == null)
+ return sdl.makeError();
+ defer sdl.c.SDL_free(basepath);
+
+ try std.os.chdir(std.mem.span(basepath));
+
+ self.window = try sdl.createWindow(
+ config.title,
+ sdl.WindowPosition.default,
+ sdl.WindowPosition.default,
+ config.width,
+ config.height,
+ .{ .resizable = true },
+ );
+ errdefer self.window.destroy();
+
+ self.renderer = try sdl.createRenderer(
+ self.window,
+ null,
+ sdl.RendererFlags{ .accelerated = true },
+ );
+ errdefer self.renderer.destroy();
+
+ _ = sdl.setHint(
+ sdl.hint.render_scale_quality,
+ if (config.render == .HdRender) "1" else "0",
+ );
+
+ return self;
+}
+
+pub fn deinit(self: Self) void {
+ self.renderer.destroy();
+ self.window.destroy();
+ sdl.quit();
+}