Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with C++ SDL tutourial

Options
  • 22-01-2009 1:57pm
    #1
    Closed Accounts Posts: 2,219 ✭✭✭


    Hi All,
    I just recently started a online SDL tutourial and am having problems already.
    The bit of code should change the background from black to a colour,and leave the message "hello world"(you know you've along way to go when you see that quote in a tutourial!).

    The code compiles fine but the background stays black,doesant change colour and the message doesnt show.

    Im running xp pro and using VisualExpress 2008+SP1.

    Any help much appreciated as im at me wits end over it!

    Labmouse

    #include "SDL.h"
    #include<string>
    
    
    
    //constant varibles
    const int SCREEN_WIDTH=640;
    const int SCREEN_HEIGHT=480;
    const int SCREEN_BPP=32;//BPP=bits per pixel--32 bit colour!!!
    
    //the surfaces that will be used
    
    SDL_Surface *message=NULL;// "hello world!
    SDL_Surface *background=NULL;//background image
    SDL_Surface *screen=NULL;//screen
    
    SDL_Surface *load_image(std::string filename)
    {
        //temp storage for image thats stored
        SDL_Surface *loadedImage=NULL;
    
        //optimised image that will be used
        SDL_Surface *optimizedImage=NULL;
    
        //load the image
        loadedImage=SDL_LoadBMP(filename.c_str());
    
        //if nothing went wrong loading image
        if(loadedImage!=NULL)
        {
            //create an optimised image
            optimizedImage=SDL_DisplayFormat(loadedImage);
    
            //free the old image
            SDL_FreeSurface(loadedImage);
        }
    
            //return the opti image
            return optimizedImage;
        }
    
        void apply_surface(int x,int y,SDL_Surface*source,SDL_Surface*destination)
        {
       //make a temp rexct to hold the offsets
        SDL_Rect offset;
    
        //give the offsets to the rect;
        offset.x=x;
        offset.y=y;
    
        //draw the surface(image)
        SDL_BlitSurface(source,NULL,destination,&offset);
        }
    
    
        int main (int argc,char** args)
        {
            //init all sdl subsystems
            if(SDL_Init(SDL_INIT_EVERYTHING)==-1)
            {
                return 1;
            }
    
            //set up the screen
            screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
            //if error in setting up screen
            if(screen==NULL)
            {
                return 1;
            }
    
            //set window caption
            SDL_WM_SetCaption("hello World",NULL);
    
            //load the images
            message=load_image("hello_world.bmp");
            background=load_image("background.bmp");
    
            //apply background to the screen
            apply_surface(0,0,background,screen);
    
            //apply message to screen
            apply_surface(180,140,message,screen);
    
            //update the screen
            if(SDL_Flip(screen)==-1)
            {
                return 1;
            }
    
            //wait 2 seconds
            SDL_Delay(2000);
    
    
            //free surfaces
            SDL_FreeSurface(message);
            SDL_FreeSurface(background);
    
            //quitsdl
            SDL_Quit();
            return 0;
    
        }
    
    


Comments

  • Registered Users Posts: 378 ✭✭sicruise


    note:I haven't looked at your code integrity.

    The first obvious question is... are the image files actually in your debug/project folder? It may be easier for you to specify the exact path.


  • Closed Accounts Posts: 2,219 ✭✭✭Lab_Mouse


    hi sicruise..that hadnt occurred to me.The images arent stored in the debugg file..in the tutourial he had the SDL files on his c drive whereas i have it in my documents and have it linked to there..will move the sdl folder and see if that gets it going.

    How would you set the path for the images?

    thanks for tyhe quick reply

    the image IS in the debug folder so i thimk it is looking else where for the image.

    again excuse my ignorance


  • Registered Users Posts: 378 ✭✭sicruise


    Just change
    message=load_image("hello_world.bmp");
    

    to
    message=load_image("c:\\location\\hello_world.bmp");
    

    No problem... its usually something simple that gets you stuck at the start.


  • Closed Accounts Posts: 2,219 ✭✭✭Lab_Mouse


    Thanks sicruise that worked straight off..Will know next time to check the paths for the bitmaps


  • Registered Users Posts: 378 ✭✭sicruise


    No problem, glad you got it sorted.


  • Advertisement
  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    You can also do

    message=load_image("c:/location/hello_world.bmp");

    which looks nicer and might be less problematic in certain cases :)


Advertisement