SDL_RWread -- A helper macro to simplify reading from an SDL_RWops structure


#include "SDL_rwops.h"

#define SDL_RWread(ctx, ptr, size, n)   (ctx)->read(ctx, ptr, size, n)


SDL_RWread is a helper macro calls the function pointed to by an SDL_RWops structure's read member. It takes the same 4 parameters as the function pointer:

  1. A pointer to an SDL_RWops structure
  2. A pointer to an area of memory to read data into
  3. The size of each block of memory to read
  4. The maxinum number of memory blocks to read(it may read less)

Return Value

It returns the number of memory blocks read, or -1 if the read failed.

Note: This is not a built in function. This is a C macro that calls whatever function happens to be in the 'read' member of the SDL_RWops structure.

Bug: until SDL 1.2.9 this function returned inconsistent values, that depend on type of underlying stream. Read this mail for more details, or here:

"Reading from memory was not consistant with how reading from files worked. A file of 55 bytes stored into 'ptr' by using 6 sets of 10 bytes would copy 55 bytes into 'ptr' and SDL_RWread would return 5. But the same act done on mem RWop would result in 50 bytes copied and 5 returned."

This bug is now solved.


#include <stdio.h>
#include "SDL_rwops.h"
int main()
  int blocks;
  char buf[256];
  SDL_RWops *rw=SDL_RWFromFile("file.bin","rb");
    fprintf(stderr,"Couldn't open file.bin\n");

    fprintf(stderr,"Couldn't read from file.bin\n");

  fprintf(stderr,"Read %d 16-byte blocks\n",blocks);

See Also

SDL_RWseek, SDL_RWwrite, SDL_RWclose, SDL_RWFromFile

SDL_RWread (last edited 2008-04-17 08:18:12 by localhost)