Skip to content

Commit 5d93c2c

Browse files
author
Sirvoid
authored
cleanup & stuff
1 parent 08dc8ad commit 5d93c2c

10 files changed

+75
-72
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Raylib_VoxelEngine
22
Voxel engine in progress programmed in C using Raylib.
3-
![Image](https://i.imgur.com/gZzZvRt.png)
3+
![Image](https://i.imgur.com/IX5fpJg.png)

blockfacehelper.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int BFH_verticesI = 0;
1010
int BFH_texI = 0;
1111
int BFH_colorsI = 0;
1212

13-
float texCoords[12] = {
13+
float BFH_texCoords[12] = {
1414
1, 1, 0, 0, 1, 0,
1515
0, 0, 1, 1, 0, 1
1616

@@ -61,7 +61,7 @@ Vector3 BFH_GetDirection(BlockFace face) {
6161
return (Vector3){ 0, 0, 0 };
6262
}
6363

64-
void BFH_ResetIndexes() {
64+
void BFH_ResetIndexes(void) {
6565
BFH_verticesI = 0;
6666
BFH_texI = 0;
6767
BFH_colorsI = 0;
@@ -100,8 +100,8 @@ void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID) {
100100
}
101101

102102
for(int i = 0; i < 6; i++) {
103-
mesh->texcoords[BFH_texI++] = (texCoords[texI++] + textureX) / 16.0f;
104-
mesh->texcoords[BFH_texI++] = (texCoords[texI++] + textureY) / 16.0f;
103+
mesh->texcoords[BFH_texI++] = (BFH_texCoords[texI++] + textureX) / 16.0f;
104+
mesh->texcoords[BFH_texI++] = (BFH_texCoords[texI++] + textureY) / 16.0f;
105105
}
106106
}
107107

blockfacehelper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "chunkmesh.h"
88

99
//Reset memory counters.
10-
void BFH_ResetIndexes();
10+
void BFH_ResetIndexes(void);
1111

1212
//Add a block face to a given mesh.
1313
void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID);

chunk.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ void Chunk_Init(Chunk *chunk, Vector3 pos) {
1717
if(chunk->position.y > 0) return;
1818

1919
for(int i = 0; i < CHUNK_SIZE; i++) {
20-
Vector3 npos = Vector3Add(Chunk_IndexToPos(i), Vector3Multiply(chunk->position, (Vector3) {CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z}) );
20+
Vector3 npos = Vector3Add(Chunk_IndexToPos(i), Vector3Multiply(chunk->position, CHUNK_SIZE_VEC3));
2121
float noise = (stb_perlin_fbm_noise3( npos.x / WORLD_SIZE_X / 4.0f, 1.0f, npos.z / WORLD_SIZE_Z / 4.0f, 2.0f, 0.5f, 3) + 1.0f) / 2.0f * WORLD_SIZE_Y;
2222
float ny = npos.y - 8;
2323

2424
if(ny < noise) chunk->data[i] = 3;
2525
if(ny + 1 < noise) chunk->data[i] = 2;
2626
if(ny + 2 < noise) chunk->data[i] = 1;
27+
2728
}
2829

2930
}
@@ -65,12 +66,11 @@ void Chunk_BuildMesh(Chunk *chunk) {
6566
BFH_ResetIndexes();
6667
Chunk_facesCounter = 0;
6768

68-
Vector3 chunkWorldPos = (Vector3) { chunk->position.x * CHUNK_SIZE_X, chunk->position.y * CHUNK_SIZE_Y, chunk->position.z * CHUNK_SIZE_Z };
69+
Vector3 chunkWorldPos = Vector3Multiply(chunk->position, CHUNK_SIZE_VEC3);
6970

7071
for(int i = 0; i < CHUNK_SIZE; i++) {
7172
Vector3 pos = Chunk_IndexToPos(i);
7273
Vector3 worldPos = Vector3Add(pos, chunkWorldPos);
73-
7474
Chunk_AddCube(chunk, chunk->mesh, pos, worldPos, chunk->data[i]);
7575
}
7676

@@ -111,26 +111,25 @@ int Chunk_GetBlock(Chunk *chunk, Vector3 pos) {
111111
return 0;
112112
}
113113

114-
void Chunk_GetBorderingChunks(Chunk *chunk, Vector3 pos, Chunk *(*dest)[3]) {
115-
int i = 0;
116-
114+
void Chunk_RefreshBorderingChunks(Chunk *chunk, Vector3 pos) {
117115
if(pos.x == 0) {
118-
*dest[i++] = World_GetChunkAt(Vector3Add(chunk->position, (Vector3){-1, 0, 0}));
116+
Chunk_BuildMesh(World_GetChunkAt(Vector3Add(chunk->position, (Vector3){-1, 0, 0})));
119117
} else if(pos.x == CHUNK_SIZE_X - 1) {
120-
*dest[i++] = World_GetChunkAt(Vector3Add(chunk->position, (Vector3){1, 0, 0}));
118+
Chunk_BuildMesh(World_GetChunkAt(Vector3Add(chunk->position, (Vector3){1, 0, 0})));
121119
}
122120

123121
if(pos.y == 0) {
124-
*dest[i++] = World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, -1, 0}));
122+
Chunk_BuildMesh(World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, -1, 0})));
125123
} else if(pos.y == CHUNK_SIZE_Y - 1) {
126-
*dest[i++] = World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, 1, 0}));
124+
Chunk_BuildMesh(World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, 1, 0})));
127125
}
128126

