SDL_AllocRW

Use this function to allocate an empty, unpopulated SDL_RWops structure. 使用这个函数可以在内存中分配一个空的SDL_RWops 结构函数。

Syntax 语法

  1. 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. 返回内存的指针代表成功,NULL则是失败;调用SDL_GetError()查看更多信息。

Code Examples 代码示例

  1. /* These functions should not be used except from pointers in an SDL_RWops 这个函数只能在SDL_RWops中使用 */
  2. static int myseekfunc(SDL_RWops *context, int offset, int whence)
  3. {
  4. return SDL_SetError("Can't seek in this kind of SDL_RWops");
  5. }
  6. static int myreadfunc(SDL_RWops *context, void *ptr, int size, int maxnum)
  7. {
  8. SDL_memset(ptr,0,size*maxnum);
  9. return maxnum;
  10. }
  11. static int mywritefunc(SDL_RWops *context, const void *ptr, int size, int num)
  12. {
  13. return num;
  14. }
  15. static int myclosefunc(SDL_RWops *context)
  16. {
  17. if(context->type != 0xdeadbeef)
  18. {
  19. return SDL_SetError("Wrong kind of SDL_RWops for myclosefunc()");
  20. }
  21. free(context->hidden.unknown.data1);
  22. SDL_FreeRW(context);
  23. return 0;
  24. }
  25. SDL_RWops *MyCustomRWop()
  26. {
  27. SDL_RWops *c=SDL_AllocRW();
  28. if(c==NULL) return NULL;
  29. c->seek =myseekfunc;
  30. c->read =myreadfunc;
  31. c->write=mywritefunc;
  32. c->close=myclosefunc;
  33. c->type =0xdeadbeef;
  34. c->hidden.unknown.data1=malloc(256);
  35. return c;
  36. }

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. 如果应用程序不需要这个函数的话,那他们就要提供自己的SDL_RWops去实现这个功能。如果你只需要SDL_RWops 进行读写的通用数据源,你应该使用内置SDL的去实现,像SDL_RWFromFile()或者SDL_RWF()等。

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(). 你必须释放SDL_FromRW()的指针,根据你的操作系统和编译器,有可能是mallo()和free()的程序使用和SDL内部调用版本之间的差异。

Related Functions 相关函数

SDL_FreeRW


@SixerMe Translated