将SDL表面传递到功能

Passing an SDL surface to a function

本文关键字:功能 SDL 表面      更新时间:2023-10-16

我有点困惑如何将我的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: %sn",
         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); 
}

此功能在我的主要函数中称为:

SDL_Surface *screen;
SDL_Start(*screen); 

有什么想法是什么错误?

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: %sn",
         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();

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);
}