Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Some advantages of SDL3 include windowing, audio, gamepad, keyboard, mouse, rend
To use zig-sdl3 with zig 0.15.1, you need to add it as a dependency to your project.
The master branch of zig is not currently supported.
Choose the command that matches your desired zig-sdl3 version and run it in your project's root directory:
* For the latest tag release:
* For the latest tagged release:
```sh
zig fetch --save git+https://github.com/Gota7/zig-sdl3#v0.1.3
zig fetch --save git+https://github.com/Gota7/zig-sdl3#v0.1.4
```
* For in progress updates (nightly):
```sh
Expand Down
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn build(b: *std.Build) !void {
.version = .{
.major = 0,
.minor = 1,
.patch = 3,
.patch = 4,
},
};

Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = .sdl3,
.version = "0.1.3",
.version = "0.1.4",
.minimum_zig_version = "0.15.1",
.dependencies = .{
.sdl = .{
Expand Down
50 changes: 50 additions & 0 deletions examples/custom-allocator.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const sdl3 = @import("sdl3");
const std = @import("std");

const fps = 60;
const screen_width = 640;
const screen_height = 480;

pub fn main() !void {

// Setup custom allocator.
var debug_allocator = std.heap.DebugAllocator(.{}).init;
_ = try sdl3.setMemoryFunctionsByAllocator(debug_allocator.allocator());
defer if (debug_allocator.detectLeaks()) @panic("Memory leaks detected!");

// Shutdown SDL.
defer sdl3.shutdown();

// Initialize SDL with subsystems you need here.
const init_flags = sdl3.InitFlags{ .video = true };
try sdl3.init(init_flags);
defer sdl3.quit(init_flags);

// Initial window setup.
const window = try sdl3.video.Window.init("Hello SDL3", screen_width, screen_height, .{});
defer window.deinit();

// Useful for limiting the FPS and getting the delta time.
var fps_capper = sdl3.extras.FramerateCapper(f32){ .mode = .{ .limited = fps } };

var quit = false;
while (!quit) {

// Delay to limit the FPS, returned delta time not needed.
const dt = fps_capper.delay();
_ = dt;

// Update logic.
const surface = try window.getSurface();
try surface.fillRect(null, surface.mapRgb(128, 30, 255));
try window.updateSurface();

// Event logic.
while (sdl3.events.poll()) |event|
switch (event) {
.quit => quit = true,
.terminating => quit = true,
else => {},
};
}
}
70 changes: 36 additions & 34 deletions src/sdl3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1988,51 +1988,53 @@ pub fn stepUtf8(
/// Custom allocator to use for `setMemoryFunctionsByAllocator()`.
var custom_allocator: std.mem.Allocator = undefined;

const Allocation = struct {
// Special thanks to castholm for fixing up these allocator functions.
const Allocation = extern struct {
size: usize,
data: void align(@alignOf(std.c.max_align_t)),
};

fn allocationSize(request_size: usize) usize {
var size: usize = request_size;
while (size % @min(@sizeOf(@cImport(@cInclude("stddef.h")).max_align_t), @sizeOf(?*anyopaque) * 2) != 0) // TODO: Optimize this?
size += 1;
return size;
fn makeAllocation(size: usize, zero_init: bool) ?[*]u8 {
const bytes = custom_allocator.allocWithOptions(u8, @sizeOf(Allocation) + size, .of(Allocation), null) catch return null;
if (zero_init) @memset(bytes, 0);
const allocation: *Allocation = @ptrCast(@alignCast(bytes.ptr));
allocation.size = bytes.len;
const mem: [*]u8 = @ptrCast(&allocation.data);
return mem;
}

fn makeAllocation(total_size: usize, comptime memset: bool) ?[*]u8 {
const total_buf = custom_allocator.alloc(u8, allocationSize(total_size) + @sizeOf(Allocation)) catch return null;
if (memset)
@memset(total_buf, 0);
const allocation: *Allocation = @ptrCast(@alignCast(total_buf.ptr));
allocation.size = total_buf.len;
const data_ptr: [*]u8 = @ptrFromInt(@intFromPtr(total_buf.ptr) + @sizeOf(Allocation));
// std.debug.print("MAKE PTR: {p}, {d}\n", .{ data_ptr, allocation.size });
return data_ptr;
}

fn allocCalloc(num_members: usize, size: usize) ?[*]u8 {
return makeAllocation(num_members * size, true);
fn allocMalloc(size: usize) ?[*]u8 {
return makeAllocation(size, false);
}

fn allocFree(mem: [*]u8) void {
const allocation: *Allocation = @ptrFromInt(@intFromPtr(mem) - @sizeOf(Allocation));
// std.debug.print("CLEAR PTR: {p}, {d}\n", .{ raw_ptr, allocation.size });
custom_allocator.free(@as([*]u8, @ptrCast(allocation))[0..allocation.size]);
fn allocCalloc(nmemb: usize, size: usize) ?[*]u8 {
return makeAllocation(nmemb * size, true);
}

fn allocMalloc(size: usize) ?[*]u8 {
return makeAllocation(size, false);
fn allocRealloc(mem: ?[*]u8, size: usize) ?[*]u8 {
const old_mem = mem orelse return allocMalloc(size);
const old_data: *align(@alignOf(std.c.max_align_t)) void = @ptrCast(@alignCast(old_mem));
const old_allocation: *Allocation = @fieldParentPtr("data", old_data);
const old_bytes_ptr: [*]align(@alignOf(Allocation)) u8 = @ptrCast(old_allocation);
const old_bytes = old_bytes_ptr[0..old_allocation.size];
if (custom_allocator.remap(old_bytes, @sizeOf(Allocation) + size)) |new_bytes| {
const new_allocation: *Allocation = @ptrCast(@alignCast(new_bytes.ptr));
new_allocation.size = new_bytes.len;
const new_mem: [*]u8 = @ptrCast(&new_allocation.data);
return new_mem;
}
const new_mem = makeAllocation(size, false) orelse return null;
@memcpy(new_mem[0..size], old_mem[0..size]);
custom_allocator.free(old_bytes);
return new_mem;
}

fn allocRealloc(mem: ?[*]u8, size: usize) ?[*]u8 {
const raw_ptr = mem orelse return allocMalloc(size);
// const allocation: *Allocation = @alignCast(@fieldParentPtr("buf", @as(*void, @ptrCast(raw_ptr))));
allocFree(raw_ptr);
return allocMalloc(size);
// const total_buf = custom_allocator.realloc(@as([*]u8, @ptrCast(raw_ptr))[0..allocation.size], allocationSize(size) + @sizeOf(Allocation)) catch return null;
// allocation = @ptrCast(@alignCast(total_buf.ptr));
// allocation.size = total_buf.len;
// return &allocation.buf;
fn allocFree(mem: [*]u8) void {
const data: *align(@alignOf(std.c.max_align_t)) void = @ptrCast(@alignCast(mem));
const allocation: *Allocation = @fieldParentPtr("data", data);
const bytes_ptr: [*]align(@alignOf(Allocation)) u8 = @ptrCast(allocation);
const bytes = bytes_ptr[0..allocation.size];
custom_allocator.free(bytes);
}

/// Replace SDL's memory allocation functions to use with an allocator.
Expand Down