Sir
Sir

Reputation: 8280

Passing an SDL surface to a function

I'm a bit confused how i pass my SDL_Surface to my function so that i can setup my screen in SDL.

This is my error:

 No operator "=" matches these operands

My function is this:

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

This function is called in my main function like so :

SDL_Surface *screen;
SDL_Start(*screen); 

Any ideas what the mistake is ?

Upvotes: 1

Views: 1889

Answers (2)

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

SDL_SetVideoMode returns a pointer to SDL_Surface. But screen is not a pointer. You shouldn't be passing SDL_Surface objects around by value anyway, that will mess with their reference counting, and possibly other problems. You have two options here, as I see it. Pass an SDL_Surface pointer in by reference:

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

Or, have the function take no parameters, and return a pointer:

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

Upvotes: 1

Pubby
Pubby

Reputation: 53017

SDL_SetVideoMode returns a SDL_Surface* (pointer type) but you're assigning it to a SDL_Surface (non pointer).

EDIT: this is probably what you want then. Return a pointer to the new 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();

Upvotes: 1

Related Questions