Skip to content

Commit 8c6617c

Browse files
committed
sdl3.zig consistent string types and coersions
1 parent 73f14bb commit 8c6617c

File tree

1 file changed

+76
-48
lines changed

1 file changed

+76
-48
lines changed

src/sdl3.zig

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,14 @@ pub fn getPointerProperty(props: PropertiesID, name: [:0]const u8, default_value
223223
}
224224
extern fn SDL_GetPointerProperty(props: PropertiesID, name: [*c]const u8, default_value: ?*anyopaque) ?*anyopaque;
225225

226-
pub fn getStringProperty(props: PropertiesID, name: [:0]const u8, default_value: ?[*:0]const u8) ?[*:0]const u8 {
227-
return @ptrCast(SDL_GetStringProperty(props, @ptrCast(name.ptr), default_value));
226+
pub fn getStringProperty(props: PropertiesID, name: [:0]const u8, default_value: ?[:0]const u8) ?[*:0]const u8 {
227+
return @ptrCast(SDL_GetStringProperty(
228+
props,
229+
@ptrCast(name.ptr),
230+
if (default_value) |default_str| @ptrCast(default_str.ptr) else null,
231+
));
228232
}
229-
extern fn SDL_GetStringProperty(props: PropertiesID, name: [*c]const u8, default_value: ?[*:0]const u8) [*c]const u8;
233+
extern fn SDL_GetStringProperty(props: PropertiesID, name: [*c]const u8, default_value: [*c]const u8) [*c]const u8;
230234

231235
pub fn getNumberProperty(props: PropertiesID, name: [:0]const u8, default_value: i64) i64 {
232236
return SDL_GetNumberProperty(props, @ptrCast(name.ptr), default_value);
@@ -348,7 +352,7 @@ extern fn SDL_ResetLogPriorities() void;
348352
pub fn setLogPriorityPrefix(priority: LogPriority, prefix: ?[:0]const u8) Error!void {
349353
if (!SDL_SetLogPriorityPrefix(
350354
priority,
351-
if (prefix) |p| @as([*c]const u8, @ptrCast(p.ptr)) else null,
355+
if (prefix) |p| @ptrCast(p.ptr) else null,
352356
)) {
353357
return makeError();
354358
}
@@ -369,9 +373,9 @@ pub fn log(comptime fmt: []const u8, args: anytype) void {
369373
return;
370374
},
371375
};
372-
SDL_Log(message.ptr);
376+
SDL_Log(@ptrCast(message.ptr));
373377
}
374-
extern fn SDL_Log(fmt: [*:0]const u8, ...) void;
378+
extern fn SDL_Log(fmt: [*c]const u8, ...) void;
375379

376380
/// Log a message with SDL_LOG_PRIORITY_TRACE.
377381
pub fn logTrace(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -387,9 +391,9 @@ pub fn logTrace(category: LogCategory, comptime fmt: []const u8, args: anytype)
387391
return;
388392
},
389393
};
390-
SDL_LogTrace(@intFromEnum(category), message.ptr);
394+
SDL_LogTrace(@intFromEnum(category), @ptrCast(message.ptr));
391395
}
392-
extern fn SDL_LogTrace(category: c_int, fmt: [*:0]const u8, ...) void;
396+
extern fn SDL_LogTrace(category: c_int, fmt: [*c]const u8, ...) void;
393397

394398
/// Log a message with SDL_LOG_PRIORITY_VERBOSE.
395399
pub fn logVerbose(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -405,9 +409,9 @@ pub fn logVerbose(category: LogCategory, comptime fmt: []const u8, args: anytype
405409
return;
406410
},
407411
};
408-
SDL_LogVerbose(@intFromEnum(category), message.ptr);
412+
SDL_LogVerbose(@intFromEnum(category), @ptrCast(message.ptr));
409413
}
410-
extern fn SDL_LogVerbose(category: c_int, fmt: [*:0]const u8, ...) void;
414+
extern fn SDL_LogVerbose(category: c_int, fmt: [*c]const u8, ...) void;
411415

412416
/// Log a message with SDL_LOG_PRIORITY_DEBUG.
413417
pub fn logDebug(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -423,9 +427,9 @@ pub fn logDebug(category: LogCategory, comptime fmt: []const u8, args: anytype)
423427
return;
424428
},
425429
};
426-
SDL_LogDebug(@intFromEnum(category), message.ptr);
430+
SDL_LogDebug(@intFromEnum(category), @ptrCast(message.ptr));
427431
}
428-
extern fn SDL_LogDebug(category: c_int, fmt: [*:0]const u8, ...) void;
432+
extern fn SDL_LogDebug(category: c_int, fmt: [*c]const u8, ...) void;
429433

