Skip to content

Commit

Permalink
World Generation, Chat, Optimizations and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirvoid committed Nov 21, 2021
1 parent af532f7 commit dfcff7c
Show file tree
Hide file tree
Showing 32 changed files with 723 additions and 148 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Raylib Voxel Engine
# IsleForge
![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)
Open-Source Voxel Game made in C using Raylib.

*Multiplayer is not fully implemented yet so the server code will come later.
*Multiplayer is not fully implemented yet so the server code will come later.

## Dependencies

| Dependency | Version |
|---------------|---------|
| Raylib | 4.0 |
| Zpl-c/ENet | 2.3.0 |
| FastNoiseLite | - |
7 changes: 7 additions & 0 deletions block.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include "block.h"
#include "raylib.h"

Expand Down
7 changes: 7 additions & 0 deletions block.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_BLOCK_H
#define G_BLOCK_H

Expand Down
7 changes: 7 additions & 0 deletions blockfacehelper.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include <stdlib.h>
#include <string.h>
#include <math.h>
Expand Down
7 changes: 7 additions & 0 deletions blockfacehelper.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_BLOCKFACEHELPER_H
#define G_BLOCKFACEHELPER_H

Expand Down
100 changes: 100 additions & 0 deletions chat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "raylib.h"
#include "raygui.h"
#include "chat.h"
#include "player.h"
#include "networkhandler.h"
#include "packet.h"
#include "screens.h"

char* chatLines[64];
int currentLine = 0;

void Chat_AddLine(char *line) {
if(chatLines[currentLine]) MemFree(chatLines[currentLine]);
chatLines[currentLine++] = line;
if(currentLine >= 64) {
currentLine = 0;
}
}


char Chat_input[64] = "";
bool Chat_editMode = false;
bool Chat_open = false;

void Chat_Draw(Vector2 offset, Color uiColor) {

if(!Network_connectedToServer) return;

int chatWidth = 352;
int fontSize = 10;

//Draw Background
if(Chat_editMode) DrawRectangle(offset.x, offset.y - 184 + 46, chatWidth, 184, uiColor);

//Draw Lines
int lineAdded = 0;

int index = currentLine;
while(lineAdded < 13) {
if(chatLines[index]) {
int textLength = TextLength(chatLines[index]);
int startPos = 0;
char *drawLines[3];
int drawLinesCnt = 0;
for(int i = 0; i < textLength; i++) {
const char* sub = TextSubtext(chatLines[index], startPos, i - startPos + 1);
int textWidth = MeasureText(sub, fontSize);
if(textWidth >= chatWidth - fontSize - 4 || i == textLength - 1) {
drawLines[drawLinesCnt] = MemAlloc(64);
TextCopy(drawLines[drawLinesCnt], sub);
drawLinesCnt++;
startPos = i;
}
}
for(int i = drawLinesCnt - 1; i >= 0; i--) {
if(!drawLines[i]) continue;
DrawText(drawLines[i], offset.x + 4, offset.y - lineAdded * fontSize, fontSize, WHITE);
lineAdded++;
}
}

index--;
if(index < 0) index = 63;
if(index == currentLine) break;
}

//Chat input
if(Chat_editMode) GuiTextBox((Rectangle) { offset.x, offset.y + 22, chatWidth, 24 }, Chat_input, 64, Chat_editMode);

if(IsKeyPressed(KEY_ENTER)) {
if(Chat_open) {
char *message = MemAlloc(64);
for(int i = 0; i < 64; i++) {
message[i] = Chat_input[i];
Chat_input[i] = '\0';
}
Network_Send(Packet_SendMessage(message));
DisableCursor();
Chat_open = false;
Screen_cursorEnabled = false;
}
}

if(Chat_open) {
Chat_editMode = true;
} else {
Chat_editMode = false;
}

}
16 changes: 16 additions & 0 deletions chat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_CHAT_H
#define G_CHAT_H

extern bool Chat_open;

void Chat_AddLine(char *line);
void Chat_Draw(Vector2 offset, Color uiColor);

#endif
7 changes: 7 additions & 0 deletions chunk.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include <stdio.h>
#include <stdlib.h>
#include "raylib.h"
Expand Down
7 changes: 7 additions & 0 deletions chunk.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_CHUNK_H
#define G_CHUNK_H

Expand Down
7 changes: 7 additions & 0 deletions chunk_shader.fs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

