• Advertisement
Sign in to follow this  

Undetectable bugs.... yay -_-

This topic is 1780 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 am trying to create a program that is supposed to make specified colors transparent based off of Lazy Foo's 5th SDL tutorial for Windows, Visual c++ 2010 express, but I've run into a problem.  The pictures that I have specified aren't showing up at all.  I have all of the neccesary files where they need to be, and I have checked through the code twice at least, comparing it to previous projects and the tutorial, and looking for my usual mistakes (using == where I should be using =, simple spelling mistakes, ETC.) but I can't find anything.  I may just be dense and am missing a single character somewhere in this code, but I would appreciate any help that can be given.

 

Thank you for your time, here is the code:

 

 

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

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

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

SDL_Event Event;

SDL_Surface* LoadImage(std::string filename)
{
    //the image that's loaded
    SDL_Surface* LoadedImage = NULL;

    //optimized image
    SDL_Surface* OptimizedImage = NULL;

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

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

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

    return OptimizedImage;
}

//apply the surfaces to the screen
void ApplySurface (int x, int y, SDL_Surface* Source,SDL_Surface* Destination)
{
    //temp rect
    SDL_Rect offset;

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

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

bool Init()
{
    //initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    }

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

    //if screen initialazation failed
    if(Screen == NULL)
    {
        return false;
    }

    //set window caption
    SDL_WM_SetCaption("WOOOHOOO FOR NO BOXES!!",NULL);

    //if everything is right
    return true;
}

bool LoadFiles()
{
    //load the background
    Background = LoadImage("Background.png");

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

    //load the image
    Image = LoadImage("Tree.png");

    if(Image == NULL)
    {
        return false;
    }

    //if everything loaded properly
    return true;
}

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

    //free the image
    SDL_FreeSurface(Background);

    //quit SDL
    SDL_Quit();
}

int main (int argc,char* args[])
{
    //keep the program active
    bool Quit = false;

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

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

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

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

    //start the loop
    while (Quit == false)
    {
        //get input
        while (SDL_PollEvent(&Event))
        {
            if(Event.type == SDL_QUIT)
            {
                Quit = true;
            }
        }
    }

    //free the surfaces
    Cleanup();

    return 0;
}
 

 

Share this post


Link to post
Share on other sites
Advertisement

Your globals point to locations in memory, however those locations I believe pass out of scope at the end of the function. You are only creating images that exist for the duration of your load function, and, at the end of the function, they no longer point to valid points in memory.

Share this post


Link to post
Share on other sites

Your globals point to locations in memory, however those locations I believe pass out of scope at the end of the function. You are only creating images that exist for the duration of your load function, and, at the end of the function, they no longer point to valid points in memory.

How do I take care of that?  I may have forgotten to mention that I'm pretty new to C++ so I don't understand how to fix that.

Share this post


Link to post
Share on other sites

Your globals point to locations in memory, however those locations I believe pass out of scope at the end of the function.

That part appears to be fine. Background and Image are set with LoadImage which returns a heap-allocated pointer. SDL_FreeSurface is necessary to free the loaded images (used correctly in Cleanup()).

 

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

Here, you're not actually calling LoadFiles (the brackets are missing). Instead, this code is checking if the function pointer LoadFiles is false i.e. zero, which it isn't of course as it's a valid function. 

 

Thank you very much!  This fixed the problem.  now that the images are loading all I need to do is replace the transparency code and the program will be fine!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement