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,22 +545,21 @@ SDL_RWread() previously returned 0 at end of file or other error. Now it returns
Code that used to look like this: Code that used to look like this:
``` ```
if (!SDL_RWread(context, ptr, size, 1)) { size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
... handle error {
} return (size_t)SDL_RWread(stream, ptr, size, nitems);
}
``` ```
should be changed to: should be changed to:
``` ```
if (SDL_RWread(context, ptr, size) != size) { size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
... handle error {
} Sint64 amount = SDL_RWread(stream, ptr, size * nitems);
``` if (amount <= 0) {
or, if you're using a custom non-blocking context or are handling variable size data: return 0;
```
Sint64 amount = SDL_RWread(context, ptr, maxsize);
if (amount < 0) {
... handle error
} }
return (size_t)(amount / size);
}
``` ```
Similarly, SDL_RWwrite() can return -2 for data not ready in the case of a non-blocking context. There is currently no way to create a non-blocking context, we have simply defined the semantic for your own custom SDL_RWops object. Similarly, SDL_RWwrite() can return -2 for data not ready in the case of a non-blocking context. There is currently no way to create a non-blocking context, we have simply defined the semantic for your own custom SDL_RWops object.