Skip to content

Commit

Permalink
cleanup software-renderer related code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Dec 12, 2024
1 parent e9d25a0 commit fab1905
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/hello.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var suppress: bool = true;
pub fn init(ctx: jok.Context) !void {
std.log.info("game init", .{});

try physfs.setWriteDir(physfs.getPrefDir(ctx));
try physfs.setWriteDir(physfs.getPrefDir("myorg", "mygame"));

batchpool_2d = try @TypeOf(batchpool_2d).init(ctx);
batchpool_3d = try @TypeOf(batchpool_3d).init(ctx);
Expand Down
14 changes: 0 additions & 14 deletions src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ pub const Config = struct {
/// Assets accessing method
jok_enable_physfs: bool = true,

/// Application preference directory info
jok_pref_org: [*:0]const u8 = "myorg",
jok_pref_app: [*:0]const u8 = "mygame",

/// Whether use pure-software renderer (NOTE: SDL might ignore this setting when GPU is available)
jok_software_renderer: bool = false,

/// Whether fallback to software renderer when gpu isn't found
jok_software_renderer_fallback: bool = true,

/// Canvas size (default to framebuffer's size)
jok_canvas_size: ?jok.Size = null,

Expand Down Expand Up @@ -107,10 +97,6 @@ pub fn init(comptime game: anytype) Config {
.{ .name = "jok_mem_leak_checks", .desc = "whether default memory allocator check memleak when exiting" },
.{ .name = "jok_mem_detail_logs", .desc = "whether default memory allocator print detailed memory alloc/free logs" },
.{ .name = "jok_enable_physfs", .desc = "whether use physfs to access game assets" },
.{ .name = "jok_pref_org", .desc = "Org part of app preference path" },
.{ .name = "jok_pref_app", .desc = "App part of app preference path" },
.{ .name = "jok_software_renderer", .desc = "whether use software renderer" },
.{ .name = "jok_software_renderer_fallback", .desc = "whether fallback to software renderer when hardware acceleration isn't available" },
.{ .name = "jok_canvas_size", .desc = "size of canvas" },
.{ .name = "jok_enable_post_processing", .desc = "whether enable post-processing facility" },
.{ .name = "jok_window_title", .desc = "title of window" },
Expand Down
8 changes: 1 addition & 7 deletions src/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ pub fn JokContext(comptime cfg: config.Config) type {

// Renderer instance
_renderer: jok.Renderer = undefined,
_is_software: bool = false,

// Rendering target
_canvas_texture: jok.Texture = undefined,
Expand Down Expand Up @@ -335,7 +334,7 @@ pub fn JokContext(comptime cfg: config.Config) type {
) void {
const pc_threshold: u64 = switch (cfg.jok_fps_limit) {
.none => 0,
.auto => if (self._is_software) @divTrunc(self._pc_freq, 30) else 0,
.auto => 0,
.manual => |_fps| self._pc_freq / @as(u64, _fps),
};

Expand Down Expand Up @@ -565,11 +564,9 @@ pub fn JokContext(comptime cfg: config.Config) type {
\\ Platform : {s}
\\ Memory : {d}MB
\\ App Dir : {s}
\\ Data Dir : {s}
\\
\\Renderer info:
\\ Vertical Sync : {}
\\ GPU Enabled : {}
\\ Max Texture Size : {d}*{d}
\\
\\
Expand All @@ -586,9 +583,7 @@ pub fn JokContext(comptime cfg: config.Config) type {
@tagName(target.os.tag),
ram_size,
physfs.getBaseDir(),
physfs.getPrefDir(self._ctx),
info.flags & sdl.SDL_RENDERER_PRESENTVSYNC != 0,
info.flags & sdl.SDL_RENDERER_ACCELERATED != 0,
info.max_texture_width,
info.max_texture_height,
},
Expand Down Expand Up @@ -897,7 +892,6 @@ pub fn JokContext(comptime cfg: config.Config) type {
imgui.text("Window Size: {d:.0}x{d:.0}", .{ ws.width, ws.height });
imgui.text("Canvas Size: {d:.0}x{d:.0}", .{ cs.width, cs.height });
imgui.text("Display DPI: {d:.1}", .{self._display_dpi});
imgui.text("GPU Enabled: {}", .{!self._is_software});
imgui.text("V-Sync Enabled: {}", .{rdinfo.flags & sdl.SDL_RENDERER_PRESENTVSYNC != 0});
imgui.text("Optimize Mode: {s}", .{@tagName(builtin.mode)});
imgui.separator();
Expand Down
20 changes: 3 additions & 17 deletions src/renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,27 @@ pub const dcstats = struct {

pub const Renderer = struct {
ptr: *sdl.SDL_Renderer,
is_software: bool,
cfg: jok.config.Config,

// Create hardware accelerated renderer
// Fallback to software renderer if allowed
pub fn init(cfg: jok.config.Config, window: jok.Window) !Renderer {
var flags: u32 = sdl.SDL_RENDERER_TARGETTEXTURE;
if (cfg.jok_software_renderer) {
flags |= sdl.SDL_RENDERER_SOFTWARE;
}
var flags: u32 = sdl.SDL_RENDERER_TARGETTEXTURE | sdl.SDL_RENDERER_ACCELERATED;
if (cfg.jok_fps_limit == .auto) {
flags |= sdl.SDL_RENDERER_PRESENTVSYNC;
}
var ptr = sdl.SDL_CreateRenderer(window.ptr, -1, flags);
if (ptr == null and
!cfg.jok_software_renderer and
cfg.jok_software_renderer_fallback)
{
log.warn("Hardware accelerated renderer isn't supported, fallback to software backend", .{});
flags |= sdl.SDL_RENDERER_SOFTWARE;
ptr = sdl.SDL_CreateRenderer(window.ptr, -1, flags);
}
const ptr = sdl.SDL_CreateRenderer(window.ptr, -1, flags);
if (ptr == null) {
log.err("create renderer failed: {s}", .{sdl.SDL_GetError()});
return sdl.Error.SdlError;
}

var rd = Renderer{
.ptr = ptr.?,
.is_software = undefined,
.cfg = cfg,
};
try rd.setBlendMode(.blend);
const rdinfo = try rd.getInfo();
rd.is_software = ((rdinfo.flags & sdl.SDL_RENDERER_SOFTWARE) != 0);
assert((rdinfo.flags & sdl.SDL_RENDERER_ACCELERATED) != 0);
return rd;
}

Expand Down
4 changes: 2 additions & 2 deletions src/vendor/physfs/physfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ pub fn getBaseDir() [*:0]const u8 {
/// "/Users/bob/Library/Application Support/My Program Name"
///
/// (etc.)
pub fn getPrefDir(ctx: jok.Context) [*:0]const u8 {
if (PHYSFS_getPrefDir(ctx.cfg().jok_pref_org, ctx.cfg().jok_pref_app)) |p| {
pub fn getPrefDir(org: [*:0]const u8, app: [*:0]const u8) [*:0]const u8 {
if (PHYSFS_getPrefDir(org, app)) |p| {
return p;
}
@panic("can't get data dir");
Expand Down

0 comments on commit fab1905

Please sign in to comment.