Creating a Tile Engine with SDL Problem

So below is my code that compiles with no errors. But i know something is wrong as it does not display anything. I took out the snippets that I'm using from my big pile of code and now its just doing my head in. This particular part I've been working on for ages and searching one to many tile engine tutorials online. But none of them are broken apart into separate files. Basically the one I'm making has to be in separate parts. So I've tried a few things: I did have it a little different than this and all it would display is one type of tile all over the map. So I did have it partially working at one stage.

But since then I've tried making it display the map that's loaded into the SDL_Rect clips - tile sheet. I do know that it's loaded in there as it was displaying fine before and when it was all written on a single page it loaded the tiles fine. Now as I see it (I'm not that great with pointers or SDL) the pointers aren't exactly pointing how i want them to. So somewhere i know memory is pointing to the wrong place.

Now if i run this in debug mode I'll get this error as a pop up message:

Unhandled exception at 0x004124df in SDL_FIRST_RUN.exe: 0xC0000005: Access violation reading location 0xccccccd2.


And down the bottom in the debug box (Using VS 2005)
Name:clip, Value:0x0012eb9c
Name:clip[i], Value:0xcccccccc {x=??? y=??? w=??? ...}

So as you can see I'm a little lost and I've been trying for far to long now with this chunk of code and just can't go any further with it myself - It's stumped me.

Everything works fine except anything related with clips.

Any help is greatly appreciated - Thanks

main.cpp
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
/*main.cpp*/
//Tile Variables
const int TOTAL_TILES = 1156;
const int TILE_SPRITES = 12;

//The surfaces
SDL_Surface *screen = NULL;
SDL_Surface *tileSheet = NULL;

//Sprite From TileSheet
SDL_Rect clips[TILE_SPRITES];

//Camera
SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };

for( int t = 0; t < TOTAL_TILES; t++ )
{
	SDL_Rect *tempRect[TILE_SPRITES];
	for(int i = 0; i < TILE_SPRITES; i++)
	{
		tempRect[i] = &clips[i];
		tiles[ t ]->show(&camera, box, screen, tileSheet, tempRect);
	}
	
}


tile_class.cpp
1
2
3
4
5
6
7
8
9
10
/*tile_class.cpp*/
void Tile::show(SDL_Rect *pCamera, SDL_Rect *pBox, SDL_Surface *pScreen, SDL_Surface *pTileSheet, SDL_Rect *pClips[])
{
    //If the tile is on screen
    if( check_collision(pCamera, pBox) == true )
    {
        //Show the tile
		apply_surface(box.x - pCamera->x, box.y - pCamera->y, pTileSheet, pScreen, pClips);    
    }
}


graphix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*graphix.cpp*/
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip[])
{
    //Holds offsets
    SDL_Rect offset;
    
    //Get offsets
    offset.x = x;
    offset.y = y;
	SDL_Rect tempClip;
	for(int i = 0; i < 12; i++)
	{
		tempClip.h = clip[i]->h;
		tempClip.w = clip[i]->w;
		tempClip.x = clip[i]->x;
		tempClip.y = clip[i]->y;

		SDL_BlitSurface(source, &tempClip, destination, &offset );
	}
}
Last edited on
Topic archived. No new replies allowed.