Skip to content

Commit

Permalink
Multithreading, Lightning, Sprites, UI, ...
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sirvoid committed Nov 13, 2021
1 parent 746ea79 commit 798ec8c
Show file tree
Hide file tree
Showing 27 changed files with 1,693 additions and 340 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Raylib_VoxelEngine
Voxel engine in progress programmed in C using Raylib.
![Image](https://i.imgur.com/XyDTSOS.png)
# Raylib Voxel Engine
![Image](https://i.imgur.com/mgC5tN3.png)
[![Chat](https://img.shields.io/badge/chat-on%20discord-7289da.svg)](https://discord.gg/tZthSbpUcV)

Voxel Engine made in C using Raylib.
Requires [Raylib](https://github.com/raysan5/raylib) and [ENet](https://github.com/zpl-c/enet)

*Multiplayer is not fully implemented yet so the server code will come later.
58 changes: 58 additions & 0 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,68 @@

Block Block_definition[256];

void Block_BuildDefinition(void) {

for(int i = 0; i < 256; i++) {
Block_Define(i, "invalid", 0, 0, 0);
Block_definition[i].colliderType = BlockColliderType_None;
}

Block_Define(0, "air", 0, 0, 0);
Block_definition[0].modelType = BlockModelType_Gas;
Block_definition[0].renderType = BlockRenderType_Transparent;
Block_definition[0].colliderType = BlockColliderType_None;

Block_Define(1, "stone", 1, 1, 1);
Block_Define(2, "dirt", 2, 2, 2);
Block_Define(3, "grass", 0, 2, 3);
Block_Define(4, "wood", 4, 4, 4);

Block_Define(5, "water", 14, 14, 14);
Block_definition[5].renderType = BlockRenderType_Translucent;
Block_definition[5].colliderType = BlockColliderType_Liquid;

Block_Define(6, "sand", 11, 11, 11);
Block_Define(7, "iron_ore", 6, 6, 6);
Block_Define(8, "coal_ore", 7, 7, 7);
Block_Define(9, "gold_ore", 5, 5, 5);
Block_Define(10, "log", 9, 9, 8);
Block_Define(11, "leaves", 10, 10, 10);
Block_definition[11].renderType = BlockRenderType_Transparent;

Block_Define(12, "rose", 12, 12, 12);
Block_definition[12].modelType = BlockModelType_Sprite;
Block_definition[12].renderType = BlockRenderType_Transparent;
Block_definition[12].colliderType = BlockColliderType_None;

Block_Define(13, "dandelion", 13, 13, 13);
Block_definition[13].modelType = BlockModelType_Sprite;
Block_definition[13].renderType = BlockRenderType_Transparent;
Block_definition[13].colliderType = BlockColliderType_None;

Block_Define(14, "glass", 17, 17, 17);
Block_definition[14].renderType = BlockRenderType_Transparent;

Block_Define(15, "fire", 16, 16, 16);
Block_definition[15].renderType = BlockRenderType_Transparent;
Block_definition[15].modelType = BlockModelType_Sprite;
Block_definition[15].colliderType = BlockColliderType_None;
Block_definition[15].lightType = BlockLightType_Emit;

Block_Define(16, "lava", 15, 15, 15);
Block_definition[16].colliderType = BlockColliderType_None;
Block_definition[16].lightType = BlockLightType_Emit;
}

Block* Block_Define(int ID, char name[], int topTex, int bottomTex, int sideTex) {
Block *block = &Block_definition[ID];
TextCopy(block->name, name);

block->modelType = BlockModelType_Solid;
block->renderType = BlockRenderType_Opaque;
block->colliderType = BlockColliderType_Solid;
block->lightType = BlockLightType_None;

Block_SetTexture(block, BlockFace_Top, topTex);
Block_SetTexture(block, BlockFace_Bottom, bottomTex);
Block_SetTexture(block, BlockFace_Left, sideTex);
Expand Down
30 changes: 30 additions & 0 deletions block.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,44 @@ typedef enum BlockFace{
BlockFace_Back
} BlockFace;

typedef enum BlockModelType{
BlockModelType_Gas,
BlockModelType_Solid,
BlockModelType_Sprite
} BlockType;

typedef enum BlockLightType {
BlockLightType_None,
BlockLightType_Emit
} BlockLightType;

typedef enum BlockRenderType{
BlockRenderType_Opaque,
BlockRenderType_Transparent,
BlockRenderType_Translucent
} BlockRenderType;

typedef enum BlockColliderType{
BlockColliderType_None,
BlockColliderType_Solid,
BlockColliderType_Liquid
} BlockColliderType;

typedef struct Block {
char name[16];
int textures[6];
int modelType;
int renderType;
int colliderType;
int lightType;
} Block;

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

//Define All Blocks
void Block_BuildDefinition(void);

//Define a block.
Block* Block_Define(int ID, char name[], int topTex, int bottomTex, int sideTex);

Expand Down
153 changes: 99 additions & 54 deletions blockfacehelper.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,81 @@
#include <stdlib.h>
#include <string.h>
#include "raylib.h"
#include "rlgl.h"
#include "block.h"
#include "math.h"
#include "chunkmesh.h"
#include "blockfacehelper.h"

int BFH_verticesI = 0;
int BFH_texI = 0;
int BFH_colorsI = 0;
int BFH_verticesI[] = {0, 0};
int BFH_texI[] = {0, 0};
int BFH_colorsI[] = {0, 0};

float BFH_texCoords[12] = {
1, 1, 0, 0, 1, 0,
0, 0, 1, 1, 0, 1

};

Vector3 BFH_facesPosition[36] = {
//left
(Vector3) {0, 0, 0}, (Vector3) {0, 1, 1},
(Vector3) {0, 1, 0}, (Vector3) {0, 1, 1},
(Vector3) {0, 0, 0}, (Vector3) {0, 0, 1},
//right
(Vector3) {1, 0, 1}, (Vector3) {1, 1, 0},
(Vector3) {1, 1, 1}, (Vector3) {1, 1, 0},
(Vector3) {1, 0, 1}, (Vector3) {1, 0, 0},
//top
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 0},
(Vector3) {0, 1, 0}, (Vector3) {1, 1, 0},
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
//bottom
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 1},
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 0},
//front
(Vector3) {0, 0, 1}, (Vector3) {1, 1, 1},
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
//back
(Vector3) {1, 0, 0}, (Vector3) {0, 1, 0},
(Vector3) {1, 1, 0}, (Vector3) {0, 1, 0},
(Vector3) {1, 0, 0}, (Vector3) {0, 0, 0}
};
void BFH_GetFacesPosition(Block b, Vector3 *facesPosition) {

if(b.modelType == BlockModelType_Sprite) {

Vector3 nFacesPosition[24] = {
//left
(Vector3) {0, 0, 0}, (Vector3) {1, 1, 1},
(Vector3) {0, 1, 0}, (Vector3) {1, 1, 1},
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 1},
//right
(Vector3) {1, 0, 1}, (Vector3) {0, 1, 0},
(Vector3) {1, 1, 1}, (Vector3) {0, 1, 0},
(Vector3) {1, 0, 1}, (Vector3) {0, 0, 0},
//front
(Vector3) {0, 0, 1}, (Vector3) {1, 1, 0},
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 0},
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 0},
//back
(Vector3) {1, 0, 0}, (Vector3) {0, 1, 1},
(Vector3) {1, 1, 0}, (Vector3) {0, 1, 1},
(Vector3) {1, 0, 0}, (Vector3) {0, 0, 1}
};

memcpy(facesPosition, nFacesPosition, 24 * sizeof(Vector3));

} else {

Vector3 nFacesPosition[36] = {
//left
(Vector3) {0, 0, 0}, (Vector3) {0, 1, 1},
(Vector3) {0, 1, 0}, (Vector3) {0, 1, 1},
(Vector3) {0, 0, 0}, (Vector3) {0, 0, 1},
//right
(Vector3) {1, 0, 1}, (Vector3) {1, 1, 0},
(Vector3) {1, 1, 1}, (Vector3) {1, 1, 0},
(Vector3) {1, 0, 1}, (Vector3) {1, 0, 0},
//top
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 0},
(Vector3) {0, 1, 0}, (Vector3) {1, 1, 0},
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
//bottom
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 1},
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
(Vector3) {0, 0, 0}, (Vector3) {1, 0, 0},
//front
(Vector3) {0, 0, 1}, (Vector3) {1, 1, 1},
(Vector3) {0, 1, 1}, (Vector3) {1, 1, 1},
(Vector3) {0, 0, 1}, (Vector3) {1, 0, 1},
//back
(Vector3) {1, 0, 0}, (Vector3) {0, 1, 0},
(Vector3) {1, 1, 0}, (Vector3) {0, 1, 0},
(Vector3) {1, 0, 0}, (Vector3) {0, 0, 0}
};