"#version 330\n"
"in vec2 fragTexCoord;"
"in vec4 fragColor;"
Expand Down
7 changes: 7 additions & 0 deletions chunk_shader.vs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

"#version 330\n"
"in vec3 vertexPosition;"
"in vec2 vertexTexCoord;"
Expand Down
43 changes: 24 additions & 19 deletions chunkmesh.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include <stdlib.h>
#include "raylib.h"
#include "raymath.h"
Expand Down Expand Up @@ -50,18 +57,27 @@ void ChunkMesh_Unload(ChunkMesh mesh, bool isEndOfChunk) {
RL_FREE(mesh.colors);
}

void ChunkMesh_Draw(ChunkMesh mesh, Material material, Matrix transform) {
void ChunkMesh_PrepareDrawing(Material mat) {
rlEnableShader(mat.shader.id);
rlEnableTexture(mat.maps[0].texture.id);

float drawDistance = world.drawDistance * 16.0f;
rlSetUniform(rlGetLocationUniform(mat.shader.id, "drawDistance"), &drawDistance, RL_SHADER_UNIFORM_FLOAT, 1);
}

rlEnableShader(material.shader.id);
void ChunkMesh_FinishDrawing(void) {
rlDisableShader();
rlDisableTexture();
}

void ChunkMesh_Draw(ChunkMesh mesh, Material material, Matrix transform) {

Matrix matView = rlGetMatrixModelview();
Matrix matModelView = matView;
Matrix matProjection = rlGetMatrixProjection();

matModelView = MatrixMultiply(transform, MatrixMultiply(rlGetMatrixTransform(), matView));

rlEnableTexture(material.maps[0].texture.id);

if (!rlEnableVertexArray(mesh.vaoId)) {
rlEnableVertexBuffer(mesh.vboId[0]);
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_POSITION], 3, RL_UNSIGNED_BYTE, 0, 0, 0);
Expand All @@ -71,31 +87,20 @@ void ChunkMesh_Draw(ChunkMesh mesh, Material material, Matrix transform) {
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_TEXCOORD01], 2, RL_UNSIGNED_BYTE, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_TEXCOORD01]);

if (material.shader.locs[SHADER_LOC_VERTEX_COLOR] != -1) {
rlEnableVertexBuffer(mesh.vboId[2]);
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_COLOR], 1, RL_UNSIGNED_BYTE, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_COLOR]);
}

rlEnableVertexBuffer(mesh.vboId[2]);
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_COLOR], 1, RL_UNSIGNED_BYTE, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_COLOR]);
}

Matrix matMVP = MatrixIdentity();
matMVP = MatrixMultiply(matModelView, matProjection);

float drawDistance = world.drawDistance * 16.0f;

rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_MVP], matMVP);
rlSetUniform(rlGetLocationUniform(material.shader.id, "drawDistance"), &drawDistance, RL_SHADER_UNIFORM_FLOAT, 1);


rlDrawVertexArray(0, mesh.drawVertexCount);

rlDisableTexture();

rlDisableVertexArray();
rlDisableVertexBuffer();
rlDisableVertexBufferElement();

rlDisableShader();

rlSetMatrixModelview(matView);
rlSetMatrixProjection(matProjection);
Expand Down
9 changes: 9 additions & 0 deletions chunkmesh.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_CHUNKMESH_H
#define G_CHUNKMESH_H

Expand All @@ -18,6 +25,8 @@ typedef struct ChunkMesh {

void ChunkMesh_Upload(ChunkMesh *mesh);
void ChunkMesh_Unload(ChunkMesh mesh, bool isEndOfChunk);
void ChunkMesh_PrepareDrawing(Material mat);
void ChunkMesh_FinishDrawing(void);
void ChunkMesh_Draw(ChunkMesh mesh, Material material, Matrix transform);

#endif
7 changes: 7 additions & 0 deletions client.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
Expand Down
7 changes: 7 additions & 0 deletions client.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_CLIENT_H
#define G_CLIENT_H

Expand Down
7 changes: 7 additions & 0 deletions entity.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#include "raylib.h"
#include "raymath.h"
#include "entity.h"
Expand Down
7 changes: 7 additions & 0 deletions entity.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Copyright (c) 2021 Sirvoid
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#ifndef G_ENTITY_H
#define G_ENTITY_H

Expand Down
Loading

0 comments on commit dfcff7c

Please sign in to comment.