Skip to content

Commit 798ec8c

Browse files
authored
Multithreading, Lightning, Sprites, UI, ...
Added: -Chunks built on another thread -Lightning -Sprite blocks -Menu and in game UI -Started working on multiplayer using ENet (Server code not available as of now) -Save singleplayer world -other tweaks
1 parent 746ea79 commit 798ec8c

27 files changed

+1693
-340
lines changed

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
# Raylib_VoxelEngine
2-
Voxel engine in progress programmed in C using Raylib.
3-
![Image](https://i.imgur.com/XyDTSOS.png)
1+
# Raylib Voxel Engine
2+
![Image](https://i.imgur.com/mgC5tN3.png)
3+
[![Chat](https://img.shields.io/badge/chat-on%20discord-7289da.svg)](https://discord.gg/tZthSbpUcV)
4+
5+
Voxel Engine made in C using Raylib.
6+
Requires [Raylib](https://github.com/raysan5/raylib) and [ENet](https://github.com/zpl-c/enet)
7+
8+
*Multiplayer is not fully implemented yet so the server code will come later.

block.c

+58
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,68 @@
33

44
Block Block_definition[256];
55

6+
void Block_BuildDefinition(void) {
7+
8+
for(int i = 0; i < 256; i++) {
9+
Block_Define(i, "invalid", 0, 0, 0);
10+
Block_definition[i].colliderType = BlockColliderType_None;
11+
}
12+
13+
Block_Define(0, "air", 0, 0, 0);
14+
Block_definition[0].modelType = BlockModelType_Gas;
15+
Block_definition[0].renderType = BlockRenderType_Transparent;
16+
Block_definition[0].colliderType = BlockColliderType_None;
17+
18+
Block_Define(1, "stone", 1, 1, 1);
19+
Block_Define(2, "dirt", 2, 2, 2);
20+
Block_Define(3, "grass", 0, 2, 3);
21+
Block_Define(4, "wood", 4, 4, 4);
22+
23+
Block_Define(5, "water", 14, 14, 14);
24+
Block_definition[5].renderType = BlockRenderType_Translucent;
25+
Block_definition[5].colliderType = BlockColliderType_Liquid;
26+
27+
Block_Define(6, "sand", 11, 11, 11);
28+
Block_Define(7, "iron_ore", 6, 6, 6);
29+
Block_Define(8, "coal_ore", 7, 7, 7);
30+
Block_Define(9, "gold_ore", 5, 5, 5);
31+
Block_Define(10, "log", 9, 9, 8);
32+
Block_Define(11, "leaves", 10, 10, 10);
33+
Block_definition[11].renderType = BlockRenderType_Transparent;
34+
35+
Block_Define(12, "rose", 12, 12, 12);
36+
Block_definition[12].modelType = BlockModelType_Sprite;
37+
Block_definition[12].renderType = BlockRenderType_Transparent;
38+
Block_definition[12].colliderType = BlockColliderType_None;
39+
40+
Block_Define(13, "dandelion", 13, 13, 13);
41+
Block_definition[13].modelType = BlockModelType_Sprite;
42+
Block_definition[13].renderType = BlockRenderType_Transparent;
43+
Block_definition[13].colliderType = BlockColliderType_None;
44+
45+
Block_Define(14, "glass", 17, 17, 17);
46+
Block_definition[14].renderType = BlockRenderType_Transparent;
47+
48+
Block_Define(15, "fire", 16, 16, 16);
49+
Block_definition[15].renderType = BlockRenderType_Transparent;
50+
Block_definition[15].modelType = BlockModelType_Sprite;
51+
Block_definition[15].colliderType = BlockColliderType_None;
52+
Block_definition[15].lightType = BlockLightType_Emit;
53+
54+
Block_Define(16, "lava", 15, 15, 15);
55+
Block_definition[16].colliderType = BlockColliderType_None;
56+
Block_definition[16].lightType = BlockLightType_Emit;
57+
}
58+
659
Block* Block_Define(int ID, char name[], int topTex, int bottomTex, int sideTex) {
760
Block *block = &Block_definition[ID];
861
TextCopy(block->name, name);
962

63+
block->modelType = BlockModelType_Solid;
64+
block->renderType = BlockRenderType_Opaque;
65+
block->colliderType = BlockColliderType_Solid;
66+
block->lightType = BlockLightType_None;
67+
1068
Block_SetTexture(block, BlockFace_Top, topTex);
1169
Block_SetTexture(block, BlockFace_Bottom, bottomTex);
1270
Block_SetTexture(block, BlockFace_Left, sideTex);

block.h

+30
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,44 @@ typedef enum BlockFace{
1010
BlockFace_Back
1111
} BlockFace;
1212

13+
typedef enum BlockModelType{
14+
BlockModelType_Gas,
15+
BlockModelType_Solid,
16+
BlockModelType_Sprite
17+
} BlockType;
18+
19+
typedef enum BlockLightType {
20+
BlockLightType_None,
21+
BlockLightType_Emit
22+
} BlockLightType;
23+
24+
typedef enum BlockRenderType{
25+
BlockRenderType_Opaque,
26+
BlockRenderType_Transparent,
27+
BlockRenderType_Translucent
28+
} BlockRenderType;
29+
30+
typedef enum BlockColliderType{
31+
BlockColliderType_None,
32+
BlockColliderType_Solid,
33+
BlockColliderType_Liquid
34+
} BlockColliderType;
35+
1336
typedef struct Block {
1437
char name[16];
1538
int textures[6];
39+
int modelType;
40+
int renderType;
41+
int colliderType;
42+
int lightType;
1643
} Block;
1744

1845
//Definiton of every blocks.
1946
extern Block Block_definition[256];
2047

48+
//Define All Blocks
49+
void Block_BuildDefinition(void);
50+
2151
//Define a block.
2252
Block* Block_Define(int ID, char name[], int topTex, int bottomTex, int sideTex);
2353

blockfacehelper.c

+99-54
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,81 @@
11
#include <stdlib.h>
2+
#include <string.h>
23
#include "raylib.h"
34
#include "rlgl.h"
45
#include "block.h"
56
#include "math.h"
67
#include "chunkmesh.h"
78
#include "blockfacehelper.h"
89

9-
int BFH_verticesI = 0;
10-
int BFH_texI = 0;
11-
int BFH_colorsI = 0;
10+
int BFH_verticesI[] = {0, 0};
11+
int BFH_texI[] = {0, 0};
12+
int BFH_colorsI[] = {0, 0};
1213

1314
float BFH_texCoords[12] = {
1415
1, 1, 0, 0, 1, 0,
1516
0, 0, 1, 1, 0, 1
1617

1718
};
1819

19-
Vector3 BFH_facesPosition[36] = {
20-
//left
21-
(Vector3) {0, 0, 0}, (Vector3) {0, 1, 1},
22-
(Vector3) {0, 1, 0}, (Vector3) {0, 1, 1},
23-
(Vector3) {0, 0, 0}, (Vector3) {0, 0, 1},
24-
//right
25-
(Vector3) {1, 0, 1}, (Vector3) {1, 1, 0},
26-
(Vector3) {1, 1, 1}, (Vector3) {1, 1, 0},
27-
(Vector3) {1, 0, 1}, (Vector3) {1, 0, 0},
28-
//top
29-
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 0},
30-
(Vector3) {0, 1, 0}, (Vector3) {1, 1, 0},
31-
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
32-
//bottom
33-
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 1},
34-
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
35-
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 0},
36-
//front
37-
(Vector3) {0, 0, 1}, (Vector3) {1, 1, 1},
38-
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
39-
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
40-
//back
41-
(Vector3) {1, 0, 0}, (Vector3) {0, 1, 0},
42-
(Vector3) {1, 1, 0}, (Vector3) {0, 1, 0},
43-
(Vector3) {1, 0, 0}, (Vector3) {0, 0, 0}
44-
};
20+
void BFH_GetFacesPosition(Block b, Vector3 *facesPosition) {
21+
22+
if(b.modelType == BlockModelType_Sprite) {
23+
24+
Vector3 nFacesPosition[24] = {
25+
//left
26+
(Vector3) {0, 0, 0}, (Vector3) {1, 1, 1},
27+
(Vector3) {0, 1, 0}, (Vector3) {1, 1, 1},
28+
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 1},
29+
//right
30+
(Vector3) {1, 0, 1}, (Vector3) {0, 1, 0},
31+
(Vector3) {1, 1, 1}, (Vector3) {0, 1, 0},
32+
(Vector3) {1, 0, 1}, (Vector3) {0, 0, 0},
33+
//front
34+
(Vector3) {0, 0, 1}, (Vector3) {1, 1, 0},
35+
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 0},
36+
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 0},
37+
//back
38+
(Vector3) {1, 0, 0}, (Vector3) {0, 1, 1},
39+
(Vector3) {1, 1, 0}, (Vector3) {0, 1, 1},
40+
(Vector3) {1, 0, 0}, (Vector3) {0, 0, 1}
41+
};
42+
43+
memcpy(facesPosition, nFacesPosition, 24 * sizeof(Vector3));
44+
45+
} else {
46+
47+
Vector3 nFacesPosition[36] = {
48+
//left
49+
(Vector3) {0, 0, 0}, (Vector3) {0, 1, 1},
50+
(Vector3) {0, 1, 0}, (Vector3) {0, 1, 1},
51+
(Vector3) {0, 0, 0}, (Vector3) {0, 0, 1},
52+
//right
53+
(Vector3) {1, 0, 1}, (Vector3) {1, 1, 0},
54+
(Vector3) {1, 1, 1}, (Vector3) {1, 1, 0},
55+
(Vector3) {1, 0, 1}, (Vector3) {1, 0, 0},
56+
//top
57+
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 0},
58+
(Vector3) {0, 1, 0}, (Vector3) {1, 1, 0},
59+
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
60+
//bottom
61+
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 1},
62+
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
63+
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 0},
64+
//front
65+
(Vector3) {0, 0, 1}, (Vector3) {1, 1, 1},
66+
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
67+
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
68+
//back
69+
(Vector3) {1, 0, 0}, (Vector3) {0, 1, 0},
70+
(Vector3) {1, 1, 0}, (Vector3) {0, 1, 0},
71+
(Vector3) {1, 0, 0}, (Vector3) {0, 0, 0}
72+
};
73+
74+
memcpy(facesPosition, nFacesPosition, 36 * sizeof(Vector3));
75+
76+
}
77+
78+
}
4579

