SDL copy surface problem

closed account (zwA4jE8b)
I am working on a resource manager to store my objects/images in memory instead of reading from the HD everytime an image is loaded. The sdl site said that using SDL_Surface* temp = SDL_ConvertSurface(bullet, bullet->format, bullet->flags); is the proper way to make a real copy of a surface.

The problem is that a few bullets will spawn but then the program crashes.
What is the proper way to create and/or pass a copy of an sdl surface?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "Resource_Manager.h"

Resource_Manager::Resource_Manager()
{
	ok = true;
}

void Resource_Manager :: init()
{
	p1 = IMG_Load("robo_walking-x95-y120.bmp");
	SDL_SetColorKey(p1, SDL_SRCCOLORKEY, SDL_MapRGB(p1->format, 255, 0, 255));

	npc = IMG_Load("enemy.bmp");
	SDL_SetColorKey(npc, SDL_SRCCOLORKEY, SDL_MapRGB(npc->format, 255, 0, 255));

	bullet = IMG_Load("bullet_right.png");
	SDL_SetAlpha(bullet, SDL_SRCALPHA, 0);

	if (p1==NULL)
	{
		std::cout << "Can't find p1's image" << std::endl;
		ok = false;
	}
	if (npc==NULL)
	{
		std::cout << "Can't find npc's image" << std::endl;
		ok = false;
	}
	if (bullet==NULL)
	{
		std::cout << "Can't find bullet's image" << std::endl;
		ok = false;
	}
};

SDL_Surface* Resource_Manager :: get_p1()
{
	SDL_Surface* temp = SDL_ConvertSurface(p1, p1->format, p1->flags);
	return temp;
};

SDL_Surface* Resource_Manager :: get_npc()
{
	SDL_Surface* temp = SDL_ConvertSurface(npc, npc->format,npc->flags);
	return temp;
};	

SDL_Surface* Resource_Manager :: get_bullet()
{
	SDL_Surface* temp = SDL_ConvertSurface(bullet, bullet->format, bullet->flags);
	return bullet;
};

Resource_Manager::~Resource_Manager()
{
	SDL_FreeSurface(p1);
	SDL_FreeSurface(npc);
	SDL_FreeSurface(bullet);
}



the way i am using the get function

 
p1 = new player_one(rm.get_p1(), 'p', 4, 0, 300, 4, 1);


and the objects constructor

1
2
3
4
5
player_one::player_one(SDL_Surface* _object, char type, int framerate, int x, int y, int fx, int fy)
{
	this->object = _object;



object is an SDL_Surface*
Last edited on
closed account (zwA4jE8b)
Sorry, apparently I needed to use this method instead.

1
2
3
4
5
6
7
 1     SDL_Surface *Srfc1, *Srfc2;
   2     Srfc1= IMG_Load("foo.png");
   3     Srfc2= Srfc1;
   4     Srfc2->refcount++;
   5 ...
   6     SDL_FreeSurface(Srfc1);
   7     SDL_FreeSurface(Srfc2);
Topic archived. No new replies allowed.