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
1310int 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);
0 commit comments