Skip to content

Commit

Permalink
refactor: use smart pointer instead of raw pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumichocopengin8 committed Mar 29, 2022
1 parent 8f96282 commit 16c2023
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 48 deletions.
5 changes: 3 additions & 2 deletions biome/Biome.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ class Biome {
[[maybe_unused]] std::string id;
int x;
int z;
BiomeType *type;
std::shared_ptr<BiomeType> type;

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

[[nodiscard]] float distance_to(float x, float z) const;
};
Expand Down
10 changes: 10 additions & 0 deletions biome/BiomeType.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
#include "../block/types_of_block.h"

struct BiomeType {
BiomeType() {
this->id = "";
this->ground = GRASS;
}

explicit BiomeType(const BiomeType *pType) {
this->id = pType->id;
this->ground = pType->ground;
}

std::string id;
int ground = GRASS;
float tree_frequency = 0;
Expand Down
6 changes: 3 additions & 3 deletions chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#include "../utils/Utils.h"


Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
Chunk::Chunk(int chunk_x, int chunk_z, BiomeType* biomeType) {
this->x = chunk_x;
this->z = chunk_z;
this->biome = biome;

std::unique_ptr<BiomeType> temp(biomeType); // TODO: is it a clean way to make a unique pointer?
this->biome = std::move(temp);

// PerlinNoise Setup
uint32_t seed = utils::random(RAND_MAX);
Expand Down
4 changes: 2 additions & 2 deletions chunk/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class Chunk {

int x;
int z;
BiomeType *biome; // TODO
std::unique_ptr<BiomeType> biome;

int blocks[CHUNK_SIZE][CHUNK_HEIGHT][CHUNK_SIZE]{};
int heights[CHUNK_SIZE][CHUNK_SIZE]{};

explicit Chunk(int chunk_x, int chunk_z, BiomeType *biome);
explicit Chunk(int chunk_x, int chunk_z, BiomeType *biomeType);

void render();

Expand Down
52 changes: 16 additions & 36 deletions world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#include "../utils/Utils.h"

World::World() {
this->chunks = std::map<std::string, Chunk *>();
this->biome_types = std::map<std::string, BiomeType *>();

this->chunks = std::map<std::string, std::shared_ptr<Chunk>>();
this->biome_types = std::map<std::string, std::shared_ptr<BiomeType>>();
this->initializeBiomes();
}

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

void World::loadChunk(int chunk_x, int chunk_y) {

std::string key = std::to_string(chunk_x) + "_" + std::to_string(chunk_y);
Chunk *chunk = nullptr;
std::shared_ptr<Chunk> chunk;

if (this->chunks.count(key) > 0) {
chunk = this->chunks[key];
} else {
chunk = this->generateChunk(chunk_x, chunk_y);
chunk = std::move(this->generateChunk(chunk_x, chunk_y));
this->chunks[key] = chunk;
}


chunk->render();
}

Chunk *World::generateChunk(int chunk_x, int chunk_y) {
std::unique_ptr<Chunk> World::generateChunk(int chunk_x, int chunk_y) {
std::string key = std::to_string(chunk_x) + "_" + std::to_string(chunk_y);

BiomeType *biome = this->chooseChunkBiome(chunk_x, chunk_y);

Chunk *chunk = nullptr;
chunk = new Chunk(chunk_x, chunk_y, biome);


std::shared_ptr<BiomeType> biome_type = this->chooseChunkBiome(chunk_x, chunk_y);
std::unique_ptr<Chunk> chunk(new Chunk(chunk_x, chunk_y, biome_type.get()));
return chunk;
}

int World::getBlock(int x, int y, int z) {

std::string key = std::to_string(int(floor(x / 16.0))) + "_" + std::to_string(int(floor(z / 16.0)));

Chunk *chunk = nullptr;
std::shared_ptr<Chunk> chunk;
///Chunk
if (this->chunks.count(key) > 0) {
chunk = this->chunks[key];
Expand All @@ -78,12 +67,11 @@ int World::getBlock(int x, int y, int z) {
}

return chunk->getBlock(x, y, z);

}

int World::getTerrainHeight(int x, int z) {
std::string key = std::to_string(int(floor(x / 16.0))) + "_" + std::to_string(int(floor(z / 16.0)));
Chunk *chunk = nullptr;
std::shared_ptr<Chunk> chunk;
if (this->chunks.count(key) > 0) {
chunk = this->chunks[key];
} else {
Expand All @@ -103,8 +91,7 @@ int World::getTerrainHeight(int x, int z) {
return chunk->getHeight(x, z);
}

BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {

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


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

if (best_distance > 20) {

std::vector<std::string> possibleBiomeTypes;

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


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

std::vector<Chunk *> World::getNeighborsChunks(int x, int z) {

std::vector<Chunk *> neighbors;
std::vector<std::shared_ptr<Chunk>> World::getNeighborsChunks(int x, int z) {
std::vector<std::shared_ptr<Chunk>> neighbors;

for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
Expand All @@ -163,24 +146,21 @@ std::vector<Chunk *> World::getNeighborsChunks(int x, int z) {
}
}
}

return std::vector<Chunk *>();
return neighbors;
}

void World::initializeBiomes() {

this->biome_types["prairie"] = new BiomeType();
this->biome_types["prairie"] = std::make_unique<BiomeType>(new BiomeType());
this->biome_types["prairie"]->id = "prairie";
this->biome_types["prairie"]->ground = GRASS;
this->biome_types["prairie"]->tree_frequency = 0.02;

this->biome_types["desert"] = new BiomeType();
this->biome_types["desert"] = std::make_unique<BiomeType>(new BiomeType());
this->biome_types["desert"]->id = "desert";
this->biome_types["desert"]->ground = SAND;
this->biome_types["desert"]->cactus_frequency = 0.02;


this->biome_types["mountain"] = new BiomeType();
this->biome_types["mountain"] = std::make_unique<BiomeType>(new BiomeType());
this->biome_types["mountain"]->id = "mountain";
this->biome_types["mountain"]->ground = GRASS;
this->biome_types["mountain"]->rock_frequency = 0.05;
Expand Down
10 changes: 5 additions & 5 deletions world/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class World {

public:

std::map<std::string, Chunk *> chunks;
std::map<std::string, std::shared_ptr<Chunk>> chunks;
std::vector<Biome> biomes;

std::map<std::string, BiomeType *> biome_types;
std::map<std::string, std::shared_ptr<BiomeType>> biome_types;


World();
Expand All @@ -29,15 +29,15 @@ class World {

void loadChunk(int chunk_x, int chunk_y);

Chunk *generateChunk(int chunk_x, int chunk_y);
std::unique_ptr<Chunk> generateChunk(int chunk_x, int chunk_y);

BiomeType *chooseChunkBiome(int chunk_x, int chunk_y);
std::shared_ptr<BiomeType> chooseChunkBiome(int chunk_x, int chunk_y);

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

int getTerrainHeight(int x, int z);

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


void initializeBiomes();
Expand Down

0 comments on commit 16c2023

Please sign in to comment.