Hi, Can somebody PLEASE explain how I implement a tiled map in my game? I have literally been trying for hours, I just don't understand why I am finding it difficult to think about.
This is the only thing holding me back and it is so annoying!! I just wanted to try to make a simple tiled map and have some collision. The tutorial I look at, lazyfoo, seem to skip over the most important parts!
In the lazyfoo examples, I cannot find any evidence of where he assigns a surface to his tile array OR where he checks what the tileID is. So, this is how I have tried to do this:
My First Step: I have a tile class with an SDL_Rect, an int TileID and an enum TypeID(TYPE_NONE,TYPE_Solid) and also an SDL_Surface*.
Question on this: I have my map file as a .txt. With a 15x20 matrix of the numbers 1 or 0. 1 Should represent the solid type. If I check for collisions for example, do I need to check which type the tile is every loop?
My Second Step: I created a 2D array using the max number of tiles that will be in the level. Tile *tileArray[TOTAL_TILES];
My Third Step: I read the txt file using std::ifstream. I loop through the array, creating a new Tile each iteration using, tile = new Tile(x,y,tileType);.........Also, each iteration, the width is added onto X, and I then check X against the width of the screen. If X>=SCREEN_WIDTH then it will set Y+=TILE_HEIGHT.
My forth Step: I loop through the array again, this time Loading a surface for each tile using each tiles type which was entered earlier.
for(int i=0;i<TOTAL_TILES;i++)
{
tileList->load(tileList->getType());
}
This is the load function:
void Tiles::load(int typeID)
{
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
if(typeID == TYPE_None)
{
tile = SDL_LoadBMP("white.bmp");
}
else if(typeID == TYPE_Solid)
{
tile = SDL_LoadBMP("brick.bmp");
}
if( tile != NULL ) {
optimizedImage = SDL_DisplayFormat( tile ); //Free the old image
SDL_FreeSurface( tile ); }
tile = optimizedImage;
}
My Fifth Step: In my main game loop, I then call draw() on each tile.
void Tiles::draw()
{
SDL_BlitSurface( tile, NULL, g_SrWindow, &box );
}
Am I doing this correctly? I know I havent used the tileID, thats because I am trying it with a single 40x40 tile, not a tileset yet. Is there a better way to do this?
Important Question : When checking for collisions with the map, I am not sure of what the logic is, can someone help me complete the following?
Loop through tile array
if(tile.x > cameras.x AND tile.x <= cameras.x+SCREEN_WIDTH)//if tile is on screen?? Would this work?
{
loop through all entities // ? This is all I can think of, but it has time complexity of O(n^2) doesnt it?
if(entity.x > cameras.x AND entity.x <= cameras.x+SCREEN_WIDTH)
{
drawAll()
if(tile.type == SOLID)
{
checkCollision()
}
else
//..........etc
}
}
I would REALLY appreciate some input here. Thanks.
Sorry I should have said, My map prints out as expected now, But I would still like your input on my methods and an answer to my questions if you will.
EDIT: I also get this error after I CLOSE the game window: Unhandled exception at 0x1048bfc8 (msvcp100d.dll) in TilingTest.exe: 0xC0000005: Access violation reading location 0xfeeefef2.