33 lines
1.2 KiB
C++
33 lines
1.2 KiB
C++
#include "world.h"
|
|
|
|
World::World(const char* file, glm::vec2 blockSize) {
|
|
// load from file
|
|
this -> blocksize = blockSize;
|
|
unsigned int tileCode;
|
|
std::string line;
|
|
std::ifstream fstream(file);
|
|
if (fstream)
|
|
{
|
|
while (std::getline(fstream, line)) // read each line from level file
|
|
{
|
|
std::istringstream sstream(line);
|
|
std::vector<unsigned int> row;
|
|
while (sstream >> tileCode) // read each word seperated by spaces
|
|
row.push_back(tileCode);
|
|
this->tileData.push_back(row);
|
|
}
|
|
for (int y = 0; y < this->tileData.size(); y++) {
|
|
for (int x = 0; x < this->tileData[0].size(); x++) {
|
|
if (this->tileData[y][x]) {
|
|
this->objects.push_back(new Object(this->Sprite, glm::vec2(0.0f + blocksize.x * (float)x, 0.0f + blocksize.y * (float)y), blocksize, this->tileData[y][x]));
|
|
//std::cout << this->tileData[y][x];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
void World::drawWorld(SpriteRenderer& renderer) {
|
|
for (int i = 0; i < this->objects.size(); i++) {
|
|
this->objects[i]->drawObject(renderer);
|
|
}
|
|
} |