Skip to content

Commit 696747b

Browse files
committed
changes
1 parent 44b9bf8 commit 696747b

File tree

7 files changed

+121
-93
lines changed

7 files changed

+121
-93
lines changed

src/Chunk.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,32 @@ Mesh* Chunk::getMesh() {
335335
Model* Chunk::getModel() {
336336
return &m_model;
337337
}
338+
339+
void Chunk::cameraLook(Ray ray, HitResult& coll) {
340+
coll.coll = GetRayCollisionMesh(ray, m_model.meshes[0], MatrixTranslate(m_pos.x * chunkSize, 0.f, m_pos.z * chunkSize));
341+
if (coll.coll.hit) {
342+
auto normal = coll.coll.normal;
343+
if (normal == Vector3 {0, 1, 0})
344+
coll.face = Faces::Up;
345+
else if (normal == Vector3 {0, -1, 0})
346+
coll.face = Faces::Down;
347+
else if (normal == Vector3 {1, 0, 0})
348+
coll.face = Faces::Left;
349+
else if (normal == Vector3 {-1, 0, 0})
350+
coll.face = Faces::Right;
351+
else if (normal == Vector3 {0, 0, 1})
352+
coll.face = Faces::Back;
353+
else if (normal == Vector3 {0, 0, -1})
354+
coll.face = Faces::Front;
355+
356+
auto pos = coll.coll.point;
357+
coll.blockPos = {static_cast<int>(pos.x), static_cast<int>(pos.y), static_cast<int>(pos.z)};
358+
359+
if (coll.face == Faces::Up)
360+
coll.blockPos.y--;
361+
if (coll.face == Faces::Back)
362+
coll.blockPos.z--;
363+
if (coll.face == Faces::Left)
364+
coll.blockPos.x--;
365+
}
366+
}

src/Chunk.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "includes.hpp"
33
#include "Level.hpp"
4+
#include "HitResult.hpp"
45

56
class Level;
67

@@ -20,6 +21,7 @@ class Chunk {
2021
void calcLightDepths();
2122
Mesh* getMesh();
2223
Model* getModel();
24+
void cameraLook(Ray ray, HitResult& coll);
2325

2426
private:
2527
void generate();

src/HitResult.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include "includes.hpp"
3+
4+
struct HitResult {
5+
RayCollision coll;
6+
Faces face;
7+
BlockPos blockPos;
8+
};

src/Level.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Level::Level() {
77
void Level::generate() {
88
for (uint8_t x = 0; x < chunksCount; x++) {
99
for (uint8_t z = 0; z < chunksCount; z++) {
10-
m_chunks.insert({{x,z}, std::make_shared<Chunk>(ChunkPos {x, z}, this)});
11-
// m_chunks.push_back(std::make_shared<Chunk>(ChunkPos {x, z}, this));
10+
m_chunks.insert({{x, z}, std::make_shared<Chunk>(ChunkPos {x, z}, this)});
1211
}
1312
}
1413

@@ -29,19 +28,11 @@ void Level::render() {
2928
}
3029

3130
std::shared_ptr<Chunk> Level::getChunk(const ChunkPos& pos) {
32-
// for (const auto& chunk : m_chunks) {
33-
// if (chunk->getPos() == pos)
34-
// return chunk;
35-
// }
36-
37-
// return nullptr;
38-
3931
return m_chunks.count(pos) != 0 ? m_chunks[pos] : nullptr;
4032
}
4133

4234
std::shared_ptr<Chunk> Level::getChunk(const BlockPos& pos) {
43-
return getChunk(
44-
ChunkPos {static_cast<int32_t>(floorf(pos.x / (float)chunkSize)), static_cast<int32_t>(floorf(pos.z / (float)chunkSize))});
35+
return getChunk(pos.chunkPos());
4536
}
4637

4738
BlockTypes Level::getBlock(const BlockPos& pos) {
@@ -70,7 +61,6 @@ void Level::setTile(const BlockPos& pos, BlockTypes type) {
7061
chunk->generateMesh();
7162

7263
auto localPos = pos.local();
73-
// logD("localpos {} {} {} chunkpos {} {}", localPos.x, localPos.y, localPos.z, chunkPos.x, chunkPos.z);
7464

7565
if (localPos.x == 0) {
7666
if (auto c = getChunk(ChunkPos {chunkPos.x - 1, chunkPos.z}))

src/Level.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ class Level {
2121

2222
private:
2323
std::unordered_map<ChunkPos, std::shared_ptr<Chunk>> m_chunks;
24-
// std::vector<std::shared_ptr<Chunk>> m_chunks;
2524
};

src/main.cpp

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
#include "Level.hpp"
33
#include "Player.hpp"
44
#include "Timer.hpp"
5+
#include "HitResult.hpp"
56
// #include "rcamera.h"
67

7-
struct HitResult {
8-
RayCollision coll;
9-
Faces face;
10-
BlockPos blockPos;
11-
};
8+
using namespace std::chrono;
129

1310
int main() {
1411
SetRandomSeed(time(0));
@@ -23,6 +20,12 @@ int main() {
2320
// SetTargetFPS(60);
2421
SetTargetFPS(2600); // so it isnt too much bc then my pc starts making a high frequency noise which isnt good i suppose
2522

23+
BeginDrawing();
24+
ClearBackground(RAYWHITE);
25+
auto txtSize = MeasureText2("Please wait...", 30);
26+
DrawText("Please wait...", winW / 2 - txtSize.x / 2, winH / 2 - txtSize.y / 2, 30, GRAY);
27+
EndDrawing();
28+
2629
glEnable(GL_TEXTURE_2D);
2730
glShadeModel(GL_SMOOTH);
2831
glClearColor(.5f, .8f, 1.f, 0.f);
@@ -49,6 +52,9 @@ int main() {
4952
UploadMesh(&plane, false);
5053
auto planeModel = LoadModelFromMesh(plane);
5154

55+
auto lastTime = system_clock::now();
56+
size_t frames = 0;
57+
5258
while (!WindowShouldClose()) {
5359
timer->advanceTime();
5460

@@ -70,39 +76,34 @@ int main() {
7076

7177
// block selection
7278

73-
auto chunk = lvl->getChunk({(int)playerPos.x, (int)playerPos.y, (int)playerPos.z});
74-
if (chunk) {
75-
auto model = chunk->getModel();
76-
auto ray = cam.GetViewRay();
77-
coll.coll = GetRayCollisionMesh(ray, chunk->getModel()->meshes[0],
78-
MatrixTranslate(chunk->getPos().x * chunkSize, 0.f, chunk->getPos().z * chunkSize));
79-
if (coll.coll.hit) {
80-
auto normal = coll.coll.normal;
81-
if (normal == Vector3 {0, 1, 0})
82-
coll.face = Faces::Up;
83-
else if (normal == Vector3 {0, -1, 0})
84-
coll.face = Faces::Down;
85-
else if (normal == Vector3 {1, 0, 0})
86-
coll.face = Faces::Left;
87-
else if (normal == Vector3 {-1, 0, 0})
88-
coll.face = Faces::Right;
89-
else if (normal == Vector3 {0, 0, 1})
90-
coll.face = Faces::Back;
91-
else if (normal == Vector3 {0, 0, -1})
92-
coll.face = Faces::Front;
93-
94-
auto pos = coll.coll.point;
95-
coll.blockPos = {static_cast<int>(pos.x), static_cast<int>(pos.y), static_cast<int>(pos.z)};
96-
97-
if (coll.face == Faces::Up)
98-
coll.blockPos.y--;
99-
if (coll.face == Faces::Back)
100-
coll.blockPos.z--;
101-
if (coll.face == Faces::Left)
102-
coll.blockPos.x--;
79+
// auto chunk = lvl->getChunk({(int)playerPos.x, (int)playerPos.y, (int)playerPos.z});
80+
// if (chunk) {
81+
// chunk->cameraLook(cam.GetViewRay(), coll);
82+
// } else {
83+
// coll.coll.hit = false;
84+
// }
85+
86+
auto camRay = cam.GetViewRay();
87+
88+
coll.coll.hit = false;
89+
auto plBlockPos = BlockPos {(int)playerPos.x, (int)playerPos.y, (int)playerPos.z};
90+
auto chunkPos = plBlockPos.chunkPos();
91+
vector<std::shared_ptr<Chunk>> chunksAroundPlayer;
92+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {0, 0}));
93+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {1, 0}));
94+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {0, 1}));
95+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {1, 1}));
96+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {-1, 0}));
97+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {0, -1}));
98+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {-1, -1}));
99+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {1, -1}));
100+
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {-1, 1}));
101+
for (const auto& chunk : chunksAroundPlayer) {
102+
if (chunk){
103+
chunk->cameraLook(camRay, coll);
104+
if (coll.coll.hit) break;
103105
}
104-
} else {
105-
coll.coll.hit = false;
106+
106107
}
107108

