Skip to content

Commit 34f81fb

Browse files
committed
sdl3.zig implement Object Properties API bindings
1 parent 7e12e31 commit 34f81fb

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

src/sdl3.zig

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,132 @@ extern fn SDL_SetAppMetadata(appname: [*c]const u8, appversion: [*c]const u8, ap
129129
// Object Properties (SDL_properties.h)
130130
//
131131
//--------------------------------------------------------------------------------------------------
132-
// TODO
132+
pub const PropertiesID = u32;
133+
134+
pub const PropertyType = enum(c_int) {
135+
invalid = 0,
136+
pointer = 1,
137+
string = 2,
138+
number = 3,
139+
float = 4,
140+
boolean = 5,
141+
};
142+
143+
pub const CleanupPropertyCallback = *const fn (userdata: ?*anyopaque, value: ?*anyopaque) callconv(.C) void;
144+
145+
pub const EnumeratePropertiesCallback = *const fn (userdata: ?*anyopaque, props: PropertiesID, name: [*c]const u8) callconv(.C) void;
146+
147+
pub const getGlobalProperties = SDL_GetGlobalProperties;
148+
extern fn SDL_GetGlobalProperties() PropertiesID;
149+
150+
pub const createProperties = SDL_CreateProperties;
151+
extern fn SDL_CreateProperties() PropertiesID;
152+
153+
pub fn copyProperties(src: PropertiesID, dst: PropertiesID) Error!void {
154+
if (!SDL_CopyProperties(src, dst)) return makeError();
155+
}
156+
extern fn SDL_CopyProperties(src: PropertiesID, dst: PropertiesID) bool;
157+
158+
pub fn lockProperties(props: PropertiesID) Error!void {
159+
if (!SDL_LockProperties(props)) return makeError();
160+
}
161+
extern fn SDL_LockProperties(props: PropertiesID) bool;
162+
163+
pub const unlockProperties = SDL_UnlockProperties;
164+
extern fn SDL_UnlockProperties(props: PropertiesID) void;
165+
166+
pub fn setPointerPropertyWithCleanup(
167+
props: PropertiesID,
168+
name: [:0]const u8,
169+
value: ?*anyopaque,
170+
cleanup: ?CleanupPropertyCallback,
171+
userdata: ?*anyopaque,
172+
) Error!void {
173+
if (!SDL_SetPointerPropertyWithCleanup(props, @ptrCast(name.ptr), value, cleanup, userdata)) {
174+
return makeError();
175+
}
176+
}
177+
extern fn SDL_SetPointerPropertyWithCleanup(
178+
props: PropertiesID,
179+
name: [*c]const u8,
180+
value: ?*anyopaque,
181+
cleanup: ?CleanupPropertyCallback,
182+
userdata: ?*anyopaque,
183+
) bool;
184+
185+
pub fn setPointerProperty(props: PropertiesID, name: [:0]const u8, value: ?*anyopaque) Error!void {
186+
if (!SDL_SetPointerProperty(props, @ptrCast(name.ptr), value)) return makeError();
187+
}
188+
extern fn SDL_SetPointerProperty(props: PropertiesID, name: [*c]const u8, value: ?*anyopaque) bool;
189+
190+
pub fn setStringProperty(props: PropertiesID, name: [:0]const u8, value: ?[:0]const u8) Error!void {
191+
const val_ptr = if (value) |v| @as([*c]const u8, @ptrCast(v.ptr)) else null;
192+
if (!SDL_SetStringProperty(props, @ptrCast(name.ptr), val_ptr)) return makeError();
193+
}
194+
extern fn SDL_SetStringProperty(props: PropertiesID, name: [*c]const u8, value: [*c]const u8) bool;
195+
196+
pub fn setNumberProperty(props: PropertiesID, name: [:0]const u8, value: i64) Error!void {
197+
if (!SDL_SetNumberProperty(props, @ptrCast(name.ptr), value)) return makeError();
198+
}
199+
extern fn SDL_SetNumberProperty(props: PropertiesID, name: [*c]const u8, value: i64) bool;
200+
201+
pub fn setFloatProperty(props: PropertiesID, name: [:0]const u8, value: f32) Error!void {
202+
if (!SDL_SetFloatProperty(props, @ptrCast(name.ptr), value)) return makeError();
203+
}
204+
extern fn SDL_SetFloatProperty(props: PropertiesID, name: [*c]const u8, value: f32) bool;
205+
206+
pub fn setBooleanProperty(props: PropertiesID, name: [:0]const u8, value: bool) Error!void {
207+
if (!SDL_SetBooleanProperty(props, @ptrCast(name.ptr), value)) return makeError();
208+
}
209+
extern fn SDL_SetBooleanProperty(props: PropertiesID, name: [*c]const u8, value: bool) bool;
210+
211+
pub fn hasProperty(props: PropertiesID, name: [:0]const u8) bool {
212+
return SDL_HasProperty(props, @ptrCast(name.ptr));
213+
}
214+
extern fn SDL_HasProperty(props: PropertiesID, name: [*c]const u8) bool;
215+
216+
pub fn getPropertyType(props: PropertiesID, name: [:0]const u8) PropertyType {
217+
return SDL_GetPropertyType(props, @ptrCast(name.ptr));
218+
}
219+
extern fn SDL_GetPropertyType(props: PropertiesID, name: [*c]const u8) PropertyType;
220+
221+
pub fn getPointerProperty(props: PropertiesID, name: [:0]const u8, default_value: ?*anyopaque) ?*anyopaque {
222+
return SDL_GetPointerProperty(props, @ptrCast(name.ptr), default_value);
223+
}
224+
extern fn SDL_GetPointerProperty(props: PropertiesID, name: [*c]const u8, default_value: ?*anyopaque) ?*anyopaque;
225+
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));
228+
}
229+
extern fn SDL_GetStringProperty(props: PropertiesID, name: [*c]const u8, default_value: ?[*:0]const u8) [*c]const u8;
230+
231+
pub fn getNumberProperty(props: PropertiesID, name: [:0]const u8, default_value: i64) i64 {
232+
return SDL_GetNumberProperty(props, @ptrCast(name.ptr), default_value);
233+
}
234+
extern fn SDL_GetNumberProperty(props: PropertiesID, name: [*c]const u8, default_value: i64) i64;
235+
236+
pub fn getFloatProperty(props: PropertiesID, name: [:0]const u8, default_value: f32) f32 {
237+
return SDL_GetFloatProperty(props, @ptrCast(name.ptr), default_value);
238+
}
239+
extern fn SDL_GetFloatProperty(props: PropertiesID, name: [*c]const u8, default_value: f32) f32;
240+
241+
pub fn getBooleanProperty(props: PropertiesID, name: [:0]const u8, default_value: bool) bool {
242+
return SDL_GetBooleanProperty(props, @ptrCast(name.ptr), default_value);
243+
}
244+
extern fn SDL_GetBooleanProperty(props: PropertiesID, name: [*c]const u8, default_value: bool) bool;
245+
246+
pub fn clearProperty(props: PropertiesID, name: [:0]const u8) Error!void {
247+
if (!SDL_ClearProperty(props, @ptrCast(name.ptr))) return makeError();
248+
}
249+
extern fn SDL_ClearProperty(props: PropertiesID, name: [*c]const u8) bool;
250+
251+
pub fn enumerateProperties(props: PropertiesID, callback: EnumeratePropertiesCallback, userdata: ?*anyopaque) Error!void {
252+
if (!SDL_EnumerateProperties(props, callback, userdata)) return makeError();
253+
}
254+
extern fn SDL_EnumerateProperties(props: PropertiesID, callback: EnumeratePropertiesCallback, userdata: ?*anyopaque) bool;
255+
256+
pub const destroyProperties = SDL_DestroyProperties;
257+
extern fn SDL_DestroyProperties(props: PropertiesID) void;
133258

134259
//--------------------------------------------------------------------------------------------------
135260
//

0 commit comments

Comments
 (0)