SDL - Can't blit a surface

I'm working on building a map editor, and for some reason I can't blit from one surface to another. My debugger hands me an access violation warning with some hex addresses, and under locals "this" is highlighted in red.

To lessen the post size, assume that the vector sizes and data are initialized properly.

The breakpoints are line 20 or 24 in the blitting function, depending on the check of parameters.

the structs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct tilecoords {int x; int y;};
struct map_dat {
	std::string map_name;
	int map_id;
	int width_in_tiles;
	int height_in_tiles;
	int n_tsets_used;
	std::string tset_index;
};
struct tile_dat{
	std::string tileset;
	tilecoords maploc;
	tilecoords tsetloc;
	int layer;
};


the declariations:
1
2
3
4
5
vector <tile_dat> tiledat;
vector <string> tsets;
vector <map_dat>maps_db;

vector <SDL_Surface*>tset_surf;


The function that does the blitting
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
void editorui::draw_map_fromfile()//called from within load file, where everything is initialized and loaded
{
	init_layers();
	for(int i = 0; i < maps_db[ed.active_map_id].height_in_tiles*maps_db[ed.active_map_id].width_in_tiles*4; i++)
	{
		SDL_Rect src;
		SDL_Rect dst;
		src.x = tiledat[i].tsetloc.x;
		src.y = tiledat[i].tsetloc.y;
		src.w = ed.tilesize;
		src.h = ed.tilesize;

		dst.x = tiledat[i].maploc.x;
		dst.y = tiledat[i].maploc.y;
		dst.w = ed.tilesize;
		dst.h = ed.tilesize;
		string check = "-1";
		if(tiledat[i].tileset != check)
		{
			SDL_BlitSurface(tset_surf[get_tile_tset(i)], &src, layer_buffer[tiledat[i].layer], &dst);
		}
		else
		{
			SDL_BlitSurface(empty_tile, NULL, layer_buffer[tiledat[i].layer], &dst);
		}
	}
	build_layer(1);
	build_layer(2);
	build_layer(3);
	build_layer(4);
}


The re-initialization of the layers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void editorui::init_layers()
{
	for(int i = 0; i < 4; i++)
	{
		SDL_FreeSurface(edui.layer_buffer[i]);
	}
	for(int i = 0; i < 4; i++)
	{
		layer_buffer[i] = SDL_CreateRGBSurface(0,maps_db[ed.active_map_id].width_in_tiles*ed.tilesize,
												maps_db[ed.active_map_id].height_in_tiles*ed.tilesize,
												24,0,0,0,0xFF);
		SDL_FillRect(layer_buffer[i], NULL, SDL_MapRGB(layer_buffer[i]->format, 0x0,0x0,0x0));
	}
}


The initialization of empty_tile - Done on program load
1
2
empty_tile = SDL_CreateRGBSurface(0,ed.tilesize, ed.tilesize, 24, 0, 0, 0, 0xFF);
	SDL_FillRect(empty_tile, NULL, SDL_MapRGB(empty_tile->format, 0x0,0x0,0x0));
closed account (N36fSL3A)
1. SDL 1.2?

2. I don't see where you're setting the image's pixels...

3. I don't know why you're creating a Surface to fill it's rectangle (?)

EDIT: Where exactly are you loading them in the first time around?
Last edited on
1. 2.0

2. you mean what is being blitted? if so then the tiledat.tsets variable holds a string, which is used to evaluate an integer identifier of pre-built tilesets on load via an index struct which i didn't show here.

3.do you mean why am I calling line 12 in the last piece of code? If so, you're right and it was redundant, I was just calling it because I thought the problem was that the surface wasn't initialized.

I fixed the problem, it wasn't in any of the code i showed as the problem lied between calling new_map() and draw_map_fromfile(), which was a call to an incomplete function which cleared the tiledat struct, and thus had me trying to blit to a non-existant layer[] array.
I thought blitting was removed in 2.0?
Of course you can, 2.0 has all of 1.2's functionality afaik.

You can't blit to/from textures, but surfaces still work the way they did in 1.2.
closed account (N36fSL3A)
Of course you can, 2.0 has all of 1.2's functionality afaik.
It isn't backwards compatible if that's what you need?
Topic archived. No new replies allowed.