108109
if (coll.coll.hit) {
@@ -112,7 +113,7 @@ int main() {
112113

113114
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
114115
lvl->setTile(coll.blockPos + BlockPos {(int)coll.coll.normal.x, (int)coll.coll.normal.y, (int)coll.coll.normal.z},
115-
coll.blockPos.y == surfaceLevel - 1 ? BlockTypes::Grass : BlockTypes::Rock);
116+
coll.blockPos.y + (int)coll.coll.normal.y == surfaceLevel ? BlockTypes::Grass : BlockTypes::Rock);
116117
}
117118
}
118119

@@ -137,47 +138,23 @@ int main() {
137138
DrawRay({{0, 0, 0}, {0, 0, 1}}, BLUE); // z
138139

139140
if (coll.coll.hit) {
140-
// DrawSphere(coll.coll.point, .2f, RED);
141141
auto millis =
142142
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
143143
auto col = Color {255, 255, 255, static_cast<uint8_t>((sin(millis / 100.0) * .2f + .4f) * 255.f)};
144-
// DrawPlane(coll.blockPos + Vector3 {0.5f, 1.01f, 0.5f}, {1.f, 1.f},
145-
// {255, 255, 255, static_cast<uint8_t>((sin(millis / 100.0) * .2f + .4f) * 255.f)});
146144
drawFace(coll.blockPos, coll.face, col);
147-
// Vector3 pos, axis;
148-
// switch(coll.face) {
149-
// case Faces::Up:
150-
// pos = coll.blockPos + Vector3 {0, 1, 0};
151-
// axis = {0, 0, 0};
152-
// break;
153-
// case Faces::Down:
154-
// pos = coll.blockPos + Vector3 {0, 0, 0};
155-
// axis = {0, 0, 0};
156-
// break;
157-
// case Faces::Left:
158-
// pos = coll.blockPos + Vector3 {1, 0, 0};
159-
// axis = {1, 0, 0};
160-
// break;
161-
// case Faces::Right:
162-
// pos = coll.blockPos + Vector3 {0, 0, 0};
163-
// axis = {-1, 0, 0};
164-
// break;
165-
// case Faces::Front:
166-
// pos = coll.blockPos + Vector3 {0.5, 0.5, 0};
167-
// axis = {1, 0, 0};
168-
// break;
169-
// case Faces::Back:
170-
// pos = coll.blockPos + Vector3 {0, 0, 1};
171-
// axis = {0, 0, -1};
172-
// break;
173-
// }
174-
// DrawModelEx(planeModel, pos, axis, 90.f, {1.f, 1.f, 1.f}, col);
175145
}
176146

177147
cam.EndMode3D();
178148

179149
DrawFPS(0, 0);
180150
EndDrawing();
151+
152+
frames++;
153+
if (system_clock::now() - lastTime >= 1s) {
154+
lastTime = system_clock::now();
155+
logD("{} fps", frames);
156+
frames = 0;
157+
}
181158
}
182159

