Sign in to follow this  
Kryodus

SDL screen loading in seperate class problem

Recommended Posts

In my game I have a problem when I put my initialization of video and screen in another seperate class, outside of the main.cpp file (where the event loop + main method is in) SDL_Surface *screen; void initVideo() {if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_HWPALETTE); if ( screen == NULL ) { printf("Unable to set 800x600 video: %s\n", SDL_GetError()); exit(1); } } int main(int argc, char *argv[]) { initVideo(); } would work fine.... But when I put the initVideo method in another class as a static method Static void initVideo(SDL_Surface *screen) {*same method as above* } then call it in the main method Video::initVideo(screen); my program crashes..... Ive had similar problems when I load an SDL background and blit it to a screen this way as well... Help would be massivly apreciated , thanks in advance.

Share this post


Link to post
Share on other sites
Looks like you're not assigning anything to the global screen you want to assign stuff to. You're passing in the pointer. As far as your Video::initVideo() is concerned, "screen" is a local variable... to which you assign a value successfully. But then, you're trying to do something with your uninitialized screen global. I bet you do something with your uninitialized screen after. Here, compile and run this to see what I mean:


#include <iostream>
using namespace std;

void assignX(int *x)
{
x = new int(5);
cout << "AssignX: " << *x << endl;
}

int main(void)
{
int *x = NULL;
assignX(x);
if(x == NULL) {
cout << "oh crap, I screwed up\n";
}

return 0;
}


Changing your init to return the screen (screen = Video::initVideo(); ) would be a quick fix, but there's more organized ways... but I'm just mentioning why it's crashing :-).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this