Updated .gitignore and added Chunk loading and saving

This commit is contained in:
Simon Lübeß
2021-02-24 20:07:02 +01:00
parent afc7594df9
commit 4dcf1b5bec
3 changed files with 53 additions and 9 deletions
+48 -4
View File
@@ -7,6 +7,7 @@ using System.Threading;
using System.Collections;
using GlitchyEngine.ImGui;
using ImGui;
using System.IO;
namespace Sandbox.VoxelFun
{
@@ -65,6 +66,9 @@ namespace Sandbox.VoxelFun
GraphicsContext _context ~ _?.ReleaseRef();
int _viewDistance = 8;
private World _world;
private String _chunkBasePath ~ delete _;
Dictionary<Point3, Chunk> chunks = new .() ~ DeleteDictionaryAndValues!(_);
@@ -84,10 +88,19 @@ namespace Sandbox.VoxelFun
Monitor chunkPosChanged = new Monitor()..Enter() ~ delete _;
public World World => _world;
public this(GraphicsContext context, VertexLayout vertexLayout)
public this(GraphicsContext context, VertexLayout vertexLayout, World world)
{
_context = context..AddRef();
_world = world;
_chunkBasePath = Path.InternalCombine(.. new String(), _world.Directory, "chunks", "");
if(!Directory.Exists(_chunkBasePath))
{
Directory.CreateDirectory(_chunkBasePath);
}
voxelGeoGen.Context = _context;
voxelGeoGen.Layout = vertexLayout;
@@ -173,14 +186,17 @@ namespace Sandbox.VoxelFun
if(!chunks.ContainsKey(chunkCoordinate))
{
var chunk = new Chunk();
chunk.Position = chunkCoordinate * .(VoxelChunk.SizeX, VoxelChunk.SizeY, VoxelChunk.SizeZ);
chunk.Transform = .Translation(chunk.Position.X, chunk.Position.Y, chunk.Position.Z);
if(LoadChunkFromFile(chunkCoordinate, chunk) case .Err)
{
chunk.Position = chunkCoordinate * .(VoxelChunk.SizeX, VoxelChunk.SizeY, VoxelChunk.SizeZ);
chunk.Transform = .Translation(chunk.Position.X, chunk.Position.Y, chunk.Position.Z);
GenTestChunk(chunk);
SaveChunkToFile(chunkCoordinate, chunk);
}
GenChunkGeo(chunk);
chunkListLock.Enter();
chunks.Add(chunkCoordinate, chunk);
chunkListLock.Exit();
@@ -190,11 +206,36 @@ namespace Sandbox.VoxelFun
Result<void> LoadChunkFromFile(Point3 chunkCoordinate, Chunk outChunk)
{
return .Err;
String chunkFileName = scope String(_chunkBasePath);
chunkFileName.AppendF($"{chunkCoordinate.X}_{chunkCoordinate.Y}_{chunkCoordinate.Z}.cdata");
if(!File.Exists(chunkFileName))
return .Err;
FileStream fs = scope FileStream();
fs.Open(chunkFileName, .Read, .Read);
outChunk.Data = fs.Read<VoxelChunk>();
fs.Close();
return .Ok;
}
Result<void> SaveChunkToFile(Point3 chunkCoordinate, Chunk outChunk)
{
String chunkFileName = scope String(_chunkBasePath);
chunkFileName.AppendF($"{chunkCoordinate.X}_{chunkCoordinate.Y}_{chunkCoordinate.Z}.cdata");
FileStream fs = scope FileStream();
fs.Open(chunkFileName, FileMode.Create, .Write);
fs.Write(outChunk.Data);
fs.Close();
return .Ok;
}
@@ -328,7 +369,10 @@ namespace Sandbox.VoxelFun
{
GenerateTerrain(chunk);
GroundLayers(chunk);
}
void GenChunkGeo(Chunk chunk)
{
chunk.Geometry?.ReleaseRef();
chunk.Geometry = voxelGeoGen.GenerateGeometry(chunk.Data);
}
+4 -5
View File
@@ -190,17 +190,16 @@ namespace Sandbox.VoxelFun
blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All);
_alphaBlendState = new BlendState(_context, blendDesc);
_opaqueBlendState = new BlendState(_context, .Default);
_chunkManager = new ChunkManager(_context, _vertexLayout);
_chunkManager.Texture = _texture..AddRef();
_chunkManager.TextureEffect = _textureEffect..AddRef();
_world = new World();
if(World.CreateWorld("test", 1337, _world) case .Err(.WorldAlreadyExists))
{
World.LoadWorld("test", _world);
}
_chunkManager = new ChunkManager(_context, _vertexLayout, _world);
_chunkManager.Texture = _texture..AddRef();
_chunkManager.TextureEffect = _textureEffect..AddRef();
}
/*
void GenerateTerrain(ref VoxelChunk vc)