1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
// Bindings for [[ SDL2 ; https://wiki.libsdl.org/SDL2/FrontPage ]].
package sdl2
/*
Simple DirectMedia Layer
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
import "core:c"
import "base:intrinsics"
when ODIN_OS == .Windows {
@(ignore_duplicates)
foreign import lib "SDL2.lib"
} else {
@(ignore_duplicates)
foreign import lib "system:SDL2"
}
version :: struct {
major: u8, /**< major version */
minor: u8, /**< minor version */
patch: u8, /**< update version */
}
MAJOR_VERSION :: 2
MINOR_VERSION :: 0
PATCHLEVEL :: 16
VERSION :: proc "contextless" (ver: ^version) {
ver.major = MAJOR_VERSION
ver.minor = MINOR_VERSION
ver.patch = PATCHLEVEL
}
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
GetVersion :: proc(ver: ^version) ---
GetRevision :: proc() -> cstring ---
}
InitFlag :: enum u32 {
TIMER = 0x00,
AUDIO = 0x04,
VIDEO = 0x05,
JOYSTICK = 0x09,
HAPTIC = 0x0c,
GAMECONTROLLER = 0x0d,
EVENTS = 0x0e,
SENSOR = 0x0f,
NOPARACHUTE = 0x14,
}
InitFlags :: bit_set[InitFlag; u32]
INIT_TIMER :: InitFlags{.TIMER}
INIT_AUDIO :: InitFlags{.AUDIO}
INIT_VIDEO :: InitFlags{.VIDEO} /**< INIT_VIDEO implies INIT_EVENTS */
INIT_JOYSTICK :: InitFlags{.JOYSTICK} /**< INIT_JOYSTICK implies INIT_EVENTS */
INIT_HAPTIC :: InitFlags{.HAPTIC}
INIT_GAMECONTROLLER :: InitFlags{.GAMECONTROLLER} /**< INIT_GAMECONTROLLER implies INIT_JOYSTICK */
INIT_EVENTS :: InitFlags{.EVENTS}
INIT_SENSOR :: InitFlags{.SENSOR}
INIT_NOPARACHUTE :: InitFlags{.NOPARACHUTE} /**< compatibility; this flag is ignored. */
INIT_EVERYTHING :: InitFlags{.TIMER, .AUDIO, .VIDEO, .EVENTS, .JOYSTICK, .HAPTIC, .GAMECONTROLLER, .SENSOR}
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
Init :: proc(flags: InitFlags) -> c.int ---
InitSubSystem :: proc(flags: InitFlags) -> c.int ---
QuitSubSystem :: proc(flags: InitFlags) ---
WasInit :: proc(flags: InitFlags) -> InitFlags ---
Quit :: proc() ---
}
// Atomic
// NOTE: Prefer the intrinsics built into Odin 'package intrinsics'
SpinLock :: distinct c.int
atomic_t :: struct { value: c.int }
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
AtomicTryLock :: proc(lock: ^SpinLock) -> bool ---
AtomicLock :: proc(lock: ^SpinLock) ---
AtomicUnlock :: proc(lock: ^SpinLock) ---
MemoryBarrierReleaseFunction :: proc() ---
MemoryBarrierAcquireFunction :: proc() ---
AtomicCAS :: proc(a: ^atomic_t, oldval, newval: c.int) -> bool ---
AtomicSet :: proc(a: ^atomic_t, v: c.int) -> c.int ---
AtomicGet :: proc(a: ^atomic_t) -> c.int ---
AtomicAdd :: proc(a: ^atomic_t, v: c.int) -> c.int ---
AtomicCASPtr :: proc(a: ^rawptr, oldval, newval: rawptr) -> bool ---
AtomicSetPtr :: proc(a: ^rawptr, v: rawptr) -> rawptr ---
AtomicGetPtr :: proc(a: ^rawptr) -> rawptr ---
}
// Bits
MostSignificantBitIndex32 :: #force_inline proc "c" (x: u32) -> c.int {
return c.int(intrinsics.count_leading_zeros(x))
}
HasExactlyOneBitSet32 :: #force_inline proc "c" (x: u32) -> bool {
return intrinsics.count_ones(x) == 1
}
// Clipboard
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
SetClipboardText :: proc(text: cstring) -> c.int ---
GetClipboardText :: proc() -> cstring ---
HasClipboardText :: proc() -> bool ---
}
// Error
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
SetError :: proc(fmt: cstring, #c_vararg args: ..any) -> c.int ---
GetError :: proc() -> cstring ---
GetErrorMsg :: proc(errstr: [^]u8, maxlen: c.int) -> cstring ---
ClearError :: proc() ---
}
GetErrorString :: proc "c" () -> string {
return string(GetError())
}
GetErrorMsgString :: proc "c" (buf: []u8) -> string {
cstr := GetErrorMsg(raw_data(buf), c.int(len(buf)))
return string(cstr)
}
/**
* \name Internal error functions
*
* \internal
* Private error reporting function - used internally.
*/
OutOfMemory :: #force_inline proc "c" () -> c.int { return Error(.ENOMEM) }
Unsupported :: #force_inline proc "c" () -> c.int { return Error(.UNSUPPORTED) }
InvalidParamError :: #force_inline proc "c" (param: cstring) -> c.int { return SetError("Parameter '%s' is invalid", param) }
errorcode :: enum c.int {
ENOMEM,
EFREAD,
EFWRITE,
EFSEEK,
UNSUPPORTED,
LASTERROR,
}
/* SDL_Error() unconditionally returns -1. */
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
Error :: proc(code: errorcode) -> c.int ---
}
// Filesystem
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
GetBasePath :: proc() -> cstring ---
GetPrefPath :: proc(org, app: cstring) -> cstring ---
}
// loadso
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
LoadObject :: proc(sofile: cstring) -> rawptr ---
LoadFunction :: proc(handle: rawptr, name: cstring) -> rawptr ---
UnloadObject :: proc(handle: rawptr) ---
}
// locale
Locale :: struct {
language: cstring, /**< A language name, like "en" for English. */
country: cstring, /**< A country, like "US" for America. Can be NULL. */
}
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
GetPreferredLocales :: proc() -> [^]Locale ---
}
// misc
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
OpenURL :: proc(url: cstring) -> c.int ---
}
// platform
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
GetPlatform :: proc() -> cstring ---
}
// power
PowerState :: enum c.int {
UNKNOWN, /**< cannot determine power status */
ON_BATTERY, /**< Not plugged in, running on the battery */
NO_BATTERY, /**< Plugged in, no battery available */
CHARGING, /**< Plugged in, charging battery */
CHARGED, /**< Plugged in, battery charged */
}
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
GetPowerInfo :: proc(secs: ^c.int, pct: ^c.int) -> PowerState ---
}
// quit
QuitRequested :: #force_inline proc "c" () -> bool {
PumpEvents()
return bool(PeepEvents(nil, 0, .PEEKEVENT, .QUIT, .QUIT) > 0)
}
// sensor
Sensor :: struct {}
SensorID :: distinct i32
SensorType :: enum c.int {
INVALID = -1, /**< Returned for an invalid sensor */
UNKNOWN, /**< Unknown sensor type */
ACCEL, /**< Accelerometer */
GYRO, /**< Gyroscope */
}
STANDARD_GRAVITY :: 9.80665
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
LockSensors :: proc() ---
UnlockSensors :: proc() ---
NumSensors :: proc() -> c.int ---
SensorGetDeviceName :: proc(device_index: c.int) -> cstring ---
SensorGetDeviceType :: proc(device_index: c.int) -> SensorType ---
SensorGetDeviceNonPortableType :: proc(device_index: c.int) -> c.int ---
SensorGetDeviceInstanceID :: proc(device_index: c.int) -> SensorID ---
SensorOpen :: proc(device_index: c.int) -> ^Sensor ---
SensorFromInstanceID :: proc(instance_id: SensorID) -> ^Sensor ---
SensorGetName :: proc(sensor: ^Sensor) -> cstring ---
SensorGetType :: proc(sensor: ^Sensor) -> SensorType ---
SensorGetNonPortableType :: proc(sensor: ^Sensor) -> c.int ---
SensorGetInstanceID :: proc(sensor: ^Sensor) -> SensorID ---
SensorGetData :: proc(sensor: ^Sensor, data: [^]f32, num_values: c.int) -> c.int ---
SensorClose :: proc(sensor: ^Sensor) ---
SensorUpdate :: proc() ---
}
// shape
NONSHAPEABLE_WINDOW :: -1
INVALID_SHAPE_ARGUMENT :: -2
WINDOW_LACKS_SHAPE :: -3
WindowShapeModeEnum :: enum c.int {
/** \brief The default mode, a binarized alpha cutoff of 1. */
Default,
/** \brief A binarized alpha cutoff with a given integer value. */
BinarizeAlpha,
/** \brief A binarized alpha cutoff with a given integer value, but with the opposite comparison. */
ReverseBinarizeAlpha,
/** \brief A color key is applied. */
ColorKey,
}
SHAPEMODEALPHA :: #force_inline proc "c" (mode: WindowShapeModeEnum) -> bool {
return bool(mode == .Default || mode == .BinarizeAlpha || mode == .ReverseBinarizeAlpha)
}
WindowShapeParams :: struct #raw_union {
binarizationCutoff: u8,
colorKey: Color,
}
WindowShapeMode :: struct {
mode: WindowShapeModeEnum,
parameters: WindowShapeParams,
}
@(default_calling_convention="c", link_prefix="SDL_")
foreign lib {
CreateShapedWindow :: proc(title: cstring, x, y, w, h: c.uint, flags: WindowFlags) -> ^Window ---
IsShapedWindow :: proc(window: ^Window) -> bool ---
SetWindowShape :: proc(window: ^Window, shape: ^Surface, shape_mode: ^WindowShapeMode) -> c.int ---
GetShapedWindowMode :: proc(window: ^Window, shape_mode: ^WindowShapeMode) -> c.int ---
}
|