430434
/// Log a message with SDL_LOG_PRIORITY_INFO.
431435
pub fn logInfo(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -441,9 +445,9 @@ pub fn logInfo(category: LogCategory, comptime fmt: []const u8, args: anytype) v
441445
return;
442446
},
443447
};
444-
SDL_LogInfo(@intFromEnum(category), message.ptr);
448+
SDL_LogInfo(@intFromEnum(category), @ptrCast(message.ptr));
445449
}
446-
extern fn SDL_LogInfo(category: c_int, fmt: [*:0]const u8, ...) void;
450+
extern fn SDL_LogInfo(category: c_int, fmt: [*c]const u8, ...) void;
447451

448452
/// Log a message with SDL_LOG_PRIORITY_WARN.
449453
pub fn logWarn(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -459,9 +463,9 @@ pub fn logWarn(category: LogCategory, comptime fmt: []const u8, args: anytype) v
459463
return;
460464
},
461465
};
462-
SDL_LogWarn(@intFromEnum(category), message.ptr);
466+
SDL_LogWarn(@intFromEnum(category), @ptrCast(message.ptr));
463467
}
464-
extern fn SDL_LogWarn(category: c_int, fmt: [*:0]const u8, ...) void;
468+
extern fn SDL_LogWarn(category: c_int, fmt: [*c]const u8, ...) void;
465469

466470
/// Log a message with SDL_LOG_PRIORITY_ERROR.
467471
pub fn logError(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -477,9 +481,9 @@ pub fn logError(category: LogCategory, comptime fmt: []const u8, args: anytype)
477481
return;
478482
},
479483
};
480-
SDL_LogError(@intFromEnum(category), message.ptr);
484+
SDL_LogError(@intFromEnum(category), @ptrCast(message.ptr));
481485
}
482-
extern fn SDL_LogError(category: c_int, fmt: [*:0]const u8, ...) void;
486+
extern fn SDL_LogError(category: c_int, fmt: [*c]const u8, ...) void;
483487

484488
/// Log a message with SDL_LOG_PRIORITY_CRITICAL.
485489
pub fn logCritical(category: LogCategory, comptime fmt: []const u8, args: anytype) void {
@@ -495,9 +499,9 @@ pub fn logCritical(category: LogCategory, comptime fmt: []const u8, args: anytyp
495499
return;
496500
},
497501
};
498-
SDL_LogCritical(@intFromEnum(category), message.ptr);
502+
SDL_LogCritical(@intFromEnum(category), @ptrCast(message.ptr));
499503
}
500-
extern fn SDL_LogCritical(category: c_int, fmt: [*:0]const u8, ...) void;
504+
extern fn SDL_LogCritical(category: c_int, fmt: [*c]const u8, ...) void;
501505

502506
/// Log a message with the specified category and priority.
503507
pub fn logMessage(category: LogCategory, priority: LogPriority, comptime fmt: []const u8, args: anytype) void {
@@ -513,9 +517,9 @@ pub fn logMessage(category: LogCategory, priority: LogPriority, comptime fmt: []
513517
return;
514518
},
515519
};
516-
SDL_LogMessage(@intFromEnum(category), priority, message.ptr);
520+
SDL_LogMessage(@intFromEnum(category), priority, @ptrCast(message.ptr));
517521
}
518-
extern fn SDL_LogMessage(category: c_int, priority: LogPriority, fmt: [*:0]const u8, ...) void;
522+
extern fn SDL_LogMessage(category: c_int, priority: LogPriority, fmt: [*c]const u8, ...) void;
519523

520524
/// The prototype for the log output callback function.
521525
///
@@ -526,7 +530,7 @@ pub const LogOutputFunction = *const fn (
526530
userdata: ?*anyopaque,
527531
category: c_int,
528532
priority: LogPriority,
529-
message: [*:0]const u8,
533+
message: [*c]const u8,
530534
) callconv(.c) void;
531535