memcpy(facesPosition, nFacesPosition, 36 * sizeof(Vector3));

}

}

Vector3 BFH_GetDirection(BlockFace face) {
switch (face) {
Expand All @@ -62,14 +96,20 @@ Vector3 BFH_GetDirection(BlockFace face) {
}

void BFH_ResetIndexes(void) {
BFH_verticesI = 0;
BFH_texI = 0;
BFH_colorsI = 0;
BFH_verticesI[0] = 0;
BFH_texI[0] = 0;
BFH_colorsI[0] = 0;

BFH_verticesI[1] = 0;
BFH_texI[1] = 0;
BFH_colorsI[1] = 0;
}

void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID) {
void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block blockDef, int translucent, int light) {

int texID = Block_definition[blockID].textures[(int)face];
Vector3 facesPosition[36] = {0};
BFH_GetFacesPosition(blockDef, facesPosition);
int texID = blockDef.textures[(int)face];

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

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

switch(face) {
case BlockFace_Bottom:
mesh->colors[BFH_colorsI++] = 100;
break;
case BlockFace_Left:
case BlockFace_Right:
mesh->colors[BFH_colorsI++] = 150;
break;
case BlockFace_Front:
case BlockFace_Back:
mesh->colors[BFH_colorsI++] = 200;
break;
default:
mesh->colors[BFH_colorsI++] = 255;
break;
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].x + pos.x) * 15.99f);
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].y + pos.y) * 15.99f);
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].z + pos.z) * 15.99f);

