Sign in to follow this  

SDL Time!

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

Hello, I was creating a little thingy just to mess around with and learn, but I've encountered a problem, The images wont show up! [sad] I dont get any errors, the window shows up, but the screen is just black. Please help, its my first day of SDL [smile] My source:
#include <SDL/SDL.h>
#include <string>

using namespace std;

// screen attributes
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

// images
SDL_Surface * tLeft = 0;
SDL_Surface * bLeft = 0;
SDL_Surface * bRight = 0;
SDL_Surface * tRight = 0;
SDL_Surface * screen = 0;
SDL_Surface * backGround = 0;

SDL_Surface * loadImage( string fileName )
{
    // temp + optimized images
    SDL_Surface * loadedImage = 0;
    SDL_Surface * optImage = 0;
    
    // loads image
    loadedImage = SDL_LoadBMP( fileName.c_str() );
    
    // if image loads fine
    if( loadedImage != NULL )
    {
        optImage = SDL_DisplayFormat( loadedImage );
        
        // frees old image
        SDL_FreeSurface( loadedImage );
    }
    
    return optImage;
}

void applyImage( int x, int y, SDL_Surface * pic, SDL_Surface * dest )
{
    SDL_Rect offSet;
    
    // sets the offSet x/y
    offSet.x = x;
    offSet.y = y;
    
    SDL_BlitSurface( pic, NULL, dest, &offSet );
}

int main( int argc, char* args[] )
{
    // initializes SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }
    
    // loads all the images
    backGround = loadImage( "background.bmp" );
    tLeft = loadImage( "top_left.bmp" );
    tRight = loadImage( "top_right.bmp" );
    bLeft = loadImage( "bottom_left.bmp" );
    bRight = loadImage( "bottom_right.bmp" );
    
    // loads images to screen
    applyImage( 0, 0, backGround, screen );
    applyImage( 0, 0, tLeft, screen );
    applyImage( 0, 500, bLeft, screen );
    applyImage( 500, 0, tRight, screen );
    applyImage( 500, 500, bRight, screen );
    
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    // if screen isnt set up
    if( screen == NULL )
    {
        return 1;
    }
    
    SDL_WM_SetCaption( "The Eye of All Things Holy!", NULL );
    
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
    
    SDL_Delay( 3000 );
    
    // free all the images
    SDL_FreeSurface( tLeft );
    SDL_FreeSurface( tRight );
    SDL_FreeSurface( bLeft );
    SDL_FreeSurface( bRight );
    
    SDL_Quit();
    
    return 0;
}

Share this post


Link to post
Share on other sites
To clarify what lazyfoo is saying, you are creating your window after you're drawing. Not how it's suppose to be done.

You have:
    applyImage( 0, 0, backGround, screen );
applyImage( 0, 0, tLeft, screen );
applyImage( 0, 500, bLeft, screen );
applyImage( 500, 0, tRight, screen );
applyImage( 500, 500, bRight, screen );

screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );


It's suppose to be:
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

applyImage( 0, 0, backGround, screen );
applyImage( 0, 0, tLeft, screen );
applyImage( 0, 500, bLeft, screen );
applyImage( 500, 0, tRight, screen );
applyImage( 500, 500, bRight, screen );


SDL_SetVideoMode is setting up a window with 'screen' as you're painting surface. You can't paint to 'screen' until after it's set up. Or, to be more accurate, you can draw to it, but you are then overwriting it with SDL_SetVideoMode's return value.

At least, that's what I think lazyfoo is saying. He knows more about SDL than I. (I actaully learned from his tutorials [grin])

[Edit:] Also, in an unrelated matter, you're not freeing 'screen' and 'backGround', although this isn't what is causing your problem, and typically your program's resources are freed naturally(if windows 2000/ME/XP) it's generally a good idea to free them all, including your window surface.('screen')

Share this post


Link to post
Share on other sites
Quote:
Original post by Lazy Foo
how can you apply an image to the window if the window isn't set up?


Hey lazy foo! I learned this from ur tutorials!

I have a few question:

1) what exactly does SDL_Flip() do?
2) How do i make it so the console window disapears when i run ur 2nd tut?

Thanx for the help and the good and simple tut's [smile]

Share this post


Link to post
Share on other sites
SDL_Flip updates the screen buffer. For more information: SDL_Flip

The first tutorial at lazy foo's site actually explains how to remove the console window. http://lazyfooproductions.com/SDL_tutorials/lesson01/index.php. If you for example use dev-cpp under windows, you need to change the general project options to Win32 GUI.

Share this post


Link to post
Share on other sites
Quote:
Also, in an unrelated matter, you're not freeing 'screen' ...


You shouldnt free the surface returned by SDL_SetVideoMode.

SDL will free that surface its elf when it has to, wither when you shut down SDL or just its video subsystem, or when you change the resolution by re-calling SDL_SetVideoMode.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Also, in an unrelated matter, you're not freeing 'screen' ...


You shouldnt free the surface returned by SDL_SetVideoMode.

SDL will free that surface its elf when it has to, wither when you shut down SDL or just its video subsystem, or when you change the resolution by re-calling SDL_SetVideoMode.


Really? I never knew that. Still, he didn't free backGround either, unless I'm mistaken.

Share this post


Link to post
Share on other sites

This topic is 4111 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.

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