Skip to content

Commit 231a60c

Browse files
author
Benoit Lormeau
committed
Level files factory
1 parent a3d38a4 commit 231a60c

13 files changed

+381
-36
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endfunction(PREPEND)
2222
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
2323

2424
project(NETiles)
25+
add_definitions("-std=c++17")
2526
add_definitions("-W -Wall -Wextra")
2627

2728
########################################

editor/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ PREPEND(EDITOR_SOURCES ${EDITOR_SOURCE_DIR}
1515
Ressources/Manager.cpp
1616
States/Main.cpp
1717
Tiles/Cursor.cpp
18+
Tiles/LevelFactory.cpp
19+
Tiles/LevelReaderLVL2.cpp
20+
Tiles/LevelReaderLVL2.hpp
1821
Tiles/Map.cpp
1922
Tiles/Tile.cpp
2023
)
@@ -29,6 +32,9 @@ PREPEND(EDITOR_INCLUDES ${EDITOR_INCLUDE_DIR}
2932
States/Menu.hpp
3033
States/State.hpp
3134
Tiles/Cursor.hpp
35+
Tiles/ILevelReader.hpp
36+
Tiles/ILevelWriter.hpp
37+
Tiles/LevelFactory.hpp
3238
Tiles/Map.hpp
3339
Tiles/Tile.hpp
3440
Utils/Singleton.hpp

editor/include/States/State.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class State
3535
virtual void update() = 0;
3636
virtual void render(sf::RenderWindow &target) = 0;
3737

38-
bool isRequestingClose()
38+
bool isRequestingClose() const
3939
{
4040
return m_requestingClose;
4141
}

editor/include/Tiles/ILevelReader.hpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
** EPITECH PROJECT, 2018
3+
** NETiles
4+
** File description:
5+
** Tiles / ILevelReader.hpp
6+
*/
7+
8+
#pragma once
9+
10+
////////////////////////////////////////////////////////////////////////////////
11+
12+
namespace Tiles {
13+
class ILevelReader;
14+
}
15+
16+
////////////////////////////////////////////////////////////////////////////////
17+
18+
#include <fstream>
19+
20+
#include "Tiles/LevelFactory.hpp"
21+
#include "Tiles/Tile.hpp"
22+
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
namespace Tiles
26+
{
27+
28+
class ILevelReader
29+
{
30+
public:
31+
virtual ~ILevelReader() = default;
32+
33+
public:
34+
virtual bool loadMapFromStream(std::ifstream &stream, TileArray &tiles) = 0;
35+
};
36+
37+
}

editor/include/Tiles/ILevelWriter.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
** EPITECH PROJECT, 2018
3+
** NETiles
4+
** File description:
5+
** Tiles / ILevelWriter.hpp
6+
*/
7+
8+
#pragma once
9+
10+
////////////////////////////////////////////////////////////////////////////////
11+
12+
namespace Tiles {
13+
class ILevelWriter;
14+
}
15+
16+
////////////////////////////////////////////////////////////////////////////////
17+
18+
#include "Tiles/LevelFactory.hpp"
19+
20+
////////////////////////////////////////////////////////////////////////////////
21+
22+
namespace Tiles
23+
{
24+
25+
class ILevelWriter
26+
{
27+
public:
28+
virtual ~ILevelWriter() = default;
29+
30+
public:
31+
virtual bool writeMap(const std::string &filename) = 0;
32+
};
33+
34+
}

editor/include/Tiles/LevelFactory.hpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
** EPITECH PROJECT, 2018
3+
** NETiles
4+
** File description:
5+
** Tiles / LevelFactory.hpp
6+
*/
7+
8+
#pragma once
9+
10+
////////////////////////////////////////////////////////////////////////////////
11+
12+
namespace Tiles {
13+
namespace priv {}
14+
15+
class LevelFactory;
16+
}
17+
18+
////////////////////////////////////////////////////////////////////////////////
19+
20+
#include <cstddef>
21+
#include <iostream>
22+
#include <vector>
23+
24+
#include "Tiles/ILevelReader.hpp"
25+
#include "Tiles/ILevelWriter.hpp"
26+
#include "Tiles/LevelReaderLVL2.hpp"
27+
28+
////////////////////////////////////////////////////////////////////////////////
29+
30+
namespace Tiles
31+
{
32+
33+
namespace priv
34+
{
35+
template <typename T>
36+
ILevelReader *createReader() {
37+
return new T();
38+
}
39+
40+
template <typename T>
41+
ILevelWriter *createWriter() {
42+
return new T();
43+
}
44+
}
45+
46+
class LevelFactory
47+
{
48+
public:
49+
template <typename T>
50+
static void registerReader()
51+
{
52+
unregisterReader<T>();
53+
54+
FactoryReader factory;
55+
factory.check = &T::check;
56+
factory.create = &priv::createReader<T>;
57+
58+
s_readers.push_back(factory);
59+
}
60+
61+
template <typename T>
62+
static void unregisterReader()
63+
{
64+
for (ReaderFactoryArray::iterator it = s_readers.begin(); it != s_readers.end(); ) {
65+
if (it->create == &priv::createReader<T>)
66+
it = s_readers.erase(it);
67+
else
68+
++it;
69+
}
70+
}
71+
72+
template <typename T>
73+
static void registerWriter()
74+
{
75+
unregisterWriter<T>();
76+
77+
FactoryWriter factory;
78+
factory.check = &T::check;
79+
factory.create = &priv::createWriter<T>;
80+
81+
s_writers.push_back(factory);
82+
}
83+
84+
template <typename T>
85+
static void unregisterWriter()
86+
{
87+
for (WriterFactoryArray::iterator it = s_writers.begin(); it != s_writers.end(); ) {
88+
if (it->create == &priv::createWriter<T>)
89+
it = s_writers.erase(it);
90+
else
91+
++it;
92+
}
93+
}
94+
95+
96+
static ILevelReader *getReaderFromStream(std::ifstream &stream);
97+
static ILevelWriter *getLastestWriter();
98+
99+
private:
100+
struct FactoryReader
101+
{
102+
bool (*check)(std::ifstream &stream);
103+
ILevelReader *(*create)();
104+
};
105+
typedef std::vector<FactoryReader> ReaderFactoryArray;
106+
107+
struct FactoryWriter
108+
{
109+
bool (*check)();
110+
ILevelWriter *(*create)();
111+
};
112+
typedef std::vector<FactoryWriter> WriterFactoryArray;
113+
114+
static ReaderFactoryArray s_readers;
115+
static WriterFactoryArray s_writers;
116+
};
117+
118+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
** EPITECH PROJECT, 2018
3+
** NETiles
4+
** File description:
5+
** Tiles / LevelReaderLVL2.hpp
6+
*/
7+
8+
#pragma once
9+
10+
////////////////////////////////////////////////////////////////////////////////
11+
12+
namespace Tiles {
13+
class LevelReaderLVL2;
14+
}
15+
16+
////////////////////////////////////////////////////////////////////////////////
17+
18+
#include <cstddef>
19+
#include <cstring>
20+
#include <fstream>
21+
#include <sstream>
22+
23+
#include "Tiles/ILevelReader.hpp"
24+
#include "Tiles/Tile.hpp"
25+
26+
////////////////////////////////////////////////////////////////////////////////
27+
28+
namespace Tiles
29+
{
30+
31+
class LevelReaderLVL2 : public ILevelReader
32+
{
33+
public:
34+
LevelReaderLVL2() = default;
35+
~LevelReaderLVL2() = default;
36+
37+
public:
38+
static bool check(std::ifstream &stream);
39+
40+
bool loadMapFromStream(std::ifstream &stream, TileArray &tiles);
41+
};
42+
43+
}

editor/include/Tiles/Map.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ namespace Tiles {
1919
#include <cerrno>
2020
#include <cstring>
2121
#include <fstream>
22-
#include <sstream>
2322
#include <iostream>
2423
#include <string>
2524

2625
#include <SFML/Graphics.hpp>
2726

28-
#include "Ressources/Manager.hpp"
29-
#include "Tiles/Tile.hpp"
27+
#include "../Ressources/Manager.hpp"
28+
#include "LevelFactory.hpp"
29+
#include "Tile.hpp"
3030

3131
////////////////////////////////////////////////////////////////////////////////
3232

editor/include/Tiles/Tile.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Tiles {
1515

1616
////////////////////////////////////////////////////////////////////////////////
1717

18+
#include <vector>
19+
1820
#include <SFML/Graphics.hpp>
1921
#include <SFML/Window.hpp>
2022

@@ -31,13 +33,17 @@ class Tile
3133
Tile(const sf::Vector2i &pos, const sf::Vector2i &tex);
3234
Tile(const sf::Vector2i &pos, const sf::Vector2i &tex, const sf::Vector2i &telePos);
3335

36+
public:
3437
sf::Color getColorType();
3538

39+
public:
3640
sf::Vector2i pos;
3741
sf::Vector2i tex;
3842
bool walkable;
3943
bool tele;
4044
sf::Vector2i telePos;
4145
};
4246

47+
typedef std::vector<Tile> TileArray;
48+
4349
}

editor/src/States/Main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Main::Main(const std::string &mapPath)
2121
, m_tileTypesFilter(false)
2222
, m_initialZoomFactor(2 * Tiles::Map::TILE_SIZE)
2323
{
24+
if (!m_tileMap.loadFromFile())
25+
throw std::runtime_error("Could not load map file.");
26+
2427
m_SFGDesktop.SetProperty("Window", "BackgroundColor", "#000000af");
2528
m_SFGDesktop.SetProperty("Window", "BorderWidth", 0);
2629
m_SFGDesktop.SetProperty("Window > Label", "FontSize", 24.f);
@@ -36,8 +39,6 @@ Main::Main(const std::string &mapPath)
3639
m_SFGWindow->Add(m_SFGLabel);
3740
m_SFGDesktop.Add(m_SFGWindow);
3841

39-
m_tileMap.loadFromFile();
40-
4142
Display::setViewCenter(m_tileMap.getCenter());
4243
Display::zoomView(1 / m_initialZoomFactor);
4344
}

0 commit comments

Comments
 (0)