Jump to content
  • Advertisement
Sign in to follow this  
c4c0d3m0n

[SDL] Initializing problem (screen surface)

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

mysdlfuncs.h
/**
  * mysdlfuncs.h
  * Functions that enlighten SDL-hell
  *
**/


// Initialize SDL stuff
bool Init( SDL_Surface *screensurface, int screen_width, int screen_height )
{
    // Initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return false;
    }

    // Make sure SDL cleans up before exit
    atexit( SDL_Quit );

    // Create a new window
    SDL_WM_SetCaption( "TetriSDL2", NULL );
    screensurface = SDL_SetVideoMode( screen_width, screen_height, 32, SDL_SWSURFACE);
    if ( !screensurface )
    {
        printf( "Unable to set video: %s\n", SDL_GetError() );
        return false;
    }

    return true;
}


// SDL_MapRGB, but then with an SDL_Color
Uint32 SDL_MapColor( SDL_PixelFormat *fmt, SDL_Color color )
{
    return SDL_MapRGB( fmt, color.r, color.g, color.b );
}


/**
  * End of mysdlfuncs.h
  *
**/

globals.h
/**
  * myglobals.h
  * All your globals are belong to us
  *
**/

// Constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;



// Enumeration for colornames
enum colornames
{
    yellowa, yellowb, yellowc,
    orangea, orangeb, orangec,
    browna, brownb, brownc,
    greena, greenb, greenc,
    bluea, blueb, bluec,
    purplea, purpleb, purplec,
    reda, redb, redc,
    whitea, whiteb, whitec,
    blacka, blackb, blackc
};
// An array with the colors in it (All Tango! colors)
SDL_Color color[27] = { { 0xfc, 0xe9, 0x4f }, { 0xed, 0xd4, 0x00 }, { 0xc4, 0xa0, 0x00 }, // Butter
                        { 0xfc, 0xaf, 0x3e }, { 0xf5, 0x79, 0x00 }, { 0xce, 0x5c, 0x00 }, // Orange
                        { 0xe9, 0xb9, 0x6e }, { 0xc1, 0x7d, 0x11 }, { 0x8f, 0x59, 0x02 }, // Chocolate
                        { 0x8a, 0xe2, 0x34 }, { 0x73, 0xd2, 0x16 }, { 0x4e, 0x9a, 0x06 }, // Chameleon
                        { 0x72, 0x9f, 0xcf }, { 0x34, 0x65, 0xa4 }, { 0x20, 0x4a, 0x87 }, // Sky Blue
                        { 0xad, 0x7f, 0xa8 }, { 0x75, 0x50, 0x7b }, { 0x5c, 0x35, 0x66 }, // Plum
                        { 0xef, 0x29, 0x29 }, { 0xcc, 0x00, 0x00 }, { 0xa4, 0x00, 0x00 }, // Scarlet Red
                        { 0xee, 0xee, 0xee }, { 0xd3, 0xd7, 0xcd }, { 0xba, 0xbd, 0xb6 }, // Aluminium
                        { 0x88, 0x8a, 0x85 }, { 0x55, 0x57, 0x53 }, { 0x2e, 0x34, 0x36 } };


// The screen surface that will be flipped
SDL_Surface *screen = NULL;

// Event structure
SDL_Event event;



/**
  * End of myglobals.h
  *
**/

