|
Size: 625
Comment: create page, add content (Wed Mar 10 ver; changeset 4428)
|
Size: 2250
Comment: Changed example to use return value of SDL_SetError(), changed return statements looking like function calls.
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 2: | Line 2: |
| #pragma disable-camelcase ||<tablewidth="100%" style="color: #FF0000;" :> DRAFT|| |
#pragma camelcase off |
| Line 6: | Line 5: |
| Use this function to ^allocate a read/write to a specific SDL_RWops structure^. ??? | Use this function to allocate an empty, unpopulated SDL_RWops structure. |
| Line 16: | Line 15: |
| ^Reads or writes to the specified SDL_RWops structure.^ ??? | Returns a pointer to the allocated memory on success, or NULL on failure; call [[SDL_GetError]]() for more information. |
| Line 20: | Line 19: |
| You can add your code example here | /* These functions should not be used except from pointers in a RWops */ static int myseekfunc(SDL_RWops *context, int offset, int whence) { return SDL_SetError("Can't seek in this kind of RWops"); } static int myreadfunc(SDL_RWops *context, void *ptr, int size, int maxnum) { SDL_memset(ptr,0,size*maxnum); return maxnum; } static int mywritefunc(SDL_RWops *context, const void *ptr, int size, int num) { return num; } static int myclosefunc(SDL_RWops *context) { if(context->type != 0xdeadbeef) { return SDL_SetError("Wrong kind of RWops for myclosefunc()"); } free(context->hidden.unknown.data1); SDL_FreeRW(context); return 0; } SDL_RWops *MyCustomRWop() { SDL_RWops *c=SDL_AllocRW(); if(c==NULL) return NULL; c->seek =myseekfunc; c->read =myreadfunc; c->write=mywritefunc; c->close=myclosefunc; c->type =0xdeadbeef; c->hidden.unknown.data1=malloc(256); return c; } |
| Line 24: | Line 64: |
| ''You can add useful comments here'' | Applications do not need to use this function unless they are providing their own RWops implementation. If you just need a RWops to read/write a common data source, you should use the built-in implementations in SDL, like [[SDL_RWFromFile]]() or [[SDL_RWFromMem]](), etc. You must free the returned pointer 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. Since all RWops must free themselves when their '''close''' method is called, all RWops must be allocated through this function, so they can all be freed correctly with [[SDL_FreeRW]](). |
| Line 27: | Line 69: |
| .[[SDL_FreeRW]] ??? | .[[SDL_FreeRW]] |
SDL_AllocRW
Use this function to allocate an empty, unpopulated SDL_RWops structure.
Syntax
SDL_RWops* SDL_AllocRW(void)
Return Value
Returns a pointer to the allocated memory on success, or NULL on failure; call SDL_GetError() for more information.
Code Examples
/* These functions should not be used except from pointers in a RWops */
static int myseekfunc(SDL_RWops *context, int offset, int whence)
{
return SDL_SetError("Can't seek in this kind of RWops");
}
static int myreadfunc(SDL_RWops *context, void *ptr, int size, int maxnum)
{
SDL_memset(ptr,0,size*maxnum);
return maxnum;
}
static int mywritefunc(SDL_RWops *context, const void *ptr, int size, int num)
{
return num;
}
static int myclosefunc(SDL_RWops *context)
{
if(context->type != 0xdeadbeef)
{
return SDL_SetError("Wrong kind of RWops for myclosefunc()");
}
free(context->hidden.unknown.data1);
SDL_FreeRW(context);
return 0;
}
SDL_RWops *MyCustomRWop()
{
SDL_RWops *c=SDL_AllocRW();
if(c==NULL) return NULL;
c->seek =myseekfunc;
c->read =myreadfunc;
c->write=mywritefunc;
c->close=myclosefunc;
c->type =0xdeadbeef;
c->hidden.unknown.data1=malloc(256);
return c;
}
Remarks
Applications do not need to use this function unless they are providing their own RWops implementation. If you just need a RWops to read/write a common data source, you should use the built-in implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc.
You must free the returned pointer 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. Since all RWops must free themselves when their close method is called, all RWops must be allocated through this function, so they can all be freed correctly with SDL_FreeRW().
