SFML community forums

Help => Graphics => Topic started by: DylanMorgan on February 19, 2017, 09:53:41 pm

Title: Creating a tilemap
Post by: DylanMorgan on February 19, 2017, 09:53:41 pm
Hey guys, I am trying to create a game which uses a tile map to decide how the level loops. where '1,1' is a white square, and if it is 0,0 then nothing is created. For collision purposes I decided to make an object for each of these squares (don't know if this is stupid or not) then each white square will be displayed on the screen. These white squares are called 'levelSurface.h' and I have a vector holding copies of these surfaces inside 'levelhandler'

I found some code on a website and tried to understand how it works and implement it. However, I still do not understand how this works!

This is my levelHandler.cpp which has the code with the mapping in them. Please can someone explain whats going on here :) I have tried a few things so I am not sure if they are good or not.
#include "Stdafx.h"
#include "levelHandler.h"

LevelHandler::LevelHandler()
{
        if (!texture.loadFromFile("resources/tilemap.png"));

        std::ifstream openfile("resources/level1.txt");

        while (!openfile.eof())
        {
                std::string str;
                openfile >> str;
                char x = str[0], y = str[2];

                if (isdigit(x) || !isdigit(y))
                {
                        // This is meant to push back and object each time there is a digit right.
                        vectorOfSurfaces.push_back(LevelSurface(texture));
                }

                if (openfile.peek() == '\n')
                {
                        loadCounter.x = 0;
                        loadCounter.y++;
                }
                else
                {
                        loadCounter.x++;
                }
        }

        loadCounter = sf::Vector2i(0, 0);
}

LevelHandler::~LevelHandler()
{
}

std::vector<LevelSurface>& LevelHandler::getVector()
{
        return vectorOfSurfaces;
}

void LevelHandler::loadMap()
{
        std::ifstream openfile(location);

        // Load text file into map vector
        while (!openfile.eof())
        {
                std::string str;
                openfile >> str;
                char x = str[0], y = str[2];
                if (!isdigit(x) || !isdigit(y))
                {
                        // No clue what this does
                        map[loadCounter.x][loadCounter.y] = sf::Vector2i(-1, -1);
                }
                else
                {
                        // when i change the numbers in '' then the whole output changes, so what does this do?
                        map[loadCounter.x][loadCounter.y] = sf::Vector2i(x - '2', y - '2');
                }

                if (openfile.peek() == '\n')
                {
                        loadCounter.x = 0;
                        loadCounter.y++;
                }
                else
                {
                        loadCounter.x++;
                }
        }
}

void LevelHandler::addObjectToVector(RenderEngine& renderEngine)
{
        for (int i = 0; i < loadCounter.x; i++)
        {
                for (int j = 0; j < loadCounter.y; j++)
                {
                        if (map[i][j].x != -1 && map[i][j].y != -1)
                        {
                                for (unsigned int number = 1; number < vectorOfSurfaces.size(); number++)
                                {
                                        vectorOfSurfaces[i].getSprite().setPosition(i * 32, j * 32);
                                        vectorOfSurfaces[i].getSprite().setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
                                        renderEngine.addObjectToVector(vectorOfSurfaces[i]);
                                }
                        }
                }
        }
}

I have attached the output screen.
Title: Re: Creating a tilemap
Post by: eXpl0it3r on February 19, 2017, 10:25:52 pm
What do you not understand exactly?
Title: Re: Creating a tilemap
Post by: DylanMorgan on February 20, 2017, 02:11:35 pm
I've edited the code to show the bits I don't understand in the comments :)