183160
UnloadModel(planeModel);

src/utils.hpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const int chunkSize = 16; // x and z
66
const int chunkHeight = 256; // y
7-
const int chunksCount = 8;
7+
const int chunksCount = 16;
88

99
const int surfaceLevel = 170; // surface level
1010

@@ -16,6 +16,19 @@ enum class BlockTypes : uint8_t {
1616
Grass,
1717
};
1818

19+
struct ChunkPos {
20+
int32_t x;
21+
int32_t z;
22+
23+
bool operator==(const ChunkPos& other) const {
24+
return x == other.x && z == other.z;
25+
}
26+
27+
ChunkPos operator+(const ChunkPos& other) const {
28+
return {x + other.x, z + other.z};
29+
}
30+
};
31+
1932
struct BlockPos {
2033
int32_t x;
2134
int32_t y;
@@ -49,14 +62,9 @@ struct BlockPos {
4962
bool isInChunk() const {
5063
return x >= 0 && x < chunkSize && y >= 0 && y < chunkHeight && z >= 0 && z < chunkSize;
5164
}
52-
};
5365

54-
struct ChunkPos {
55-
int32_t x;
56-
int32_t z;
57-
58-
bool operator==(const ChunkPos& other) const {
59-
return x == other.x && z == other.z;
66+
ChunkPos chunkPos() const {
67+
return ChunkPos {static_cast<int32_t>(floorf(x / (float)chunkSize)), static_cast<int32_t>(floorf(z / (float)chunkSize))};
6068
}
6169
};
6270

@@ -66,7 +74,6 @@ struct std::hash<ChunkPos> {
6674
using std::hash;
6775

6876
return hash<int32_t>()(p.x) ^ (hash<int32_t>()(p.z) << 1);
69-
// return ((hash<string>()(k.first) ^ (hash<string>()(k.second) << 1)) >> 1) ^ (hash<int>()(k.third) << 1);
7077
}
7178
};
7279

@@ -126,7 +133,7 @@ enum class Faces {
126133
};
127134

128135
inline void drawFace(const BlockPos& blockPos, Faces face, const Color& col) {
129-
const float awayFromBlock = .003f;
136+
const float awayFromBlock = .004f;
130137

131138
float x0 = blockPos.x;
132139
float y0 = blockPos.y;
@@ -192,4 +199,20 @@ inline void drawFace(const BlockPos& blockPos, Faces face, const Color& col) {
192199
#define TIME_MEASURE_BEGIN(name)
193200
#define TIME_MEASURE_END(name)
194201
#define TIME_MEASURE_DBG(name)
195-
#endif
202+
#endif
203+
204+
inline Vector2 MeasureText2(const char* text, int fontSize) {
205+
Vector2 textSize = {0.0f, 0.0f};
206+
207+
// Check if default font has been loaded
208+
if (GetFontDefault().texture.id != 0) {
209+
int defaultFontSize = 10; // Default Font chars height in pixel
210+
if (fontSize < defaultFontSize)
211+
fontSize = defaultFontSize;
212+
int spacing = fontSize / defaultFontSize;
213+
214+
textSize = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing);
215+
}
216+
217+
return textSize;
218+
}

0 commit comments

Comments
 (0)