Skip to content

Commit e092198

Browse files
refactor: make code more meaningful
1 parent 6fd4f92 commit e092198

File tree

25 files changed

+108
-133
lines changed

25 files changed

+108
-133
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.20)
22
project(project)
33

44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -framework GLUT -framework OpenGL")

biome/Biome.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
//
44

55
#include <cmath>
6+
#include <utility>
67
#include "Biome.h"
78

89
Biome::Biome(std::string id, int x, int z, BiomeType *type) {
9-
this->id = id;
10+
this->id = std::move(id);
1011
this->x = x;
1112
this->z = z;
1213
this->type = type;
1314
}
1415

15-
float Biome::distance_to(float x, float z) {
16+
float Biome::distance_to(float x, float z) const {
1617
return pow(x - this->x, 2) + pow(z - this->z, 2);
1718
}

biome/Biome.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Biome {
1919

2020
explicit Biome(std::string id, int x, int z, BiomeType *type);
2121

22-
float distance_to(float x, float z);
22+
[[nodiscard]] float distance_to(float x, float z) const;
2323
};
2424

2525

block/Block.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Block::Block(int type, int x, int y, int z) {
1717
this->z = z;
1818
}
1919

20-
void Block::render() {
20+
void Block::render() const {
2121

2222
// Transforms
2323
glPushMatrix();
@@ -26,18 +26,43 @@ void Block::render() {
2626
// Texture
2727
Texture texture;
2828

29-
if (this->type == GROUND) { texture.ground(); }
30-
else if (this->type == GRASS) { texture.grass(); }
31-
else if (this->type == TREETRUNK) { texture.treeTrunk(); }
32-
else if (this->type == TREELEAVES) { texture.treeLeaves(); }
33-
else if (this->type == SNOW) { texture.snow(); }
34-
else if (this->type == WATER) { texture.water(); }
35-
else if (this->type == CLOUD) { texture.cloud(); }
36-
else if (this->type == ORANGE) { texture.orange(); }
37-
else if (this->type == LAWNGREEN) { texture.lawngreen(); }
38-
else if (this->type == SAND) { texture.sand(); }
39-
else if (this->type == ROCK) { texture.rock(); }
40-
else { texture.ground(); }
29+
switch (this->type) {
30+
case GROUND:
31+
texture.ground();
32+
break;
33+
case GRASS:
34+
texture.grass();
35+
break;
36+
case TREETRUNK:
37+
texture.treeTrunk();
38+
break;
39+
case TREELEAVES:
40+
texture.treeLeaves();
41+
break;
42+
case SNOW:
43+
texture.snow();
44+
break;
45+
case WATER:
46+
texture.water();
47+
break;
48+
case CLOUD:
49+
texture.cloud();
50+
break;
51+
case ORANGE:
52+
texture.orange();
53+
break;
54+
case LAWNGREEN:
55+
texture.lawngreen();
56+
break;
57+
case SAND:
58+
texture.sand();
59+
break;
60+
case ROCK:
61+
texture.rock();
62+
break;
63+
default:
64+
texture.ground();
65+
}
4166

4267
glutSolidCube(1);
4368
glPopMatrix();

block/Block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Block {
1919
public:
2020
explicit Block(int type, int x, int y, int z);
2121

22-
void render();
22+
void render() const;
2323
};
2424

2525

camera/Camera.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace {
1919
}
2020

2121

22-
Camera::Camera(double camera_x, double camera_y, double camera_z, double pitch, double yaw, double roll) {
22+
Camera::Camera(float camera_x, float camera_y, float camera_z, float pitch, float yaw, float roll) {
2323
this->camera_x = camera_x;
2424
this->camera_y = camera_y;
2525
this->camera_z = camera_z;
@@ -68,9 +68,9 @@ void Camera::move_x(float speed) {
6868
if (this->x_speed < -MAX_HORIZONTAL_SPEED) this->x_speed = -MAX_HORIZONTAL_SPEED;
6969

7070

71-
double new_camera_x = this->camera_x + sin(this->yaw) * cos(this->pitch) * this->x_speed;
72-
double new_camera_y = this->camera_y + sin(this->pitch) * speed;
73-
double new_camera_z = this->camera_z - cos(this->yaw) * cos(this->pitch) * this->x_speed;
71+
float new_camera_x = this->camera_x + sin(this->yaw) * cos(this->pitch) * this->x_speed;
72+
float new_camera_y = this->camera_y + sin(this->pitch) * speed;
73+
float new_camera_z = this->camera_z - cos(this->yaw) * cos(this->pitch) * this->x_speed;
7474

7575

7676
if (worldPtr->getBlock(round(new_camera_x), round(new_camera_y) - CAMERA_HEIGHT, round(new_camera_z)) == 0) {
@@ -88,8 +88,8 @@ void Camera::move_z(float speed) {
8888
if (this->z_speed > MAX_HORIZONTAL_SPEED) this->z_speed = MAX_HORIZONTAL_SPEED;
8989
if (this->z_speed < -MAX_HORIZONTAL_SPEED) this->z_speed = -MAX_HORIZONTAL_SPEED;
9090

91-
double new_camera_x = this->camera_x + cos(this->yaw) * this->z_speed;
92-
double new_camera_z = this->camera_z + sin(this->yaw) * this->z_speed;
91+
float new_camera_x = this->camera_x + cos(this->yaw) * this->z_speed;
92+
float new_camera_z = this->camera_z + sin(this->yaw) * this->z_speed;
9393

9494
this->camera_x = new_camera_x;
9595
this->camera_z = new_camera_z;
@@ -100,7 +100,7 @@ void Camera::move_y(float speed) {
100100
if (this->y_speed > MAX_VERTICAL_SPEED) this->y_speed = MAX_VERTICAL_SPEED;
101101
if (this->y_speed < -MAX_VERTICAL_SPEED) this->y_speed = -MAX_VERTICAL_SPEED;
102102

103-
double new_camera_y = this->camera_y + cos(this->pitch) * this->y_speed;
103+
float new_camera_y = this->camera_y + cos(this->pitch) * this->y_speed;
104104

105105
if (worldPtr->getBlock(round(this->camera_x), new_camera_y - CAMERA_HEIGHT, round(this->camera_z)) == 0) {
106106
this->camera_y = new_camera_y;

camera/Camera.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
class Camera {
1111
private:
12-
double pitch; // x rotation
13-
double yaw; // y rotation
14-
double roll; // z rotation
12+
float pitch; // x rotation
13+
float yaw; // y rotation
14+
float roll; // z rotation
1515

1616
public:
17-
double camera_x;
18-
double camera_y;
19-
double camera_z;
20-
double x_speed;
21-
double y_speed;
22-
double z_speed;
23-
24-
explicit Camera(double camera_x, double camera_y, double camera_z, double pitch, double yaw, double roll);
17+
float camera_x;
18+
float camera_y;
19+
float camera_z;
20+
float x_speed;
21+
float y_speed;
22+
float z_speed;
23+
24+
explicit Camera(float camera_x, float camera_y, float camera_z, float pitch, float yaw, float roll);
2525

2626
void refresh(Light light);
2727

chunk/Chunk.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#include <iostream>
6+
#include <random>
67
#include <string>
78

89
#include "Chunk.h"
@@ -50,17 +51,11 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
5051
if (new_percent_j > 1) new_percent_j = 1;
5152

5253

53-
int height;
54-
55-
if (this->biome->id == "mountain") {
56-
height = round(perlin.octaveNoise0_1(i / fx, j / fy, octaves) * 40);
57-
} else {
58-
height = round(perlin.octaveNoise0_1(i / fx, j / fy, octaves) * 20);
59-
}
54+
float height = roundf(
55+
perlin.octaveNoise0_1(i / fx, j / fy, octaves) * (this->biome->id == "mountain" ? 40 : 20));
6056

61-
62-
int old_height_i = -1;
63-
int old_height_j = -1;
57+
float old_height_i = -1;
58+
float old_height_j = -1;
6459

6560

6661
// Get adjacent i height
@@ -73,22 +68,22 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
7368
}
7469

7570
if (i > 10) {
76-
int temp_old_height = worldPtr->getTerrainHeight((1 + this->x) * 16, this->z * 16 + j);
71+
float temp_old_height = worldPtr->getTerrainHeight((1 + this->x) * 16, this->z * 16 + j);
7772
if (temp_old_height != -1)
7873
old_height_i = temp_old_height;
7974
}
8075

8176
// Get adjacent j height
8277

8378
if (j < 5) {
84-
int temp_old_height = worldPtr->getTerrainHeight(this->x * 16 + i, this->z * 16 - 1);
79+
float temp_old_height = worldPtr->getTerrainHeight(this->x * 16 + i, this->z * 16 - 1);
8580

8681
if (temp_old_height != -1)
8782
old_height_j = temp_old_height;
8883
}
8984

9085
if (j > 10) {
91-
int temp_old_height = worldPtr->getTerrainHeight(this->x * 16 + i, (1 + this->z) * 16);
86+
float temp_old_height = worldPtr->getTerrainHeight(this->x * 16 + i, (1 + this->z) * 16);
9287

9388
if (temp_old_height != -1)
9489
old_height_j = temp_old_height;
@@ -292,11 +287,3 @@ void Chunk::generateStructures() {
292287
}
293288
}
294289
}
295-
296-
float Chunk::distance_to(Chunk *other) {
297-
return pow(other->x - this->x, 2) + pow(other->z - this->z, 2);
298-
}
299-
300-
float Chunk::distance_to(float x, float z) {
301-
return pow(x - this->x, 2) + pow(z - this->z, 2);
302-
}

chunk/Chunk.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Chunk {
2121
int z;
2222
BiomeType *biome; // TODO
2323

24-
int blocks[CHUNK_SIZE][CHUNK_HEIGHT][CHUNK_SIZE];
25-
int heights[CHUNK_SIZE][CHUNK_SIZE];
24+
int blocks[CHUNK_SIZE][CHUNK_HEIGHT][CHUNK_SIZE]{};
25+
int heights[CHUNK_SIZE][CHUNK_SIZE]{};
2626

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

@@ -34,10 +34,6 @@ class Chunk {
3434

3535
int getHeight(int x, int z);
3636

37-
float distance_to(Chunk *other);
38-
39-
float distance_to(float x, float z);
40-
4137
void generateStructures();
4238

4339
void removeHiddenBlocks();

chunk/PerlinNoise.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace siv {
3636
class PerlinNoise {
3737
private:
3838

39-
std::int32_t p[512];
39+
std::int32_t p[512]{};
4040

4141
static double Fade(double t) noexcept {
4242
return t * t * t * (t * (t * 6 - 15) + 10);
@@ -89,15 +89,15 @@ namespace siv {
8989
}
9090
}
9191

92-
double noise(double x) const {
92+
[[nodiscard]] double noise(double x) const {
9393
return noise(x, 0.0, 0.0);
9494
}
9595

96-
double noise(double x, double y) const {
96+
[[nodiscard]] double noise(double x, double y) const {
9797
return noise(x, y, 0.0);
9898
}
9999

100-
double noise(double x, double y, double z) const {
100+
[[nodiscard]] double noise(double x, double y, double z) const {
101101
const std::int32_t X = static_cast<std::int32_t>(std::floor(x)) & 255;
102102
const std::int32_t Y = static_cast<std::int32_t>(std::floor(y)) & 255;
103103
const std::int32_t Z = static_cast<std::int32_t>(std::floor(z)) & 255;
@@ -123,7 +123,7 @@ namespace siv {
123123
Grad(p[BB + 1], x - 1, y - 1, z - 1))));
124124
}
125125

126-
double octaveNoise(double x, std::int32_t octaves) const {
126+
[[nodiscard]] double octaveNoise(double x, std::int32_t octaves) const {
127127
double result = 0.0;
128128
double amp = 1.0;
129129

@@ -136,7 +136,7 @@ namespace siv {
136136
return result;
137137
}
138138

139-
double octaveNoise(double x, double y, std::int32_t octaves) const {
139+
[[nodiscard]] double octaveNoise(double x, double y, std::int32_t octaves) const {
140140
double result = 0.0;
141141
double amp = 1.0;
142142

@@ -150,7 +150,7 @@ namespace siv {
150150
return result;
151151
}
152152

153-
double octaveNoise(double x, double y, double z, std::int32_t octaves) const {
153+
[[nodiscard]] double octaveNoise(double x, double y, double z, std::int32_t octaves) const {
154154
double result = 0.0;
155155
double amp = 1.0;
156156

@@ -165,27 +165,27 @@ namespace siv {
165165
return result;
166166
}
167167

168-
double noise0_1(double x) const {
168+
[[nodiscard]] double noise0_1(double x) const {
169169
return noise(x) * 0.5 + 0.5;
170170
}
171171

172-
double noise0_1(double x, double y) const {
172+
[[nodiscard]] double noise0_1(double x, double y) const {
173173
return noise(x, y) * 0.5 + 0.5;
174174
}
175175

176-
double noise0_1(double x, double y, double z) const {
176+
[[nodiscard]] double noise0_1(double x, double y, double z) const {
177177
return noise(x, y, z) * 0.5 + 0.5;
178178
}
179179

180-
double octaveNoise0_1(double x, std::int32_t octaves) const {
180+
[[nodiscard]] double octaveNoise0_1(double x, std::int32_t octaves) const {
181181
return octaveNoise(x, octaves) * 0.5 + 0.5;
182182
}
183183

184-
double octaveNoise0_1(double x, double y, std::int32_t octaves) const {
184+
[[nodiscard]] double octaveNoise0_1(double x, double y, std::int32_t octaves) const {
185185
return octaveNoise(x, y, octaves) * 0.5 + 0.5;
186186
}
187187

188-
double octaveNoise0_1(double x, double y, double z, std::int32_t octaves) const {
188+
[[nodiscard]] double octaveNoise0_1(double x, double y, double z, std::int32_t octaves) const {
189189
return octaveNoise(x, y, z, octaves) * 0.5 + 0.5;
190190
}
191191
};

0 commit comments

Comments
 (0)