Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

SDL Time!

This topic is 4416 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
Advertisement
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
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
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!