• Advertisement
Sign in to follow this  

SDL Init problem

This topic is 4740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm learning SDL and I made a library with some functions, one is SDL_Surface Initw(); The problem is that if I use the function when I execute the program, it just open and close the window, but if I cute'n page the code inside the main, the program works perfect... Why is it doing thise? Function Code: if(!SDL_Init(SDL_INIT_VIDEO>0)) cout<<"Error:: failed init Video: "<<SDL_GetError()<<"\n\a"; if(!SDL_Init(SDL_INIT_AUDIO>0)) cout<<"Error:: failed init Audio: "<<SDL_GetError()<<"\n\a"; if(!SDL_Init(SDL_INIT_TIMER>0)) cout<<"Error:: failed init Timer: "<<SDL_GetError()<<"\n\a"; SDL_Surface *wscreen; wscreen=SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); if(wscreen!=0) cout<<"Error:: failed setting the screen: "<<SDL_GetError()<<"\n\a"; return *wscreen; Main Code: int main(int argc, char *argv[]) { int done=0; SDL_Surface *screen; *screen=Initw(); while(done == 0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } DrawScene(screen); } atexit(SDL_Quit); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
It should follow this format as well when you break it down:


if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
cout<<"Error:: failed init Video: "<<SDL_GetError()<<"\n\a";
if( SDL_InitSubSystem( SDL_INIT_AUDIO ) < 0 )
cout<<"Error:: failed init Audio: "<<SDL_GetError()<<"\n\a";
if( SDL_InitSubSystem( SDL_INIT_TIMER ) < 0 )
cout<<"Error:: failed init Timer: "<<SDL_GetError()<<"\n\a";


But since you are doing all those at once, you can just do:


if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER ) < 0 )
cout<<"Error:: failed init SDL: "<<SDL_GetError()<<"\n\a";


Here is the docs page for SDL_InitSubSystem. Note that it returns -1 on failure and 0 on success.

- Drew

ALSO
You can just do this:

void FunctionCode()
{
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER ) < 0 )
cout<<"Error:: failed init SDL: "<<SDL_GetError()<<"\n\a";
SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
}


Then in your main function:

screen = SDL_GetVideoSurface();


SDL_GetVideoSurface documentation. You really need to work with the SDL_Surface* pointers rather than dereference them and try to copy. With that said, instead of having a screen variable that you pass, you just call the SDL_GetVideoSurface everytime you need the main screen.

Share this post


Link to post
Share on other sites
Yea i know, i wrote it that way in order to give the option to init AUDIO and TIMER, i just want to know why it isn't working as a function...

Share this post


Link to post
Share on other sites
Quote:
Original post by Macerlask
Yea i know, i wrote it that way in order to give the option to init AUDIO and TIMER, i just want to know why it isn't working as a function...


I updated my post, but the the logics of your functions I believe are what making it not work. I have used SDL in .dlls before and various functions with no problems at all, so it does indeed work [smile].

Share this post


Link to post
Share on other sites
Ok I made the mods, but it still not working as a function, it just function if I insert de code in Main()

Share this post


Link to post
Share on other sites

...
return *wscreen;

...
SDL_Surface *screen;
*screen=Initw();
...



Don't you want:


...
return wscreen;

...
SDL_Surface *screen;
screen=Initw();
...



That is, to return a pointer to the screen object?

Share this post


Link to post
Share on other sites
No, I'm returning the pointer, if I compile without '*' i get thise

"cannot convert 'SDL_Surface' to 'SDL_Surface' in assigment"

but Thanks...

Share this post


Link to post
Share on other sites
I return *wscreen from function Initw()
*screen=Initw();

That answer your question?
some times my translation is not very good :p

Share this post


Link to post
Share on other sites
If you make the changes I suggested above, be sure to change the function so it returns a pointer to an SDL_Surface:


SDL_Surface * Initw(void)
{
...
return wscreen;
}

...
SDL_Surface *screen;
screen=Initw();
...


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement