Jump to content
  • Advertisement
Sign in to follow this  
Kryodus

SDL screen loading in seperate class problem

This topic is 4893 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!