Jump to content

  • Log In with Google      Sign In   
  • Create Account

Undetectable bugs.... yay -_-


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Programmer557   Members   -  Reputation: 124

Like
1Likes
Like

Posted 08 March 2013 - 09:22 PM

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;
}
 

 



Sponsor:

#2 Dragonsoulj   Crossbones+   -  Reputation: 2126

Like
1Likes
Like

Posted 08 March 2013 - 09:48 PM

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.



#3 Programmer557   Members   -  Reputation: 124

Like
0Likes
Like

Posted 08 March 2013 - 10:03 PM

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.



#4 Dragonsoulj   Crossbones+   -  Reputation: 2126

Like
1Likes
Like

Posted 08 March 2013 - 10:45 PM

Where did you store your actual image files, and what file names are you using? I may have been wrong about the scope, SDL seems like it passes the addresses correctly.



#5 shacktar   Members   -  Reputation: 841

Like
8Likes
Like

Posted 08 March 2013 - 10:46 PM

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. 


Edited by shacktar, 08 March 2013 - 10:49 PM.


#6 Dragonsoulj   Crossbones+   -  Reputation: 2126

Like
0Likes
Like

Posted 08 March 2013 - 10:50 PM

I wasn't fully familiar with how SDL was handling the images. I do agree his creation and cleanup seem fine.



#7 Programmer557   Members   -  Reputation: 124

Like
0Likes
Like

Posted 08 March 2013 - 11:37 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS