Skip to content

Commit 16c2023

Browse files
refactor: use smart pointer instead of raw pointer
1 parent 8f96282 commit 16c2023

File tree

6 files changed

+39
-48
lines changed

6 files changed

+39
-48
lines changed

biome/Biome.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class Biome {
1616
[[maybe_unused]] std::string id;
1717
int x;
1818
int z;
19-
BiomeType *type;
19+
std::shared_ptr<BiomeType> type;
2020

21-
explicit Biome(std::string id, int x, int z, BiomeType *type) : id{std::move(id)}, x{x}, z{z}, type{type} {};
21+
explicit Biome(std::string id, int x, int z, std::shared_ptr<BiomeType> type) : id{std::move(id)}, x{x}, z{z},
22+
type{std::move(type)} {};
2223

2324
[[nodiscard]] float distance_to(float x, float z) const;
2425
};

biome/BiomeType.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
#include "../block/types_of_block.h"
1313

1414
struct BiomeType {
15+
BiomeType() {
16+
this->id = "";
17+
this->ground = GRASS;
18+
}
19+
20+
explicit BiomeType(const BiomeType *pType) {
21+
this->id = pType->id;
22+
this->ground = pType->ground;
23+
}
24+
1525
std::string id;
1626
int ground = GRASS;
1727
float tree_frequency = 0;

chunk/Chunk.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
#include "../utils/Utils.h"
1919

2020

21-
Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
21+
Chunk::Chunk(int chunk_x, int chunk_z, BiomeType* biomeType) {
2222
this->x = chunk_x;
2323
this->z = chunk_z;
24-
this->biome = biome;
25-
24+
std::unique_ptr<BiomeType> temp(biomeType); // TODO: is it a clean way to make a unique pointer?
25+
this->biome = std::move(temp);
2626

2727
// PerlinNoise Setup
2828
uint32_t seed = utils::random(RAND_MAX);

chunk/Chunk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class Chunk {
1919

2020
int x;
2121
int z;
22-
BiomeType *biome; // TODO
22+
std::unique_ptr<BiomeType> biome;
2323

2424
int blocks[CHUNK_SIZE][CHUNK_HEIGHT][CHUNK_SIZE]{};
2525
int heights[CHUNK_SIZE][CHUNK_SIZE]{};
2626

27-
explicit Chunk(int chunk_x, int chunk_z, BiomeType *biome);
27+
explicit Chunk(int chunk_x, int chunk_z, BiomeType *biomeType);
2828

2929
void render();
3030

world/World.cpp

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
#include "../utils/Utils.h"
1111

1212
World::World() {
13-
this->chunks = std::map<std::string, Chunk *>();
14-
this->biome_types = std::map<std::string, BiomeType *>();
15-
13+
this->chunks = std::map<std::string, std::shared_ptr<Chunk>>();
14+
this->biome_types = std::map<std::string, std::shared_ptr<BiomeType>>();
1615
this->initializeBiomes();
1716
}
1817

@@ -28,38 +27,28 @@ void World::update(double x, double y) {
2827
}
2928

3029
void World::loadChunk(int chunk_x, int chunk_y) {
31-
3230
std::string key = std::to_string(chunk_x) + "_" + std::to_string(chunk_y);
33-
Chunk *chunk = nullptr;
31+
std::shared_ptr<Chunk> chunk;
3432

3533
if (this->chunks.count(key) > 0) {
3634
chunk = this->chunks[key];
3735
} else {
38-
chunk = this->generateChunk(chunk_x, chunk_y);
36+
chunk = std::move(this->generateChunk(chunk_x, chunk_y));
3937
this->chunks[key] = chunk;
4038
}
41-
42-
4339
chunk->render();
4440
}
4541

46-
Chunk *World::generateChunk(int chunk_x, int chunk_y) {
42+
std::unique_ptr<Chunk> World::generateChunk(int chunk_x, int chunk_y) {
4743
std::string key = std::to_string(chunk_x) + "_" + std::to_string(chunk_y);
48-
49-
BiomeType *biome = this->chooseChunkBiome(chunk_x, chunk_y);
50-
51-
Chunk *chunk = nullptr;
52-
chunk = new Chunk(chunk_x, chunk_y, biome);
53-
54-
44+
std::shared_ptr<BiomeType> biome_type = this->chooseChunkBiome(chunk_x, chunk_y);
45+
std::unique_ptr<Chunk> chunk(new Chunk(chunk_x, chunk_y, biome_type.get()));
5546
return chunk;
5647
}
5748

5849
int World::getBlock(int x, int y, int z) {
59-
6050
std::string key = std::to_string(int(floor(x / 16.0))) + "_" + std::to_string(int(floor(z / 16.0)));
61-
62-
Chunk *chunk = nullptr;
51+
std::shared_ptr<Chunk> chunk;
6352
///Chunk
6453
if (this->chunks.count(key) > 0) {
6554
chunk = this->chunks[key];
@@ -78,12 +67,11 @@ int World::getBlock(int x, int y, int z) {
7867
}
7968

8069
return chunk->getBlock(x, y, z);
81-
8270
}
8371

8472
int World::getTerrainHeight(int x, int z) {
8573
std::string key = std::to_string(int(floor(x / 16.0))) + "_" + std::to_string(int(floor(z / 16.0)));
86-
Chunk *chunk = nullptr;
74+
std::shared_ptr<Chunk> chunk;
8775
if (this->chunks.count(key) > 0) {
8876
chunk = this->chunks[key];
8977
} else {
@@ -103,8 +91,7 @@ int World::getTerrainHeight(int x, int z) {
10391
return chunk->getHeight(x, z);
10492
}
10593

106-
BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
107-
94+
std::shared_ptr<BiomeType> World::chooseChunkBiome(int chunk_x, int chunk_y) {
10895
// For the first chunk
10996
if (this->biomes.empty()) {
11097
// Force desert because the biome is cool
@@ -115,7 +102,6 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
115102
return this->biome_types[key];
116103
}
117104

118-
119105
// Get the nearest biome
120106
float best_distance = this->biomes[0].distance_to(chunk_x, chunk_y);
121107
Biome best_distance_biome = this->biomes[0];
@@ -129,7 +115,6 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
129115
}
130116

131117
if (best_distance > 20) {
132-
133118
std::vector<std::string> possibleBiomeTypes;
134119

135120
for (auto const &biome_type: this->biome_types) {
@@ -138,7 +123,6 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
138123
}
139124
}
140125

141-
142126
auto it = possibleBiomeTypes.begin();
143127
advance(it, utils::random(RAND_MAX) % possibleBiomeTypes.size());
144128
Biome biome(*it, chunk_x, chunk_y, this->biome_types[*it]);
@@ -149,9 +133,8 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {
149133
return best_distance_biome.type;
150134
}
151135

152-
std::vector<Chunk *> World::getNeighborsChunks(int x, int z) {
153-
154-
std::vector<Chunk *> neighbors;
136+
std::vector<std::shared_ptr<Chunk>> World::getNeighborsChunks(int x, int z) {
137+
std::vector<std::shared_ptr<Chunk>> neighbors;
155138

156139
for (int i = -1; i <= 1; ++i) {
157140
for (int j = -1; j <= 1; ++j) {
@@ -163,24 +146,21 @@ std::vector<Chunk *> World::getNeighborsChunks(int x, int z) {
163146
}
164147
}
165148
}
166-
167-
return std::vector<Chunk *>();
149+
return neighbors;
168150
}
169151

170152
void World::initializeBiomes() {
171-
172-
this->biome_types["prairie"] = new BiomeType();
153+
this->biome_types["prairie"] = std::make_unique<BiomeType>(new BiomeType());
173154
this->biome_types["prairie"]->id = "prairie";
174155
this->biome_types["prairie"]->ground = GRASS;
175156
this->biome_types["prairie"]->tree_frequency = 0.02;
176157

177-
this->biome_types["desert"] = new BiomeType();
158+
this->biome_types["desert"] = std::make_unique<BiomeType>(new BiomeType());
178159
this->biome_types["desert"]->id = "desert";
179160
this->biome_types["desert"]->ground = SAND;
180161
this->biome_types["desert"]->cactus_frequency = 0.02;
181162

182-
183-
this->biome_types["mountain"] = new BiomeType();
163+
this->biome_types["mountain"] = std::make_unique<BiomeType>(new BiomeType());
184164
this->biome_types["mountain"]->id = "mountain";
185165
this->biome_types["mountain"]->ground = GRASS;
186166
this->biome_types["mountain"]->rock_frequency = 0.05;

world/World.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class World {
1717

1818
public:
1919

20-
std::map<std::string, Chunk *> chunks;
20+
std::map<std::string, std::shared_ptr<Chunk>> chunks;
2121
std::vector<Biome> biomes;
2222

23-
std::map<std::string, BiomeType *> biome_types;
23+
std::map<std::string, std::shared_ptr<BiomeType>> biome_types;
2424

2525

2626
World();
@@ -29,15 +29,15 @@ class World {
2929

3030
void loadChunk(int chunk_x, int chunk_y);
3131

32-
Chunk *generateChunk(int chunk_x, int chunk_y);
32+
std::unique_ptr<Chunk> generateChunk(int chunk_x, int chunk_y);
3333

34-
BiomeType *chooseChunkBiome(int chunk_x, int chunk_y);
34+
std::shared_ptr<BiomeType> chooseChunkBiome(int chunk_x, int chunk_y);
3535

3636
int getBlock(int x, int y, int z);
3737

3838
int getTerrainHeight(int x, int z);
3939

40-
std::vector<Chunk *> getNeighborsChunks(int x, int z);
40+
std::vector<std::shared_ptr<Chunk>> getNeighborsChunks(int x, int z);
4141

4242

4343
void initializeBiomes();

0 commit comments

Comments
 (0)