This is a header just to group my globals Main function
int main ( int argc, char* argv[] )
{
    if( !Init( screen, SCREEN_WIDTH, SCREEN_HEIGHT ) ) {
        printf( "Initialization failed, returning...\n" );
        return -1;
    }

    // Timestep variables
    int t = 0;
    const int dt = 1;
    int current_time = SDL_GetTicks();
    int accumulator = 0;
    int new_time;
    int d_time;

    // Loading...
    SDL_Rect backgroundrect;
    backgroundrect.x = backgroundrect.y = 0;
    backgroundrect.h = SCREEN_HEIGHT;
    backgroundrect.w = SCREEN_WIDTH;


    // MAIN LOOP STARTS HERE

    bool quit = false;
    while( !quit ) {
        // TIMESTEP STARTS HERE

        new_time = SDL_GetTicks();
        d_time = new_time - current_time;
        current_time = new_time;

        accumulator += d_time;

        while ( accumulator >= dt )
        {
            // Process here
            t += dt;
            accumulator -= dt;
        }


        // TIMESTEP ENDS HERE

        while ( SDL_PollEvent(&event) )
        {
            if( event.type == SDL_QUIT ) {
                printf( "Received exit-request, leaving main loop...\n" );
                quit = true;
            }
        } // END OF POLLING

        // DRAWING STARTS HERE

        SDL_FillRect( screen, &backgroundrect, SDL_MapColor( screen->format, color[bluec] ) );

        // DRAWING ENDS HERE

        SDL_Flip( screen );

    } // END OF MAIN LOOP

    return 0;
}
The correct files are #included, this is just the main function Does it compile?: Yes it does :) The problem: The window shuts down straight-away when running this. Detailed analysis: If I comment out SDL_FillRect( screen, &backgroundrect, SDL_MapColor( screen->format, color[bluec] ); and SDL_Flip( screen );, the window stays open. The problem must lie in *screen still pointing to NULL. That must mean that my Init() function doesn't do what I want it to do -> Do magix to *screen Question: How can I fix my Init() function to do what I want? I hope my description is better this time ;)

Share this post


Link to post
Share on other sites
Advertisement
You pass in a copy of an SDL_Surface pointer to Init. You then alter the value of that copy. And at the end of Init, that copy is thrown away.

Either pass in the pointer by reference so that the original value (screen) is altered, or pass a pointer to a pointer and dereference that inside Init.

Better still, do neither and call SDL_GetVideoSurface() when you need the screen.

Share this post


Link to post
Share on other sites
In Init you need to pass a pointer to pointer of SDL_Surface
since you want to return the pointer.

bool Init( SDL_Surface **screensurface, int screen_width, int screen_height )
{
...
...
*screensurface = SDL_SetVideoMode( screen_width, screen_height, 32, SDL_SWSURFACE);
}


and in main you call
Init( &screen, SCREEN_WIDTH, SCREEN_HEIGHT ) 


Hope this helps

Edit: yeah, what Kylotan said ;)

Share this post


Link to post
Share on other sites
Alright, I went with the way described in CableGuy's post. It works. I knew I was gonna have to do something with pointers and references, but I don't really know what's what, so I tried gambling around with the symbols for a while, without success...

Can someone please explain this better method a little further, of calling SDL_GetVideoSurface()? Do I just call it instead of using screen wherever I would normally use screen?

Share this post


Link to post
Share on other sites
Quote:
Original post by c4c0d3m0n
Alright, I went with the way described in CableGuy's post. It works. I knew I was gonna have to do something with pointers and references, but I don't really know what's what, so I tried gambling around with the symbols for a while, without success...

Can someone please explain this better method a little further, of calling SDL_GetVideoSurface()? Do I just call it instead of using screen wherever I would normally use screen?


Pretty much. That is what I do. Because SDL only allows you to have one screen at a time, I just use that function and don't bother passing the screen pointer around.

An safer alternative to CableGuys method would be to use a reference:

typedef SDL_Surface *SurfacePtr;

bool Init( SurfacePtr &screensurface, int screen_width, int screen_height )
{
// ...
// ...
// no longer need to dereference!
screensurface = SDL_SetVideoMode( screen_width, screen_height, 32, SDL_SWSURFACE);
}

// Usage:
int main ( int argc, char* argv[] )
{
// no need to take address
if( !Init( screen, SCREEN_WIDTH, SCREEN_HEIGHT ) ) {
printf( "Initialization failed, returning...\n" );
return -1;
}

Share this post


Link to post
Share on other sites
Alright, thanks :D I've learned something again today. One of these days, I'll get a full grasp on SDL hehe. Now on to the real challenges of Tetris. You'll be hearing from me :)

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!