Skip to content

Latest commit

 

History

History
98 lines (73 loc) · 2.42 KB

SDL_AllocRW.mediawiki

File metadata and controls

98 lines (73 loc) · 2.42 KB

Table of Contents

SDL_AllocRW

Use this function to allocate an empty, unpopulated SDL_RWops structure.

Syntax

<syntaxhighlight lang="c"> SDL_RWops* SDL_AllocRW(void); </syntaxhighlight>

Return Value

Returns a pointer to the allocated memory on success, or NULL on failure; call SDL_GetError() for more information.

Remarks

Applications do not need to use this function unless they are providing their own SDL_RWops implementation. If you just need a SDL_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 SDL_RWops must free themselves when their close method is called, all SDL_RWops must be allocated through this function, so they can all be freed correctly with SDL_FreeRW().

Version

This function is available since SDL 2.0.0.

Code Examples

<syntaxhighlight lang="c++"></syntaxhighlight>

  1. include <stdlib.h>
  2. include "SDL.h"
/* These functions should not be used except from pointers in an SDL_RWops */ static Sint64 mysizefunc(SDL_RWops * context) {

    return &#45;1;

}

static Sint64 myseekfunc(SDL_RWops *context, Sint64 offset, int whence) {

    return SDL_SetError(&quot;Can't seek in this kind of SDL_RWops&quot;);

}

static size_t myreadfunc(SDL_RWops *context, void *ptr, size_t size, size_t maxnum) {

    SDL_memset(ptr,0,size*maxnum);
    return maxnum;

}

static size_t mywritefunc(SDL_RWops *context, const void *ptr, size_t size, size_t num) {

    return num;

}

static int myclosefunc(SDL_RWops *context) {

    if(context&#45;&gt;type &#33;= 0xdeadbeef) &#123;
        return SDL_SetError(&quot;Wrong kind of SDL_RWops for myclosefunc()&quot;);
    &#125;

    free(context&#45;&gt;hidden.unknown.data1);
    SDL_FreeRW(context);
    return 0;

}

SDL_RWops *MyCustomRWop(void) {

    SDL_RWops *c=SDL_AllocRW();
    if(c==NULL) return NULL;

    c&#45;&gt;size = mysizefunc;
    c&#45;&gt;seek = myseekfunc;
    c&#45;&gt;read = myreadfunc;
    c&#45;&gt;write = mywritefunc;
    c&#45;&gt;close = myclosefunc;
    c&#45;&gt;type = 0xdeadbeef;
    c&#45;&gt;hidden.unknown.data1 = SDL_malloc(256);
    return c;

} &lt;/syntaxhighlight&gt;</stdlib.h>

Related Functions

SDL_FreeRW

CategoryAPI, CategoryIO