Program won't show picture OR stop when told

Started by
6 comments, last by Poigahn 11 years, 1 month ago

I am working through Lazy Foo's tutorials on SDL, and I was on lesson 4 (Event Driven Programming) and everything was going exactly how it should have been going, untill I tried to run my program. The program would run like it was supposed to, meaning that there were no warnings about improper sintax or missing DLLs or anything like that, but all I was seeing was a blank window pop up.

The program is supposed to show a picture (in my case a tree but that isn't important), and stay open untill I hit the X in the upper right hand corner, but for some reason it's not doing either of those. Like I said I'm seeing a blank screen, but the program also won't close when told to. I'm able to close it through stopping the debug, but that's not how it's supposed to work.

I've attached the program file here: [attachment=14025:Events.zip] so that you can see if I've missed something there, and here is the code in case you can't open the files on your computer:


#include "SDL.h"
#include "SDL_image.h"
#include <string>

const int ScreenWidth = 640;
const int ScreenHeight = 480;
const int ScreenBPP = 32;

SDL_Surface* Image = NULL;
SDL_Surface* Screen = NULL;

//SDL event structure
SDL_Event Event;

SDL_Surface* load_image(std::string filename)
{
    //Load Image
    SDL_Surface* LoadedImage = NULL;

    //Optimize
    SDL_Surface* OptimizedImage = NULL;

    //load image with SDL image
    LoadedImage = IMG_Load(filename.c_str());

    //if the image loaded fine
    if (LoadedImage != NULL)
    {
        //create the optimized image
        OptimizedImage = SDL_DisplayFormat(LoadedImage);

        //free the old image
        SDL_FreeSurface(LoadedImage);
    }

    //return the optimized image
    return OptimizedImage;
}

void ApplySurface(int x, int y, SDL_Surface* Source, SDL_Surface* Destination)
{
    //temp rectangle to hold offsets
    SDL_Rect offset;

    //get the offsets
    offset.x;
    offset.y;

    //blit to the surface
    SDL_BlitSurface(Source,NULL,Destination,&offset);
}

bool Init()
{
    //terminate program if init fails
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    }

    //set up the screen
    Screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight, ScreenBPP, SDL_SWSURFACE);

    //if the screen failed to start
    if(Screen == NULL)
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption("Event Test",NULL);

    //if everything went fine
    return true;
}

bool LoadFiles()
{
    //load the image
    Image = load_image("Tree.png");

    //if there was an error
    if (Image == NULL)
    {
        return false;
    }

    //if everything loaded properly
    return true;
}

void Cleanup()
{
    //free the image
    SDL_FreeSurface(Image);

    //quit SDL
    SDL_Quit();
}

int main (int argc, char* args[])
{
    //make sure the program doesn't terminate immediately
    bool Quit = false;

    //initialize
    if (Init() == false)
    {
        return 1;
    }

    //load the image
    if (LoadFiles() == false)
    {
        return 1;
    }

    //apply the surface
    ApplySurface(0,0,Image,Screen);

    //update the screen
    if (SDL_Flip(Screen) == -1)
    {
        return 1;
    }

    //start main loop
    while (Quit == false)
    {
        //get input of some kind
        while (SDL_PollEvent(&Event))
        {
            //if user X's out
            if (Event.type == SDL_QUIT)
            {
                //quit the program
                Quit == true;
            }
        }
    }

    //free the surface and quit the program
    Cleanup();

    return 0;
}
 

Any help would be apreciated. Thank you in advance.

Advertisement

//quit the program
Quit == true;

This part needs some fixing.


//quit the program
Quit == true;

This part needs some fixing.

oh.... It's amazing that no matter how hard I look I always miss that one little detail. Thanks!


//quit the program
Quit == true;

This part needs some fixing.

Well now the program will close, but the picture still isn't showing up.

//get the offsets
offset.x;

offset.y;

Uh... No, it doesn't.

Whack the warning level up to the max. You should get warnings about statements having no effect (or is that gcc only?)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Just curious. Did you try stepping through the program with the debugger?

I guess I'm assuming windows and Visual Studio..

-=[Megahertz]=-

Since you are in the Type - Test - Figure it out phase, Try Hardwiring the variables you are attempting to pass. Until you are totally comfortable with writing, and I know all to well, the authors of books make typo errors. Also, to start with I think you are trying to do much error trapping. This in itself could also become a big trap until you become totally comfortable with it.

CONFESSION : I myself do not know all the ins and outs of the C++ and it's many libraries but try simplifying it.

Instead of all of the sub-routines, write everything in MAIN in linear format, Keep it simple. Load your image into a variable called MYTREE. , then apply it to your screen. Do not worry about the error trapping ,Focus on loading the image. also make sure that your code and image are set to the same Directory.

If they are in different directories, you could very well not be loading nothing at all and that is what you are seeing.

You may want to write a small snippet, with an error message box to test if the "Tree.PNG" file exist where you are telling the program to look for it. In Fact I think that is the first thing you should check, Make sure your image is in your active directory.

Let me know if that was the problem!

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

This topic is closed to new replies.

Advertisement