From 7c5f4751f673ee9a75ad882c33f6303d3f855239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bend=C3=ADk?= Date: Sat, 1 Sep 2012 16:40:18 +0200 Subject: [PATCH] Player attributes moved to separate class. --- CMakeLists.txt | 4 ++- PlayerAttributes.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++ PlayerAttributes.h | 38 +++++++++++++++++++++++ TileGenerator.cpp | 47 ++++------------------------ 4 files changed, 120 insertions(+), 42 deletions(-) create mode 100644 PlayerAttributes.cpp create mode 100644 PlayerAttributes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 198e1f3..0c8e5d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,13 +55,15 @@ include_directories( set(mapper_HDRS PixelAttributes.h + PlayerAttributes.h TileGenerator.h ) set(mapper_SRCS - mapper.cpp PixelAttributes.cpp + PlayerAttributes.cpp TileGenerator.cpp + mapper.cpp ) add_executable(minetest_mapper diff --git a/PlayerAttributes.cpp b/PlayerAttributes.cpp new file mode 100644 index 0000000..5c9d4d5 --- /dev/null +++ b/PlayerAttributes.cpp @@ -0,0 +1,73 @@ +/* + * ===================================================================== + * Version: 1.0 + * Created: 01.09.2012 14:38:05 + * Author: Miroslav Bendík + * Company: LinuxOS.sk + * ===================================================================== + */ + +#include +#include +#include +#include "config.h" +#include "PlayerAttributes.h" + +using namespace std; + +PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory) +{ + + string playersPath = sourceDirectory + "players"; + DIR *dir; + dir = opendir (playersPath.c_str()); + if (dir == NULL) { + return; + } + + struct dirent *ent; + while ((ent = readdir (dir)) != NULL) { + if (ent->d_name[0] == '.') { + continue; + } + + string path = playersPath + PATH_SEPARATOR + ent->d_name; + + ifstream in; + in.open(path.c_str(), ifstream::in); + string buffer; + string name; + string position; + while (getline(in, buffer)) { + if (buffer.find("name = ") == 0) { + name = buffer.substr(7); + } + else if (buffer.find("position = ") == 0) { + position = buffer.substr(12, buffer.length() - 13); + } + } + char comma; + Player player; + istringstream positionStream(position, istringstream::in); + positionStream >> player.x; + positionStream >> comma; + positionStream >> player.y; + positionStream >> comma; + positionStream >> player.z; + player.name = name; + + m_players.push_back(player); + } + closedir(dir); +} + +PlayerAttributes::Players::iterator PlayerAttributes::begin() +{ + return m_players.begin(); +} + +PlayerAttributes::Players::iterator PlayerAttributes::end() +{ + return m_players.end(); +} + diff --git a/PlayerAttributes.h b/PlayerAttributes.h new file mode 100644 index 0000000..3cd1a0c --- /dev/null +++ b/PlayerAttributes.h @@ -0,0 +1,38 @@ +/* + * ===================================================================== + * Version: 1.0 + * Created: 01.09.2012 14:38:08 + * Author: Miroslav Bendík + * Company: LinuxOS.sk + * ===================================================================== + */ + +#ifndef PLAYERATTRIBUTES_H_D7THWFVV +#define PLAYERATTRIBUTES_H_D7THWFVV + +#include +#include + +struct Player +{ + std::string name; + double x; + double y; + double z; +}; /* ----- end of struct Player ----- */ + +class PlayerAttributes +{ +public: + typedef std::list Players; + + PlayerAttributes(const std::string &sourceDirectory); + Players::iterator begin(); + Players::iterator end(); + +private: + Players m_players; +}; /* ----- end of class PlayerAttributes ----- */ + +#endif /* end of include guard: PLAYERATTRIBUTES_H_D7THWFVV */ + diff --git a/TileGenerator.cpp b/TileGenerator.cpp index d307dee..2809a64 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -14,8 +14,8 @@ #include #include #include -#include #include "config.h" +#include "PlayerAttributes.h" #include "TileGenerator.h" #include "colors.h" @@ -566,49 +566,14 @@ void TileGenerator::renderPlayers(const std::string &inputPath) { int color = rgb2int(m_playerColor.r, m_playerColor.g, m_playerColor.b); - string playersPath = inputPath + "players"; - DIR *dir; - dir = opendir (playersPath.c_str()); - if (dir == NULL) { - return; - } - - struct dirent *ent; - while ((ent = readdir (dir)) != NULL) { - if (ent->d_name[0] == '.') { - continue; - } - - string path = playersPath + PATH_SEPARATOR + ent->d_name; - - ifstream in; - in.open(path.c_str(), ifstream::in); - string buffer; - string name; - string position; - while (getline(in, buffer)) { - if (buffer.find("name = ") == 0) { - name = buffer.substr(7); - } - else if (buffer.find("position = ") == 0) { - position = buffer.substr(12, buffer.length() - 13); - } - } - double x, y, z; - char comma; - istringstream positionStream(position, istringstream::in); - positionStream >> x; - positionStream >> comma; - positionStream >> y; - positionStream >> comma; - positionStream >> z; - int imageX = x / 10 - m_xMin * 16 + m_border; - int imageY = m_mapHeight - (z / 10 - m_zMin * 16) + m_border; + PlayerAttributes players(inputPath); + for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) { + int imageX = player->x / 10 - m_xMin * 16 + m_border; + int imageY = m_mapHeight - (player->z / 10 - m_zMin * 16) + m_border; gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color); - gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast(const_cast(name.c_str())), color); + gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast(const_cast(player->name.c_str())), color); } - closedir(dir); } inline std::list TileGenerator::getZValueList() const