Player attributes moved to separate class.

This commit is contained in:
Miroslav Bendík 2012-09-01 16:40:18 +02:00
parent f0e064744b
commit 7c5f4751f6
4 changed files with 120 additions and 42 deletions

@ -55,13 +55,15 @@ include_directories(
set(mapper_HDRS set(mapper_HDRS
PixelAttributes.h PixelAttributes.h
PlayerAttributes.h
TileGenerator.h TileGenerator.h
) )
set(mapper_SRCS set(mapper_SRCS
mapper.cpp
PixelAttributes.cpp PixelAttributes.cpp
PlayerAttributes.cpp
TileGenerator.cpp TileGenerator.cpp
mapper.cpp
) )
add_executable(minetest_mapper add_executable(minetest_mapper

73
PlayerAttributes.cpp Normal file

@ -0,0 +1,73 @@
/*
* =====================================================================
* Version: 1.0
* Created: 01.09.2012 14:38:05
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#include <dirent.h>
#include <fstream>
#include <sstream>
#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();
}

38
PlayerAttributes.h Normal file

@ -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 <list>
#include <string>
struct Player
{
std::string name;
double x;
double y;
double z;
}; /* ----- end of struct Player ----- */
class PlayerAttributes
{
public:
typedef std::list<Player> 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 */

@ -14,8 +14,8 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <zlib.h> #include <zlib.h>
#include <dirent.h>
#include "config.h" #include "config.h"
#include "PlayerAttributes.h"
#include "TileGenerator.h" #include "TileGenerator.h"
#include "colors.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); int color = rgb2int(m_playerColor.r, m_playerColor.g, m_playerColor.b);
string playersPath = inputPath + "players"; PlayerAttributes players(inputPath);
DIR *dir; for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
dir = opendir (playersPath.c_str()); int imageX = player->x / 10 - m_xMin * 16 + m_border;
if (dir == NULL) { int imageY = m_mapHeight - (player->z / 10 - m_zMin * 16) + m_border;
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;
gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color); gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color);
gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(name.c_str())), color); gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(player->name.c_str())), color);
} }
closedir(dir);
} }
inline std::list<int> TileGenerator::getZValueList() const inline std::list<int> TileGenerator::getZValueList() const