532536
/// Get the default log output function.
@@ -645,12 +649,12 @@ pub fn posCenteredDisplay(x: i32) i32 {
645649
const pos_undefined_mask: i32 = 0x1fff_0000;
646650
const pos_centered_mask: i32 = 0x2fff_0000;
647651

648-
pub fn createWindow(title: ?[*:0]const u8, w: c_int, h: c_int, flags: Window.Flags) Error!*Window {
652+
pub fn createWindow(title: [:0]const u8, w: c_int, h: c_int, flags: Window.Flags) Error!*Window {
649653
assert(w > 0);
650654
assert(h > 0);
651-
return SDL_CreateWindow(title, w, h, flags) orelse return makeError();
655+
return SDL_CreateWindow(@ptrCast(title.ptr), w, h, flags) orelse return makeError();
652656
}
653-
extern fn SDL_CreateWindow(title: ?[*:0]const u8, w: c_int, h: c_int, flags: Window.Flags) ?*Window;
657+
extern fn SDL_CreateWindow(title: [*c]const u8, w: c_int, h: c_int, flags: Window.Flags) ?*Window;
654658

655659
pub const destroyWindow = SDL_DestroyWindow;
656660
extern fn SDL_DestroyWindow(window: *Window) void;
@@ -772,8 +776,8 @@ pub const gl = struct {
772776
}
773777
extern fn SDL_GL_SwapWindow(window: *Window) bool;
774778

775-
pub fn getProcAddress(proc: [*:0]const u8) callconv(.c) FunctionPointer {
776-
return SDL_GL_GetProcAddress(proc);
779+
pub fn getProcAddress(proc: [:0]const u8) FunctionPointer {
780+
return SDL_GL_GetProcAddress(@ptrCast(proc.ptr));
777781
}
778782
extern fn SDL_GL_GetProcAddress(proc: [*c]const u8) callconv(.c) FunctionPointer;
779783

@@ -888,12 +892,17 @@ extern fn SDL_GetNumRenderDrivers() c_int;
888892
/// The names of drivers are all simple, low-ASCII identifiers, like "opengl",
889893
/// "direct3d12" or "metal". These never have Unicode characters, and are not
890894
/// meant to be proper names.
891-
pub const getRenderDriver = SDL_GetRenderDriver;
892-
extern fn SDL_GetRenderDriver(index: c_int) ?[*:0]const u8;
895+
pub fn getRenderDriver(index: c_int) ?[:0]const u8 {
896+
if (SDL_GetRenderDriver(index)) |ptr| {
897+
return std.mem.span(ptr);
898+
}
899+
return null;
900+
}
901+
extern fn SDL_GetRenderDriver(index: c_int) [*c]const u8;
893902

894903
/// Create a window and default renderer.
895904
pub fn createWindowAndRenderer(
896-
window_title: ?[*:0]const u8,
905+
window_title: [:0]const u8,
897906
width: c_int,
898907
height: c_int,
899908
window_flags: Window.Flags,
@@ -903,7 +912,7 @@ pub fn createWindowAndRenderer(
903912
assert(width > 0);
904913
assert(height > 0);
905914
if (!SDL_CreateWindowAndRenderer(
906-
window_title,
915+
@ptrCast(window_title.ptr),
907916
width,
908917
height,
909918
window_flags,
@@ -914,7 +923,7 @@ pub fn createWindowAndRenderer(
914923
}
915924
}
916925
extern fn SDL_CreateWindowAndRenderer(
917-
title: ?[*:0]const u8,
926+
title: [*c]const u8,
918927
width: c_int,
919928
height: c_int,
920929
window_flags: Window.Flags,
@@ -1220,10 +1229,10 @@ extern fn SDL_RenderReadPixels(
12201229
pitch: c_int,
12211230
) bool;
12221231

1223-
pub fn renderDebugText(renderer: *Renderer, x: f32, y: f32, str: [*:0]const u8) Error!void {
1224-
if (!SDL_RenderDebugText(renderer, x, y, str)) return makeError();
1232+
pub fn renderDebugText(renderer: *Renderer, x: f32, y: f32, str: [:0]const u8) Error!void {
1233+
if (!SDL_RenderDebugText(renderer, x, y, @ptrCast(str.ptr))) return makeError();
12251234
}
1226-
extern fn SDL_RenderDebugText(renderer: *Renderer, x: f32, y: f32, str: [*:0]const u8) bool;
1235+
extern fn SDL_RenderDebugText(renderer: *Renderer, x: f32, y: f32, str: [*c]const u8) bool;
12271236

12281237
//--------------------------------------------------------------------------------------------------
12291238
//
@@ -1595,8 +1604,8 @@ pub const vk = struct {
15951604
pub const FunctionPointer = ?*const anyopaque;
15961605
pub const Instance = enum(usize) { null_handle = 0, _ };
15971606

1598-
pub fn loadLibrary(path: ?[*:0]const u8) Error!void {
1599-
if (!SDL_Vulkan_LoadLibrary(@ptrCast(path))) return makeError();
1607+
pub fn loadLibrary(path: [:0]const u8) Error!void {
1608+
if (!SDL_Vulkan_LoadLibrary(@ptrCast(path.ptr))) return makeError();
16001609
}
16011610
extern fn SDL_Vulkan_LoadLibrary(path: [*c]const u8) bool;
16021611

@@ -1852,7 +1861,7 @@ pub const TextEditingEvent = extern struct {
18521861
reserved: u32,
18531862
timestamp: u64,
18541863
window_id: WindowId,
1855-
text: ?[*:0]const u8,
1864+
text: [*c]const u8,
18561865
start: i32,
18571866
length: i32,
18581867
};
@@ -1862,7 +1871,7 @@ pub const TextEditingCandidatesEvent = extern struct {
18621871
reserved: u32,
18631872
timestamp: u64,
18641873
window_id: WindowId,
1865-
candidates: *const [*:0]const u8,
1874+
candidates: *const [*c]const u8,
18661875
num_candidates: i32,
18671876
selected_candidate: i32,
18681877
horizontal: bool,
@@ -1877,7 +1886,7 @@ pub const TextInputEvent = extern struct {
18771886
reserved: u32,
18781887
timestamp: u64,
18791888
window_id: WindowId,
1880-
text: ?[*:0]const u8,
1889+
text: [*c]const u8,
18811890
};
18821891

18831892
pub const MouseDeviceEvent = extern struct {
@@ -2149,11 +2158,18 @@ pub const DropEvent = extern struct {
21492158
window_id: WindowId,
21502159
x: f32,
21512160
y: f32,
2152-
source: ?[*:0]const u8,
2153-
data: ?[*:0]const u8,
2161+
source: [*c]const u8,
2162+
data: [*c]const u8,
21542163
};
21552164

2156-
pub const ClipboardEvent = extern struct { type: EventType, reserved: u32, timestamp: u64, owner: bool, num_mime_types: i32, mime_types: ?*[*:0]const u8 };
2165+
pub const ClipboardEvent = extern struct {
2166+
type: EventType,
2167+
reserved: u32,
2168+
timestamp: u64,
2169+
owner: bool,
2170+
num_mime_types: i32,
2171+
mime_types: [*c][*c]const u8,
2172+
};
21572173

21582174
pub const SensorEvent = extern struct {
21592175
type: EventType,
@@ -3450,12 +3466,24 @@ pub const getNumAudioDrivers = SDL_GetNumAudioDrivers;
34503466
extern fn SDL_GetNumAudioDrivers() c_int;
34513467

34523468
/// Use this function to get the name of a built in audio driver.
3453-
pub const getAudioDriver = SDL_GetAudioDriver;
3454-
extern fn SDL_GetAudioDriver(index: c_int) [*:0]const u8;
3469+
pub fn getAudioDriver(index: c_int) ?[:0]const u8 {
3470+
if (SDL_GetAudioDriver(index)) |name| {
3471+
return std.mem.span(name);
3472+
} else {
3473+
return null;
3474+
}
3475+
}
3476+
extern fn SDL_GetAudioDriver(index: c_int) [*c]const u8;
34553477

34563478
/// Get the name of the current audio driver.
3457-
pub const getCurrentAudioDriver = SDL_GetCurrentAudioDriver;
3458-
extern fn SDL_GetCurrentAudioDriver() [*:0]const u8;
3479+
pub fn getCurrentAudioDriver() ?[:0]const u8 {
3480+
if (SDL_GetCurrentAudioDriver()) |name| {
3481+
return std.mem.span(name);
3482+
} else {
3483+
return null;
3484+
}
3485+
}
3486+
extern fn SDL_GetCurrentAudioDriver() [*c]const u8;
34593487

34603488
/// Get a list of currently-connected audio playback devices.
34613489
///

0 commit comments

Comments
 (0)