Skip to content

Commit 9125117

Browse files
Add SDL_init.inc
1 parent bb0b609 commit 9125117

File tree

1 file changed

+363
-0
lines changed

1 file changed

+363
-0
lines changed

units/SDL_init.inc

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
{
2+
This file is part of:
3+
4+
SDL3 for Pascal
5+
(https://github.com/PascalGameDevelopment/SDL3-for-Pascal)
6+
SPDX-License-Identifier: Zlib
7+
}
8+
9+
{*
10+
* # CategoryInit
11+
*
12+
* All SDL programs need to initialize the library before starting to work
13+
* with it.
14+
*
15+
* Almost everything can simply call SDL_Init() near startup, with a handful
16+
* of flags to specify subsystems to touch. These are here to make sure SDL
17+
* does not even attempt to touch low-level pieces of the operating system
18+
* that you don't intend to use. For example, you might be using SDL for video
19+
* and input but chose an
20+
external library for audio, and in this case you
21+
* would just need to leave off the `SDL_INIT_AUDIO` flag to make sure that
22+
*
23+
external library has complete control.
24+
*
25+
* Most apps, when terminating, should call SDL_Quit(). This will clean up
26+
* (nearly) everything that SDL might have allocated, and crucially, it'll
27+
* make sure that the display's resolution is back to what the user expects if
28+
* you had previously changed it for your game.
29+
*
30+
* SDL3 apps are strongly encouraged to call SDL_SetAppMetadata() at startup
31+
* to fill in details about the program. This is completely optional, but it
32+
* helps in small ways (we can provide an About dialog box for the macOS menu,
33+
* we can name the app in the system's audio mixer, etc). Those that want to
34+
* provide a _lot_ of information should look at the more-detailed
35+
* SDL_SetAppMetadataProperty().
36+
}
37+
38+
{*
39+
* Initialization flags for SDL_Init and/or SDL_InitSubSystem
40+
*
41+
* These are the flags which may be passed to SDL_Init(). You should specify
42+
* the subsystems which you will be using in your application.
43+
*
44+
* \since This datatype is available since SDL 3.1.3.
45+
*
46+
* \sa SDL_Init
47+
* \sa SDL_Quit
48+
* \sa SDL_InitSubSystem
49+
* \sa SDL_QuitSubSystem
50+
* \sa SDL_WasInit
51+
}
52+
type
53+
PPSDL_InitFlags = ^PSDL_InitFlags;
54+
PSDL_InitFlags = ^TSDL_InitFlags;
55+
TSDL_InitFlags = cuint32;
56+
57+
const
58+
SDL_INIT_AUDIO = TSDL_InitFlags($00000010); { `SDL_INIT_AUDIO` implies `SDL_INIT_EVENTS` }
59+
SDL_INIT_VIDEO = TSDL_InitFlags($00000020); { `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS` }
60+
SDL_INIT_JOYSTICK = TSDL_InitFlags($00000200); { `SDL_INIT_JOYSTICK` implies `SDL_INIT_EVENTS`, should be initialized on the same thread as SDL_INIT_VIDEO on Windows if you don't set SDL_HINT_JOYSTICK_THREAD }
61+
SDL_INIT_HAPTIC = TSDL_InitFlags($00001000);
62+
SDL_INIT_GAMEPAD = TSDL_InitFlags($00002000); { `SDL_INIT_GAMEPAD` implies `SDL_INIT_JOYSTICK` }
63+
SDL_INIT_EVENTS = TSDL_InitFlags($00004000);
64+
SDL_INIT_SENSOR = TSDL_InitFlags($00008000); { `SDL_INIT_SENSOR` implies `SDL_INIT_EVENTS` }
65+
SDL_INIT_CAMERA = TSDL_InitFlags($00010000); { `SDL_INIT_CAMERA` implies `SDL_INIT_EVENTS` }
66+
67+
{*
68+
* Return values for optional main callbacks.
69+
*
70+
* Returning SDL_APP_SUCCESS or SDL_APP_FAILURE from SDL_AppInit,
71+
* SDL_AppEvent, or SDL_AppIterate will terminate the program and report
72+
* success/failure to the operating system. What that means is
73+
* platform-dependent. On Unix, for example, on success, the process error
74+
* code will be zero, and on failure it will be 1. This interface doesn't
75+
* allow you to return specific exit codes, just whether there was an error
76+
* generally or not.
77+
*
78+
* Returning SDL_APP_CONTINUE from these functions will let the app continue
79+
* to run.
80+
*
81+
* See
82+
* [Main callbacks in SDL3](https://wiki.libsdl.org/SDL3/README/main-functions#main-callbacks-in-sdl3)
83+
* for complete details.
84+
*
85+
* \since This enum is available since SDL 3.1.3.
86+
}
87+
type
88+
PPSDL_AppResult = ^PSDL_AppResult;
89+
PSDL_AppResult = ^TSDL_AppResult;
90+
TSDL_AppResult = Integer;
91+
const
92+
SDL_APP_CONTINUE = 0; {*< Value that requests that the app continue from the main callbacks. }
93+
SDL_APP_SUCCESS = 1; {*< Value that requests termination with success from the main callbacks. }
94+
SDL_APP_FAILURE = 2; {*< Value that requests termination with error from the main callbacks. }
95+
96+
type
97+
TSDL_AppInit_func = function(appstate: PPointer; argc: cint; argv: PPAnsiChar): TSDL_AppResult; cdecl;
98+
TSDL_AppIterate_func = function(appstate: Pointer): TSDL_AppResult; cdecl;
99+
//TSDL_AppEvent_func = function(appstate: Pointer; event: PSDL_Event): TSDL_AppResult; cdecl; // uncomment after EVENT translation
100+
TSDL_AppQuit_func = procedure(appstate: Pointer; result: TSDL_AppResult); cdecl;
101+
102+
{*
103+
* Initialize the SDL library.
104+
*
105+
* SDL_Init() simply forwards to calling SDL_InitSubSystem(). Therefore, the
106+
* two may be used interchangeably. Though for readability of your code
107+
* SDL_InitSubSystem() might be preferred.
108+
*
109+
* The file I/O (for example: SDL_IOFromFile) and threading (SDL_CreateThread)
110+
* subsystems are initialized by default. Message boxes
111+
* (SDL_ShowSimpleMessageBox) also attempt to work without initializing the
112+
* video subsystem, in hopes of being useful in showing an error dialog when
113+
* SDL_Init fails. You must specifically initialize other subsystems if you
114+
* use them in your application.
115+
*
116+
* Logging (such as SDL_Log) works without initialization, too.
117+
*
118+
* `flags` may be any of the following OR'd together:
119+
*
120+
* - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
121+
* subsystem
122+
* - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
123+
* subsystem
124+
* - `SDL_INIT_JOYSTICK`: joystick subsystem; automatically initializes the
125+
* events subsystem
126+
* - `SDL_INIT_HAPTIC`: haptic (force feedback) subsystem
127+
* - `SDL_INIT_GAMEPAD`: gamepad subsystem; automatically initializes the
128+
* joystick subsystem
129+
* - `SDL_INIT_EVENTS`: events subsystem
130+
* - `SDL_INIT_SENSOR`: sensor subsystem; automatically initializes the events
131+
* subsystem
132+
* - `SDL_INIT_CAMERA`: camera subsystem; automatically initializes the events
133+
* subsystem
134+
*
135+
* Subsystem initialization is ref-counted, you must call SDL_QuitSubSystem()
136+
* for each SDL_InitSubSystem() to correctly shutdown a subsystem manually (or
137+
* call SDL_Quit() to force shutdown). If a subsystem is already loaded then
138+
* this call will increase the ref-count and return.
139+
*
140+
* Consider reporting some basic metadata about your application before
141+
* calling SDL_Init, using either SDL_SetAppMetadata() or
142+
* SDL_SetAppMetadataProperty().
143+
*
144+
* \param flags subsystem initialization flags.
145+
* \returns true on success or false on failure; call SDL_GetError() for more
146+
* information.
147+
*
148+
* \since This function is available since SDL 3.1.3.
149+
*
150+
* \sa SDL_SetAppMetadata
151+
* \sa SDL_SetAppMetadataProperty
152+
* \sa SDL_InitSubSystem
153+
* \sa SDL_Quit
154+
* \sa SDL_SetMainReady
155+
* \sa SDL_WasInit
156+
}
157+
158+
function SDL_Init(flags: TSDL_InitFlags): cbool; cdecl;
159+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Init' {$ENDIF} {$ENDIF};
160+
161+
{*
162+
* Compatibility function to initialize the SDL library.
163+
*
164+
* This function and SDL_Init() are interchangeable.
165+
*
166+
* \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
167+
* \returns true on success or false on failure; call SDL_GetError() for more
168+
* information.
169+
*
170+
* \since This function is available since SDL 3.1.3.
171+
*
172+
* \sa SDL_Init
173+
* \sa SDL_Quit
174+
* \sa SDL_QuitSubSystem
175+
}
176+
function SDL_InitSubSystem(flags: TSDL_InitFlags): cbool; cdecl;
177+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_InitSubSystem' {$ENDIF} {$ENDIF};
178+
179+
{*
180+
* Shut down specific SDL subsystems.
181+
*
182+
* You still need to call SDL_Quit() even if you close all open subsystems
183+
* with SDL_QuitSubSystem().
184+
*
185+
* \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
186+
*
187+
* \since This function is available since SDL 3.1.3.
188+
*
189+
* \sa SDL_InitSubSystem
190+
* \sa SDL_Quit
191+
}
192+
procedure SDL_QuitSubSystem(flags: TSDL_InitFlags); cdecl;
193+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_QuitSubSystem' {$ENDIF} {$ENDIF};
194+
195+
{*
196+
* Get a mask of the specified subsystems which are currently initialized.
197+
*
198+
* \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
199+
* \returns a mask of all initialized subsystems if `flags` is 0, otherwise it
200+
* returns the initialization status of the specified subsystems.
201+
*
202+
* \since This function is available since SDL 3.1.3.
203+
*
204+
* \sa SDL_Init
205+
* \sa SDL_InitSubSystem
206+
}
207+
function SDL_WasInit(flags: TSDL_InitFlags): TSDL_InitFlags; cdecl;
208+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_WasInit' {$ENDIF} {$ENDIF};
209+
210+
{*
211+
* Clean up all initialized subsystems.
212+
*
213+
* You should call this function even if you have already shutdown each
214+
* initialized subsystem with SDL_QuitSubSystem(). It is safe to call this
215+
* function even in the case of errors in initialization.
216+
*
217+
* You can use this function with atexit() to ensure that it is run when your
218+
* application is shutdown, but it is not wise to do this from a library or
219+
* other dynamically loaded code.
220+
*
221+
* \since This function is available since SDL 3.1.3.
222+
*
223+
* \sa SDL_Init
224+
* \sa SDL_QuitSubSystem
225+
}
226+
procedure SDL_Quit; cdecl;
227+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Quit' {$ENDIF} {$ENDIF};
228+
229+
{*
230+
* Specify basic metadata about your app.
231+
*
232+
* You can optionally provide metadata about your app to SDL. This is not
233+
* required, but strongly encouraged.
234+
*
235+
* There are several locations where SDL can make use of metadata (an "About"
236+
* box in the macOS menu bar, the name of the app can be shown on some audio
237+
* mixers, etc). Any piece of metadata can be left as nil, if a specific
238+
* detail doesn't make sense for the app.
239+
*
240+
* This function should be called as early as possible, before SDL_Init.
241+
* Multiple calls to this function are allowed, but various state might not
242+
* change once it has been set up with a previous call to this function.
243+
*
244+
* Passing a nil removes any previous metadata.
245+
*
246+
* This is a simplified interface for the most important information. You can
247+
* supply significantly more detailed metadata with
248+
* SDL_SetAppMetadataProperty().
249+
*
250+
* \param appname The name of the application ("My Game 2: Bad Guy's
251+
* Revenge!").
252+
* \param appversion The version of the application ("1.0.0beta5" or a git
253+
* hash, or whatever makes sense).
254+
* \param appidentifier A unique string in reverse-domain format that
255+
* identifies this app ("com.example.mygame2").
256+
* \returns true on success or false on failure; call SDL_GetError() for more
257+
* information.
258+
*
259+
* \threadsafety It is safe to call this function from any thread.
260+
*
261+
* \since This function is available since SDL 3.1.3.
262+
*
263+
* \sa SDL_SetAppMetadataProperty
264+
}
265+
function SDL_SetAppMetadata(appname: PAnsiChar; appversion: PAnsiChar; appidentifier: PAnsiChar): cbool; cdecl;
266+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetAppMetadata' {$ENDIF} {$ENDIF};
267+
268+
{*
269+
* Specify metadata about your app through a set of properties.
270+
*
271+
* You can optionally provide metadata about your app to SDL. This is not
272+
* required, but strongly encouraged.
273+
*
274+
* There are several locations where SDL can make use of metadata (an "About"
275+
* box in the macOS menu bar, the name of the app can be shown on some audio
276+
* mixers, etc). Any piece of metadata can be left out, if a specific detail
277+
* doesn't make sense for the app.
278+
*
279+
* This function should be called as early as possible, before SDL_Init.
280+
* Multiple calls to this function are allowed, but various state might not
281+
* change once it has been set up with a previous call to this function.
282+
*
283+
* Once set, this metadata can be read using SDL_GetMetadataProperty().
284+
*
285+
* These are the supported properties:
286+
*
287+
* - `SDL_PROP_APP_METADATA_NAME_STRING`: The human-readable name of the
288+
* application, like "My Game 2: Bad Guy's Revenge!". This will show up
289+
* anywhere the OS shows the name of the application separately from window
290+
* titles, such as volume control applets, etc. This defaults to "SDL
291+
* Application".
292+
* - `SDL_PROP_APP_METADATA_VERSION_STRING`: The version of the app that is
293+
* running; there are no rules on format, so "1.0.3beta2" and "April 22nd,
294+
* 2024" and a git hash are all valid options. This has no default.
295+
* - `SDL_PROP_APP_METADATA_IDENTIFIER_STRING`: A unique string that
296+
* identifies this app. This must be in reverse-domain format, like
297+
* "com.example.mygame2". This string is used by desktop compositors to
298+
* identify and group windows together, as well as match applications with
299+
* associated desktop settings and icons. If you plan to package your
300+
* application in a container such as Flatpak, the app ID should match the
301+
* name of your Flatpak container as well. This has no default.
302+
* - `SDL_PROP_APP_METADATA_CREATOR_STRING`: The human-readable name of the
303+
* creator/developer/maker of this app, like "MojoWorkshop, LLC"
304+
* - `SDL_PROP_APP_METADATA_COPYRIGHT_STRING`: The human-readable copyright
305+
* notice, like "Copyright (c) 2024 MojoWorkshop, LLC" or whatnot. Keep this
306+
* to one line, don't paste a copy of a whole software license in here. This
307+
* has no default.
308+
* - `SDL_PROP_APP_METADATA_URL_STRING`: A URL to the app on the web. Maybe a
309+
* product page, or a storefront, or even a GitHub repository, for user's
310+
* further information This has no default.
311+
* - `SDL_PROP_APP_METADATA_TYPE_STRING`: The type of application this is.
312+
* Currently this string can be "game" for a video game, "mediaplayer" for a
313+
* media player, or generically "application" if nothing else applies.
314+
* Future versions of SDL might add new types. This defaults to
315+
* "application".
316+
*
317+
* \param name the name of the metadata property to set.
318+
* \param value the value of the property, or nil to remove that property.
319+
* \returns true on success or false on failure; call SDL_GetError() for more
320+
* information.
321+
*
322+
* \threadsafety It is safe to call this function from any thread.
323+
*
324+
* \since This function is available since SDL 3.1.3.
325+
*
326+
* \sa SDL_GetAppMetadataProperty
327+
* \sa SDL_SetAppMetadata
328+
}
329+
function SDL_SetAppMetadataProperty(name: PAnsiChar; value: PAnsiChar): cbool; cdecl;
330+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetAppMetadataProperty' {$ENDIF} {$ENDIF};
331+
332+
const
333+
SDL_PROP_APP_METADATA_NAME_STRING = 'SDL.app.metadata.name';
334+
SDL_PROP_APP_METADATA_VERSION_STRING = 'SDL.app.metadata.version';
335+
SDL_PROP_APP_METADATA_IDENTIFIER_STRING = 'SDL.app.metadata.identifier';
336+
SDL_PROP_APP_METADATA_CREATOR_STRING = 'SDL.app.metadata.creator';
337+
SDL_PROP_APP_METADATA_COPYRIGHT_STRING = 'SDL.app.metadata.copyright';
338+
SDL_PROP_APP_METADATA_URL_STRING = 'SDL.app.metadata.url';
339+
SDL_PROP_APP_METADATA_TYPE_STRING = 'SDL.app.metadata.type';
340+
341+
{*
342+
* Get metadata about your app.
343+
*
344+
* This returns metadata previously set using SDL_SetAppMetadata() or
345+
* SDL_SetAppMetadataProperty(). See SDL_SetAppMetadataProperty() for the list
346+
* of available properties and their meanings.
347+
*
348+
* \param name the name of the metadata property to get.
349+
* \returns the current value of the metadata property, or the default if it
350+
* is not set, nil for properties with no default.
351+
*
352+
* \threadsafety It is safe to call this function from any thread, although
353+
* the string returned is not protected and could potentially be
354+
* freed if you call SDL_SetAppMetadataProperty() to set that
355+
* property from another thread.
356+
*
357+
* \since This function is available since SDL 3.1.3.
358+
*
359+
* \sa SDL_SetAppMetadata
360+
* \sa SDL_SetAppMetadataProperty
361+
}
362+
function SDL_GetAppMetadataProperty(name: PAnsiChar): PAnsiChar; cdecl;
363+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetAppMetadataProperty' {$ENDIF} {$ENDIF};

0 commit comments

Comments
 (0)