SDL_AllocRW

Name

SDL_AllocRW -- Allocates an empty, unpopulated SDL_RWops structure.

Synopsis

   1 #include "SDL_rwops.h"
   2 
   3 SDL_RWops *SDL_AllocRW(void);

Description

SDL_AllocRW allocates an empty, unpopulated SDL_RWops structure. You must fill out the fields yourself.

Return Value

Returns a pointer to the allocated memory on success, or NULL on error.

Note: You must free any memory allocated with SDL_AllocRW with SDL_FreeRW. Depending on your operating system and compiler, there may be a difference between the malloc() and free() your program uses and the versions SDL calls internally. Trying to mix the two can cause crashing such as segmentation faults.

Example

   1 #include <string.h>    /* for memset */
   2 #include "SDL_error.h" /* for SDL_SetError */
   3 #include "SDL_rwops.h"
   4 
   5 /* These functions should not be used except from pointers in a RWops */
   6 static int myseekfunc(SDL_RWops *context, int offset, int whence)
   7 {
   8   SDL_SetError("Can't seek in this kind of RWops");
   9   return(-1);
  10 }
  11 
  12 static int myreadfunc(SDL_RWops *context, void *ptr, int size, int maxnum)
  13 {
  14   memset(ptr,0,size*maxnum);
  15   return(maxnum);
  16 }
  17 
  18 static int mywritefunc(SDL_RWops *context, const void *ptr, int size, int num)
  19 {
  20   return(num);
  21 }
  22 
  23 static int myclosefunc(SDL_RWops *context)
  24 {
  25   if(context->type != 0xdeadbeef)
  26   {
  27     SDL_SetError("Wrong kind of RWops for myclosefunc()");
  28     return(-1);
  29   }
  30 
  31   free(context->hidden.unknown.data1);
  32   SDL_FreeRW(context);
  33   return(0);
  34 }
  35 
  36 /* Note that this function is NOT static -- we want it directly callable from other source files */
  37 SDL_RWops *MyCustomRWop()
  38 {
  39   SDL_RWops *c=SDL_AllocRW();
  40   if(c==NULL) return(NULL);
  41 
  42   c->seek =myseekfunc;
  43   c->read =myreadfunc;
  44   c->write=mywritefunc;
  45   c->close=myclosefunc;
  46   c->type =0xdeadbeef;
  47   c->hidden.unknown.data1=malloc(256);
  48   return(c);
  49 }

See Also

SDL_FreeRW

SDL_AllocRW (last edited 2009-02-13 23:35:01 by AlexisDM)