if(blockDef.modelType != BlockModelType_Sprite) {
switch(face) {
case BlockFace_Bottom:
mesh->colors[BFH_colorsI[translucent]++] = fmin(100, fmax(16, 100 - light));
break;
case BlockFace_Left:
case BlockFace_Right:
mesh->colors[BFH_colorsI[translucent]++] = fmin(150, fmax(16, 150 - light));
break;
case BlockFace_Front:
case BlockFace_Back:
mesh->colors[BFH_colorsI[translucent]++] = fmin(200, fmax(16, 200 - light));
break;
default:
mesh->colors[BFH_colorsI[translucent]++] = fmin(255, fmax(16, 255 - light));
break;
}
} else {
mesh->colors[BFH_colorsI[translucent]++] = fmax(16, 255 - light);
}

mesh->texcoords[BFH_texI++] = (unsigned char)((BFH_texCoords[texI++] + textureX));
mesh->texcoords[BFH_texI++] = (unsigned char)((BFH_texCoords[texI++] + textureY));
mesh->texcoords[BFH_texI[translucent]++] = (unsigned char)((BFH_texCoords[texI++] + textureX));
mesh->texcoords[BFH_texI[translucent]++] = (unsigned char)((BFH_texCoords[texI++] + textureY));
}

}
Expand Down
4 changes: 3 additions & 1 deletion blockfacehelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
void BFH_ResetIndexes(void);

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

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

void BFH_GetFacesPosition(Block b, Vector3 *facesPosition);

#endif
Loading

0 comments on commit 798ec8c

Please sign in to comment.