| 1 | package SDL2::FFI 0.07 { | ||
| 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 | bundle => { | ||
| 96 | ffi_pl_bundle_init => [ [ 'string', 'int', 'opaque' ] ], | ||
| 97 | ffi_pl_bundle_fini => [ ['string'] ] | ||
| 98 | }; | ||
| 99 | ffi_pl_bundle_init( __PACKAGE__, scalar @ARGV, @ARGV ); | ||
| 100 | END { ffi_pl_bundle_fini(__PACKAGE__) } | ||
| 101 | # | ||
| 102 | # Define a four character code as a Uint32 | ||
| 103 | sub SDL_FOURCC ( $A, $B, $C, $D ) { | ||
| 104 | ( ord($A) << 0 ) | ( ord($B) << 8 ) | ( ord($C) << 16 ) | ( ord($D) << 24 ); | ||
| 105 | } | ||
| 106 | |||
| 107 | # TODO | ||
| 108 | package SDL2::Mixer { | ||
| 109 | use SDL2::Utils; | ||
| 110 | has; | ||
| 111 | }; | ||
| 112 | |||
| 113 | package SDL2::Mixer::Mix::Chunk { | ||
| 114 | use SDL2::Utils; | ||
| 115 | has; | ||
| 116 | }; | ||
| 117 | |||
| 118 | package SDL2::Mixer::Mix::Fading { | ||
| 119 | use SDL2::Utils; | ||
| 120 | has; | ||
| 121 | }; | ||
| 122 | |||
| 123 | package SDL2::Mixer::Mix::MusicType { | ||
| 124 | use SDL2::Utils; | ||
| 125 | has; | ||
| 126 | }; | ||
| 127 | |||
| 128 | package SDL2::Mixer::Mix::Music { | ||
| 129 | use SDL2::Utils; | ||
| 130 | has; | ||
| 131 | }; | ||
| 132 | |||
| 133 | package SDL2::Mixer::Chunk { | ||
| 134 | use SDL2::Utils; | ||
| 135 | has; | ||
| 136 | }; | ||
| 137 | |||
| 138 | package SDL2::Mixer::Fading { | ||
| 139 | use SDL2::Utils; | ||
| 140 | has; | ||
| 141 | }; | ||
| 142 | |||
| 143 | package SDL2::Mixer::MusicType { | ||
| 144 | use SDL2::Utils; | ||
| 145 | has; | ||
| 146 | }; | ||
| 147 | |||
| 148 | package SDL2::Mixer::Music { | ||
| 149 | use SDL2::Utils; | ||
| 150 | has; | ||
| 151 | }; | ||
| 152 | |||
| 153 | package SDL2::Image { | ||
| 154 | use SDL2::Utils; | ||
| 155 | has; | ||
| 156 | }; | ||
| 157 | |||
| 158 | package SDL2::TTF { | ||
| 159 | use SDL2::Utils; | ||
| 160 | has; | ||
| 161 | }; | ||
| 162 | |||
| 163 | package SDL2::TTF::Image { | ||
| 164 | use SDL2::Utils; | ||
| 165 | has; | ||
| 166 | }; | ||
| 167 | |||
| 168 | package SDL2::TTF::Font { | ||
| 169 | use SDL2::Utils; | ||
| 170 | has; | ||
| 171 | }; | ||
| 172 | |||
| 173 | package SDL2::TTF::PosBuf { | ||
| 174 | use SDL2::Utils; | ||
| 175 | has; | ||
| 176 | }; | ||
| 177 | |||
| 178 | package SDL2::Net { | ||
| 179 | use SDL2::Utils; | ||
| 180 | has; | ||
| 181 | }; | ||
| 182 | |||
| 183 | package SDL2::RTF { | ||
| 184 | use SDL2::Utils; | ||
| 185 | has; | ||
| 186 | }; | ||
| 187 | |||
| 188 | package SDL2::RTF::Context { | ||
| 189 | use SDL2::Utils; | ||
| 190 | has; | ||
| 191 | }; | ||
| 192 | |||
| 193 | package SDL2::RTF::FontEngine { | ||
| 194 | use SDL2::Utils; | ||
| 195 | has; | ||
| 196 | }; | ||
| 197 | |||
| 198 | # bundled code testing | ||
| 199 | #my $holder; | ||
| 200 | if ( threads_wrapped() ) { | ||
| 201 | attach | ||
| 202 | debug => { Bundle_SDL_PrintEvent => [ ['SDL_Event'] ], }, | ||
| 203 | events => { Bundle_SDL_Yield => [ [] ], }; | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | define events => [ [ SDL_Yield => sub () {1} ] ]; | ||
| 207 | } | ||
| 208 | |||
| 209 | #warn SDL2::SDLK_UP(); | ||
| 210 | #warn SDL2::SDLK_DOWN(); | ||
| 211 | # https://github.com/libsdl-org/SDL/blob/main/include/SDL_hints.h | ||
| 212 | # Export symbols! | ||
| 213 | our @EXPORT_OK = map {@$_} values %EXPORT_TAGS; | ||
| 214 | |||
| 215 | #$EXPORT_TAGS{default} = []; # Export nothing by default | ||
| 216 | $EXPORT_TAGS{all} = \@EXPORT_OK; # Export everything with :all tag | ||
| 217 | |||
| 218 | #use Data::Dump; | ||
| 219 | #ddx \%EXPORT_TAGS; | ||
| 220 | #ddx \%SDL2::; | ||
| 221 | }; | ||
| 222 | 1; | ||
| 223 | |||
| 224 | =encoding utf-8 | ||
| 225 | |||
| 226 | =head1 NAME | ||
| 227 | |||
| 228 | SDL2::FFI - FFI Wrapper for SDL (Simple DirectMedia Layer) Development Library | ||
| 229 | |||
| 230 | =head1 SYNOPSIS | ||
| 231 | |||
| 232 | use SDL2::FFI qw[:all]; | ||
| 233 | die 'Error initializing SDL: ' . SDL_GetError() unless SDL_Init(SDL_INIT_VIDEO) == 0; | ||
| 234 | my $win = SDL_CreateWindow( 'Example window!', | ||
| 235 | SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE ); | ||
| 236 | die 'Could not create window: ' . SDL_GetError() unless $win; | ||
| 237 | my $event = SDL2::Event->new; | ||
| 238 | SDL_Init(SDL_INIT_VIDEO); | ||
| 239 | my $renderer = SDL_CreateRenderer( $win, -1, 0 ); | ||
| 240 | SDL_SetRenderDrawColor( $renderer, 242, 242, 242, 255 ); | ||
| 241 | do { | ||
| 242 | SDL_WaitEventTimeout( $event, 10 ); | ||
| 243 | SDL_RenderClear($renderer); | ||
| 244 | SDL_RenderPresent($renderer); | ||
| 245 | } until $event->type == SDL_QUIT; | ||
| 246 | SDL_DestroyRenderer($renderer); | ||
| 247 | SDL_DestroyWindow($win); | ||
| 248 | SDL_Quit(); | ||
| 249 | |||
| 250 | =head1 DESCRIPTION | ||
| 251 | |||
| 252 | SDL2::FFI is an L<FFI::Platypus> backed bindings to the B<S>imple | ||
| 253 | B<D>irectMedia B<L>ayer - a cross-platform development library designed to | ||
| 254 | provide low level access to audio, keyboard, mouse, joystick, and graphics | ||
| 255 | hardware. | ||
| 256 | |||
| 257 | =head1 Initialization and Shutdown | ||
| 258 | |||
| 259 | The functions in this category are used to set SDL up for use and generally | ||
| 260 | have global effects in your program. These functions may be imported with the | ||
| 261 | C<:init> or C<:default> tag. | ||
| 262 | |||
| 263 | =head2 C<SDL_Init( ... )> | ||
| 264 | |||
| 265 | Initializes the SDL library. This must be called before using most other SDL | ||
| 266 | functions. | ||
| 267 | |||
| 268 | SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ); | ||
| 269 | |||
| 270 | C<SDL_Init( ... )> simply forwards to calling L<< C<SDL_InitSubSystem( ... | ||
| 271 | )>|/C<SDL_InitSubSystem( ... )> >>. Therefore, the two may be used | ||
| 272 | interchangeably. Though for readability of your code L<< C<SDL_InitSubSystem( | ||
| 273 | ... )>|/C<SDL_InitSubSystem( ... )> >> might be preferred. | ||
| 274 | |||
| 275 | The file I/O (for example: C<SDL_RWFromFile( ... )>) and threading | ||
| 276 | (C<SDL_CreateThread( ... )>) subsystems are initialized by default. Message | ||
| 277 | boxes (C<SDL_ShowSimpleMessageBox( ... )>) also attempt to work without | ||
| 278 | initializing the video subsystem, in hopes of being useful in showing an error | ||
| 279 | dialog when SDL_Init fails. You must specifically initialize other subsystems | ||
| 280 | if you use them in your application. | ||
| 281 | |||
| 282 | Logging (such as C<SDL_Log( ... )>) works without initialization, too. | ||
| 283 | |||
| 284 | Expected parameters include: | ||
| 285 | |||
| 286 | =over | ||
| 287 | |||
| 288 | =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 | ||
| 289 | |||
| 290 | =back | ||
| 291 | |||
| 292 | Subsystem initialization is ref-counted, you must call L<< C<SDL_QuitSubSystem( | ||
| 293 | ... )>|/C<SDL_QuitSubSystem( ... )> >> for each L<< C<SDL_InitSubSystem( ... | ||
| 294 | )>|/C<SDL_InitSubSystem( ... )> >> to correctly shutdown a subsystem manually | ||
| 295 | (or call L<< C<SDL_Quit( )>|/C<SDL_Quit( )> >> to force shutdown). If a | ||
| 296 | subsystem is already loaded then this call will increase the ref-count and | ||
| 297 | return. | ||
| 298 | |||
| 299 | Returns C<0> on success or a negative error code on failure; call | ||
| 300 | C<SDL_GetError( )> for more information. | ||
| 301 | |||
| 302 | =head2 C<SDL_InitSubSystem( ... )> | ||
| 303 | |||
| 304 | Compatibility function to initialize the SDL library. | ||
| 305 | |||
| 306 | In SDL2, this function and L<< C<SDL_Init( ... )>|/C<SDL_Init( ... )> >> are | ||
| 307 | interchangeable. | ||
| 308 | |||
| 309 | SDL_InitSubSystem( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ); | ||
| 310 | |||
| 311 | Expected parameters include: | ||
| 312 | |||
| 313 | =over | ||
| 314 | |||
| 315 | =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. | ||
| 316 | |||
| 317 | =back | ||
| 318 | |||
| 319 | Returns C<0> on success or a negative error code on failure; call | ||
| 320 | C<SDL_GetError( )> for more information. | ||
| 321 | |||
| 322 | =head2 C<SDL_QuitSubSystem( ... )> | ||
| 323 | |||
| 324 | Shut down specific SDL subsystems. | ||
| 325 | |||
| 326 | SDL_QuitSubSystem( SDL_INIT_VIDEO ); | ||
| 327 | |||
| 328 | If you start a subsystem using a call to that subsystem's init function (for | ||
| 329 | example C<SDL_VideoInit( )>) instead of L<< C<SDL_Init( ... )>|/C<SDL_Init( ... | ||
| 330 | )> >> or L<< C<SDL_InitSubSystem( ... )>|/C<SDL_InitSubSystem( ... )> >>, L<< | ||
| 331 | C<SDL_QuitSubSystem( ... )>|/C<SDL_QuitSubSystem( ... )> >> and L<< | ||
| 332 | C<SDL_WasInit( ... )>|/C<SDL_WasInit( ... )> >> will not work. You will need to | ||
| 333 | use that subsystem's quit function (C<SDL_VideoQuit( )> directly instead. But | ||
| 334 | generally, you should not be using those functions directly anyhow; use L<< | ||
| 335 | C<SDL_Init( ... )>|/C<SDL_Init( ... )> >> instead. | ||
| 336 | |||
| 337 | You still need to call L<< C<SDL_Quit( )>|/C<SDL_Quit( )> >> even if you close | ||
| 338 | all open subsystems with L<< C<SDL_QuitSubSystem( ... )>|/C<SDL_QuitSubSystem( | ||
| 339 | ... )> >>. | ||
| 340 | |||
| 341 | Expected parameters include: | ||
| 342 | |||
| 343 | =over | ||
| 344 | |||
| 345 | =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. | ||
| 346 | |||
| 347 | =back | ||
| 348 | |||
| 349 | =head2 C<SDL_WasInit( ... )> | ||
| 350 | |||
| 351 | Get a mask of the specified subsystems which are currently initialized. | ||
| 352 | |||
| 353 | SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ); | ||
| 354 | warn SDL_WasInit( SDL_INIT_TIMER ); # false | ||
| 355 | warn SDL_WasInit( SDL_INIT_VIDEO ); # true (32 == SDL_INIT_VIDEO) | ||
| 356 | my $mask = SDL_WasInit( ); | ||
| 357 | warn 'video init!' if ($mask & SDL_INIT_VIDEO); # yep | ||
| 358 | warn 'video timer!' if ($mask & SDL_INIT_TIMER); # nope | ||
| 359 | |||
| 360 | Expected parameters include: | ||
| 361 | |||
| 362 | =over | ||
| 363 | |||
| 364 | =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. | ||
| 365 | |||
| 366 | =back | ||
| 367 | |||
| 368 | If C<flags> is C<0>, it returns a mask of all initialized subsystems, otherwise | ||
| 369 | it returns the initialization status of the specified subsystems. | ||
| 370 | |||
| 371 | The return value does not include C<SDL_INIT_NOPARACHUTE>. | ||
| 372 | |||
| 373 | =head2 C<SDL_Quit( )> | ||
| 374 | |||
| 375 | Clean up all initialized subsystems. | ||
| 376 | |||
| 377 | SDL_Quit( ); | ||
| 378 | |||
| 379 | You should call this function even if you have already shutdown each | ||
| 380 | initialized subsystem with C<SDL_QuitSubSystem( )>. It is safe to call this | ||
| 381 | function even in the case of errors in initialization. | ||
| 382 | |||
| 383 | If you start a subsystem using a call to that subsystem's init function (for | ||
| 384 | example C<SDL_VideoInit( )>) instead of L<< C<SDL_Init( ... )>|/C<SDL_Init( ... | ||
| 385 | )> >> or L<< C<SDL_InitSubSystem( ... )>|/C<SDL_InitSubSystem( ... )> >>, then | ||
| 386 | you must use that subsystem's quit function (C<SDL_VideoQuit( )>) to shut it | ||
| 387 | down before calling C<SDL_Quit( )>. But generally, you should not be using | ||
| 388 | those functions directly anyhow; use L<< C<SDL_Init( ... )>|/C<SDL_Init( ... )> | ||
| 389 | >> instead. | ||
| 390 | |||
| 391 | You can use this function in an C<END { ... }> block to ensure that it is run | ||
| 392 | when your application is shutdown. | ||
| 393 | |||
| 394 | =head1 Defined Values and Enumerations | ||
| 395 | |||
| 396 | Defined values may be imported by name or with given tag. | ||
| 397 | |||
| 398 | =head2 C<SDL_INIT_*> | ||
| 399 | |||
| 400 | These are the flags which may be passed to C<SDL_Init( ... )>. You should | ||
| 401 | specify the subsystems which you will be using in your application. These may | ||
| 402 | be imported with the C<:init> or C<:default> tag. | ||
| 403 | |||
| 404 | =over | ||
| 405 | |||
| 406 | =item C<SDL_INIT_TIMER> | ||
| 407 | |||
| 408 | =item C<SDL_INIT_AUDIO> | ||
| 409 | |||
| 410 | =item C<SDL_INIT_VIDEO> - C<SDL_INIT_VIDEO> implies C<SDL_INIT_EVENTS> | ||
| 411 | |||
| 412 | =item C<SDL_INIT_JOYSTICK> - C<SDL_INIT_JOYSTICK> implies C<SDL_INIT_EVENTS> | ||
| 413 | |||
| 414 | =item C<SDL_INIT_HAPTIC> | ||
| 415 | |||
| 416 | =item C<SDL_INIT_GAMECONTROLLER> - C<SDL_INIT_GAMECONTROLLER> implies C<SDL_INIT_JOYSTICK> | ||
| 417 | |||
| 418 | =item C<SDL_INIT_EVENTS> | ||
| 419 | |||
| 420 | =item C<SDL_INIT_SENSOR> | ||
| 421 | |||
| 422 | =item C<SDL_INIT_NOPARACHUTE> - compatibility; this flag is ignored | ||
| 423 | |||
| 424 | =item C<SDL_INIT_EVERYTHING> | ||
| 425 | |||
| 426 | =back | ||
| 427 | |||
| 428 | =head1 LICENSE | ||
| 429 | |||
| 430 | Copyright (C) Sanko Robinson. | ||
| 431 | |||
| 432 | This library is free software; you can redistribute it and/or modify it under | ||
| 433 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 434 | conditions may apply to data transmitted through this module. | ||
| 435 | |||
| 436 | =head1 AUTHOR | ||
| 437 | |||
| 438 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 439 | |||
| 440 | =begin stopwords | ||
| 441 | |||
| 442 | imple irectMedia ayer | ||
| 443 | |||
| 444 | =end stopwords | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | # Examples: | ||
| 449 | # - https://github.com/crust/sdl2-examples | ||
| 450 | # | ||