将SDL表面传递给函数

时间:2012-10-31 04:13:33

标签: c++ sdl

我有点困惑如何将SDL_Surface传递给我的函数,以便我可以在SDL中设置我的屏幕。

这是我的错误:

 No operator "=" matches these operands

我的功能是:

void SDL_Start(SDL_Surface screen){
Uint32 videoflags = SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;// | SDL_FULLSCREEN;

// Initialize the SDL library 
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
         fprintf(stderr, "Couldn't initialize SDL: %s\n",
         SDL_GetError());
         exit(500);
    }   
//get player's screen info
const SDL_VideoInfo* myScreen = SDL_GetVideoInfo();


//SDL screen

int reso_x = myScreen->current_w;
int reso_y = myScreen->current_h;
Uint8  video_bpp = 32;

//setup Screen [Error on the line below]
screen = SDL_SetVideoMode(reso_x, reso_y, video_bpp, videoflags|SDL_FULLSCREEN); 
}

这个函数在我的main函数中调用,如:

SDL_Surface *screen;
SDL_Start(*screen); 

任何想法是什么错误?

2 个答案:

答案 0 :(得分:1)

SDL_SetVideoMode返回SDL_Surface*(指针类型),但您将其分配给SDL_Surface(非指针)。

编辑:这可能是你想要的。返回指向新曲面的指针。

SDL_Surface* SDL_Start() {
Uint32 videoflags = SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;// | SDL_FULLSCREEN;

// Initialize the SDL library 
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
         fprintf(stderr, "Couldn't initialize SDL: %s\n",
         SDL_GetError());
         exit(500);
    }   
//get player's screen info
const SDL_VideoInfo* myScreen = SDL_GetVideoInfo();


//SDL screen

int reso_x = myScreen->current_w;
int reso_y = myScreen->current_h;
Uint8  video_bpp = 32;

//setup Screen [Error on the line below]
return SDL_SetVideoMode(reso_x, reso_y, video_bpp, videoflags|SDL_FULLSCREEN); 
}

// ...

SDL_Surface *screen = SDL_Start();

答案 1 :(得分:1)

SDL_SetVideoMode返回指向SDL_Surface的指针。但screen不是指针。你不应该通过值传递SDL_Surface对象,这会弄乱他们的引用计数,以及可能的其他问题。我看到你有两个选择。通过引用传递SDL_Surface指针:

void SDL_Start(SDL_Surface*& screen) {
    ...
    screen = SDL_SetVideoMode(reso_x, reso_y, video_bpp, videoflags|SDL_FULLSCREEN);
}

或者,让函数不带参数,并返回一个指针:

SDL_Surface* SDL_Start() {
    ...
    return SDL_SetVideoMode(reso_x, reso_y, video_bpp, videoflags|SDL_FULLSCREEN);
}