Jump to content

  • Log In with Google      Sign In   
  • Create Account


LatchGameDev

Member Since 12 Apr 2012
Offline Last Active Sep 20 2014 11:37 PM

Posts I've Made

In Topic: SDL and Copy Constructors

05 December 2013 - 08:01 PM

Is it common practice to include the header file more then once? In my main file i just have .h after .h listed. I always thought this way was better since I used to do it like

a.h

a.cpp

b.h

b.cpp

c.h

c.cpp

 

Now a days I do

 

a.h

b.h

c.h

 

with the .cpp included from the .h


In Topic: SDL and Copy Constructors

05 December 2013 - 07:25 PM

The constructor function is called as soon as that object is created, so if your object is not dynamically allocated to memory during run time this will be before the main function of your program is even called. Depending on how you've set up your program, your constructor may be calling an SDL function before SDL has been initialized.

 

 

OMG I had no idea that copy constructor would run before main. I changed it to a pointer and now use the new keyword when I get into main. It stopped the crashing and after a few more hours of poking around I got it to work how I wanted to. So thank you, I feel like I need to research pointers more. I get the concept behind them but actually using them is giving me trouble. It seems I only run into problems with SDL_Surface tho.

 

 

#include"SmartImage.cpp"

What is that?

 

 

 

It's the .cpp file to the .h I don't see a problem with that line of code. I also listed the .cpp in my posts soooooooo what's up?


In Topic: SDL and Copy Constructors

05 December 2013 - 03:44 PM

I know of the rule of three but i'm having trouble implementing it. I'm looking at a tutorial and trying to implement the copy constructor,copy assignment operator, and the destructor. This is what i'm looking at http://www.cplusplus.com/doc/tutorial/classes2/ and i'm about half way down the page.

 

Modified the .h to try and include the assignment operator. It more matches what the tutorial has but I don't think it's going to do what i want. I don't exactly understand how to use the assignment operator tho. Still crashes when I try to make a SmartImage object which means something is majorly wrong right?

class SmartImage
{
    private:
            bool image_loaded;
            
    public:
           SmartImage();
           ~SmartImage();
           // copy constructor:
           SmartImage (const SmartImage& param) : surface(new SDL_Surface(param.content())) {}
           //access content
           const SDL_Surface& content() const {return *surface;}
           SDL_Surface *surface;
           void load(std::string filename);
           void copy_surface(SDL_Surface *target_surface);
};

#include"SmartImage.cpp"

the .cpp

SmartImage::SmartImage()
{
 surface=NULL;
 image_loaded = false;     
}

SmartImage::~SmartImage()
{
 SDL_FreeSurface(surface);      
}

void SmartImage::load(std::string filename)
{
    if(image_loaded == false)
    {
     surface = load_image(filename);
     image_loaded = true;
    }    
    else
    {
     SDL_FreeSurface(surface);
     surface = load_image(filename);
    }  
}

void SmartImage::copy_surface(SDL_Surface *temp_surface)
{
     if(image_loaded == true)
     {
      SDL_FreeSurface(surface);               
     }
 surface = SDL_DisplayFormat(temp_surface); 
 image_loaded = true;     
}

In Topic: SDL Question

13 April 2012 - 10:27 AM

Thank you, Thank you, Thank you :D
I'm only copying surfaces when i'm loaded/reloading the map and it was taking 44 seconds to load my map before. I used your first suggestion and it reduced the time to less then a second! :D

PARTNERS