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.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 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); Voxels.Add(new Voxel(0, 0, 0, 0, 0, 0)); Voxels.Add(new Voxel(9, 9, 9, 9, 9, 9)); Voxels.Add(new Voxel(10, 10, 10, 10, 10, 10)); Voxels.Add(new Voxel(11, 11, 11, 11, 11, 11)); Voxels.Add(new Voxel(12, 12, 12, 12, 12, 12)); Voxels.Add(new Voxel(3, 3, 3, 3, 3, 3)); Voxels.Add(new Voxel(1, 1, 1, 1, 2, 3)); Voxels.Add(new Voxel(4, 4, 4, 4, 6, 6)); Voxels.Add(new Voxel(5, 5, 5, 5, 6, 6)); Voxels.Add(new Voxel(7, 7, 7, 7, 7, 7)); Voxels.Add(new Voxel(17, 18, 19, 20, 21, 22)); Voxels.Add(new Voxel(23, 23, 23, 23, 23, 23)); Voxels.Add(new Voxel(17, 18, 19, 20, -1, -1)); Voxels.Add(new Voxel(23, 23, 23, 23, -1, -1)); Voxels.Add(new Voxel(-1, -1, -1, -1, 21, 22)); Voxels.Add(new Voxel(-1, -1, -1, -1, 23, 23)); Voxels.Add(new Voxel(1, 13, 13, 13, 13, -1, -1)); Voxels.Add(new Voxel(1, 14, 14, 14,14, -1, -1)); Voxels.Add(new Voxel(1, 15, 15, 15,15, -1, -1)); Voxels.Add(new Voxel(1, 16, 16, 16,16, -1, -1)); Random rnd = new Random(); for(int i = 0; i < 25; i++) { Voxels.Add(new Voxel(rnd.Next(0,TextureManager.textures.Count - 1), rnd.Next(0, TextureManager.textures.Count - 1), rnd.Next(0, TextureManager.textures.Count - 1), rnd.Next(0, TextureManager.textures.Count - 1), rnd.Next(0, TextureManager.textures.Count - 1), rnd.Next(0, TextureManager.textures.Count - 1))); } TextureManager.GenerateVoxelFaces(); for (int i = 0; i < Voxels.Count; i++) { //TextureManager.CreateModel(Voxels[i]); } 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) { SecondFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; FPS = Frames; Frames = 0; } if (up && LightingManager.AmbientColouring[0] < 1.5f) { //LightingManager.AmbientColouring[0] += 0.01f; //LightingManager.AmbientColouring[1] += 0.01f; //LightingManager.AmbientColouring[2] += 0.01f; } else if (up) up = false; else if (LightingManager.AmbientColouring[0] > 0.5f) { //LightingManager.AmbientColouring[0] -= 0.01f; //LightingManager.AmbientColouring[1] -= 0.01f; //LightingManager.AmbientColouring[2] -= 0.01f; } else up = true; } } 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); int x = 0; int y = 0; foreach(Voxel voxel in Voxels) { voxel.Draw(g,2 + x,2 + y); x += 36; if ((x+32) * scale > Width) { y += 36; x = 0; } } g.ResetTransform(); g.DrawString(String.Format("FPS: {0}", FPS), SystemFonts.DefaultFont, Brushes.Red, new PointF(0, 0)); Frames++; } public void ResizeDisplay() { Display.Left = 0; Display.Top = 0; Display.Width = Width; Display.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.Up) scale += 0.25f; if (e.KeyCode == Keys.Down) scale -= 0.25f; } private void KeyUpEvent(object sender, KeyEventArgs e) { } private void Terrain4_Load(object sender, EventArgs e) { } } }