Provide a better real-world example of the SDL_RWread() API change

This commit is contained in:
Sam Lantinga
2023-01-05 00:48:44 -08:00
parent 228d9ae791
commit bb34441474

View File

@@ -545,21 +545,20 @@ SDL_RWread() previously returned 0 at end of file or other error. Now it returns
Code that used to look like this:
```
if (!SDL_RWread(context, ptr, size, 1)) {
... handle error
size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
{
return (size_t)SDL_RWread(stream, ptr, size, nitems);
}
```
should be changed to:
```
if (SDL_RWread(context, ptr, size) != size) {
... handle error
size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
{
Sint64 amount = SDL_RWread(stream, ptr, size * nitems);
if (amount <= 0) {
return 0;
}
```
or, if you're using a custom non-blocking context or are handling variable size data:
```
Sint64 amount = SDL_RWread(context, ptr, maxsize);
if (amount < 0) {
... handle error
return (size_t)(amount / size);
}
```