Thread: Need Help With SDL

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    5

    Need Help With SDL

    So iv been Coding in C++ for a while, but I only recently started in SDL. After reveiwing my code for hours, and even comparing it with the code available for download, I only found differences were I changed the names of files and chose not to make new files for each progress, and just continued working were the last tut left off. Iv been using Lazyfoo's (Lazy Foo' Productions) Amazingly nice tuts, im on lesson #7 (Lazy Foo' Productions) and this is where I started having the problem.

    The problem is: My window closes instantly after opening without showing any images.

    I get no errors what so ever, and suspected the png/bmp files to be corrupt so I made new ones and still no progress. I also thought that it might have been in the quit loop where it got caught and closed, however I looked over and even copied his and it didn't fix it. Iv got no clue what else it could be so I turned to here. (on an off topic, im currently in a huge blizzard, so if i try a suggestion that works, or I dont reply, im just trying to do stuff as quickly as possible) Im not sure(or I am just stupid) how to post my code as a text file or whatever, so im uploading it to google docs(https://docs.google.com/document/d/1...yoCzNfQm8/edit ) - view only. im putting the link to the editable one here (https://docs.google.com/demo/edit?id...+docs#document )
    If you know for a fact something will fix it just edit there and let me know!

    Thanks for all help, I tried to be as descriptive as possible!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The code:

    Code:
    #include "SDL.h"
    #include "SDL_image.h"
    #include "SDL_ttf.h"
    #include <string>
    //Screen diameters
    const int SCREEN_WIDTH = 720;
    const int SCREEN_HEIGHT = 480;
    const int SCREEN_BPP = 32;
    
    //The event stucture that will be used
    SDL_Event event;
    
    //The font thats's going to be used
    TTF_Font *font = NULL;
    
    // the color of the text
    SDL_Color textColor = {255, 255, 255};
    
    //the portions of the sprite map to be blitted
    SDL_Rect clip [ 4 ];
    
    //The images
    SDL_Surface* EAB = NULL;
    SDL_Surface* Background = NULL;
    SDL_Surface* TSS = NULL;
    SDL_Surface* screen = NULL;
    SDL_Surface* Message = NULL;
    
    
    //image loading function
    SDL_Surface *load_image( std::string filename)
    {
        //temporary storage for the image thats loaded
        SDL_Surface* loadedImage = NULL;
    
        //The optimized image that will be used
        SDL_Surface* optimizedImage = NULL;
           
        //Load Image
       loadedImage = IMG_Load( filename.c_str() );
    
        //If the image loaded
        if ( loadedImage != NULL )
        {
            //create an optimized image
            optimizedImage = SDL_DisplayFormat( loadedImage );
    
            //Free the old image
            SDL_FreeSurface( loadedImage );
    
        }
        //return the optimezed image
        return optimizedImage;
    }
    
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
    {
        //Hold the offsets
        SDL_Rect offset;
    
        //Giving the offsets to the rectangle
        offset.x = x;
        offset.y = y;
    
        //Blit the surface
        SDL_BlitSurface ( source, clip, destination, &offset );
    }
    
    bool init()
    {
        //initialize all SDL subsystems
        if (SDL_Init ( SDL_INIT_EVERYTHING ) == -1 )
        {
            return false;
        }
    
        //Set up the screen
        screen= SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
        //if there was an error setting up the screen
        if (screen == NULL)
        {
            return false;
        }
    
        //Initialize SDL_ttf
        if ( TTF_Init() == -1 )
        {
            return false;
        }
    
        //Set window caption
        SDL_WM_SetCaption( "Nekro Mezuka", NULL );
    
        //if everything initizlized fine
        return true;
    }
    
    bool load_files()
    {
        //Load the images
        Background = load_image ( "Background.bmp" );
        EAB = load_image( "EAB.png" );
        TSS = load_image( "TSS.png" );
    
        //IF NOTHING WENT WRONG WHEN LOADING THE IMAGE
        if( Background == NULL )
        {
            return false;
        }
        //IF NOTHING WENT WRONG WHEN LOADING THE IMAGE
        if( EAB == NULL )
        {
            return false;
        }
    
        // open the font
        font = TTF_OpenFont( "lazy.ttf", 28 ) ;
    
        // if there was an error loading the font
        if ( font == NULL )
        {
            return false;
        }
    
    
        return true;
    }
    void clean_up()
    {
        //Remove loaded image
        SDL_FreeSurface( EAB );
        SDL_FreeSurface( Background);
        SDL_FreeSurface( TSS );
        SDL_FreeSurface( Message );
    
        //Close the font that was used
       TTF_CloseFont( font );
        TTF_Quit();
    
        //Quit SDL
        SDL_Quit();
    }
    
    int main ( int argc, char* args[] )
    {
       
        //quit flag
        bool quit = false;
    
        //initialize
        if( init() == false )
        {
            return 1;
        }
        //Load the files
        if ( load_files() == false )
        {
            return 1;
        }
    
        //Clip for the top left
        clip[ 0 ].x = 0;
        clip[ 0 ].y = 0;   
        clip[ 0 ].w = 100;
        clip[ 0 ].h = 100;
    
        //Clip for the top right
        clip [ 1 ].x = 100;
        clip [ 1 ].y = 0;
        clip [ 1 ].w = 100;
        clip [ 1 ].h = 100;
    
        //Clip for the bottom left
        clip[ 2 ].x = 0;
        clip[ 2 ].y = 100;
        clip[ 2 ].w = 100;
        clip[ 2 ].h = 100;
    
        //Clip for the bottom right
        clip[ 3 ].x = 100;
        clip[ 3 ].y = 100;
        clip[ 3 ].w = 100;
        clip[ 3 ].h = 100;
    
        //render the text
        Message = TTF_RenderText_Solid( font, "Please feel free to eat as many bags as you would like", textColor );
    
        //if there was an error rendering the text
        if (Message == NULL)
        {
            return 1;
        }
    
    
        //Apply the background to the screen
        apply_surface( 0, 0, Background, screen);
        //Apply EAB to the screen
        apply_surface( 230, 130, EAB, screen);
        //Appply The test sprite sheet
        apply_surface(0, 0, TSS, screen, &clip [ 0 ] ) ;
        apply_surface(620, 0, TSS, screen, &clip [ 1 ] ) ;
        apply_surface(0, 380, TSS, screen, &clip [ 2 ] ) ;
        apply_surface(620, 380, TSS, screen, &clip [ 3 ] ) ;
    
    
        //Update Screen
        if (SDL_Flip( screen ) == -1)
        {
            return 1;
        }
    
        //while user hasnt quit
        while (quit == false )
        {
            //while theres an event to handle
            while ( SDL_PollEvent( &event ) )
            {
                // if the user has Xed out
                if( event.type == SDL_QUIT )
                {
                    //quit the game
                    quit = true;
                }
            }
    
        }
       
        //free up and quit
        clean_up();
    
        return 0;
    }
    /Not sure how copying and pasting code into code tags, as described in the sticky post entitled "Posting Code? Read This First!", is difficult.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    My guess is either init() or load-files() is returning 1. Since I can't test your PNGs I would ask you to do one of two things.

    Either
    1. Comment out your return 1; inside the load files function.
    Or better.
    2. Comment out all the calls to load the images. If the program runs then test each picture one at a time and see which one is crashing the program.

    Side note I have spent hours on something like this only to realize I saved it as a wrong filetype or a letter wasn't capitalized properly.

    If you do all those things and still it doesn't work report back here.

    I should probably clarify don't apply surface on a surface after you comment out the line that loads an image to it. Since it will be null.
    Last edited by Lesshardtofind; 12-26-2012 at 08:13 PM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    :P sorry I didnt see that, iv been really stressed with Xmas, and this was the last thing i needed, so i just wanted to get it out there.
    Last edited by bigfatmeany; 12-26-2012 at 11:02 PM.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    I tried all of your recomendations/suggestions and no luck. I have even caped my screen with the code on the left and my files for the project on the right- Here( http://puu.sh/1FNBr/9564edf9c6e39e30a28b4394c57a5405 )

    I also changed( and i hope I did this rightt )
    Code:
        //IF NOTHING WENT WRONG WHEN LOADING THE IMAGE    if( EAB == NULL )
        {
            return false;
        }
    
    
        // open the font
        font = TTF_OpenFont( "lazy.ttf", 28 ) ;
    
    
        // if there was an error loading the font
        if ( font == NULL )
        {
            return false;
        }
    To
    Code:
        //IF NOTHING WENT WRONG WHEN LOADING THE IMAGE    if( EAB == NULL )
        {
            return false;
        }
    
    
        // open the font One that is actually installed with windows
        font = TTF_OpenFont( "Lucida Bright.ttf", 28 ) ;
    
    
        // if there was an error loading the font
        if ( font == NULL )
        {
            return false;
        }

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Can you copy paste those errors and post them? Your picture quality blurs the text.

    Also
    Code:
     /IF NOTHING WENT WRONG WHEN LOADING THE IMAGE if( EAB == NULL ) 
    { 
      return false; 
    }
    Is not what I meant and should not compile.

    My suggestion is to take your time and comment out lines that you think may be breaking the code. If you comment something and it does not fix the problem then uncomment that line.

    Do not keep a change in your code that you made to "fix" something if it did not fix it as this will lead to more and more bugs.

    1. Take your time
    2. Analyze each line
    3. If you don't understand a line google it.
    4. Once you understand move to another line.
    5. When u stumble on a line that could break your code adjust it
    6. If step 5. doesn't fix it revert the line back to its original condition
    7. Start back at step one.

    By the end you will understand every line which will fix the problem.

    Once I get home in an hour or two I will post an example of your code adjusted to how I would troubleshoot it. Hope you find the problem first though.
    Last edited by Lesshardtofind; 12-26-2012 at 11:51 PM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Ok, well I fixed it. For some reason, EVERY time I extracted the font file it would corrupt, so what I did was remove it, which didnt fix the problem, and I never put it back. and after stopping for a while then coming back to the tuts I forgot that I needed to extract to windows/fonts and then Copy-Paste ( which worked) and as You said the image quality was a little blurry or i would have thought you might have caught the .ttf file wasnt in the correct folder. So I have now fixed my problem. Also, your right that didnt compile, that was an error on my part which I fixed after making that post, I just didnt catch it then and there. Those errors are not errors, as my last build did include be successful other than the insta-closing.
    Otherwize, Thanks if I seemed hard to work with, its my first time using these forums, or really any forums that has to do with C/C++.

    P.S. I do understand what every single line of my code does, and what is affected by each one. also like I said originally I spent HOURS looking through code and other stuff, and even re-writ the program.
    P.S.S I played your colorblocks game, and unless I missed something, an instruction page is much needed, otherwize it feels like something I would play when im not coding/watching youtube

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Otherwize, Thanks if I seemed hard to work with, its my first time using these forums, or really any forums that has to do with C/C++.
    Welcome to the forums!

    Not at all hard to work with. The fact that you went through the problem solving techniques needed to solve the problem shows that you are at least putting forward the effort to fix and understand the solution. These are extremely important qualities when it comes to programming. You are always going to end up running in to walls and trying to solve new issues and fix bugs that you created! I can't grab the name right now but one of the user's here has a signature that says something like "If you know what you are doing you then you aren't learning." I love that because it really describes what programming is like. When you are learning new things it can be tough escpecially SDL. I remember spending hours trying to get true type fonts to work. Honestly I remember giving up for a week and then coming back and still not getting it. Programming is like anything and it will just take time.

    I would suggest bookmarking the forum if you haven't already! If you stick with programming then I know this forum will turn out to be in invaluable resource to you.

    I played your colorblocks game, and unless I missed something, an instruction page is much needed, otherwize it feels like something I would play when im not coding/watching youtube
    Thanks!

    Yea alot of content is on my list to add. I just kinda whipped the whole thing up yesterday. I just learned JavaScript last week and I had a thought last night.

    "What if I finish this game and it only works in the browser on the OS I have?" (ubuntu 12.1 & firefox)

    That would be a disaster so I figured I should post it for beta and see if any bug reports showed up, since I don't have access to all the resources to test it myself.

    That being said can you tell me what OS/Browser version you tested it with?
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    I would suggest bookmarking the forum if you haven't already! If you stick with programming then I know this forum will turn out to be in invaluable resource to you.
    Done and Done

    That being said can you tell me what OS/Browser version you tested it with?
    Im using windows 8, and i tested on Google chrome, firefox, and Internet explorer, and safari. The only one it wouldnt work with was safari, in that case I just got a blank page, however I never used safari and im not sure why its on my computer. So maybe someone more experienced with it could help in that sense.

    The only bugs I found with the game itself, is that when you click 3 block in a ' ' formation, then they dont add the correct score. Also, the score thing to the left is really buggy, doesn't appear in full AT ALL, and i can barley make out what they are. '

    I also noticed, that if you go to www.lesshardtofind.com then click the tab that is supposed to take you to the game, it throws a 404 error.

Popular pages Recent additions subscribe to a feed

Tags for this Thread