Skip to content

Commit 7c5f475

Browse files
committed
Player attributes moved to separate class.
1 parent f0e0647 commit 7c5f475

File tree

4 files changed

+120
-42
lines changed

4 files changed

+120
-42
lines changed

CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ include_directories(
5555

5656
set(mapper_HDRS
5757
PixelAttributes.h
58+
PlayerAttributes.h
5859
TileGenerator.h
5960
)
6061

6162
set(mapper_SRCS
62-
mapper.cpp
6363
PixelAttributes.cpp
64+
PlayerAttributes.cpp
6465
TileGenerator.cpp
66+
mapper.cpp
6567
)
6668

6769
add_executable(minetest_mapper

PlayerAttributes.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* =====================================================================
3+
* Version: 1.0
4+
* Created: 01.09.2012 14:38:05
5+
* Author: Miroslav Bendík
6+
* Company: LinuxOS.sk
7+
* =====================================================================
8+
*/
9+
10+
#include <dirent.h>
11+
#include <fstream>
12+
#include <sstream>
13+
#include "config.h"
14+
#include "PlayerAttributes.h"
15+
16+
using namespace std;
17+
18+
PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory)
19+
{
20+
21+
string playersPath = sourceDirectory + "players";
22+
DIR *dir;
23+
dir = opendir (playersPath.c_str());
24+
if (dir == NULL) {
25+
return;
26+
}
27+
28+
struct dirent *ent;
29+
while ((ent = readdir (dir)) != NULL) {
30+
if (ent->d_name[0] == '.') {
31+
continue;
32+
}
33+
34+
string path = playersPath + PATH_SEPARATOR + ent->d_name;
35+
36+
ifstream in;
37+
in.open(path.c_str(), ifstream::in);
38+
string buffer;
39+
string name;
40+
string position;
41+
while (getline(in, buffer)) {
42+
if (buffer.find("name = ") == 0) {
43+
name = buffer.substr(7);
44+
}
45+
else if (buffer.find("position = ") == 0) {
46+
position = buffer.substr(12, buffer.length() - 13);
47+
}
48+
}
49+
char comma;
50+
Player player;
51+
istringstream positionStream(position, istringstream::in);
52+
positionStream >> player.x;
53+
positionStream >> comma;
54+
positionStream >> player.y;
55+
positionStream >> comma;
56+
positionStream >> player.z;
57+
player.name = name;
58+
59+
m_players.push_back(player);
60+
}
61+
closedir(dir);
62+
}
63+
64+
PlayerAttributes::Players::iterator PlayerAttributes::begin()
65+
{
66+
return m_players.begin();
67+
}
68+
69+
PlayerAttributes::Players::iterator PlayerAttributes::end()
70+
{
71+
return m_players.end();
72+
}
73+

PlayerAttributes.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* =====================================================================
3+
* Version: 1.0
4+
* Created: 01.09.2012 14:38:08
5+
* Author: Miroslav Bendík
6+
* Company: LinuxOS.sk
7+
* =====================================================================
8+
*/
9+
10+
#ifndef PLAYERATTRIBUTES_H_D7THWFVV
11+
#define PLAYERATTRIBUTES_H_D7THWFVV
12+
13+
#include <list>
14+
#include <string>
15+
16+
struct Player
17+
{
18+
std::string name;
19+
double x;
20+
double y;
21+
double z;
22+
}; /* ----- end of struct Player ----- */
23+
24+
class PlayerAttributes
25+
{
26+
public:
27+
typedef std::list<Player> Players;
28+
29+
PlayerAttributes(const std::string &sourceDirectory);
30+
Players::iterator begin();
31+
Players::iterator end();
32+
33+
private:
34+
Players m_players;
35+
}; /* ----- end of class PlayerAttributes ----- */
36+
37+
#endif /* end of include guard: PLAYERATTRIBUTES_H_D7THWFVV */
38+

TileGenerator.cpp

+6-41
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include <iostream>
1515
#include <sstream>
1616
#include <zlib.h>
17-
#include <dirent.h>
1817
#include "config.h"
18+
#include "PlayerAttributes.h"
1919
#include "TileGenerator.h"
2020
#include "colors.h"
2121

@@ -566,49 +566,14 @@ void TileGenerator::renderPlayers(const std::string &inputPath)
566566
{
567567
int color = rgb2int(m_playerColor.r, m_playerColor.g, m_playerColor.b);
568568

569-
string playersPath = inputPath + "players";
570-
DIR *dir;
571-
dir = opendir (playersPath.c_str());
572-
if (dir == NULL) {
573-
return;
574-
}
575-
576-
struct dirent *ent;
577-
while ((ent = readdir (dir)) != NULL) {
578-
if (ent->d_name[0] == '.') {
579-
continue;
580-
}
581-
582-
string path = playersPath + PATH_SEPARATOR + ent->d_name;
583-
584-
ifstream in;
585-
in.open(path.c_str(), ifstream::in);
586-
string buffer;
587-
string name;
588-
string position;
589-
while (getline(in, buffer)) {
590-
if (buffer.find("name = ") == 0) {
591-
name = buffer.substr(7);
592-
}
593-
else if (buffer.find("position = ") == 0) {
594-
position = buffer.substr(12, buffer.length() - 13);
595-
}
596-
}
597-
double x, y, z;
598-
char comma;
599-
istringstream positionStream(position, istringstream::in);
600-
positionStream >> x;
601-
positionStream >> comma;
602-
positionStream >> y;
603-
positionStream >> comma;
604-
positionStream >> z;
605-
int imageX = x / 10 - m_xMin * 16 + m_border;
606-
int imageY = m_mapHeight - (z / 10 - m_zMin * 16) + m_border;
569+
PlayerAttributes players(inputPath);
570+
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
571+
int imageX = player->x / 10 - m_xMin * 16 + m_border;
572+
int imageY = m_mapHeight - (player->z / 10 - m_zMin * 16) + m_border;
607573

608574
gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color);
609-
gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(name.c_str())), color);
575+
gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(player->name.c_str())), color);
610576
}
611-
closedir(dir);
612577
}
613578

614579
inline std::list<int> TileGenerator::getZValueList() const

0 commit comments

Comments
 (0)