Jump to content
  • Advertisement
Sign in to follow this  

SDL problem

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

I have been going thru the SDL tutorials at Lazy Foo productions. I have made it to the true type font and key press tutorials. When I have gone thru and compile my program, it just flashes up and flashes away. it doesn't allow me to do anything. I have tried adding a SDL_Delay() just to see some output and nothing. Here is my code:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *background = NULL;
SDL_Surface *up = NULL;
SDL_Surface *down = NULL;
SDL_Surface *left = NULL;
SDL_Surface *right = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;

SDL_Event event;

TTF_Font *font = NULL;

SDL_Color textColor = { 0, 0, 0 };

SDL_Surface *load_image( std::string filename ) 
{
    SDL_Surface* loadedImage = NULL;
    
    SDL_Surface* optimizedImage = NULL;
    
    loadedImage = IMG_Load( filename.c_str() );
    
    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        SDL_FreeSurface( loadedImage );
        
        if( optimizedImage != NULL )
        {
            SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }
    
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    SDL_Rect offset;
    
    offset.x = x;
    offset.y = y;
    
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;    
    }
    
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    if( screen == NULL )
    {
        return false;    
    }
    
    if( TTF_Init() == -1 )
    {
        return false;    
    }
    
    SDL_WM_SetCaption( "Press an Arrow Key", NULL );
    
    return true;
}

bool load_files()
{
    background = load_image( "background.png" );
    
    font = TTF_OpenFont( "lazy.ttf", 72 );
    
    if( background == NULL )
    {
        return false;    
    }
    
    if( font == NULL )
    {
        return false;
    }
    
    return true;
}

void clean_up()
{
    SDL_FreeSurface( background );
    SDL_FreeSurface( message );
    SDL_FreeSurface( up );
    SDL_FreeSurface( down );
    SDL_FreeSurface( left );
    SDL_FreeSurface( right );
    
    TTF_CloseFont( font );
    
    TTF_Quit();
    
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    bool quit = false;
    
    if( init() == false )
    {
        return 1;
    }
    
    if( load_files() == false )
    {
        return 1;
    }
    
    up = TTF_RenderText_Solid( font, "Up was pressed.", textColor );
    down = TTF_RenderText_Solid( font, "Down was pressed.", textColor );
    left = TTF_RenderText_Solid( font, "Left was pressed", textColor );
    right = TTF_RenderText_Solid( font, "Right was pressed", textColor );
    
    apply_surface( 0, 0, background, screen );
    
    while( quit == false )
    {
        if( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                switch( event.key.keysym.sym )
                {
                    case SDLK_UP: message = up; break;
                    case SDLK_DOWN: message = down; break;
                    case SDLK_LEFT: message = left; break;
                    case SDLK_RIGHT: message = right; break;    
                }
            }
            
            else if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        
        if( message != NULL )
        {
            apply_surface( 0, 0, background, screen );
            
            apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );
            
            message = NULL;
        }
    
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;    
        }
    }
        
    clean_up();
    
    return 0;    
}

what am I doing wrong

Share this post


Link to post
Share on other sites
Advertisement
Did you try debugging this code already? Might be tricky if it's a full-screen app, in that case, you could try logging to a text file, to see how the program flow went, or using assertions to make sure some variabeles don't have values they shouldn't have.

I don't have any experience with SDL (yet), but at least I can give you some idea's on how to approach this. Hope it helps.

Share this post


Link to post
Share on other sites
Hi bub.

I've taken your code and compiled and gaved me the same result.
The cause was that I was missing the two files:
background.png
lazy.ttf
and because of that load_files() returned false and the program exits after the window is created.

I went to the tutorial site and downloaded the zip file for lesson8 (I assumed it was that lesson) and copied those two files to the directory where my code is and it worked fine. So, just make shure your program can find those two files. If they are not in the same directory where your code is, try to give the absolute paths to them in load_files.

<edit performed for typos>

[Edited by - wolverine on January 26, 2007 8:41:26 AM]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!