Skip to content

Commit

Permalink
Fix namespace (use PSR-4) and add full SDL 2.0.12 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Apr 11, 2020
1 parent 7a0959d commit ded8d90
Show file tree
Hide file tree
Showing 85 changed files with 643 additions and 2,372 deletions.
300 changes: 4 additions & 296 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"char **",
);

override(\SDL\SDL::new(), map([
override(\Serafim\SDL\SDL::new(), map([
'' => '@',
// mixed
"void *" => \FFI\CScalar::class,
Expand Down Expand Up @@ -137,301 +137,9 @@
"char *" => \FFI\CStringPtr::class,
"char **" => \FFI\CStringPtrPtr::class,
]));
override(\SDL\Support\ProxyTrait::new(), map([
'' => type(0),
// mixed
"void *" => \FFI\CScalar::class,

// floats
"float" => \FFI\CFloat::class,
"float *" => \FFI\CFloatPtr::class,
"float **" => \FFI\CFloatPtrPtr::class,

"double" => \FFI\CFloat::class,
"double *" => \FFI\CFloatPtr::class,
"double **" => \FFI\CFloatPtrPtr::class,

"long double" => \FFI\CFloat::class,
"long double *" => \FFI\CFloatPtr::class,
"long double **" => \FFI\CFloatPtrPtr::class,
// integers
"int" => \FFI\CInt::class,
"int *" => \FFI\CIntPtr::class,
"int **" => \FFI\CIntPtrPtr::class,
"uint8_t" => \FFI\CInt::class,
"uint8_t *" => \FFI\CIntPtr::class,
"uint8_t **" => \FFI\CIntPtrPtr::class,

"int8_t" => \FFI\CInt::class,
"int8_t*" => \FFI\CIntPtr::class,
"int8_t *" => \FFI\CIntPtr::class,
"int8_t**" => \FFI\CIntPtrPtr::class,
"int8_t **" => \FFI\CIntPtrPtr::class,

"uint16_t" => \FFI\CInt::class,
"uint16_t *" => \FFI\CIntPtr::class,
"uint16_t **" => \FFI\CIntPtrPtr::class,

"int16_t" => \FFI\CInt::class,
"int16_t *" => \FFI\CIntPtr::class,
"int16_t **" => \FFI\CIntPtrPtr::class,

"uint32_t" => \FFI\CInt::class,
"uint32_t *" => \FFI\CIntPtr::class,
"uint32_t **" => \FFI\CIntPtrPtr::class,

"int32_t" => \FFI\CInt::class,
"int32_t *" => \FFI\CIntPtr::class,
"int32_t **" => \FFI\CIntPtrPtr::class,

"uint64_t" => \FFI\CInt::class,
"uint64_t *" => \FFI\CIntPtr::class,
"uint64_t **" => \FFI\CIntPtrPtr::class,

"int64_t" => \FFI\CInt::class,
"int64_t *" => \FFI\CIntPtr::class,
"int64_t **" => \FFI\CIntPtrPtr::class,

// bool
"bool" => \FFI\CBool::class,
"bool *" => \FFI\CBoolPtr::class,
"bool **" => \FFI\CBoolPtrPtr::class,

// string
"char" => \FFI\CChar::class,
"char *" => \FFI\CStringPtr::class,
"char **" => \FFI\CStringPtrPtr::class,
]));

override(\SDL\SDL::addr(), map([
'' => '@Ptr',
]));
override(\SDL\Support\ProxyTrait::addr(), map([
'' => '@Ptr',
]));

expectedArguments(\SDL\SDL::new(), 0, argumentsSet('ffiCType'));
expectedArguments(\SDL\Support\ProxyTrait::new(), 0, argumentsSet('ffiCType'));

expectedArguments(\SDL\SDL::cast(), 0, argumentsSet('ffiCType'));
expectedArguments(\SDL\Support\ProxyTrait::cast(), 0, argumentsSet('ffiCType'));

}


/**
* This file is part of SDL package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPSTORM_META {

registerArgumentsSet('ffiCType',
"void *",
// floats
"float",
"float *",
"float **",

"double",
"double *",
"double **",

"long double",
"long double *",
"long double **",
// integers
"int",
"int *",
"int **",
"uint8_t",
"uint8_t *",
"uint8_t **",

"int8_t",
"int8_t*",
"int8_t *",
"int8_t**",
"int8_t **",

"uint16_t",
"uint16_t *",
"uint16_t **",

"int16_t",
"int16_t *",
"int16_t **",

"uint32_t",
"uint32_t *",
"uint32_t **",

"int32_t",
"int32_t *",
"int32_t **",

"uint64_t",
"uint64_t *",
"uint64_t **",

"int64_t",
"int64_t *",
"int64_t **",

// bool
"bool",
"bool *",
"bool **",

// string
"char",
"char *",
"char **",
);

override(\SDL\SDL::new(), map([
'' => '@',
// mixed
"void *" => \FFI\CScalar::class,

// floats
"float" => \FFI\CFloat::class,
"float *" => \FFI\CFloatPtr::class,
"float **" => \FFI\CFloatPtrPtr::class,

"double" => \FFI\CFloat::class,
"double *" => \FFI\CFloatPtr::class,
"double **" => \FFI\CFloatPtrPtr::class,

"long double" => \FFI\CFloat::class,
"long double *" => \FFI\CFloatPtr::class,
"long double **" => \FFI\CFloatPtrPtr::class,
// integers
"int" => \FFI\CInt::class,
"int *" => \FFI\CIntPtr::class,
"int **" => \FFI\CIntPtrPtr::class,
"uint8_t" => \FFI\CInt::class,
"uint8_t *" => \FFI\CIntPtr::class,
"uint8_t **" => \FFI\CIntPtrPtr::class,

"int8_t" => \FFI\CInt::class,
"int8_t*" => \FFI\CIntPtr::class,
"int8_t *" => \FFI\CIntPtr::class,
"int8_t**" => \FFI\CIntPtrPtr::class,
"int8_t **" => \FFI\CIntPtrPtr::class,

"uint16_t" => \FFI\CInt::class,
"uint16_t *" => \FFI\CIntPtr::class,
"uint16_t **" => \FFI\CIntPtrPtr::class,

"int16_t" => \FFI\CInt::class,
"int16_t *" => \FFI\CIntPtr::class,
"int16_t **" => \FFI\CIntPtrPtr::class,

"uint32_t" => \FFI\CInt::class,
"uint32_t *" => \FFI\CIntPtr::class,
"uint32_t **" => \FFI\CIntPtrPtr::class,

"int32_t" => \FFI\CInt::class,
"int32_t *" => \FFI\CIntPtr::class,
"int32_t **" => \FFI\CIntPtrPtr::class,

"uint64_t" => \FFI\CInt::class,
"uint64_t *" => \FFI\CIntPtr::class,
"uint64_t **" => \FFI\CIntPtrPtr::class,

"int64_t" => \FFI\CInt::class,
"int64_t *" => \FFI\CIntPtr::class,
"int64_t **" => \FFI\CIntPtrPtr::class,

// bool
"bool" => \FFI\CBool::class,
"bool *" => \FFI\CBoolPtr::class,
"bool **" => \FFI\CBoolPtrPtr::class,

// string
"char" => \FFI\CChar::class,
"char *" => \FFI\CStringPtr::class,
"char **" => \FFI\CStringPtrPtr::class,
]));
override(\SDL\Support\ProxyTrait::new(), map([
'' => type(0),
// mixed
"void *" => \FFI\CScalar::class,

// floats
"float" => \FFI\CFloat::class,
"float *" => \FFI\CFloatPtr::class,
"float **" => \FFI\CFloatPtrPtr::class,

"double" => \FFI\CFloat::class,
"double *" => \FFI\CFloatPtr::class,
"double **" => \FFI\CFloatPtrPtr::class,

"long double" => \FFI\CFloat::class,
"long double *" => \FFI\CFloatPtr::class,
"long double **" => \FFI\CFloatPtrPtr::class,
// integers
"int" => \FFI\CInt::class,
"int *" => \FFI\CIntPtr::class,
"int **" => \FFI\CIntPtrPtr::class,
"uint8_t" => \FFI\CInt::class,
"uint8_t *" => \FFI\CIntPtr::class,
"uint8_t **" => \FFI\CIntPtrPtr::class,

"int8_t" => \FFI\CInt::class,
"int8_t*" => \FFI\CIntPtr::class,
"int8_t *" => \FFI\CIntPtr::class,
"int8_t**" => \FFI\CIntPtrPtr::class,
"int8_t **" => \FFI\CIntPtrPtr::class,

"uint16_t" => \FFI\CInt::class,
"uint16_t *" => \FFI\CIntPtr::class,
"uint16_t **" => \FFI\CIntPtrPtr::class,

"int16_t" => \FFI\CInt::class,
"int16_t *" => \FFI\CIntPtr::class,
"int16_t **" => \FFI\CIntPtrPtr::class,

"uint32_t" => \FFI\CInt::class,
"uint32_t *" => \FFI\CIntPtr::class,
"uint32_t **" => \FFI\CIntPtrPtr::class,

"int32_t" => \FFI\CInt::class,
"int32_t *" => \FFI\CIntPtr::class,
"int32_t **" => \FFI\CIntPtrPtr::class,

"uint64_t" => \FFI\CInt::class,
"uint64_t *" => \FFI\CIntPtr::class,
"uint64_t **" => \FFI\CIntPtrPtr::class,

"int64_t" => \FFI\CInt::class,
"int64_t *" => \FFI\CIntPtr::class,
"int64_t **" => \FFI\CIntPtrPtr::class,

// bool
"bool" => \FFI\CBool::class,
"bool *" => \FFI\CBoolPtr::class,
"bool **" => \FFI\CBoolPtrPtr::class,

// string
"char" => \FFI\CChar::class,
"char *" => \FFI\CStringPtr::class,
"char **" => \FFI\CStringPtrPtr::class,
]));

override(\SDL\SDL::addr(), map([
override(\Serafim\SDL\SDL::addr(), map([
'' => '@Ptr',
]));
override(\SDL\Support\ProxyTrait::addr(), map([
'' => '@Ptr',
]));

expectedArguments(\SDL\SDL::new(), 0, argumentsSet('ffiCType'));
expectedArguments(\SDL\Support\ProxyTrait::new(), 0, argumentsSet('ffiCType'));

expectedArguments(\SDL\SDL::cast(), 0, argumentsSet('ffiCType'));
expectedArguments(\SDL\Support\ProxyTrait::cast(), 0, argumentsSet('ffiCType'));
expectedArguments(\Serafim\SDL\SDL::new(), 0, argumentsSet('ffiCType'));
expectedArguments(\Serafim\SDL\SDL::cast(), 0, argumentsSet('ffiCType'));
}
29 changes: 7 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,6 @@ The library API completely supports and repeats the analogue in the C language.

- [SDL2 official documentation](https://wiki.libsdl.org/FrontPage)

To support autocomplete, please add a link to `\SDL\NativeApiAutocomplete`:

```php
/** @var \SDL\SDLNativeApiAutocomplete $sdl */
$sdl = new \SDL\SDL();
```

In addition, the library contains functionality adapted for PHP.
- All methods are converted to the PSR style
- For example `$sdl->init(...)` instead of `$sdl->SDL_Init(...)`.
- In case of errors, methods throw exceptions.
- Removed passing arguments by reference during initialization
- For example `$sdl->getVersion()` instead of `$sdl->SDL_GetVersion($versionPointer)`.
- All arguments that accept a boolean in c-format (short int) are replaced by a boolean.
- Added default arguments in some methods

#### Notes

- API not yet fully documented and may not work in places.
Expand All @@ -74,11 +58,10 @@ In addition, the library contains functionality adapted for PHP.
## Example

```php
use SDL\SDL;
use SDL\Event;
use SDL\Kernel\Event\Type;
use Serafim\SDL\SDL;
use Serafim\SDL\Event;
use Serafim\SDL\Kernel\Event\Type;

/** @var SDL|\SDL\SDLNativeApiAutocomplete $sdl */
$sdl = new SDL();

$sdl->SDL_Init(SDL::SDL_INIT_VIDEO);
Expand All @@ -97,10 +80,12 @@ if ($window === null) {
}

$event = $sdl->new(Event::class);
$running = true;

while($sdl->SDL_PollEvent(SDL::addr($event))) {
while ($running) {
$sdl->SDL_PollEvent(SDL::addr($event));
if ($event->type === Type::SDL_QUIT) {
break;
$running = false;
}
}

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
],
"require": {
"php": ">=7.4",
"ext-ffi": "*"
"ext-ffi": "*",
"serafim/ffi-loader": "^1.0"
},
"autoload": {
"psr-4": {
"SDL\\": "src"
"Serafim\\SDL\\": "src"
}
},
"require-dev": {
Expand All @@ -39,7 +40,7 @@
},
"autoload-dev": {
"psr-4": {
"SDL\\Tests\\": "tests"
"Serafim\\SDL\\Tests\\": "tests"
}
},
"config": {
Expand Down
Loading

0 comments on commit ded8d90

Please sign in to comment.