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
+1
View File
@@ -5,3 +5,4 @@ build/
recovery/ recovery/
vendor/directx vendor/directx
Sandbox/imgui.ini Sandbox/imgui.ini
Sandbox/worlds
+47 -3
View File
@@ -7,6 +7,7 @@ using System.Threading;
using System.Collections; using System.Collections;
using GlitchyEngine.ImGui; using GlitchyEngine.ImGui;
using ImGui; using ImGui;
using System.IO;
namespace Sandbox.VoxelFun namespace Sandbox.VoxelFun
{ {
@@ -65,6 +66,9 @@ namespace Sandbox.VoxelFun
GraphicsContext _context ~ _?.ReleaseRef(); GraphicsContext _context ~ _?.ReleaseRef();
int _viewDistance = 8; int _viewDistance = 8;
private World _world;
private String _chunkBasePath ~ delete _;
Dictionary<Point3, Chunk> chunks = new .() ~ DeleteDictionaryAndValues!(_); Dictionary<Point3, Chunk> chunks = new .() ~ DeleteDictionaryAndValues!(_);
@@ -84,10 +88,19 @@ namespace Sandbox.VoxelFun
Monitor chunkPosChanged = new Monitor()..Enter() ~ delete _; 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(); _context = context..AddRef();
_world = world;
_chunkBasePath = Path.InternalCombine(.. new String(), _world.Directory, "chunks", "");
if(!Directory.Exists(_chunkBasePath))
{
Directory.CreateDirectory(_chunkBasePath);
}
voxelGeoGen.Context = _context; voxelGeoGen.Context = _context;
voxelGeoGen.Layout = vertexLayout; voxelGeoGen.Layout = vertexLayout;
@@ -173,14 +186,17 @@ namespace Sandbox.VoxelFun
if(!chunks.ContainsKey(chunkCoordinate)) if(!chunks.ContainsKey(chunkCoordinate))
{ {
var chunk = new Chunk(); 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) 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); GenTestChunk(chunk);
SaveChunkToFile(chunkCoordinate, chunk);
} }
GenChunkGeo(chunk);
chunkListLock.Enter(); chunkListLock.Enter();
chunks.Add(chunkCoordinate, chunk); chunks.Add(chunkCoordinate, chunk);
chunkListLock.Exit(); chunkListLock.Exit();
@@ -190,11 +206,36 @@ namespace Sandbox.VoxelFun
Result<void> LoadChunkFromFile(Point3 chunkCoordinate, Chunk outChunk) Result<void> LoadChunkFromFile(Point3 chunkCoordinate, Chunk outChunk)
{ {
String chunkFileName = scope String(_chunkBasePath);
chunkFileName.AppendF($"{chunkCoordinate.X}_{chunkCoordinate.Y}_{chunkCoordinate.Z}.cdata");
if(!File.Exists(chunkFileName))
return .Err; 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) 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; return .Ok;
} }
@@ -328,7 +369,10 @@ namespace Sandbox.VoxelFun
{ {
GenerateTerrain(chunk); GenerateTerrain(chunk);
GroundLayers(chunk); GroundLayers(chunk);
}
void GenChunkGeo(Chunk chunk)
{
chunk.Geometry?.ReleaseRef(); chunk.Geometry?.ReleaseRef();
chunk.Geometry = voxelGeoGen.GenerateGeometry(chunk.Data); chunk.Geometry = voxelGeoGen.GenerateGeometry(chunk.Data);
} }
+4 -5
View File
@@ -191,16 +191,15 @@ namespace Sandbox.VoxelFun
_alphaBlendState = new BlendState(_context, blendDesc); _alphaBlendState = new BlendState(_context, blendDesc);
_opaqueBlendState = new BlendState(_context, .Default); _opaqueBlendState = new BlendState(_context, .Default);
_chunkManager = new ChunkManager(_context, _vertexLayout);
_chunkManager.Texture = _texture..AddRef();
_chunkManager.TextureEffect = _textureEffect..AddRef();
_world = new World(); _world = new World();
if(World.CreateWorld("test", 1337, _world) case .Err(.WorldAlreadyExists)) if(World.CreateWorld("test", 1337, _world) case .Err(.WorldAlreadyExists))
{ {
World.LoadWorld("test", _world); World.LoadWorld("test", _world);
} }
_chunkManager = new ChunkManager(_context, _vertexLayout, _world);
_chunkManager.Texture = _texture..AddRef();
_chunkManager.TextureEffect = _textureEffect..AddRef();
} }
/* /*
void GenerateTerrain(ref VoxelChunk vc) void GenerateTerrain(ref VoxelChunk vc)