Added a basic skylight engine. Currently it's really laggy.

Also tried fixing the autobuild action since it fetches the repo without submodules.
This commit is contained in:
milkwx3
2026-06-24 18:58:15 +03:00
parent 3ad54bd59d
commit d02d751275
8 changed files with 190 additions and 36 deletions
+3 -1
View File
@@ -11,13 +11,15 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
submodules: recursive
- name: Install build tools - name: Install build tools
run: | run: |
# echo "deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://archive.ubuntu.com/ubuntu noble main" | sudo tee /tmp/only.list # echo "deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://archive.ubuntu.com/ubuntu noble main" | sudo tee /tmp/only.list
# sudo apt-get update -o Dir::Etc::sourcelist=/tmp/only.list -o Dir::Etc::sourceparts="-" -qq # sudo apt-get update -o Dir::Etc::sourcelist=/tmp/only.list -o Dir::Etc::sourceparts="-" -qq
sudo apt update sudo apt update
sudo apt install -y build-essential cmake zip unzip make libxrandr-dev g++-mingw-w64-x86-64 gcc-mingw-w64-x86-64 libxinerama-dev libxcursor-dev libxi-dev libgl-dev sudo apt install -y build-essential cmake zip unzip make libxrandr-dev g++-mingw-w64-x86-64 gcc-mingw-w64-x86-64 g++-mingw-i686-x86-64 gcc-mingw-i686-x86-64 libxinerama-dev libxcursor-dev libxi-dev libgl-dev
- name: Build makeheaders - name: Build makeheaders
run: | run: |
+103 -1
View File
@@ -1,3 +1,4 @@
#include <unordered_set>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "common.hpp" #include "common.hpp"
@@ -6,6 +7,7 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <queue>
#define HUMIDITY_OFFSET 12.3f #define HUMIDITY_OFFSET 12.3f
#define CAVE_OFFSET 45.6f #define CAVE_OFFSET 45.6f
@@ -107,6 +109,35 @@ int GetBlock(World &world, int x, int y, int z) {
int cz = z / CHUNK_SIZE; int cz = z / CHUNK_SIZE;
return fetch_block(world.chunks[GetIndexWorld(cx, cz)], locX, y, locZ); return fetch_block(world.chunks[GetIndexWorld(cx, cz)], locX, y, locZ);
} }
int GetHighestBlock(World &world, int x, int z) {
if(CheckOOBWorld(x, 0, z)) return 0;
int locX = x % CHUNK_SIZE;
int locZ = z % CHUNK_SIZE;
int cx = x / CHUNK_SIZE;
int cz = z / CHUNK_SIZE;
return world.chunks[GetIndexWorld(cx, cz)].highestBlock[GetIndexChunk2D(locX, locZ)];
}
int GetSkyLight(World &world, int x, int y, int z) {
if(CheckOOBWorld(x, y, z)) return 0;
int locX = x % CHUNK_SIZE;
int locZ = z % CHUNK_SIZE;
int cx = x / CHUNK_SIZE;
int cz = z / CHUNK_SIZE;
uint16_t light = world.chunks[GetIndexWorld(cx, cz)].light[GetIndexChunk(locX, y, locZ)];
int skyLight = (light >> 12) & 0xF;
return skyLight;
}
Color GetLight(World &world, int x, int y, int z) {
if(CheckOOBWorld(x, y, z)) return {0, 0, 0, 0};
int locX = x % CHUNK_SIZE;
int locZ = z % CHUNK_SIZE;
int cx = x / CHUNK_SIZE;
int cz = z / CHUNK_SIZE;
uint16_t light = world.chunks[GetIndexWorld(cx, cz)].light[GetIndexChunk(locX, y, locZ)];
int skyLight = ((light >> 12) & 0xF) * 16;
//return {((light >> 8) & 0xF) * 16 + skyLight, (light >> 4) * 16 + skyLight, (light & 0xF) * 16 + skyLight, 255};
return {skyLight, skyLight, skyLight, 255};
}
bool CanTick(int blockType) { // TODO: make smarter in case other blocks can tick too! bool CanTick(int blockType) { // TODO: make smarter in case other blocks can tick too!
return blockType == BLOCK_SAND; return blockType == BLOCK_SAND;
} }
@@ -211,6 +242,19 @@ void SetBlock(World &world, int x, int y, int z, int t) {
int cz = z / CHUNK_SIZE; int cz = z / CHUNK_SIZE;
SetBlockChunk(&world.chunks[GetIndexWorld(cx, cz)], locX, y, locZ, t); SetBlockChunk(&world.chunks[GetIndexWorld(cx, cz)], locX, y, locZ, t);
} }
void SetLightChunk(Chunk *c, int x, int y, int z, uint8_t sky, uint8_t r, uint8_t g, uint8_t b) {
c->light[GetIndexChunk(x,y,z)] = sky << 12 | r << 8 | g << 4 | b;
}
void SetLight(World &world, int x, int y, int z, uint8_t sky, uint8_t r, uint8_t g, uint8_t b) {
if(CheckOOBWorld(x, y, z)) { return; }
int locX = x % CHUNK_SIZE;
int locZ = z % CHUNK_SIZE;
int cx = x / CHUNK_SIZE;
int cz = z / CHUNK_SIZE;
SetLightChunk(&world.chunks[GetIndexWorld(cx, cz)], locX, y, locZ, sky, r, g, b);
}
void InitWorldgen(int worldType) { void InitWorldgen(int worldType) {
const siv::PerlinNoise::seed_type seed = 123456u; const siv::PerlinNoise::seed_type seed = 123456u;
const siv::PerlinNoise perlin{ seed }; const siv::PerlinNoise perlin{ seed };
@@ -232,7 +276,7 @@ Chunk GenerateChunk(int _x, int _y) {
Chunk new_chunk = { 0 }; Chunk new_chunk = { 0 };
new_chunk.x = _x; new_chunk.x = _x;
new_chunk.z = _y; new_chunk.z = _y;
memset(&new_chunk.light, 0, sizeof(new_chunk.light));
for (int x = 0; x < CHUNK_SIZE; x++) for (int x = 0; x < CHUNK_SIZE; x++)
{ {
for (int y = 0; y < WORLD_HEIGHT; y++) for (int y = 0; y < WORLD_HEIGHT; y++)
@@ -480,6 +524,10 @@ void TickChunk(Chunk *chunk, World &world, int wx, int wz) {
} }
} }
} }
struct LightNode {
Vector3I position;
int value;
};
void RebuildOpaqueMask(Chunk &chunk) { void RebuildOpaqueMask(Chunk &chunk) {
const int GROUP = 16; const int GROUP = 16;
const int GROUPS = CHUNK_DATA_SIZE / GROUP; const int GROUPS = CHUNK_DATA_SIZE / GROUP;
@@ -504,6 +552,60 @@ void RebuildOpaqueMask(Chunk &chunk) {
memcpy(chunk.airMask, aMask, sizeof(aMask)); memcpy(chunk.airMask, aMask, sizeof(aMask));
} }
void RebuildLighting(World &world, int startX, int startZ) {
std::queue<LightNode> nodes;
for (int cx = -1; cx <= 1; cx++)
{
for (int cz = -1; cz <= 1; cz++) {
Chunk &chunk = world.chunks[GetIndexWorld(startX+cx, startZ+cz)];
memset(&chunk.light, 0, sizeof(chunk.light));
for (int x = 0; x < CHUNK_SIZE; x++) {
for (int z = 0; z < CHUNK_SIZE; z++) {
int y = chunk.highestBlock[GetIndexChunk2D(x, z)];
if (y < 0) continue;
Vector3I pos = { x + chunk.x * CHUNK_SIZE, y + 1, z + chunk.z * CHUNK_SIZE };
if (CheckOOBWorld(pos.x, pos.y, pos.z)) continue;
for (int _y = WORLD_HEIGHT; _y >= y; _y--)
{
SetLight(world, pos.x, _y, pos.z, 15, 0, 0, 0);
}
LightNode node{pos, 15};
SetLight(world, pos.x, pos.y, pos.z, 15, 0, 0, 0);
nodes.push(node);
}
}
}
}
const Vector3I dirs[6] = { (Vector3I){ 1, 0, 0 }, (Vector3I){ -1, 0, 0 }, (Vector3I){ 0, 1, 0 }, (Vector3I){ 0,-1, 0 }, (Vector3I){ 0, 0, 1 }, (Vector3I){ 0, 0,-1 } };
while (!nodes.empty()) {
std::cout << nodes.size() << std::endl;
LightNode node = nodes.front();
nodes.pop();
for (int i = 0; i < 6; i++) {
Vector3I newPos = {
node.position.x + dirs[i].x,
node.position.y + dirs[i].y,
node.position.z + dirs[i].z
};
if (CheckOOBWorld(newPos.x, newPos.y, newPos.z)) continue;
if (GetBlock(world, newPos.x, newPos.y, newPos.z) != BLOCK_AIR) continue;
int newValue = node.value - 1;
if (newValue <= 0) continue;
if (GetSkyLight(world, newPos.x, newPos.y, newPos.z) >= newValue) continue;
SetLight(world, newPos.x, newPos.y, newPos.z, newValue, 0, 0, 0);
nodes.push({ newPos, newValue });
}
}
}
#ifndef SERVER #ifndef SERVER
//Generates an opaque CRD (ChunkRenderData). Outputs the CRD. //Generates an opaque CRD (ChunkRenderData). Outputs the CRD.
ChunkRenderData GenerateCRDOpaque(Chunk &chunk, World &world) { ChunkRenderData GenerateCRDOpaque(Chunk &chunk, World &world) {
+26 -6
View File
@@ -9,6 +9,7 @@
#undef PlaySound #undef PlaySound
#undef Sound #undef Sound
#endif #endif
#include <functional>
#include "glfw_keycodes_to_string.h" #include "glfw_keycodes_to_string.h"
#include "../PerlinNoise.hpp" #include "../PerlinNoise.hpp"
#include "raylib.h" #include "raylib.h"
@@ -77,7 +78,7 @@
#define DEFAULT_FONT_SIZE 16 #define DEFAULT_FONT_SIZE 16
#define TPS 30.0 #define TPS 30.0
#define TPS_DIV 1.0 / TPS #define TPS_DIV 1.0 / TPS
#define RENDER_DISTANCE 128 #define RENDER_DISTANCE 2
#define RENDER_DISTANCE_SQUARED RENDER_DISTANCE*RENDER_DISTANCE #define RENDER_DISTANCE_SQUARED RENDER_DISTANCE*RENDER_DISTANCE
#define MAX_HP 100 #define MAX_HP 100
#define PATH_APPEND "resources/" #define PATH_APPEND "resources/"
@@ -152,7 +153,7 @@ struct Chunk {
uint16_t opaqueMask[CHUNK_DATA_SIZE/16]; uint16_t opaqueMask[CHUNK_DATA_SIZE/16];
uint16_t airMask[CHUNK_DATA_SIZE/16]; uint16_t airMask[CHUNK_DATA_SIZE/16];
uint8_t highestBlock[CHUNK_SIZE*CHUNK_SIZE]; uint8_t highestBlock[CHUNK_SIZE*CHUNK_SIZE];
uint8_t lights[CHUNK_DATA_SIZE]; uint16_t light[CHUNK_DATA_SIZE];
}; };
#if !defined(SERVER) #if !defined(SERVER)
typedef struct ChunkRenderData ChunkRenderData; typedef struct ChunkRenderData ChunkRenderData;
@@ -195,9 +196,19 @@ struct Quad2D {
#endif #endif
typedef struct Vector3I Vector3I; typedef struct Vector3I Vector3I;
struct Vector3I { struct Vector3I {
int x; int x, y, z;
int y; bool operator==(const Vector3I& other) const {
int z; return (x == other.x && y == other.y && z == other.z);
};
};
typedef struct Vector3IHash Vector3IHash;
struct Vector3IHash {
size_t operator()(const Vector3I& p) const noexcept {
size_t h1 = std::hash<int>{}(p.x);
size_t h2 = std::hash<int>{}(p.y);
size_t h3 = std::hash<int>{}(p.z);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
}; };
typedef struct Entity Entity; typedef struct Entity Entity;
struct Entity { struct Entity {
@@ -294,6 +305,9 @@ struct World {
}; };
extern const BlockDef BLOCKS[]; extern const BlockDef BLOCKS[];
int GetBlock(World&world,int x,int y,int z); int GetBlock(World&world,int x,int y,int z);
int GetHighestBlock(World&world,int x,int z);
int GetSkyLight(World&world,int x,int y,int z);
Color GetLight(World&world,int x,int y,int z);
bool CanTick(int blockType); bool CanTick(int blockType);
bool GetCloud(int x,int z); bool GetCloud(int x,int z);
bool IsTranslucent(int blockType); bool IsTranslucent(int blockType);
@@ -302,6 +316,8 @@ void RebuildHeightmap(Chunk&c);
int GetBiome(int sx,int sy); int GetBiome(int sx,int sy);
void SetBlockChunkFast(Chunk *c,int i,int t); void SetBlockChunkFast(Chunk *c,int i,int t);
void SetBlock(World&world,int x,int y,int z,int t); void SetBlock(World&world,int x,int y,int z,int t);
void SetLightChunk(Chunk *c,int x,int y,int z,uint8_t sky,uint8_t r,uint8_t g,uint8_t b);
void SetLight(World&world,int x,int y,int z,uint8_t sky,uint8_t r,uint8_t g,uint8_t b);
void InitWorldgen(int worldType); void InitWorldgen(int worldType);
Vector3 WorldPosition(int cx,int cz,int localX,int y,int localZ); Vector3 WorldPosition(int cx,int cz,int localX,int y,int localZ);
Chunk GenerateChunk(int _x,int _y); Chunk GenerateChunk(int _x,int _y);
@@ -309,6 +325,7 @@ void GenerateWorldAdditional(World&world);
void TickBlock(World&world,int wx,int wy,int wz); void TickBlock(World&world,int wx,int wy,int wz);
void TickChunk(Chunk *chunk,World&world,int wx,int wz); void TickChunk(Chunk *chunk,World&world,int wx,int wz);
void RebuildOpaqueMask(Chunk&chunk); void RebuildOpaqueMask(Chunk&chunk);
void RebuildLighting(World&world,int startX,int startZ);
#if !defined(SERVER) #if !defined(SERVER)
ChunkRenderData GenerateCRDOpaque(Chunk&chunk,World&world); ChunkRenderData GenerateCRDOpaque(Chunk&chunk,World&world);
ChunkRenderData GenerateCRDTranslucent(Chunk chunk,World&world); ChunkRenderData GenerateCRDTranslucent(Chunk chunk,World&world);
@@ -406,7 +423,7 @@ char *DrawChat();
void DrawLogoCenter(); void DrawLogoCenter();
#endif #endif
#if !defined(LEGACY_GL) && !defined(SERVER) #if !defined(LEGACY_GL) && !defined(SERVER)
MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData,Chunk chunk,int chunkWorldX,int chunkWorldY,uint8_t sideMask); MeshData GenerateChunkMesh(World&world,ChunkRenderData chunkRenderData,Chunk chunk,int chunkWorldX,int chunkWorldY,uint8_t sideMask);
MeshData MergeMeshData(const MeshData&a,const MeshData&b); MeshData MergeMeshData(const MeshData&a,const MeshData&b);
MeshData CreateCubeMeshData(const BlockDef&def,float cx,float cy,float cz); MeshData CreateCubeMeshData(const BlockDef&def,float cx,float cy,float cz);
Mesh GenChunkMesh(MeshData meshData); Mesh GenChunkMesh(MeshData meshData);
@@ -462,6 +479,9 @@ inline int GetIndexChunk2D(int x,int z){
inline bool CheckOOBWorld(int x,int y,int z){ inline bool CheckOOBWorld(int x,int y,int z){
return (x < 0 || y < 0 || z < 0 || x >= WORLD_SIZE_BLOCKS || y >= WORLD_HEIGHT || z >= WORLD_SIZE_BLOCKS); return (x < 0 || y < 0 || z < 0 || x >= WORLD_SIZE_BLOCKS || y >= WORLD_HEIGHT || z >= WORLD_SIZE_BLOCKS);
}; };
inline bool CheckOOBWorldChunk(int x,int z){
return (x < 0 || z < 0 || x >= MAX_WORLD_SIZE || z >= MAX_WORLD_SIZE);
};
inline int GetIndexWorld(int x,int z){ inline int GetIndexWorld(int x,int z){
if(CheckOOBWorld(x << 4, 0, z << 4)) return 0; if(CheckOOBWorld(x << 4, 0, z << 4)) return 0;
return x + MAX_WORLD_SIZE * z; return x + MAX_WORLD_SIZE * z;
+3
View File
@@ -39,6 +39,9 @@ inline int GetIndexChunk2D(int x, int z) {
inline bool CheckOOBWorld(int x, int y, int z) { inline bool CheckOOBWorld(int x, int y, int z) {
return (x < 0 || y < 0 || z < 0 || x >= WORLD_SIZE_BLOCKS || y >= WORLD_HEIGHT || z >= WORLD_SIZE_BLOCKS); return (x < 0 || y < 0 || z < 0 || x >= WORLD_SIZE_BLOCKS || y >= WORLD_HEIGHT || z >= WORLD_SIZE_BLOCKS);
} }
inline bool CheckOOBWorldChunk(int x, int z) {
return (x < 0 || z < 0 || x >= MAX_WORLD_SIZE || z >= MAX_WORLD_SIZE);
}
inline int GetIndexWorld(int x, int z) { inline int GetIndexWorld(int x, int z) {
if(CheckOOBWorld(x << 4, 0, z << 4)) return 0; if(CheckOOBWorld(x << 4, 0, z << 4)) return 0;
return x + MAX_WORLD_SIZE * z; return x + MAX_WORLD_SIZE * z;
+17 -15
View File
@@ -10,7 +10,7 @@
#ifndef SERVER #ifndef SERVER
#ifndef LEGACY_GL #ifndef LEGACY_GL
MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chunkWorldX, int chunkWorldY, uint8_t sideMask) MeshData GenerateChunkMesh(World &world, ChunkRenderData chunkRenderData, Chunk chunk, int chunkWorldX, int chunkWorldY, uint8_t sideMask)
{ {
MeshData out = {0}; MeshData out = {0};
@@ -31,6 +31,10 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
uint8_t crdata = chunkRenderData.sides[index]; uint8_t crdata = chunkRenderData.sides[index];
if (crdata == 0) continue; if (crdata == 0) continue;
int wx = (chunkWorldX * CHUNK_SIZE + x);
int wy = (y);
int wz = (chunkWorldY * CHUNK_SIZE + z);
int this_block = fetch_block(chunk, x,y,z); int this_block = fetch_block(chunk, x,y,z);
BlockDef definition = BLOCKS[this_block]; BlockDef definition = BLOCKS[this_block];
@@ -46,9 +50,6 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
Color color = {255, 255, 255, 255}; Color color = {255, 255, 255, 255};
Color colorShaded = {128, 128, 172, 255}; Color colorShaded = {128, 128, 172, 255};
float wx = (float)(chunkWorldX * CHUNK_SIZE + x);
float wy = (float)(y);
float wz = (float)(chunkWorldY * CHUNK_SIZE + z);
/*if(GetCloud(wx, wz)) { /*if(GetCloud(wx, wz)) {
color = ColorLerp(color, colorShaded, 0.5f); color = ColorLerp(color, colorShaded, 0.5f);
}*/ }*/
@@ -72,7 +73,8 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
positions.push_back(verts[3]); positions.push_back(verts[3]);
normals.push_back(normal); normals.push_back(normal);
colors.push_back(colorShaded); Color clr = GetLight(world, wx-1, wy, wz);
colors.push_back(clr);
uvs.push_back(corners[0]); uvs.push_back(corners[0]);
uvs.push_back(corners[1]); uvs.push_back(corners[1]);
@@ -111,7 +113,8 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
positions.push_back(verts[3]); positions.push_back(verts[3]);
normals.push_back(normal); normals.push_back(normal);
colors.push_back(colorShaded); Color clr = GetLight(world, wx+1, wy, wz);
colors.push_back(clr);
uvs.push_back(corners[0]); uvs.push_back(corners[0]);
uvs.push_back(corners[1]); uvs.push_back(corners[1]);
@@ -150,7 +153,8 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
positions.push_back(verts[3]); positions.push_back(verts[3]);
normals.push_back(normal); normals.push_back(normal);
colors.push_back(colorShaded); Color clr = GetLight(world, wx, wy-1, wz);
colors.push_back(clr);
uvs.push_back(corners[0]); uvs.push_back(corners[0]);
uvs.push_back(corners[1]); uvs.push_back(corners[1]);
@@ -189,12 +193,8 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
positions.push_back(verts[3]); positions.push_back(verts[3]);
normals.push_back(normal); normals.push_back(normal);
if(shaded) { Color clr = GetLight(world, wx, wy+1, wz);
colors.push_back(colorShaded); colors.push_back(clr);
}
else {
colors.push_back(color);
}
uvs.push_back(corners[0]); uvs.push_back(corners[0]);
uvs.push_back(corners[1]); uvs.push_back(corners[1]);
@@ -233,7 +233,8 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
positions.push_back(verts[3]); positions.push_back(verts[3]);
normals.push_back(normal); normals.push_back(normal);
colors.push_back(colorShaded); Color clr = GetLight(world, wx, wy, wz+1);
colors.push_back(clr);
uvs.push_back(corners[0]); uvs.push_back(corners[0]);
uvs.push_back(corners[1]); uvs.push_back(corners[1]);
@@ -272,7 +273,8 @@ MeshData GenerateChunkMesh(ChunkRenderData chunkRenderData, Chunk chunk, int chu
positions.push_back(verts[3]); positions.push_back(verts[3]);
normals.push_back(normal); normals.push_back(normal);
colors.push_back(colorShaded); Color clr = GetLight(world, wx, wy, wz-1);
colors.push_back(clr);
uvs.push_back(corners[0]); uvs.push_back(corners[0]);
uvs.push_back(corners[1]); uvs.push_back(corners[1]);
+5 -4
View File
@@ -66,10 +66,11 @@ static void DrawParticle(Particle particle, Color color)
rlSetTexture(0); rlSetTexture(0);
} }
void ParticlesUpdate(float dt, World &world) { void ParticlesUpdate(float dt, World &world) {
particles.remove_if([](Particle x) { return x.lifetime > 200; }); //particles.remove_if([](Particle x) { return x.lifetime > 0; });
for (Particle &part : particles) particles.remove_if([](Particle x) { return true; });
/*for (Particle &part : particles)
{ {
part.lifetime++; art.lifetime++;p
if(part.physicsType == Phys_Falling) { if(part.physicsType == Phys_Falling) {
Vector3 velocityTgt = Vector3Clamp(Vector3Add(part.velocity, {0, -dt * 9.8f, 0}), {-30.0f, -30.0f, 0-30.0f}, {30.0f, 30.0f, 30.0f}); Vector3 velocityTgt = Vector3Clamp(Vector3Add(part.velocity, {0, -dt * 9.8f, 0}), {-30.0f, -30.0f, 0-30.0f}, {30.0f, 30.0f, 30.0f});
@@ -99,7 +100,7 @@ void ParticlesUpdate(float dt, World &world) {
part.position = final; part.position = final;
part.velocity = velocityTgt; part.velocity = velocityTgt;
} }
} }*/
} }
void ParticlesDraw() { void ParticlesDraw() {
for (Particle part : particles) for (Particle part : particles)
+16 -5
View File
@@ -1,3 +1,4 @@
#include <functional>
#if INTERFACE #if INTERFACE
#ifdef Escape #ifdef Escape
@@ -101,7 +102,7 @@
#define TPS 30.0 #define TPS 30.0
#define TPS_DIV 1.0 / TPS #define TPS_DIV 1.0 / TPS
//Rendering //Rendering
#define RENDER_DISTANCE 128 #define RENDER_DISTANCE 2
#define RENDER_DISTANCE_SQUARED RENDER_DISTANCE*RENDER_DISTANCE #define RENDER_DISTANCE_SQUARED RENDER_DISTANCE*RENDER_DISTANCE
//Game stuff //Game stuff
@@ -179,7 +180,7 @@ struct Chunk {
uint16_t opaqueMask[CHUNK_DATA_SIZE/16]; uint16_t opaqueMask[CHUNK_DATA_SIZE/16];
uint16_t airMask[CHUNK_DATA_SIZE/16]; uint16_t airMask[CHUNK_DATA_SIZE/16];
uint8_t highestBlock[CHUNK_SIZE*CHUNK_SIZE]; uint8_t highestBlock[CHUNK_SIZE*CHUNK_SIZE];
uint8_t lights[CHUNK_DATA_SIZE]; uint16_t light[CHUNK_DATA_SIZE];
}; };
#ifndef SERVER #ifndef SERVER
struct ChunkRenderData { struct ChunkRenderData {
@@ -220,10 +221,20 @@ struct Quad2D {
}; };
#endif #endif
struct Vector3I { struct Vector3I {
int x; int x, y, z;
int y; bool operator==(const Vector3I& other) const {
int z; return (x == other.x && y == other.y && z == other.z);
};
}; };
struct Vector3IHash {
size_t operator()(const Vector3I& p) const noexcept {
size_t h1 = std::hash<int>{}(p.x);
size_t h2 = std::hash<int>{}(p.y);
size_t h3 = std::hash<int>{}(p.z);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
struct Entity { struct Entity {
uint8_t type; uint8_t type;
uint8_t id; uint8_t id;
+16 -3
View File
@@ -263,7 +263,12 @@ static void ResolveCollisions(float dt)
bool loadedChunks[MAX_WORLD_AREA] = { false }; bool loadedChunks[MAX_WORLD_AREA] = { false };
bool dirtyChunks[MAX_WORLD_AREA] = { false }; bool dirtyChunks[MAX_WORLD_AREA] = { false };
const Vector3I dirs2d[4] = {
(Vector3I){ 1, 0, 0},
(Vector3I){ 0, 0, 1},
(Vector3I){-1, 0, 0},
(Vector3I){ 0, 0, -1},
};
void Debug_EndMeasure(const std::string &stuff); void Debug_EndMeasure(const std::string &stuff);
void Debug_EndMeasureLoops(const std::string &stuff, int loops); void Debug_EndMeasureLoops(const std::string &stuff, int loops);
static void RemeshChunk(int x, int y) { static void RemeshChunk(int x, int y) {
@@ -274,9 +279,17 @@ static void RemeshChunk(int x, int y) {
} }
Chunk &chunk = world.chunks[idx]; Chunk &chunk = world.chunks[idx];
RebuildOpaqueMask(chunk); RebuildOpaqueMask(chunk);
/*for (int i = 0; i < 4; i++)
{
Vector3I dir = dirs2d[i];
if(!CheckOOBWorldChunk(x+dir.x, y+dir.z)) {
RebuildLighting(world, world.chunks[GetIndexWorld(x+dir.x, y+dir.z)]);
}
}*/
MeshData renderDataOpq = GenerateChunkMesh(GenerateCRDOpaque(chunk, world), chunk, x, y, 0xFF); RebuildLighting(world, chunk.x, chunk.z);
MeshData renderDataTsl = GenerateChunkMesh(GenerateCRDTranslucent(chunk, world), chunk, x, y, 0xFF); MeshData renderDataOpq = GenerateChunkMesh(world, GenerateCRDOpaque(chunk, world), chunk, x, y, 0xFF);
MeshData renderDataTsl = GenerateChunkMesh(world, GenerateCRDTranslucent(chunk, world), chunk, x, y, 0xFF);
Model model = LoadModelFromMesh(GenChunkMesh(renderDataOpq)); Model model = LoadModelFromMesh(GenChunkMesh(renderDataOpq));
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = terrain; model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = terrain;