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 Voxels = new List(); 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(); 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 < 5 && k < 5 && rnd.Next(0, 3) > 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,12,12,12, 5, 18, 5, new float[] {0.8f,0.0f,0.0f},64)); LightingManager.Lights.Add(new LightSource(LightShape.Sphere,12,12,12, 5, 8, 5, new float[] {0.0f,0.0f,0.6f},64)); TestChunk.GenerateLightMap(); TestChunk.CalculateCull(); MainThread = new Thread(Runtime); MainThread.SetApartmentState(ApartmentState.STA); MainThread.Start(); ResizeDisplay(); } private void Runtime() { bool up = false; double LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; double SecondFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; while (running) { Display.Refresh(); if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - LightingFrameTime > 16) { LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; LightingManager.UpdateLighting(); //LightingManager.sunAngles[0] += 5f; //LightingManager.sunAngles[1] += 1f; } if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondFrameTime > 1000) { int[] pos = LightingManager.Lights[0].GetPosition(); int[] pos2 = LightingManager.Lights[1].GetPosition(); if (up && pos[0] > 0) { pos[0]--; pos2[1]--; } else if (up) up = false; else if (pos[0] < 10) { pos[0]++; pos2[1]++; } else up = true; //LightingManager.Lights[0].SetPosition(pos); //LightingManager.Lights[1].SetPosition(pos2); 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(scale, scale); ChunkRegistry.RenderChunk(g, 0); 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; width = Width; height = Height; } 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) scale += 0.25f; if (e.KeyCode == Keys.Subtract) scale -= 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) { } } }