0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

Rewrote map loader

This commit is contained in:
Benedikt Galbavy 2024-01-30 14:10:26 +01:00
parent 2be05e8b9c
commit 8735edb3ad
2 changed files with 53 additions and 11 deletions

View File

@ -6,5 +6,16 @@ public:
Map() = default;
~Map() = default;
static void loadMap(const char* path, int sizeX, int sizeY);
// code comment below is a test for doxygen - do not remove
/*!
*
* \brief
* This loads a map
*
* \param path The path to the map file
* \return Boolean for success
*
*/
static bool loadMap(const char* path, int sizeX, int sizeY);
};

View File

@ -1,25 +1,56 @@
#include "Map.h"
#include <cctype>
#include <iostream>
#include <fstream>
#include <string>
#include "Constants.h"
#include "Game.h"
#include "SDL_error.h"
void Map::loadMap(const char* path, int sizeX, int sizeY)
bool Map::loadMap(const char* path, int sizeX, int sizeY)
{
char tile;
std::fstream mapFile;
std::string tileIDstr;
char singleChar;
std::ifstream mapFile;
mapFile.open(path);
for (int y = 0; y < sizeY; y++)
{
for (int x = 0; x < sizeX; x++)
{
mapFile.get(tile);
Game::addTile(atoi(&tile), x * TILE_SIZE, y * TILE_SIZE);
mapFile.ignore();
if (!mapFile.is_open()) {
SDL_SetError("Error loading map: Couldn't open map file!");
return false;
}
int x = 0, y = 0; // needed outside for-loop for error handling
bool success = true;
for (; !mapFile.eof(); mapFile.get(singleChar))
{
if (singleChar == ',' || singleChar == '\n') {
if (tileIDstr.empty())
continue;
Game::addTile(std::stoi(tileIDstr), x * TILE_SIZE, y * TILE_SIZE);
tileIDstr.clear();
if (singleChar == '\n') {
if (x != sizeX) {
SDL_SetError("Error loading map: specified x size doesn't match map file!");
success = false;
}
x = 0;
y++;
continue;
}
x++;
continue;
}
if (!std::isdigit(singleChar)) continue;
tileIDstr += singleChar;
}
if (y != sizeY) {
SDL_SetError("Error loading map: specified y size doesn't match map file!");
success = false;
}
mapFile.close();
return success
}