Hello guys :) I hope you are well.
I have a problem, I am trying to build a 2D platformer and I am stuck when it comes to building the levels.
I found a tutorial on how to build a tiling engine and I just got completely confussed.
I am using SDL and I am trying to get my Tile class to build levels without have to make 1000+ Tile class objects in a different class that just handle one tile each.
The problem is am trying to use nested vectors with shared_ptrs and am really confussed. My program just crashes straight away and I don't know what to do.
#ifndef TILE_H
#define TILE_H
//config.h holds all the gobal variable.
#include "config.h"
//Sprite.h hold the static sprite class.
#include "Sprite.h"
#include <map>
#include <memory>
#include <vector>
//This class is a tiling engine it build a level for a 2D platformer.
class Tile
{
public:
Tile();
~Tile();
//This function builds the level with a
Tile* CreateMap(SDL_Renderer* Display);
//This function clips a tile sprite sheet up into pieces.
void ClipRect();
//Updates the tiles.
void Update(int TimePassed);
//Renders the tiles to the screen.
void Renderer(SDL_Renderer* Display, SDL_Rect& Cam);
private:
//Hold the height and width of the tiles.
int mWidth;
int mHeight;
//Holds the data for clipping the tile sprite sheet.
//It is an rectangle array using enums from the config.h file for index values.
SDL_Rect clip[TOTAL_TILE_SPRITES];
//this crazy thing is two vectors that are going to hold which row and column we are currently on.
//The shared pointer a sprite class that handles the graphics logics and renders 2D sprites.
//If that still confusses you just think int[row][col] which containts the sprites that the tiles.
std::vector<std::vector<std::shared_ptr<Sprite>>> mSpriteMap;
};
#endif // TILE_H
#include "Tile.h"
#include "Collision.h"
Tile::Tile()
{
}
Tile::~Tile()
{
}
//This is the test map function
Tile* Tile::CreateMap(SDL_Renderer* Display)
{
Tile* Map = new Tile;
const int num_row = 15;
const int num_col = 20;
//I know am trying initialise the mSpriteMap to build a level with but I don't understand what going one here.
Map->mSpriteMap = std::vector<std::vector<std::shared_ptr<Sprite>>> (num_row, std::vector<std::shared_ptr<Sprite>> (num_col, std::shared_ptr<Sprite>()));
//Here am making a share_ptr of the sprite class.
std::shared_ptr<Sprite> sprite(new Sprite);
//This sprite is calling the load file function.
//This load a file with the string and the renderer, is need to make it into a texture.
sprite->LoadFile(Display, "Data/Tile.png");
//This is all to test to see if it works.
const int row = 11;
for(int col = 0; col < num_col; col++)
{
mSpriteMap[row][col] = sprite;
}
Map->mSpriteMap[10][10] = sprite;
return Map;
}
//Cliping the stuff :D
void Tile::ClipRect()
{
clip[RED_TILE] = {0, 0, 32, 32};
}
//Note: We are asumming that all tiles are the same size
void Tile::Update(int TimePassed)
{
ClipRect();
//This is looping though all the sprite tiles and updating them.
for(size_t row = 0; row < mSpriteMap.size(); row++)
{
for(size_t col = 0; col < mSpriteMap[row].size(); col++)
{
//This test if the tile we are on excities
if(mSpriteMap[row][col])
{
//This confussed me completely, what should I be updateing here? Why would updata a static sprite?
mSpriteMap[row][col]->Update(TimePassed);
}
}
}
}
void Tile::Renderer(SDL_Renderer* Display, SDL_Rect& Cam)
{
for(size_t row = 0; row < mSpriteMap.size(); row++)
{
for(size_t col = 0; col < mSpriteMap[row].size(); col++)
{
//This test if the tile we are on excities
if(mSpriteMap[row][col] && mSpriteMap[row][col] != NULL)
{
//This is rendering function, we need the renderer to render.
//mBox.x is position x to be rendered on the screen.
//mBox.y is position y to be rendered on the screen.
//&clip[RED_TILE] is what part of the tile sprite sheet I am using, which is only the red tile.
mSpriteMap[row][col]->Draw(Display, col * 32, row * 32, &clip[RED_TILE]);
}
}
}
}
Any help/suggestion would be awesome. Thank you.