0

还是使用常用的 C 函数更好?

4

1 回答 1

1

SDL 中有一个 I/O API,我不确定它是否更容易,但它应该是可移植的。这些是相关的功能:

SDL_RWops *SDL_RWFromFile(const char *file, const char *mode); // open file
SDL_RWread(ctx, ptr, size, n); //read from file
SDL_RWclose(ctx)   //close file

这里有一个例子,展示了如何打开和读取文件。

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

  blocks=SDL_RWread(rw,buf,16,256/16);
  SDL_RWclose(rw);
  if(blocks<0) {
    fprintf(stderr,"Couldn't read from file.bin\n");
    return(2);
  }

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

编辑:这里有一个关于文件API的教程,加载图像时可能更容易使用:

http://www.aspfree.com/c/a/c-sharp/game-programming-using-sdl-the-file-io-api/

于 2012-11-16T13:16:43.330 回答