From 0e0b21d8c37616e8bef416b9a57194eba92ddc71 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:11:50 -0300 Subject: [PATCH] feat: draw white texture using SDL2 --- .gitmodules | 3 +++ build.zig | 9 ++++++++- lib/SDL.zig | 1 + src/main.zig | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 .gitmodules create mode 160000 lib/SDL.zig diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4470c92 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/SDL.zig"] + path = lib/SDL.zig + url = https://github.com/MasterQ32/SDL.zig diff --git a/build.zig b/build.zig index dc9af7d..cec93c3 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const Sdk = @import("lib/SDL.zig/Sdk.zig"); pub fn build(b: *std.build.Builder) void { // Standard target options allows the person running `zig build` to choose @@ -14,7 +15,13 @@ pub fn build(b: *std.build.Builder) void { const exe = b.addExecutable("zba", "src/main.zig"); // Bitfield type from FlorenceOS: https://github.com/FlorenceOS/ - exe.addPackage(.{ .name = "bitfield", .path = .{ .path = "./lib/util/bitfield.zig" } }); + exe.addPackage(.{ .name = "bitfield", .path = .{ .path = "lib/util/bitfield.zig" } }); + + // Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig + const sdk = Sdk.init(b); + sdk.link(exe, .dynamic); + + exe.addPackage(sdk.getNativePackage("sdl2")); exe.setTarget(target); exe.setBuildMode(mode); diff --git a/lib/SDL.zig b/lib/SDL.zig new file mode 160000 index 0000000..d3a7648 --- /dev/null +++ b/lib/SDL.zig @@ -0,0 +1 @@ +Subproject commit d3a764869b31575d3d73cc874b1ff27879407b5a diff --git a/src/main.zig b/src/main.zig index f7b2b67..fd40574 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,15 +1,18 @@ const std = @import("std"); -const emu = @import("emu.zig"); +const SDL = @import("sdl2"); +const emu = @import("emu.zig"); const Bus = @import("Bus.zig"); const Arm7tdmi = @import("cpu.zig").Arm7tdmi; const Scheduler = @import("scheduler.zig").Scheduler; pub fn main() anyerror!void { + // Allocator for Emulator + CLI Aruments var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gpa.allocator(); defer std.debug.assert(!gpa.deinit()); + // Handle CLI Arguments const args = try std.process.argsAlloc(alloc); defer std.process.argsFree(alloc, args); @@ -23,6 +26,7 @@ pub fn main() anyerror!void { return; } + // Initialize Emulator var scheduler = Scheduler.init(alloc); defer scheduler.deinit(); @@ -30,14 +34,50 @@ pub fn main() anyerror!void { defer bus.deinit(); var cpu = Arm7tdmi.init(&scheduler, &bus); - cpu.skipBios(); - while (true) { + // Initialize SDL + const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO); + if (status < 0) sdlPanic(); + defer SDL.SDL_Quit(); + + var window = SDL.SDL_CreateWindow( + "Gameboy Advance Emulator", + SDL.SDL_WINDOWPOS_CENTERED, + SDL.SDL_WINDOWPOS_CENTERED, + 240 * 3, + 160 * 3, + SDL.SDL_WINDOW_SHOWN, + ) orelse sdlPanic(); + defer _ = SDL.SDL_DestroyWindow(window); + + var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED) orelse sdlPanic(); + defer _ = SDL.SDL_DestroyRenderer(renderer); + + const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_RGBA8888, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160); + defer SDL.SDL_DestroyTexture(texture); + + const buf_len = (240 * 4) * 160; + var white: [buf_len]u8 = [_]u8{0xFF} ** buf_len; + + emu_loop: while (true) { emu.runFrame(&scheduler, &cpu, &bus); + + var event: SDL.SDL_Event = undefined; + _ = SDL.SDL_PollEvent(&event); + + switch (event.type) { + SDL.SDL_QUIT => break :emu_loop, + else => {}, + } + + _ = SDL.SDL_UpdateTexture(texture, null, &white, 240 * 4); + _ = SDL.SDL_RenderCopy(renderer, texture, null, null); + SDL.SDL_RenderPresent(renderer); } } -test "basic test" { - try std.testing.expectEqual(10, 3 + 7); +fn sdlPanic() noreturn { + const str = @as(?[*:0]const u8, SDL.SDL_GetError()) orelse "unknown error"; + @panic(std.mem.sliceTo(str, 0)); }