Files
Terrain4IsoVoxelRenderer/VoxelIsometricRenderer/Terrain4.cs
2026-02-13 14:54:30 +00:00

218 lines
9.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using VoxelIsometricRenderer.Properties;
namespace VoxelIsometricRenderer
{
public partial class Terrain4 : Form
{
public static bool running = true;
public static List<Voxel> Voxels = new List<Voxel>();
public static Thread MainThread;
public static float scale = 2f;
public static int FPS = 0;
public static int Frames = 0;
public static double TotalRamUsage = 0;
public static double RamUsage = 0;
public static int width;
public static int height;
public Terrain4()
{
InitializeComponent();
TextureManager.AddBitmap(Resources.grasside);
TextureManager.AddBitmap(Resources.grasstop);
TextureManager.AddBitmap(Resources.dirt);
TextureManager.AddBitmap(Resources.log);
TextureManager.AddBitmap(Resources.sprucelog);
TextureManager.AddBitmap(Resources.logtop);
TextureManager.AddBitmap(Resources.leaves);
TextureManager.AddBitmap(Resources.spruceleaves);
TextureManager.AddBitmap(Resources.stone1);
TextureManager.AddBitmap(Resources.stone2);
TextureManager.AddBitmap(Resources.stone3);
TextureManager.AddBitmap(Resources.ore1);
TextureManager.AddBitmap(Resources.flower);
TextureManager.AddBitmap(Resources.vine);
TextureManager.AddBitmap(Resources.grasstall);
TextureManager.AddBitmap(Resources.grassmed);
TextureManager.AddBitmap(Resources.BlueSquare);
TextureManager.AddBitmap(Resources.greenSquare);
TextureManager.AddBitmap(Resources.RedSquare);
TextureManager.AddBitmap(Resources.YellowSquare);
TextureManager.AddBitmap(Resources.PinkSquare);
TextureManager.AddBitmap(Resources.OrangeSquare);
TextureManager.AddBitmap(Resources.ColourEdges);
TextureManager.GenerateVoxelFaces();
PlayerObject.CreatePlayer(new int[] { 0, 0, 0, 0, 0, 0 }, 0, new BlockState(false, BlockType.Ground, false));
Chunk TestChunk = ChunkRegistry.IndexChunk(0, 0, 0);
Random rnd = new Random();
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) {
if (i < 8 && k < 8 && rnd.Next(0,1)==0) TestChunk.CreateBlock(new int[] { rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13) }, 0, new BlockState(false, BlockType.Ground, true), i, j, k);
else TestChunk.CreateAir(i, j, k);
}
}
}
//LightingManager.Lights.Add(new LightSource(LightShape.Sphere,7,7,7, 6, 14, 7, new float[] {0.8f,0.8f,0.0f}, 255));
//LightingManager.Lights.Add(new LightSource(LightShape.Sphere, 15, 15, 15, 7,18,7, new float[] {1.8f,0.0f,0.0f},255));
//LightingManager.Lights.Add(new LightSource(LightShape.Sphere,15,15,15, 3, 8, 15, new float[] {0.0f,1.8f,0.0f},255));
LightingManager.Lights.Add(new LightSource(LightShape.Sphere,12,12,12, 10, 18, 10, new float[] {0.0f,0.0f,1.8f}, 255));
LightingManager.Lights.Add(new LightSource(LightShape.Sphere, 5, 5, 5, 4,18,4, new float[] {0.8f,0.0f,0.0f},255));
TestChunk.GenerateLightMap();
TestChunk.CalculateCull();
MainThread = new Thread(Runtime);
MainThread.SetApartmentState(ApartmentState.STA);
MainThread.Start();
ResizeDisplay();
}
private void Runtime()
{
bool up = true;
bool Down = true;
double LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
double SecondFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
double _64ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
while (running)
{
Display.Refresh();
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - LightingFrameTime > 16)
{
LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
LightingManager.UpdateLighting();
//LightingManager.Lights[0].SetPosition(PlayerObject.GetPos());
//LightingManager.sunAngles[0] += 5f;
//LightingManager.sunAngles[1] += 1f;
}
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - _64ms > 64)
{
PlayerObject.UpdateCamera();
_64ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
int[] Pos = LightingManager.Lights[0].GetPosition();
if (up && Pos[1] < 25)
{
Pos[1]++;
} else if (up) up = false;
else if (Pos[1] > -5)
{
Pos[1]--;
}
else
{
up = true;
}
LightingManager.Lights[0].SetPosition(Pos);
}
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondFrameTime > 1000)
{
TotalRamUsage = Math.Round(System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1000000.0);
RamUsage = Math.Round(GC.GetTotalMemory(false) / 1000000.0);
SecondFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
FPS = Frames;
Frames = 0;
}
}
}
private void Draw(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.PixelOffsetMode = PixelOffsetMode.Half;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.ScaleTransform(TextureManager.RenderScale, TextureManager.RenderScale);
ChunkRegistry.RenderChunk(g, 0);
PlayerObject.Draw(g);
g.ResetTransform();
g.DrawString(String.Format("FPS: {0} Ram Usage: {9}MB Ram Allocated: {8}MB\nSunValue: {1}\nMoonValue:{3}\nLightDirection:{4}\nAmbientColourGrading: R{5} G{6} B{7}\nSunAngles:{2}", FPS, LightingManager.SkyLightMetrics[0], String.Format(" x={0}° y={1}°", LightingManager.sunAngles[0], LightingManager.sunAngles[1]), LightingManager.SkyLightMetrics[2], LightingManager.SunAngle, LightingManager.SkyLightMetrics[3], LightingManager.SkyLightMetrics[4], LightingManager.SkyLightMetrics[5], TotalRamUsage, RamUsage), SystemFonts.DefaultFont, Brushes.Red, new PointF(0, 0));
Frames++;
}
public void ResizeDisplay()
{
Display.Left = 0;
Display.Top = 0;
Display.Width = Width;
Display.Height = Height -32;
width = Width;
height = Height -32;
PlayerObject.UpdateCameraCentre(Width, Height - 32);
}
private void ResizeAspectRatio(object sender, EventArgs e)
{
ResizeDisplay();
}
private void KillThreads(object sender, FormClosingEventArgs e)
{
running = false;
}
private void KeyDownEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Add) TextureManager.RenderScale += 0.25f;
if (e.KeyCode == Keys.Subtract) TextureManager.RenderScale -= 0.25f;
if (e.KeyCode == Keys.Up)
{
LightingManager.sunAngles[1] += 10f;
}
if (e.KeyCode == Keys.Down)
{
LightingManager.sunAngles[1] -= 10f;
}
if (e.KeyCode == Keys.Left)
{
LightingManager.sunAngles[0] += 10f;
}
if (e.KeyCode == Keys.Right)
{
LightingManager.sunAngles[0] += 10f;
}
if (e.KeyCode == Keys.W)
{
PlayerObject.IncrementPos(2, -1);
}
if (e.KeyCode == Keys.S)
{
PlayerObject.IncrementPos(2, 1);
}
if (e.KeyCode == Keys.A)
{
PlayerObject.IncrementPos(0, -1);
}
if (e.KeyCode == Keys.D)
{
PlayerObject.IncrementPos(0, 1);
}
if (e.KeyCode == Keys.Space)
{
PlayerObject.IncrementPos(1, 1);
}
if (e.KeyCode == Keys.ShiftKey)
{
PlayerObject.IncrementPos(1, -1);
}
}
private void KeyUpEvent(object sender, KeyEventArgs e)
{
}
private void Terrain4_Load(object sender, EventArgs e)
{
}
}
}