Hey everybody!
I'm continuing in my SFML endeavors and I just recently finished a tile map editor. Given a tilesheet and the size of each tile, the program is able to take a "map" where one specific color corresponds to a tile. At the moment you have to hard code the values of each color-tile pair, but I will make it so that it can read in a text file that has the color pairings(if anyone has a better way of doing this I'd be interested).
Now my issue lies here. At the moment my map editor only takes one image and creates another one out of it. I can display this image as the background and move it accordingly when my character moves yadda yadda. However I do not really understand where the collision detection is going to fit in here.Is it possible to have this information handled within the tile map editor itself? I remember ages ago when I first started Java I created a Tile class and each Tile said whether or not it was "walkable". However I have foregone that step in this case so I am at a bit of a loss at what to do.
While reading the forums I found some people talking about using "layers". So I would draw my map in the background as usual, but at the same time have an invisible layer on top of that with which the sprites/entities interacted with. But I would like this to be automatically coupled with the tiles(e.g. grass is walkable tile, but stone is not). Also on a similar note, I am curious as to the best way of storing this "walkable" information. As some objects could become walkable later on should the user use a certain item, for example water if the user gets into a boat. I don't think this should change water's walkable property, but instead override it somehow. Again, I am lost here.
I will include the code as an attachment as well as in the post itself for the map editor in case it is of use to the discussion.
Thanks again for the help!
-Adrian
Map.h and Map.cpp
#ifndef MAP_H
#define MAP_H
//SFML Includes
#include <SFML/Graphics.hpp>
//C++ Includes
#include <iostream>
#include <string>
using namespace std;
class Map
{
public:
//Ctors
Map();//Don't use this lol
Map(string rawMapFile,string tileFile,int tSize);//Accepts map of pixels, tile spritesheet, and the size of each tile
//Methods
void process();//Takes map of pixels and turns it into map of tiles
void initTileColors();//Initializes the color values for each tile
private:
sf::Image rawMap; //The map generated by the user where each pixel represents a tile
sf::Image tileSpriteSheet; //The map generated by the program and saved to disk for user
int numOfTiles;//How many tiles the user has in their spriteSheet
int tileSize; //The size of each tile in the spriteSheet, MUST BE SQUARE
sf::Color tileColors[];
};
#endif
//SFML Includes
#include <SFML/Graphics.hpp>
//Personal Includes
#include "Map.h"
//C++ Includes
#include <iostream>
#include <string>
using namespace std;
//Ctors
Map::Map()
{
}
Map::Map(string rawMapFile,string tileFile,int tSize)
{
//Loads the rawMap image
if(!rawMap.loadFromFile(rawMapFile))
{
cout<<"Could not load file: "<<rawMapFile<<endl;
}
//Loads the map's tile spritesheet
if(!tileSpriteSheet.loadFromFile(tileFile))
{
cout<<"Could not load file: "<<tileFile<<endl;
}
//Sets the size of each tile in the spritesheet and how may tiles there are
tileSize = tSize;
}
//Methods
void Map::initTileColors()
{
//********************************
//REMINDER!!! Make this user customizable through text file or something
//********************************
tileColors[0] = sf::Color(0,0,0,255);//Ground
tileColors[1] = sf::Color(100,100,100,255);//Stone
tileColors[2] = sf::Color(255,255,255,255);//Sky
}
void Map::process()
{
//Creates a blank image the size of rawMap times the size of each tile in tileSpriteSheet
sf::Image processedMap;
processedMap.create(rawMap.getSize().x * tileSize,rawMap.getSize().y * tileSize);
//Takes map tile spritesheet and turns all magic purple pixels into transparent pixels
tileSpriteSheet.createMaskFromColor(sf::Color(255,0,255,255),0);
//Loops through every pixel on the raw map
//When a pixel matches a color in the tileColors array it copies over the appropriate tile from spritesheet
//To the processed image
for(int x = 0; x < rawMap.getSize().x; x++)
{
for(int y = 0; y < rawMap.getSize().y; y++)
{
//Ground
if(rawMap.getPixel(x,y) == tileColors[0])
{
processedMap.copy(tileSpriteSheet,x * tileSize,y * tileSize,sf::IntRect(0,0,tileSize,tileSize),true);
}
//Stone
if(rawMap.getPixel(x,y) == tileColors[1])
{
processedMap.copy(tileSpriteSheet,x * tileSize,y * tileSize,sf::IntRect(8,0,tileSize,tileSize),true);
}
//Sky
if(rawMap.getPixel(x,y) == tileColors[2])
{
processedMap.copy(tileSpriteSheet,x * tileSize,y * tileSize,sf::IntRect(16,0,tileSize,tileSize),true);
}
}
}
processedMap.saveToFile("C:/Users/Adrian/Desktop/NewMap.png");
cout<<"Success"<<endl;
}