I'm trying to create an array of tileID', using pixels in an image as input(green pixels-grass, blue pixels-water)
as a way to make maps for my game visually. So far it's not working
When I compile this project The tiles seem randomly placed.
Arrays(and representing 2d spaces with a 1d arrays) confuse me easily , so I tried to use the same thing that was used on the tilemap example(i*j*width), which I am also using
#include "ImageToTileID.h"
#include <SFML/Graphics.hpp>
#include <string>
#include <vector>
TileID * ImageToTileID::ConvertToArray(std::string filepath)
{
sf::Image image;
//Load the image from filepath provided
image.loadFromFile(filepath);
std::map<TileID, sf::Color> mapToColor;
//Define list!!! Must update when adding a tile!!!
//grass is green
mapToColor[TileID::GRASS] = sf::Color(0, 255, 0, 255);
mapToColor[TileID::WATER] = sf::Color(0, 0, 255, 255);
//Checks if all tiles have been mapped!
if (mapToColor.size() != (int)TileID::NUMBER_OF_TYPES)
{
return nullptr;
}
//Creates the container for data to return
std::vector<TileID> container(image.getSize().x*image.getSize().y);
for (int i = 0; i < image.getSize().x; i++)
{
for (int j = 0; j < image.getSize().y; j++)
{
if (image.getPixel(i,j) == mapToColor[TileID::GRASS])
{
container[i + j*image.getSize().x] = TileID::GRASS;
} else
if (image.getPixel(i, j) == mapToColor[TileID::WATER])
{
container[i + j*image.getSize().x] = TileID::WATER;
}
}
}
//Return pointer to the first element of the vector
TileID * result = &container[0];
return result;
}
The reason I didn't use image.getPixelsPtr, is because I don't understand where they got the size from(width*height*4) where does the 4 come into play?