| 1 | package SDL2::FFI 0.06 { | ||
| 2 | 3 | ▶ | use lib '../lib', 'lib'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | # ABSTRACT: FFI Wrapper for SDL (Simple DirectMedia Layer) Development Library | ||
| 5 | 3 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use experimental 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use base 'Exporter::Tiny'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use SDL2::Utils; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | our %EXPORT_TAGS; | ||
| 10 | CORE::state $SDL_ASSERT_LEVEL = 2; | ||
| 11 | sub SDL_ASSERT_LEVEL { $SDL_ASSERT_LEVEL //= 2; $SDL_ASSERT_LEVEL; } | ||
| 12 | |||
| 13 | sub _exporter_validate_opts { | ||
| 14 | my $class = shift; | ||
| 15 | my ($globals) = @_; | ||
| 16 | if ( $globals->{'assert=0'} ) { $SDL_ASSERT_LEVEL = 0 } | ||
| 17 | elsif ( $globals->{'assert=1'} ) { $SDL_ASSERT_LEVEL = 1 } | ||
| 18 | elsif ( $globals->{'assert=2'} ) { $SDL_ASSERT_LEVEL = 2 } | ||
| 19 | elsif ( $globals->{'assert=3'} ) { $SDL_ASSERT_LEVEL = 3 } | ||
| 20 | |||
| 21 | #...; # do stuff here | ||
| 22 | #use Data::Dump; | ||
| 23 | #ddx $globals; | ||
| 24 | $class->SUPER::_exporter_validate_opts(@_); | ||
| 25 | } | ||
| 26 | my $platform = $^O; # https://perldoc.perl.org/perlport#PLATFORMS | ||
| 27 | my $Windows = !!( $platform eq 'MSWin32' ); | ||
| 28 | # | ||
| 29 | use SDL2::stdinc; | ||
| 30 | use SDL2::assert; # Enable with use var like C<use SDL2 -assert=3;> | ||
| 31 | use SDL2::atomic; | ||
| 32 | use SDL2::rwops; # Does not belong here? | ||
| 33 | use SDL2::audio; | ||
| 34 | use SDL2::clipboard; | ||
| 35 | use SDL2::cpuinfo; | ||
| 36 | use SDL2::error; | ||
| 37 | use SDL2::events; | ||
| 38 | use SDL2::filesystem; | ||
| 39 | use SDL2::gamecontroller; | ||
| 40 | use SDL2::haptic; | ||
| 41 | use SDL2::hints; | ||
| 42 | use SDL2::joystick; | ||
| 43 | use SDL2::loadso; | ||
| 44 | use SDL2::log; | ||
| 45 | use SDL2::messagebox; | ||
| 46 | use SDL2::metal; | ||
| 47 | use SDL2::mutex; | ||
| 48 | use SDL2::power; | ||
| 49 | use SDL2::render; | ||
| 50 | use SDL2::rwops; | ||
| 51 | use SDL2::sensor; | ||
| 52 | use SDL2::shape; | ||
| 53 | use SDL2::system; | ||
| 54 | use SDL2::thread; | ||
| 55 | use SDL2::timer; | ||
| 56 | use SDL2::version; | ||
| 57 | use SDL2::video; | ||
| 58 | use SDL2::locale; | ||
| 59 | use SDL2::misc; | ||
| 60 | # | ||
| 61 | use SDL2::platform; # We bypass config.h to get platform.h | ||
| 62 | use SDL2::syswm; | ||
| 63 | # | ||
| 64 | define init => [ | ||
| 65 | [ SDL_INIT_TIMER => 0x00000001 ], | ||
| 66 | [ SDL_INIT_AUDIO => 0x00000010 ], | ||
| 67 | [ SDL_INIT_VIDEO => 0x00000020 ], | ||
| 68 | [ SDL_INIT_JOYSTICK => 0x00000200 ], | ||
| 69 | [ SDL_INIT_HAPTIC => 0x00001000 ], | ||
| 70 | [ SDL_INIT_GAMECONTROLLER => 0x00002000 ], | ||
| 71 | [ SDL_INIT_EVENTS => 0x00004000 ], | ||
| 72 | [ SDL_INIT_SENSOR => 0x00008000 ], | ||
| 73 | [ SDL_INIT_NOPARACHUTE => 0x00100000 ], | ||
| 74 | [ SDL_INIT_EVERYTHING => sub { | ||
| 75 | SDL_INIT_TIMER() | SDL_INIT_AUDIO() | SDL_INIT_VIDEO() | SDL_INIT_EVENTS() | ||
| 76 | | SDL_INIT_JOYSTICK() | SDL_INIT_HAPTIC() | SDL_INIT_GAMECONTROLLER() | ||
| 77 | | SDL_INIT_SENSOR(); | ||
| 78 | } | ||
| 79 | ] | ||
| 80 | ]; | ||
| 81 | # | ||
| 82 | use Data::Dump; | ||
| 83 | |||
| 84 | # https://github.com/libsdl-org/SDL/blob/main/include/SDL.h | ||
| 85 | push @{ $EXPORT_TAGS{default} }, qw[:init]; | ||
| 86 | attach | ||
| 87 | init => { | ||
| 88 | SDL_Init => [ ['uint32'] => 'int' ], | ||
| 89 | SDL_InitSubSystem => [ ['uint32'] => 'int' ], | ||
| 90 | SDL_QuitSubSystem => [ ['uint32'] ], | ||
| 91 | SDL_WasInit => [ ['uint32'] => 'uint32' ], | ||
| 92 | SDL_Quit => [ [] ], | ||
| 93 | }, | ||
| 94 | unknown => { SDL_SetMainReady => [ [] => 'void' ] }; | ||
| 95 | # | ||
| 96 | # Define a four character code as a Uint32 | ||
| 97 | sub SDL_FOURCC ( $A, $B, $C, $D ) { | ||
| 98 | ( ord($A) << 0 ) | ( ord($B) << 8 ) | ( ord($C) << 16 ) | ( ord($D) << 24 ); | ||
| 99 | } | ||
| 100 | |||
| 101 | # TODO | ||
| 102 | package SDL2::Mixer { | ||
| 103 | use SDL2::Utils; | ||
| 104 | has; | ||
| 105 | }; | ||
| 106 | |||
| 107 | package SDL2::Mixer::Mix::Chunk { | ||
| 108 | use SDL2::Utils; | ||
| 109 | has; | ||
| 110 | }; | ||
| 111 | |||
| 112 | package SDL2::Mixer::Mix::Fading { | ||
| 113 | use SDL2::Utils; | ||
| 114 | has; | ||
| 115 | }; | ||
| 116 | |||
| 117 | package SDL2::Mixer::Mix::MusicType { | ||
| 118 | use SDL2::Utils; | ||
| 119 | has; | ||
| 120 | }; | ||
| 121 | |||
| 122 | package SDL2::Mixer::Mix::Music { | ||
| 123 | use SDL2::Utils; | ||
| 124 | has; | ||
| 125 | }; | ||
| 126 | |||
| 127 | package SDL2::Mixer::Chunk { | ||
| 128 | use SDL2::Utils; | ||
| 129 | has; | ||
| 130 | }; | ||
| 131 | |||
| 132 | package SDL2::Mixer::Fading { | ||
| 133 | use SDL2::Utils; | ||
| 134 | has; | ||
| 135 | }; | ||
| 136 | |||
| 137 | package SDL2::Mixer::MusicType { | ||
| 138 | use SDL2::Utils; | ||
| 139 | has; | ||
| 140 | }; | ||
| 141 | |||
| 142 | package SDL2::Mixer::Music { | ||
| 143 | use SDL2::Utils; | ||
| 144 | has; | ||
| 145 | }; | ||
| 146 | |||
| 147 | package SDL2::Image { | ||
| 148 | use SDL2::Utils; | ||
| 149 | has; | ||
| 150 | }; | ||
| 151 | |||
| 152 | package SDL2::TTF { | ||
| 153 | use SDL2::Utils; | ||
| 154 | has; | ||
| 155 | }; | ||
| 156 | |||
| 157 | package SDL2::TTF::Image { | ||
| 158 | use SDL2::Utils; | ||
| 159 | has; | ||
| 160 | }; | ||
| 161 | |||
| 162 | package SDL2::TTF::Font { | ||
| 163 | use SDL2::Utils; | ||
| 164 | has; | ||
| 165 | }; | ||
| 166 | |||
| 167 | package SDL2::TTF::PosBuf { | ||
| 168 | use SDL2::Utils; | ||
| 169 | has; | ||
| 170 | }; | ||
| 171 | |||
| 172 | package SDL2::Net { | ||
| 173 | use SDL2::Utils; | ||
| 174 | has; | ||
| 175 | }; | ||
| 176 | |||
| 177 | package SDL2::RTF { | ||
| 178 | use SDL2::Utils; | ||
| 179 | has; | ||
| 180 | }; | ||
| 181 | |||
| 182 | package SDL2::RTF::Context { | ||
| 183 | use SDL2::Utils; | ||
| 184 | has; | ||
| 185 | }; | ||
| 186 | |||
| 187 | package SDL2::RTF::FontEngine { | ||
| 188 | use SDL2::Utils; | ||
| 189 | has; | ||
| 190 | }; | ||
| 191 | |||
| 192 | # bundled code testing | ||
| 193 | #my $holder; | ||
| 194 | attach | ||
| 195 | debug => { Bundle_SDL_PrintEvent => [ ['SDL_Event'] ], }, | ||
| 196 | events => { Bundle_SDL_Yield => [ [] ], }; | ||
| 197 | |||
| 198 | #warn SDL2::SDLK_UP(); | ||
| 199 | #warn SDL2::SDLK_DOWN(); | ||
| 200 | # https://github.com/libsdl-org/SDL/blob/main/include/SDL_hints.h | ||
| 201 | # Export symbols! | ||
| 202 | our @EXPORT_OK = map {@$_} values %EXPORT_TAGS; | ||
| 203 | |||
| 204 | #$EXPORT_TAGS{default} = []; # Export nothing by default | ||
| 205 | $EXPORT_TAGS{all} = \@EXPORT_OK; # Export everything with :all tag | ||
| 206 | |||
| 207 | #use Data::Dump; | ||
| 208 | #ddx \%EXPORT_TAGS; | ||
| 209 | #ddx \%SDL2::; | ||
| 210 | }; | ||
| 211 | 1; | ||
| 212 | |||
| 213 | =encoding utf-8 | ||
| 214 | |||
| 215 | =head1 NAME | ||
| 216 | |||
| 217 | SDL2::FFI - FFI Wrapper for SDL (Simple DirectMedia Layer) Development Library | ||
| 218 | |||
| 219 | =head1 SYNOPSIS | ||
| 220 | |||
| 221 | use SDL2::FFI qw[:all]; | ||
| 222 | die 'Error initializing SDL: ' . SDL_GetError() unless SDL_Init(SDL_INIT_VIDEO) == 0; | ||
| 223 | my $win = SDL_CreateWindow( 'Example window!', | ||
| 224 | SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE ); | ||
| 225 | die 'Could not create window: ' . SDL_GetError() unless $win; | ||
| 226 | my $event = SDL2::Event->new; | ||
| 227 | SDL_Init(SDL_INIT_VIDEO); | ||
| 228 | my $renderer = SDL_CreateRenderer( $win, -1, 0 ); | ||
| 229 | SDL_SetRenderDrawColor( $renderer, 242, 242, 242, 255 ); | ||
| 230 | do { | ||
| 231 | SDL_WaitEventTimeout( $event, 10 ); | ||
| 232 | SDL_RenderClear($renderer); | ||
| 233 | SDL_RenderPresent($renderer); | ||
| 234 | } until $event->type == SDL_QUIT; | ||
| 235 | SDL_DestroyRenderer($renderer); | ||
| 236 | SDL_DestroyWindow($win); | ||
| 237 | SDL_Quit(); | ||
| 238 | |||
| 239 | =head1 DESCRIPTION | ||
| 240 | |||
| 241 | SDL2::FFI is an L<FFI::Platypus> backed bindings to the B<S>imple | ||
| 242 | B<D>irectMedia B<L>ayer - a cross-platform development library designed to | ||
| 243 | provide low level access to audio, keyboard, mouse, joystick, and graphics | ||
| 244 | hardware. | ||
| 245 | |||
| 246 | =head1 Initialization and Shutdown | ||
| 247 | |||
| 248 | The functions in this category are used to set SDL up for use and generally | ||
| 249 | have global effects in your program. These functions may be imported with the | ||
| 250 | C<:init> or C<:default> tag. | ||
| 251 | |||
| 252 | =head2 C<SDL_Init( ... )> | ||
| 253 | |||
| 254 | Initializes the SDL library. This must be called before using most other SDL | ||
| 255 | functions. | ||
| 256 | |||
| 257 | SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ); | ||
| 258 | |||
| 259 | C<SDL_Init( ... )> simply forwards to calling L<< C<SDL_InitSubSystem( ... | ||
| 260 | )>|/C<SDL_InitSubSystem( ... )> >>. Therefore, the two may be used | ||
| 261 | interchangeably. Though for readability of your code L<< C<SDL_InitSubSystem( | ||
| 262 | ... )>|/C<SDL_InitSubSystem( ... )> >> might be preferred. | ||
| 263 | |||
| 264 | The file I/O (for example: C<SDL_RWFromFile( ... )>) and threading | ||
| 265 | (C<SDL_CreateThread( ... )>) subsystems are initialized by default. Message | ||
| 266 | boxes (C<SDL_ShowSimpleMessageBox( ... )>) also attempt to work without | ||
| 267 | initializing the video subsystem, in hopes of being useful in showing an error | ||
| 268 | dialog when SDL_Init fails. You must specifically initialize other subsystems | ||
| 269 | if you use them in your application. | ||
| 270 | |||
| 271 | Logging (such as C<SDL_Log( ... )>) works without initialization, too. | ||
| 272 | |||
| 273 | Expected parameters include: | ||
| 274 | |||
| 275 | =over | ||
| 276 | |||
| 277 | =item C<flags> which may be any be imported with the L<< C<:init>|SDL2::Enum/C<:init> >> tag and may be OR'd together | ||
| 278 | |||
| 279 | =back | ||
| 280 | |||
| 281 | Subsystem initialization is ref-counted, you must call L<< C<SDL_QuitSubSystem( | ||
| 282 | ... )>|/C<SDL_QuitSubSystem( ... )> >> for each L<< C<SDL_InitSubSystem( ... | ||
| 283 | )>|/C<SDL_InitSubSystem( ... )> >> to correctly shutdown a subsystem manually | ||
| 284 | (or call L<< C<SDL_Quit( )>|/C<SDL_Quit( )> >> to force shutdown). If a | ||
| 285 | subsystem is already loaded then this call will increase the ref-count and | ||
| 286 | return. | ||
| 287 | |||
| 288 | Returns C<0> on success or a negative error code on failure; call | ||
| 289 | C<SDL_GetError( )> for more information. | ||
| 290 | |||
| 291 | =head2 C<SDL_InitSubSystem( ... )> | ||
| 292 | |||
| 293 | Compatibility function to initialize the SDL library. | ||
| 294 | |||
| 295 | In SDL2, this function and L<< C<SDL_Init( ... )>|/C<SDL_Init( ... )> >> are | ||
| 296 | interchangeable. | ||
| 297 | |||
| 298 | SDL_InitSubSystem( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ); | ||
| 299 | |||
| 300 | Expected parameters include: | ||
| 301 | |||
| 302 | =over | ||
| 303 | |||
| 304 | =item C<flags> which may be any be imported with the L<< C<:init>|SDL2::Enum/C<:init> >> tag and may be OR'd together. | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | Returns C<0> on success or a negative error code on failure; call | ||
| 309 | C<SDL_GetError( )> for more information. | ||
| 310 | |||
| 311 | =head2 C<SDL_QuitSubSystem( ... )> | ||
| 312 | |||
| 313 | Shut down specific SDL subsystems. | ||
| 314 | |||
| 315 | SDL_QuitSubSystem( SDL_INIT_VIDEO ); | ||
| 316 | |||
| 317 | If you start a subsystem using a call to that subsystem's init function (for | ||
| 318 | example C<SDL_VideoInit( )>) instead of L<< C<SDL_Init( ... )>|/C<SDL_Init( ... | ||
| 319 | )> >> or L<< C<SDL_InitSubSystem( ... )>|/C<SDL_InitSubSystem( ... )> >>, L<< | ||
| 320 | C<SDL_QuitSubSystem( ... )>|/C<SDL_QuitSubSystem( ... )> >> and L<< | ||
| 321 | C<SDL_WasInit( ... )>|/C<SDL_WasInit( ... )> >> will not work. You will need to | ||
| 322 | use that subsystem's quit function (C<SDL_VideoQuit( )> directly instead. But | ||
| 323 | generally, you should not be using those functions directly anyhow; use L<< | ||
| 324 | C<SDL_Init( ... )>|/C<SDL_Init( ... )> >> instead. | ||
| 325 | |||
| 326 | You still need to call L<< C<SDL_Quit( )>|/C<SDL_Quit( )> >> even if you close | ||
| 327 | all open subsystems with L<< C<SDL_QuitSubSystem( ... )>|/C<SDL_QuitSubSystem( | ||
| 328 | ... )> >>. | ||
| 329 | |||
| 330 | Expected parameters include: | ||
| 331 | |||
| 332 | =over | ||
| 333 | |||
| 334 | =item C<flags> which may be any be imported with the L<< C<:init>|SDL2::Enum/C<:init> >> tag and may be OR'd together. | ||
| 335 | |||
| 336 | =back | ||
| 337 | |||
| 338 | =head2 C<SDL_WasInit( ... )> | ||
| 339 | |||
| 340 | Get a mask of the specified subsystems which are currently initialized. | ||
| 341 | |||
| 342 | SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ); | ||
| 343 | warn SDL_WasInit( SDL_INIT_TIMER ); # false | ||
| 344 | warn SDL_WasInit( SDL_INIT_VIDEO ); # true (32 == SDL_INIT_VIDEO) | ||
| 345 | my $mask = SDL_WasInit( ); | ||
| 346 | warn 'video init!' if ($mask & SDL_INIT_VIDEO); # yep | ||
| 347 | warn 'video timer!' if ($mask & SDL_INIT_TIMER); # nope | ||
| 348 | |||
| 349 | Expected parameters include: | ||
| 350 | |||
| 351 | =over | ||
| 352 | |||
| 353 | =item C<flags> which may be any be imported with the L<< C<:init>|SDL2::Enum/C<:init> >> tag and may be OR'd together. | ||
| 354 | |||
| 355 | =back | ||
| 356 | |||
| 357 | If C<flags> is C<0>, it returns a mask of all initialized subsystems, otherwise | ||
| 358 | it returns the initialization status of the specified subsystems. | ||
| 359 | |||
| 360 | The return value does not include C<SDL_INIT_NOPARACHUTE>. | ||
| 361 | |||
| 362 | =head2 C<SDL_Quit( )> | ||
| 363 | |||
| 364 | Clean up all initialized subsystems. | ||
| 365 | |||
| 366 | SDL_Quit( ); | ||
| 367 | |||
| 368 | You should call this function even if you have already shutdown each | ||
| 369 | initialized subsystem with C<SDL_QuitSubSystem( )>. It is safe to call this | ||
| 370 | function even in the case of errors in initialization. | ||
| 371 | |||
| 372 | If you start a subsystem using a call to that subsystem's init function (for | ||
| 373 | example C<SDL_VideoInit( )>) instead of L<< C<SDL_Init( ... )>|/C<SDL_Init( ... | ||
| 374 | )> >> or L<< C<SDL_InitSubSystem( ... )>|/C<SDL_InitSubSystem( ... )> >>, then | ||
| 375 | you must use that subsystem's quit function (C<SDL_VideoQuit( )>) to shut it | ||
| 376 | down before calling C<SDL_Quit( )>. But generally, you should not be using | ||
| 377 | those functions directly anyhow; use L<< C<SDL_Init( ... )>|/C<SDL_Init( ... )> | ||
| 378 | >> instead. | ||
| 379 | |||
| 380 | You can use this function in an C<END { ... }> block to ensure that it is run | ||
| 381 | when your application is shutdown. | ||
| 382 | |||
| 383 | =head1 Defined Values and Enumerations | ||
| 384 | |||
| 385 | Defined values may be imported by name or with given tag. | ||
| 386 | |||
| 387 | =head2 C<SDL_INIT_*> | ||
| 388 | |||
| 389 | These are the flags which may be passed to C<SDL_Init( ... )>. You should | ||
| 390 | specify the subsystems which you will be using in your application. These may | ||
| 391 | be imported with the C<:init> or C<:default> tag. | ||
| 392 | |||
| 393 | =over | ||
| 394 | |||
| 395 | =item C<SDL_INIT_TIMER> | ||
| 396 | |||
| 397 | =item C<SDL_INIT_AUDIO> | ||
| 398 | |||
| 399 | =item C<SDL_INIT_VIDEO> - C<SDL_INIT_VIDEO> implies C<SDL_INIT_EVENTS> | ||
| 400 | |||
| 401 | =item C<SDL_INIT_JOYSTICK> - C<SDL_INIT_JOYSTICK> implies C<SDL_INIT_EVENTS> | ||
| 402 | |||
| 403 | =item C<SDL_INIT_HAPTIC> | ||
| 404 | |||
| 405 | =item C<SDL_INIT_GAMECONTROLLER> - C<SDL_INIT_GAMECONTROLLER> implies C<SDL_INIT_JOYSTICK> | ||
| 406 | |||
| 407 | =item C<SDL_INIT_EVENTS> | ||
| 408 | |||
| 409 | =item C<SDL_INIT_SENSOR> | ||
| 410 | |||
| 411 | =item C<SDL_INIT_NOPARACHUTE> - compatibility; this flag is ignored | ||
| 412 | |||
| 413 | =item C<SDL_INIT_EVERYTHING> | ||
| 414 | |||
| 415 | =back | ||
| 416 | |||
| 417 | =head1 LICENSE | ||
| 418 | |||
| 419 | Copyright (C) Sanko Robinson. | ||
| 420 | |||
| 421 | This library is free software; you can redistribute it and/or modify it under | ||
| 422 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 423 | conditions may apply to data transmitted through this module. | ||
| 424 | |||
| 425 | =head1 AUTHOR | ||
| 426 | |||
| 427 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 428 | |||
| 429 | =begin stopwords | ||
| 430 | |||
| 431 | imple irectMedia ayer | ||
| 432 | |||
| 433 | =end stopwords | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | # Examples: | ||
| 438 | # - https://github.com/crust/sdl2-examples | ||
| 439 | # | ||