Skip to content

Commit dff3319

Browse files
committed
refactored the whole project into ECS design pattern; Assets is turned into Singleton AssetManager; Quaternion Camera rotation; simple raycasting for destroyng blocks, that is buggy; fix on old comments mistakes; added consts on some functions
1 parent 8d4060d commit dff3319

File tree

137 files changed

+16104
-2074
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+16104
-2074
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ PublishScripts/
194194
# except build/, which is used as an MSBuild target.
195195
!**/[Pp]ackages/build/
196196
# Uncomment if necessary however generally it will be regenerated when needed
197-
#!**/[Pp]ackages/repositories.config
197+
#!**/[Pp]ackages/repositories.m_config
198198
# NuGet v3's project.json files produces more ignorable files
199199
*.nuget.props
200200
*.nuget.targets
@@ -311,7 +311,7 @@ __pycache__/
311311

312312
# Cake - Uncomment if you are using it
313313
# tools/**
314-
# !tools/packages.config
314+
# !tools/packages.m_config
315315

316316
# Tabs Studio
317317
*.tss

CMakeLists.txt

+15-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ file(GLOB SOURCES
3131
"src/core/logic/events/include/*.h"
3232
"src/lib/*.cpp"
3333
"src/lib/include/*.h"
34+
"src/lib/entityx/*.h"
35+
"src/lib/entityx/*.cc"
36+
"src/lib/entityx/3rdparty/*.hpp"
37+
"src/lib/entityx/3rdparty/*.h"
38+
"src/lib/entityx/deps/*.h"
39+
"src/lib/entityx/help/*.h"
40+
"src/lib/entityx/help/*.cc"
41+
"src/lib/entityx/tags/*.h"
42+
"src/lib/entityx/tags/*.cc"
43+
"src/core/systems/*.cpp"
44+
"src/core/systems/include/*.h"
45+
"src/core/components/*.cpp"
46+
"src/core/components/include/*.h"
47+
"src/core/events/*.h"
3448
"src/lib/utils/camera/*.cpp"
3549
"src/lib/utils/camera/include/*.h"
3650
"src/lib/utils/geometry/*.cpp"
@@ -51,7 +65,7 @@ file(GLOB SOURCES
5165
add_executable(${PROJECT_NAME} ${SOURCES})
5266

5367
# in case CMake doesn't find the libraries uncomment the following lines
54-
# and put your path to the dependencies and add the cmake/sdl2-config.cmake
68+
# and put your path to the dependencies and add the cmake/sdl2-m_config.cmake
5569
# file to the root of that directory
5670

5771
list(APPEND CMAKE_PREFIX_PATH "D:/SDL2")

src/core/components/chunk.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#include "include/chunk.h"
3+
4+
Chunk::Chunk() : m_chunkState(Empty){
5+
std::memset(&m_heightMap, 0, sizeof(m_heightMap));
6+
m_chunkContents = new std::array<std::array<std::array<Cube, CHUNK_SIZE_X>, CHUNK_SIZE_Y>, CHUNK_SIZE_Z>();
7+
}
8+
9+
Chunk::~Chunk() {
10+
delete m_chunkContents;
11+
}
12+
13+
void Chunk::setCube(CubeType type, int x, int y, int z) {
14+
m_chunkContents->at(x).at(y).at(z).m_type = type;
15+
}
16+
17+
ChunkContentsPtr Chunk::getChunkContents() const {
18+
return m_chunkContents;
19+
}
20+
21+
HeightMap* Chunk::getHeightMap() {
22+
return &m_heightMap;
23+
}
24+
25+
BiomeMap* Chunk::getBiomeMap() {
26+
return &m_biomeMap;
27+
}
28+
29+
void Chunk::setHeight(int height, int x, int z) {
30+
m_heightMap[x][z] = height;
31+
}
32+
33+
34+
35+

src/core/data/terrain/chunk_mesh.cpp src/core/components/chunk_mesh.cpp

+24-27
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
//
44

55
#include "include/chunk_mesh.h"
6-
#include "include/chunk.h"
7-
#include "../../../lib/include/lib.h"
6+
#include "../../lib/include/lib.h"
87

98

10-
ChunkMesh::ChunkMesh() {
9+
ChunkMesh::ChunkMesh() : m_chunkMeshState(UnBuilt){
1110
static bool indexBufferInitialized = false;
1211
static IndexBuffer indexBuffer;
1312
if (!indexBufferInitialized) {
@@ -31,30 +30,25 @@ ChunkMesh::ChunkMesh() {
3130
delete[] buffer;
3231
}
3332

34-
for (int i = 0; i < vao.size(); i++) {
35-
vao.at(i).bind();
36-
vbo.at(i).bind();
33+
for (int i = 0; i < m_vao.size(); i++) {
34+
m_vao.at(i).bind();
35+
m_vbo.at(i).bind();
3736
indexBuffer.bind();
3837

39-
vbo.at(i).vertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
40-
(void*) (offsetof(Vertex, position)));
41-
vbo.at(i).vertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
42-
(void*) (offsetof(Vertex, uvs)));
43-
vbo.at(i).vertexAttribIPointer(2, 1, GL_UNSIGNED_BYTE, sizeof(Vertex),
44-
(void*) offsetof(Vertex, lightningLevel));
38+
m_vbo.at(i).vertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
39+
(void*) (offsetof(Vertex, position)));
40+
m_vbo.at(i).vertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
41+
(void*) (offsetof(Vertex, uvs)));
42+
m_vbo.at(i).vertexAttribIPointer(2, 1, GL_UNSIGNED_BYTE, sizeof(Vertex),
43+
(void*) offsetof(Vertex, lightningLevel));
4544

46-
vao.at(i).unbind();
45+
m_vao.at(i).unbind();
4746
}
4847

4948
}
5049

51-
ChunkMesh::~ChunkMesh() {
52-
53-
}
54-
5550
void ChunkMesh::setMesh(std::array<std::vector<Vertex>, 3>& vertices) {
5651
for (int i = 0; i < vertices.size(); i++) {
57-
indicesCount.at(i) = 0;
5852
std::string type;
5953
if(i == 0){
6054
type = " normal";
@@ -69,37 +63,40 @@ void ChunkMesh::setMesh(std::array<std::vector<Vertex>, 3>& vertices) {
6963
(std::to_string(vertices.at(i).size()) + type +
7064
" m_vertices").c_str());
7165
if (!vertices.at(i).empty()) {
72-
vbo.at(i).bind();
73-
vbo.at(i).bufferData(vertices.at(i).size() * sizeof(Vertex), &vertices.at(i)[0], GL_STATIC_DRAW);
74-
indicesCount.at(i) = std::floor(vertices.at(i).size() / 4) * 6;
66+
m_vbo.at(i).bind();
67+
m_vbo.at(i).bufferData(vertices.at(i).size() * sizeof(Vertex), &vertices.at(i)[0], GL_STATIC_DRAW);
68+
m_indicesCount.at(i) = std::floor(vertices.at(i).size() / 4) * 6;
7569
vertices.at(i).clear();
7670
}
71+
else{
72+
m_indicesCount.at(i) = 0;
73+
}
7774

7875
}
7976
}
8077

8178
VertexArray* ChunkMesh::getVao() {
82-
return &vao.at(0);
79+
return &m_vao.at(0);
8380
}
8481

8582
VertexArray* ChunkMesh::getTransparentVao() {
86-
return &vao.at(1);
83+
return &m_vao.at(1);
8784
}
8885

8986
VertexArray* ChunkMesh::getModelVao() {
90-
return &vao.at(2);
87+
return &m_vao.at(2);
9188
}
9289

9390
unsigned int ChunkMesh::getIndicesCount() {
94-
return indicesCount.at(0);
91+
return m_indicesCount.at(0);
9592
}
9693

9794
unsigned int ChunkMesh::getTransparentIndicesCount() {
98-
return indicesCount.at(1);
95+
return m_indicesCount.at(1);
9996
}
10097

10198
unsigned int ChunkMesh::getModelIndicesCount() {
102-
return indicesCount.at(2);
99+
return m_indicesCount.at(2);
103100
}
104101

105102

src/core/components/frustum_aabb.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Created by Viktor on 7.3.2021 г..
3+
//
4+
5+
#include "include/frustum_aabb.h"
6+
7+
FrustumAABB::FrustumAABB(Vector3f dimensions, Vector3f position) : m_dimensions(dimensions), m_position(position) {
8+
9+
}
10+
11+
Vector3f FrustumAABB::getNegativeFarPoint(const Vector3f& normal) {
12+
Vector3f result = m_position;
13+
if (normal.x < 0) {
14+
result.x += m_dimensions.x;
15+
}
16+
if (normal.y < 0) {
17+
result.y += m_dimensions.y;
18+
}
19+
if (normal.z < 0) {
20+
result.z += m_dimensions.z;
21+
}
22+
return result;
23+
}
24+
25+
Vector3f FrustumAABB::getPositiveFarPoint(const Vector3f& normal) {
26+
Vector3f result = m_position;
27+
if (normal.x > 0) {
28+
result.x += m_dimensions.x;
29+
}
30+
if (normal.y > 0) {
31+
result.y += m_dimensions.y;
32+
}
33+
if (normal.z > 0) {
34+
result.z += m_dimensions.z;
35+
}
36+
return result;
37+
}

src/core/data/terrain/include/chunk.h src/core/components/include/chunk.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
//
2-
// Created by Viktor on 16.2.2021 г..
2+
// Created by Viktor on 6.3.2021 г..
33
//
44

55
#ifndef CHUNK_H
66
#define CHUNK_H
77

8+
89
#include <array>
9-
#include "../../../../lib/utils/geometry/include/vector2.h"
10-
#include "../../../../lib/utils/camera/include/frustum.h"
10+
#include "../../data/terrain/include/cube.h"
11+
#include "../../data/terrain/include/biome.h"
12+
#include "../../../lib/entityx/entityx.h"
1113
#include "chunk_mesh.h"
12-
#include "cube_data_base.h"
13-
#include "biome.h"
14+
1415

1516
typedef std::array<std::array<std::array<Cube, CHUNK_SIZE_X>, CHUNK_SIZE_Y>, CHUNK_SIZE_Z> ChunkContents;
1617
typedef std::array<std::array<std::array<Cube, CHUNK_SIZE_X>, CHUNK_SIZE_Y>, CHUNK_SIZE_Z>* ChunkContentsPtr;
@@ -22,21 +23,20 @@ enum ChunkState {
2223
Empty
2324
};
2425

25-
class Chunk {
26+
class Chunk : public entityx::Component<Chunk>{
2627
private:
27-
ChunkContents m_chunkContents;
28-
ChunkMesh m_chunkMesh;
29-
public:
28+
ChunkContentsPtr m_chunkContents;
3029
HeightMap m_heightMap;
3130
BiomeMap m_biomeMap;
31+
public:
3232
ChunkState m_chunkState;
33-
ChunkMeshState m_chunkMeshState;
34-
FrustumAABB m_frustumAABB;
35-
Chunk(int x, int z);
33+
Chunk();
3634
~Chunk();
3735
void setCube(CubeType type, int x, int y, int z);
38-
ChunkContentsPtr getChunkContents();
39-
ChunkMesh* getChunkMesh();
36+
void setHeight(int height, int x, int z);
37+
ChunkContentsPtr getChunkContents() const;
38+
HeightMap* getHeightMap();
39+
BiomeMap* getBiomeMap();
4040
};
4141

4242

src/core/data/terrain/include/chunk_mesh.h src/core/components/include/chunk_mesh.h

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
//
2-
// Created by Viktor on 17.2.2021 г..
2+
// Created by Viktor on 5.3.2021 г..
33
//
44

55
#ifndef CHUNK_MESH_H
66
#define CHUNK_MESH_H
77

8-
9-
#include <vector>
108
#include <array>
11-
#include "../../../../lib/utils/openGL/include/vertex.h"
12-
#include "../../../../lib/utils/openGL/include/vertex_array.h"
13-
#include "../../../../lib/utils/openGL/include/index_buffer.h"
14-
#include "../../../../lib/utils/openGL/include/vertex_buffer.h"
15-
#include "cube_data_base.h"
16-
#include "cube.h"
9+
#include <vector>
10+
11+
#include "../../../lib/utils/openGL/include/vertex.h"
12+
#include "../../../lib/utils/openGL/include/vertex_array.h"
13+
#include "../../../lib/utils/openGL/include/index_buffer.h"
14+
#include "../../../lib/utils/openGL/include/vertex_buffer.h"
15+
#include "../../../lib/entityx/entityx.h"
1716

1817
#define CHUNK_SIZE_X 16
1918
#define CHUNK_SIZE_Y 256
@@ -24,23 +23,21 @@ enum ChunkMeshState {
2423
UnBuilt
2524
};
2625

27-
class ChunkMesh {
26+
class ChunkMesh : public entityx::Component<ChunkMesh>{
2827
private:
29-
std::array<VertexArray, 3> vao;
30-
std::array<VertexBuffer, 3> vbo;
31-
std::array<unsigned int, 3> indicesCount;
28+
std::array<VertexArray, 3> m_vao;
29+
std::array<VertexBuffer, 3> m_vbo;
30+
std::array<unsigned int, 3> m_indicesCount;
3231
public:
3332
ChunkMesh();
34-
~ChunkMesh();
33+
ChunkMeshState m_chunkMeshState;
3534
void setMesh(std::array<std::vector<Vertex>, 3>& vertices);
3635
VertexArray* getVao();
3736
VertexArray* getTransparentVao();
3837
VertexArray* getModelVao();
3938
unsigned int getIndicesCount();
4039
unsigned int getTransparentIndicesCount();
4140
unsigned int getModelIndicesCount();
42-
43-
4441
};
4542

4643

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by Viktor on 7.3.2021 г..
3+
//
4+
5+
#ifndef FRUSTUM_AABB_H
6+
#define FRUSTUM_AABB_H
7+
8+
9+
#include "transform.h"
10+
#include "../../../lib/entityx/entityx.h"
11+
12+
class FrustumAABB : public entityx::Component<FrustumAABB>{
13+
private:
14+
Vector3f m_dimensions;
15+
Vector3f m_position;
16+
public:
17+
FrustumAABB(Vector3f dimensions, Vector3f position);
18+
Vector3f getNegativeFarPoint(const Vector3f& normal);
19+
Vector3f getPositiveFarPoint(const Vector3f& normal);
20+
};
21+
22+
23+
#endif //FRUSTUM_AABB_H

src/core/components/include/mesh.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by Viktor on 5.3.2021 г..
3+
//
4+
5+
#ifndef MESH_H
6+
#define MESH_H
7+
8+
9+
struct Mesh {
10+
std::vector<Vertex> vertices;
11+
};
12+
13+
14+
#endif //MESH_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by Viktor on 6.3.2021 г..
3+
//
4+
5+
#ifndef PLAYER_CONTROLLER_H
6+
#define PLAYER_CONTROLLER_H
7+
8+
9+
class PlayerController {
10+
11+
};
12+
13+
14+
#endif //PLAYER_CONTROLLER_H

0 commit comments

Comments
 (0)