129127
if(pos.z == 0) {
130-
*dest[i++] = World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, 0, -1}));
128+
Chunk_BuildMesh(World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, 0, -1})));
131129
} else if(pos.z == CHUNK_SIZE_Z - 1) {
132-
*dest[i++] = World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, 0, 1}));
130+
Chunk_BuildMesh(World_GetChunkAt(Vector3Add(chunk->position, (Vector3){0, 0, 1})));
133131
}
132+
134133
}
135134

136135
int Chunk_IsValidPos(Vector3 pos) {

chunk.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
#define CHUNK_SIZE_X 16
99
#define CHUNK_SIZE_Y 16
1010
#define CHUNK_SIZE_Z 16
11+
#define CHUNK_SIZE_VEC3 CLITERAL(Vector3){ CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z }
1112
#define CHUNK_SIZE (CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z)
1213

13-
1414
typedef struct Chunk{
1515
ChunkMesh *mesh;
1616
int loaded;
1717
Vector3 position;
18-
int data[CHUNK_SIZE]; //16x16x16
18+
int data[CHUNK_SIZE];
1919
} Chunk;
2020

21-
2221
//Initialize a chunk.
2322
void Chunk_Init(Chunk *chunk, Vector3 pos);
2423
//Allocate memory for a mesh.
@@ -33,7 +32,7 @@ void Chunk_BuildMesh(Chunk *chunk);
3332
void Chunk_AddCube(Chunk *chunk, ChunkMesh *mesh, Vector3 pos, Vector3 worldPos, int blockID);
3433
void Chunk_AddFace(Chunk *chunk, ChunkMesh *mesh, Vector3 pos, Vector3 worldPos, BlockFace face, int blockID);
3534

36-
void Chunk_GetBorderingChunks(Chunk *chunk, Vector3 pos, Chunk *(*dest)[3]);
35+
void Chunk_RefreshBorderingChunks(Chunk *chunk, Vector3 pos);
3736

3837
//Set a block in a chunk and refresh mesh.
3938
void Chunk_SetBlock(Chunk *chunk, Vector3 pos, int blockID);

main.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int main(void) {
99
// Initialization
1010
const int screenWidth = 800;
1111
const int screenHeight = 450;
12-
Color crosshairColor = (Color){ 0, 0, 0, 80 };
12+
Color uiColBg = (Color){ 0, 0, 0, 80 };
1313

1414
InitWindow(screenWidth, screenHeight, "Game");
1515
SetTraceLogLevel(LOG_WARNING);
@@ -54,13 +54,13 @@ int main(void) {
5454

5555
const char* coordText = TextFormat("X: %i Y: %i Z: %i", (int)player.position.x, (int)player.position.y, (int)player.position.z);
5656

57-
DrawRectangle(13, 15, MeasureText(coordText, 20) + 6, 39, crosshairColor);
57+
DrawRectangle(13, 15, MeasureText(coordText, 20) + 6, 39, uiColBg);
5858
DrawText(TextFormat("%2i FPS", GetFPS()), 16, 16, 20, WHITE);
5959
DrawText(coordText, 16, 36, 20, WHITE);
6060

61-
DrawRectangle(screenWidth / 2 - 8, screenHeight / 2 - 2, 16, 4, crosshairColor);
62-
DrawRectangle(screenWidth / 2 - 2, screenHeight / 2 + 2, 4, 6, crosshairColor);
63-
DrawRectangle(screenWidth / 2 - 2, screenHeight / 2 - 8, 4, 6, crosshairColor);
61+
DrawRectangle(screenWidth / 2 - 8, screenHeight / 2 - 2, 16, 4, uiColBg);
62+
DrawRectangle(screenWidth / 2 - 2, screenHeight / 2 + 2, 4, 6, uiColBg);
63+
DrawRectangle(screenWidth / 2 - 2, screenHeight / 2 - 8, 4, 6, uiColBg);
6464

6565
EndDrawing();
6666
}

player.c

+32-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "world.h"
66
#include "raycast.h"
77

8+
#define MOUSE_SENSITIVITY 0.003f
9+
810
Vector2 Player_oldMousePos = {0.0f, 0.0f};
911
Vector2 Player_cameraAngle = {0.0f, 0.0f};
1012

@@ -19,7 +21,7 @@ void Player_Init(Player *player) {
1921

2022
player->velocity = (Vector3) {0, 0, 0};
2123
player->position = (Vector3) { 16.0f, 16.0f, 16.0f };
22-
player->speed = 0.15f;
24+
player->speed = 0.125f;
2325

2426
player->collisionBox.min = (Vector3) { 0, 0, 0 };
2527
player->collisionBox.max = (Vector3) { 0.8f, 1.8f, 0.8f };
@@ -29,28 +31,33 @@ void Player_Init(Player *player) {
2931
}
3032

3133
void Player_CheckInputs(Player *player) {
32-
Vector2 mousePositionDelta = {0.0f, 0.0f};
34+
Vector2 mousePositionDelta = { 0.0f, 0.0f };
3335
Vector2 mousePos = GetMousePosition();
3436

3537
mousePositionDelta.x = mousePos.x - Player_oldMousePos.x;
3638
mousePositionDelta.y = mousePos.y - Player_oldMousePos.y;
3739

3840
Player_oldMousePos = GetMousePosition();
3941

40-
Player_cameraAngle.x -= (mousePositionDelta.x * -0.003f);
41-
Player_cameraAngle.y -= (mousePositionDelta.y * -0.003f);
42+
Player_cameraAngle.x -= (mousePositionDelta.x * -MOUSE_SENSITIVITY);
43+
Player_cameraAngle.y -= (mousePositionDelta.y * -MOUSE_SENSITIVITY);
4244

45+
//Limit head rotation
4346
float maxCamAngleY = PI - 0.01f;
4447
float minCamAngleY = 0.01f;
4548

46-
if(Player_cameraAngle.y >= maxCamAngleY) Player_cameraAngle.y = maxCamAngleY;
47-
if(Player_cameraAngle.y <= minCamAngleY) Player_cameraAngle.y = minCamAngleY;
49+
if(Player_cameraAngle.y >= maxCamAngleY)
50+
Player_cameraAngle.y = maxCamAngleY;
51+
else if(Player_cameraAngle.y <= minCamAngleY)
52+
Player_cameraAngle.y = minCamAngleY;
53+
4854

55+
//Calculate direction vectors of the camera angle
4956
float cx = cosf(Player_cameraAngle.x);
5057
float sx = sinf(Player_cameraAngle.x);
5158

52-
float cxS = cosf(Player_cameraAngle.x + PI / 2);
53-
float sxS = sinf(Player_cameraAngle.x + PI / 2);
59+
float cx90 = cosf(Player_cameraAngle.x + PI / 2);
60+
float sx90 = sinf(Player_cameraAngle.x + PI / 2);
5461

5562
float sy = sinf(Player_cameraAngle.y);
5663
float cy = cosf(Player_cameraAngle.y);
@@ -59,8 +66,9 @@ void Player_CheckInputs(Player *player) {
5966
float forwardY = cy;
6067
float forwardZ = sx * sy;
6168

69+
//Handle keys & mouse
6270
if(IsKeyDown(KEY_SPACE) && !player->jumped) {
63-
player->velocity.y += 0.3f;
71+
player->velocity.y += 0.2f;
6472
player->jumped = true;
6573
}
6674

@@ -75,13 +83,13 @@ void Player_CheckInputs(Player *player) {
7583
}
7684

7785
if(IsKeyDown(KEY_A)) {
78-
player->velocity.z -= sxS * player->speed;
79-
player->velocity.x -= cxS * player->speed;
86+
player->velocity.z -= sx90 * player->speed;
87+
player->velocity.x -= cx90 * player->speed;
8088
}
8189

8290
if(IsKeyDown(KEY_D)) {
83-
player->velocity.z += sxS * player->speed;
84-
player->velocity.x += cxS * player->speed;
91+
player->velocity.z += sx90 * player->speed;
92+
player->velocity.x += cx90 * player->speed;
8593
}
8694

8795
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
@@ -94,6 +102,7 @@ void Player_CheckInputs(Player *player) {
94102
}
95103
}
96104

105+
//Place camera's target to the direction looking at.
97106
player->camera.target.x = player->camera.position.x + forwardX;
98107
player->camera.target.y = player->camera.position.y + forwardY;
99108
player->camera.target.z = player->camera.position.z + forwardZ;
@@ -103,20 +112,22 @@ void Player_CheckInputs(Player *player) {
103112

104113
void Player_Update(Player *player) {
105114

106-
player->velocity.y -= 0.02f;
115+
player->velocity.y -= 0.015f;
116+
117+
Vector3 velXdt = Vector3Scale(player->velocity, GetFrameTime() * 60);
107118

108-
player->position.x += player->velocity.x;
109-
if(Player_TestCollision(player)) player->position.x -= player->velocity.x;
119+
player->position.x += velXdt.x;
120+
if(Player_TestCollision(player)) player->position.x -= velXdt.x;
110121

111-
player->position.y += player->velocity.y;
122+
player->position.y += velXdt.y;
112123
if(Player_TestCollision(player)) {
113-
player->position.y -= player->velocity.y;
124+
player->position.y -= velXdt.y;
125+
if(player->velocity.y <= 0) player->jumped = false;
114126
player->velocity.y = 0;
115-
player->jumped = false;
116127
}
117128

118-
player->position.z += player->velocity.z;
119-
if(Player_TestCollision(player)) player->position.z -= player->velocity.z;
129+
player->position.z += velXdt.z;
130+
if(Player_TestCollision(player)) player->position.z -= velXdt.z;
120131

121132
player->camera.position = player->position;
122133
player->camera.position.y += 1.8f;

raycast.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define RAYCAST_PRECISION 0.05f
77

88
RaycastResult Raycast_Do(Vector3 position, Vector3 direction) {
9-
109
float i = 0;
1110
Vector3 oldPos = position;
1211

@@ -21,11 +20,9 @@ RaycastResult Raycast_Do(Vector3 position, Vector3 direction) {
2120
int blockID = World_GetBlock(position);
2221

2322
if(blockID != 0) {
24-
RaycastResult result = (RaycastResult) {position, oldPos, blockID};
25-
return result;
23+
return (RaycastResult) {position, oldPos, blockID};
2624
}
2725
}
2826

29-
RaycastResult result = (RaycastResult) {position, oldPos, -1};
30-
return result;
27+
return (RaycastResult) {position, oldPos, -1};
3128
}

world.c

+14-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ World world;
99

1010
#define WORLD_RENDER_DISTANCE 256
1111

12-
void World_Init() {
12+
void World_Init(void) {
1313
world.mat = LoadMaterialDefault();
1414

1515
//Create Chunks
@@ -26,7 +26,7 @@ void World_Init() {
2626
}
2727
}
2828

29-
void World_Unload() {
29+
void World_Unload(void) {
3030
for(int i = 0; i < WORLD_SIZE; i++) {
3131
Chunk_Unload(&world.chunks[i]);
3232
}
@@ -44,13 +44,14 @@ void World_Draw(Vector3 camPosition) {
4444
for(int i = 0; i < WORLD_SIZE; i++) {
4545
Chunk* chunk = &world.chunks[i];
4646

47-
Vector3 cpxy = (Vector3) {chunk->position.x * CHUNK_SIZE_X, camPosition.y, chunk->position.z * CHUNK_SIZE_Z};
48-
if(Vector3Distance(cpxy, camPosition) > WORLD_RENDER_DISTANCE) continue;
47+
Vector3 chunkPosInBlocks = Vector3Multiply(chunk->position, CHUNK_SIZE_VEC3);
48+
if(Vector3Distance(chunkPosInBlocks, camPosition) > WORLD_RENDER_DISTANCE) continue;
49+
50+
Matrix matrix = (Matrix) { 1, 0, 0, chunkPosInBlocks.x,
51+
0, 1, 0, chunkPosInBlocks.y,
52+
0, 0, 1, chunkPosInBlocks.z,
53+
0, 0, 0, 1 };
4954

50-
Matrix matrix = (Matrix) { 1, 0, 0, chunk->position.x * CHUNK_SIZE_X,
51-
0, 1, 0, chunk->position.y * CHUNK_SIZE_Y,
52-
0, 0, 1, chunk->position.z * CHUNK_SIZE_Z,
53-
0, 0, 0, 1 };
5455
ChunkMesh_Draw(*chunk->mesh, world.mat, matrix);
5556
}
5657
}
@@ -83,19 +84,15 @@ void World_SetBlock(Vector3 blockPos, int blockID) {
8384

8485
//Set Block
8586
Vector3 blockPosInChunk = (Vector3) {
86-
(int)(blockPos.x - chunkPos.x * CHUNK_SIZE_X),
87-
(int)(blockPos.y - chunkPos.y * CHUNK_SIZE_Y),
88-
(int)(blockPos.z - chunkPos.z * CHUNK_SIZE_Z)
87+
(int)blockPos.x - chunkPos.x * CHUNK_SIZE_X,
88+
(int)blockPos.y - chunkPos.y * CHUNK_SIZE_Y,
89+
(int)blockPos.z - chunkPos.z * CHUNK_SIZE_Z
8990
};
91+
9092
Chunk_SetBlock(chunk, blockPosInChunk, blockID);
9193

9294
//Refresh mesh of neighbour chunks.
93-
Chunk *neighbourChunks[3] = {NULL};
94-
Chunk_GetBorderingChunks(chunk, blockPosInChunk, &neighbourChunks);
95-
for(int i = 0; i < 3; i++) {
96-
if(neighbourChunks[i] == NULL) break;
97-
Chunk_BuildMesh(neighbourChunks[i]);
98-
}
95+
Chunk_RefreshBorderingChunks(chunk, blockPosInChunk);
9996

10097
//Refresh current chunk.
10198
Chunk_BuildMesh(chunk);

world.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ typedef struct World{
1919
} World;
2020

2121
//Initialize the world.
22-
void World_Init();
22+
void World_Init(void);
2323
//Unload the world.
24-
void World_Unload();
24+
void World_Unload(void);
2525
//Draw the world.
2626
void World_Draw(Vector3 camPosition);
2727
//Apply terrain texture to the world.

0 commit comments

Comments
 (0)