Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating a tilemap  (Read 1487 times)

0 Members and 1 Guest are viewing this topic.

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Creating a tilemap
« 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.
« Last Edit: February 20, 2017, 02:16:42 pm by DylanMorgan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Creating a tilemap
« Reply #1 on: February 19, 2017, 10:25:52 pm »
What do you not understand exactly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Creating a tilemap
« Reply #2 on: February 20, 2017, 02:11:35 pm »
I've edited the code to show the bits I don't understand in the comments :)

 

anything