4680
Vector3 BFH_GetDirection(BlockFace face) {
4781
switch (face) {
@@ -62,14 +96,20 @@ Vector3 BFH_GetDirection(BlockFace face) {
6296
}
6397

6498
void BFH_ResetIndexes(void) {
65-
BFH_verticesI = 0;
66-
BFH_texI = 0;
67-
BFH_colorsI = 0;
99+
BFH_verticesI[0] = 0;
100+
BFH_texI[0] = 0;
101+
BFH_colorsI[0] = 0;
102+
103+
BFH_verticesI[1] = 0;
104+
BFH_texI[1] = 0;
105+
BFH_colorsI[1] = 0;
68106
}
69107

70-
void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID) {
108+
void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block blockDef, int translucent, int light) {
71109

72-
int texID = Block_definition[blockID].textures[(int)face];
110+
Vector3 facesPosition[36] = {0};
111+
BFH_GetFacesPosition(blockDef, facesPosition);
112+
int texID = blockDef.textures[(int)face];
73113

74114
int texI = 0;
75115
int textureX = texID % 16;
@@ -79,29 +119,34 @@ void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID) {
79119

80120
for(int i = 0; i < 6; i++) {
81121
int faceIndex = i + faceX6;
82-
mesh->vertices[BFH_verticesI++] = (unsigned char)((BFH_facesPosition[faceIndex].x + pos.x) * 15.9f);
83-
mesh->vertices[BFH_verticesI++] = (unsigned char)((BFH_facesPosition[faceIndex].y + pos.y) * 15.9f);
84-
mesh->vertices[BFH_verticesI++] = (unsigned char)((BFH_facesPosition[faceIndex].z + pos.z) * 15.9f);
85122

86-
switch(face) {
87-
case BlockFace_Bottom:
88-
mesh->colors[BFH_colorsI++] = 100;
89-
break;
90-
case BlockFace_Left:
91-
case BlockFace_Right:
92-
mesh->colors[BFH_colorsI++] = 150;
93-
break;
94-
case BlockFace_Front:
95-
case BlockFace_Back:
96-
mesh->colors[BFH_colorsI++] = 200;
97-
break;
98-
default:
99-
mesh->colors[BFH_colorsI++] = 255;
100-
break;
123+
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].x + pos.x) * 15.99f);
124+
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].y + pos.y) * 15.99f);
125+
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].z + pos.z) * 15.99f);
126+
127+
if(blockDef.modelType != BlockModelType_Sprite) {
128+
switch(face) {
129+
case BlockFace_Bottom:
130+
mesh->colors[BFH_colorsI[translucent]++] = fmin(100, fmax(16, 100 - light));
131+
break;
132+
case BlockFace_Left:
133+
case BlockFace_Right:
134+
mesh->colors[BFH_colorsI[translucent]++] = fmin(150, fmax(16, 150 - light));
135+
break;
136+
case BlockFace_Front:
137+
case BlockFace_Back:
138+
mesh->colors[BFH_colorsI[translucent]++] = fmin(200, fmax(16, 200 - light));
139+
break;
140+
default:
141+
mesh->colors[BFH_colorsI[translucent]++] = fmin(255, fmax(16, 255 - light));
142+
break;
143+
}
144+
} else {
145+
mesh->colors[BFH_colorsI[translucent]++] = fmax(16, 255 - light);
101146
}
102147

103-
mesh->texcoords[BFH_texI++] = (unsigned char)((BFH_texCoords[texI++] + textureX));
104-
mesh->texcoords[BFH_texI++] = (unsigned char)((BFH_texCoords[texI++] + textureY));
148+
mesh->texcoords[BFH_texI[translucent]++] = (unsigned char)((BFH_texCoords[texI++] + textureX));
149+
mesh->texcoords[BFH_texI[translucent]++] = (unsigned char)((BFH_texCoords[texI++] + textureY));
105150
}
106151

107152
}

blockfacehelper.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
void BFH_ResetIndexes(void);
1111

1212
//Add a block face to a given mesh.
13-
void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID);
13+
void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block blockDef, int translucent, int light);
1414

1515
//Get facing direction of a block face.
1616
Vector3 BFH_GetDirection(BlockFace face);
1717

18+
void BFH_GetFacesPosition(Block b, Vector3 *facesPosition);
19+
1820
#endif

0 commit comments

Comments
 (0)