Skip to content

Commit 4b5e62f

Browse files
Merge pull request #31 from Gumichocopengin8/feature/refactor
Code Refactor
2 parents 4add39d + 16c2023 commit 4b5e62f

39 files changed

+1116
-1190
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
cmake-build-debug/
3+
cmake-build-release/
34
.vscode/
45
.DS_Store

CMakeLists.txt

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

4-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -framework GLUT -framework OpenGL")
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -framework GLUT -framework OpenGL")
55
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
66
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os")
77

@@ -18,6 +18,8 @@ add_executable(
1818
block/types_of_block.h
1919
camera/Camera.cpp
2020
camera/Camera.h
21+
utils/Utils.cpp
22+
utils/Utils.h
2123
window/Window.cpp
2224
window/Window.h
2325
player/Player.cpp

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ Check out [this video!](https://youtu.be/Iv7E1rnGXKs)
2424
- `d`: move right
2525
- `a`: move left
2626
- `space`: jump
27-
27+
2828
You can use mouse to see around

biome/Biome.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
//
44

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

8-
using namespace std;
9-
10-
Biome::Biome(string id, int x, int z, BiomeType *type) {
11-
this->id = id;
12-
this->x = x;
13-
this->z = z;
14-
this->type = type;
15-
}
16-
17-
float Biome::distance_to(float x, float z) {
18-
return pow(x - this->x, 2) + pow(z - this->z, 2);
9+
float Biome::distance_to(float x, float z) const {
10+
return pow(x - this->x, 2) + pow(z - this->z, 2);
1911
}

biome/Biome.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77

88
#include <iostream>
99
#include <string>
10+
#include <utility>
1011

1112
#include "BiomeType.h"
1213

1314
class Biome {
14-
1515
public:
16-
std::string id;
17-
int x;
18-
int z;
19-
BiomeType *type;
16+
[[maybe_unused]] std::string id;
17+
int x;
18+
int z;
19+
std::shared_ptr<BiomeType> type;
2020

21-
Biome(std::string id, int x, int z, BiomeType *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

23-
float distance_to(float x, float z);
24+
[[nodiscard]] float distance_to(float x, float z) const;
2425
};
2526

2627

biome/BiomeType.h

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

1414
struct BiomeType {
15-
16-
std::string id;
17-
int ground = GRASS;
18-
float tree_frequency = 0;
19-
float cactus_frequency = 0;
20-
float rock_frequency = 0;
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+
25+
std::string id;
26+
int ground = GRASS;
27+
float tree_frequency = 0;
28+
float cactus_frequency = 0;
29+
float rock_frequency = 0;
2130
};
2231

2332

block/Block.cpp

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,56 @@
44

55
#include "Block.h"
66
#include "../texture/Texture.h"
7-
#include <iostream>
8-
#include <cstdlib>
97
#import <GLUT/glut.h>
108

119
#include "../main.h"
1210

13-
using namespace std;
14-
15-
Block::Block(int type, int x, int y, int z) {
16-
this->type = type;
17-
this->x = x;
18-
this->y = y;
19-
this->z = z;
20-
}
21-
22-
void Block::render() {
23-
24-
// Transforms
25-
glPushMatrix();
26-
glTranslated(x, y, z);
27-
28-
// Texture
29-
Texture texture;
30-
31-
if (this->type == GROUND) { texture.ground(); }
32-
else if (this->type == GRASS) { texture.grass(); }
33-
else if (this->type == TREETRUNK) { texture.treeTrunk(); }
34-
else if (this->type == TREELEAVES) { texture.treeLeaves(); }
35-
else if (this->type == SNOW) { texture.snow(); }
36-
else if (this->type == WATER) { texture.water(); }
37-
else if (this->type == CLOUD) { texture.cloud(); }
38-
else if (this->type == ORANGE) { texture.orange(); }
39-
else if (this->type == LAWNGREEN) { texture.lawngreen(); }
40-
else if (this->type == SAND) { texture.sand(); }
41-
else if (this->type == ROCK) { texture.rock(); }
42-
else { texture.ground(); }
43-
44-
glutSolidCube(1);
45-
glPopMatrix();
11+
void Block::render() const {
12+
// Transforms
13+
glPushMatrix();
14+
glTranslated(x, y, z);
15+
16+
// Texture
17+
Texture texture;
18+
19+
switch (this->type) {
20+
case GROUND:
21+
texture.ground();
22+
break;
23+
case GRASS:
24+
texture.grass();
25+
break;
26+
case TREETRUNK:
27+
texture.treeTrunk();
28+
break;
29+
case TREELEAVES:
30+
texture.treeLeaves();
31+
break;
32+
case SNOW:
33+
texture.snow();
34+
break;
35+
case WATER:
36+
texture.water();
37+
break;
38+
case CLOUD:
39+
texture.cloud();
40+
break;
41+
case ORANGE:
42+
texture.orange();
43+
break;
44+
case LAWNGREEN:
45+
texture.lawngreen();
46+
break;
47+
case SAND:
48+
texture.sand();
49+
break;
50+
case ROCK:
51+
texture.rock();
52+
break;
53+
default:
54+
texture.ground();
55+
}
56+
57+
glutSolidCube(1);
58+
glPopMatrix();
4659
}

block/Block.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
class Block {
1212

1313
private:
14-
int type;
15-
int x;
16-
int y;
17-
int z;
14+
int type;
15+
int x;
16+
int y;
17+
int z;
1818

1919
public:
20-
Block(int type, int x, int y, int z);
20+
explicit Block(int type, int x, int y, int z) : type{type}, x{x}, y{y}, z{z} {};
2121

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

2525

block/types_of_block.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
#ifndef PROJECT_TYPES_OF_BLOCK_H
66
#define PROJECT_TYPES_OF_BLOCK_H
77

8-
#define AIR 0
9-
#define GROUND 1
10-
#define GRASS 2
11-
#define TREETRUNK 3
12-
#define TREELEAVES 4
13-
#define SNOW 5
14-
#define WATER 6
15-
#define CLOUD 7
16-
#define ORANGE 8
17-
#define LAWNGREEN 9
18-
#define SAND 10
19-
#define ROCK 11
8+
constexpr int AIR = 0;
9+
constexpr int GROUND = 1;
10+
constexpr int GRASS = 2;
11+
constexpr int TREETRUNK = 3;
12+
constexpr int TREELEAVES = 4;
13+
constexpr int SNOW = 5;
14+
constexpr int WATER = 6;
15+
constexpr int CLOUD = 7;
16+
constexpr int ORANGE = 8;
17+
constexpr int LAWNGREEN = 9;
18+
constexpr int SAND = 10;
19+
constexpr int ROCK = 11;
2020

2121
#endif //PROJECT_TYPES_OF_BLOCK_H

0 commit comments

Comments
 (0)