Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


LatchGameDev

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

Topics I've Started

SDL and Copy Constructors

05 December 2013 - 02:17 PM

So I've been wrestling with this problem for a few days now. So I have a entity class and I thought I would use a vector to make it so i could have a dynamic array of the entity's. It crashed horribly and i learned that in my SmartImage class (located inside the entity class) was only making a shallow copy. So i'm trying to make a deep copy using the copy constructor. My program compiles but it crashes as soon as it launches. It seems even initializing the SmartImage class will cause it to crash. What am i doing wrong?

 

SmartImage.h

class SmartImage
{
    private:
            bool image_loaded;
    public:
           SmartImage();
           ~SmartImage();
           // copy constructor:
           SmartImage (const SmartImage& param);
           
           SDL_Surface *surface;
           void load(std::string filename);
           void copy_surface(SDL_Surface *target_surface);
};
#include"SmartImage.cpp"

SmartImage.cpp

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

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

// copy constructor
SmartImage::SmartImage (const SmartImage& param)
{         
 surface = SDL_DisplayFormat(param.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;     
}

SDL Question

12 April 2012 - 11:22 PM

This is my work around but it makes my program so slow

// I take the small square tile i want out of the tile_sheet and blit it onto temp_tile
apply_surface( 0, 0, tile_sheet, temp_tile, &clip);

// I save the SDL_Surface as tile.bmp
SDL_SaveBMP(temp_tile, "Graphics\\tile.bmp");

// Lastly I load the bmp into a different SDL_Surface called tile
tile = load_image("Graphics\\tile.bmp");

Like I said the above works but is so darn slow. Ideally what I'd like to do is set the clipped part of tilesheet into a seperate SDL_Surface and have it save like...

// I take the small square tile i want out of the tile_sheet and blit it onto temp_tile
apply_surface( 0, 0, tile_sheet, temp_tile, &clip);

tile = temp_tile;

Also the apply_surface function is from http://lazyfoo.net/S...son06/index.php

PARTNERS