hi all, I’m new to this forum… I have a problem regarding tilemaps… in practice, I try to read a text file where I have written numbers inside that correspond to, let’s say, a species of entity, but they are not entities they are Game objects.
Oh well in practice I can render the objects but I don’t understand why if I write a second line in the text file when he renders it he puts them all on the same line … I think I haven’t been clear … anyway I post the code and one screenshot for you to understand better…
==============================================================================
TILEMAP.cpp
#include "TileMap.hpp"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
/* ============================================================================================== */
/* TileMap */
/* ============================================================================================== */
TileMap::TileMap(std::vector<MapObject> &mapObject, const char* tileMapFile, unsigned int map_width, unsigned int map_height){
MapObj = mapObject;
std::vector<unsigned int> tileData;
unsigned int tileCode;
std::string line;
std::vector<std::vector<unsigned int>> Data;
std::ifstream file(tileMapFile);
if(!file.is_open()){
std::cout << "Error open Map File" << std::endl;
}
while(std::getline(file, line)){
std::istringstream sstream(line);
while(sstream >> tileCode){
tileData.push_back(tileCode);
}
Data.push_back(tileData);
}
file.close();
std::cout << Data.size() << std::endl; //Y
std::cout << Data[0].size() << std::endl; //X
if(Data.size() > 0){
onInit(mapObject, Data, map_width, map_height);
}
// unsigned int width = Data[0].size(); //X
// unsigned int height = Data.size(); //Y
}
/* ============================================================================================== */
/* onInit */
/* ============================================================================================== */
void TileMap::onInit(std::vector<MapObject> &mapObject, std::vector<std::vector<unsigned int>> data, unsigned int map_width, unsigned int map_height){
unsigned int width = data[0].size(); //X
unsigned int height = data.size(); //Y
unsigned int mapObjectPositionX = map_width / width;
unsigned int mapObjectPositionY = map_height / height;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
for(auto &mObj : mapObject){
if(data[y][x] == mObj.ID && mObj.ID == 1){
mObj.Position = glm::vec2(mapObjectPositionX * x, mapObjectPositionY * y);
mObj.Color = glm::vec3(0.4, 0.7, 0.1);
MapObj.push_back(mObj);
}
if(data[y][x] == mObj.ID && mObj.ID == 2){
mObj.Position = glm::vec2(mapObjectPositionX * x, mapObjectPositionY * y);
mObj.Color = glm::vec3(0.8, 0.3, 0.2);
MapObj.push_back(mObj);
}
}
}
}
}
/* ============================================================================================== */
/* onRenderer */
/* ============================================================================================== */
void TileMap::onRenderer(unsigned int screen_width, unsigned int screen_height){
for(auto mObj : MapObj){
mObj.onRenderer(screen_width, screen_height);
}
}
=====================================================================
TILEMAP.hpp
#ifndef __TILEMAP_HPP__
#define __TILEMAP_HPP__
#include "MapObject.hpp"
class TileMap {
public:
std::vector<MapObject> MapObj;
TileMap() {}
TileMap(std::vector<MapObject> &mapObject, const char* tileMapFile, unsigned int map_width, unsigned int map_height);
void onInit(std::vector<MapObject> &mapObject, std::vector<std::vector<unsigned int>> data, unsigned int map_width, unsigned int map_height);
void onRenderer(unsigned int screen_width, unsigned int screen_height);
void onDestroy();
};
#endif // __TILEMAP_HPP__
=============================================================================
TEXT FILE MAP.txt
1 2 3 4 5 6 7 8 9
1 2 34 56 852
IMAGE
as you can see the blocks should be joined and then i only wrote the number 1 and 2 twice but he renders it to me 4 times…i don’t know why…does anyone know where i’m going wrong?