mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
Rewrote map loader
This commit is contained in:
parent
2be05e8b9c
commit
8735edb3ad
@ -6,5 +6,16 @@ public:
|
|||||||
Map() = default;
|
Map() = default;
|
||||||
~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);
|
||||||
};
|
};
|
||||||
|
|||||||
51
src/Map.cpp
51
src/Map.cpp
@ -1,25 +1,56 @@
|
|||||||
#include "Map.h"
|
#include "Map.h"
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
#include "Game.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::string tileIDstr;
|
||||||
std::fstream mapFile;
|
char singleChar;
|
||||||
|
std::ifstream mapFile;
|
||||||
mapFile.open(path);
|
mapFile.open(path);
|
||||||
|
|
||||||
for (int y = 0; y < sizeY; y++)
|
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))
|
||||||
{
|
{
|
||||||
for (int x = 0; x < sizeX; x++)
|
if (singleChar == ',' || singleChar == '\n') {
|
||||||
{
|
if (tileIDstr.empty())
|
||||||
mapFile.get(tile);
|
continue;
|
||||||
Game::addTile(atoi(&tile), x * TILE_SIZE, y * TILE_SIZE);
|
Game::addTile(std::stoi(tileIDstr), x * TILE_SIZE, y * TILE_SIZE);
|
||||||
mapFile.ignore();
|
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();
|
mapFile.close();
|
||||||
|
|
||||||
|
return success
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user