using GameOfLife; using Microsoft.VisualBasic; using SFML.Graphics; using SFML.System; using SFML.Window; using System.Diagnostics; using System.Security.Cryptography.X509Certificates; using System.Security.Principal; namespace SFMLGame { public class Programm { public static Game game; public static int WindowWidth, WindowHeight; private static bool paused = false; private static bool dmsg = true; private static bool leftBtnDown; public static async Task Main() { WindowHeight = 1080; WindowWidth = 1920; byte[] fontarial = File.ReadAllBytes("fonts\\arial.ttf"); Font font = new(fontarial); RenderWindow window = new(new VideoMode((uint)WindowWidth, (uint)WindowHeight), "GameOfLife SFML"); window.Closed += new EventHandler(OnClosedEvent); window.KeyPressed += new EventHandler(OnKeyPressedEvent); window.MouseButtonPressed += new EventHandler(OnMouseDown); window.MouseMoved += new EventHandler(OnMouseMoved); window.MouseButtonReleased += new EventHandler(OnMouseReleased); Transform t = new(); t.Scale(0.5f, 0.5f); RenderStates rs = new(); rs.BlendMode = BlendMode.Add; rs.Transform = t; game = new(WindowHeight / 4, WindowWidth / 4); game.SetRandomStart(WindowHeight, WindowWidth, mod: 4); VertexArray vtx = new(PrimitiveType.Points); int framecount = 0; int CurrentFPS = 0; window.SetFramerateLimit(120); Stopwatch fpsWatch = new(); fpsWatch.Start(); int CellCount = 0; int drawcalls; List objects = []; while (window.IsOpen) { Stopwatch watch = new(); window.DispatchEvents(); window.Clear(); if (!paused) { watch.Start(); game.Update(); if (dmsg) { Console.WriteLine($"Update took {watch.ElapsedMilliseconds}ms"); } }; if (fpsWatch.ElapsedMilliseconds > 1000) { CurrentFPS = framecount; framecount = 0; fpsWatch.Restart(); } Render(game.getActiveMap(), out drawcalls, ref window); //DrawAll(ref window, ref objects); watch.Restart(); window.Draw(GetFPSText(CurrentFPS, font)); window.Draw(GetCellCountText(CellCount, font)); window.Draw(GetIterationText(game.Iteration, font)); if (dmsg) { Console.WriteLine($"UI took {watch.ElapsedMilliseconds}ms | {watch.ElapsedTicks}ticks"); } if (framecount % 5 == 0) { watch.Restart(); CellCount = game.getActiveMap().GetAliveCells(); if (dmsg) { Console.WriteLine($"Cell Count took {watch.ElapsedMilliseconds}ms | {watch.ElapsedTicks}ticks"); } } //window.Draw(vtx); //if (dmsg) //{ // Console.WriteLine($"Drawing took {watch.ElapsedMilliseconds}ms"); //} watch.Restart(); window.Display(); if (dmsg) { Console.WriteLine($"Display took {watch.ElapsedMilliseconds}ms | {watch.ElapsedTicks}ticks"); } framecount++; watch.Stop(); } window.Dispose(); } private static void DrawAll(ref RenderWindow window, ref List objects) { foreach (var draw in objects) { window.Draw(draw); } } private static Drawable GetIterationText(int iteration, Font font) { Text ret = new Text($"Iterations: {iteration}", font); ret.Position = new Vector2f(0, 20); return ret; } private static Drawable GetCellCountText(int value, Font font) { Text ret = new Text($"CellCOunt: {value}", font); ret.Position = new Vector2f(0, 40); return ret; } private static void OnMouseMoved(object? sender, MouseMoveEventArgs e) { if (leftBtnDown) { game.getActiveMap().GetCell(e.Y / 4, e.X / 4).isAlive = true; Console.WriteLine($"Edit Cell at {e.Y},{e.X}"); } } private static void OnMouseReleased(object? sender, MouseButtonEventArgs e) { if (e.Button == Mouse.Button.Left) { Console.WriteLine($"X: {e.X} Y: {e.Y}"); leftBtnDown = false; } } private static void OnMouseDown(object? sender, MouseButtonEventArgs e) { if (e.Button == Mouse.Button.Left) { Console.WriteLine($"X: {e.X} Y: {e.Y}"); game.getActiveMap().GetCell(e.Y / 4, e.X / 4).isAlive = true; leftBtnDown = true; } //game.getActiveMap().GetCell(e.Y/4, e.X/4).isAlive = true; } private static Text GetFPSText(int currentFPS, Font font) { Text txt = new($"FPS: {currentFPS}", font); return txt; } private static void Render(in GameMap gameMap, ref VertexArray vtx) { vtx.Clear(); foreach (var cell in gameMap.GetAllCells()) { vtx.Append(new Vertex(new Vector2f(cell.YCoord * 4, cell.XCoord * 4), GetColor(cell))); } } private static void Render(in GameMap gameMap, out int DrawCalls, ref RenderWindow window) { /** drawables = [];//int ctn=0; var sizeScalar = new Vector2f(4f, 4f); RectangleShape shape = new(sizeScalar); int drawCalls = 0; foreach (var cell in gameMap.GetAllCells()) { shape.Position = new Vector2f(cell.YCoord * 4, cell.XCoord * 4); //shape.Scale = new(2f, 2f); //shape.Radius = 1f; //shape.Size = sizeScalar; shape.FillColor = GetColor(cell); shape.OutlineThickness = 1f; shape.OutlineColor = Color.Black; //window.Draw(shape); window.Draw(shape); drawCalls++; //drawables.Add(shape); //ctn++; //vtx.Append(new Vertex(new Vector2f(cell.YCoord * 4, cell.XCoord * 4), GetColor(cell))); }**/ //shape.Dispose(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var sizeScalar = new Vector2f(4f, 4f); DrawCalls = 0; VertexArray vtx = new VertexArray(PrimitiveType.Quads); Color cellColor; Vector2f position; object lockObj = new object(); foreach (var cell in gameMap.GetAllCells()) { position.X = cell.YCoord * 4; position.Y = cell.XCoord * 4; cellColor = GetColor(cell); // Create four vertices for each rectangle (since it's a quad) vtx.Append(new Vertex(position, cellColor)); // Top-left vtx.Append(new Vertex(new Vector2f(position.X + sizeScalar.X, position.Y), cellColor)); // Top-right vtx.Append(new Vertex(new Vector2f(position.X + sizeScalar.X, position.Y + sizeScalar.Y), cellColor)); // Bottom-right vtx.Append(new Vertex(new Vector2f(position.X, position.Y + sizeScalar.Y), cellColor)); // Bottom-left } window.Draw(vtx); vtx.Dispose(); if(dmsg) { Console.WriteLine($"Drawing took {stopwatch.ElapsedMilliseconds}ms | {stopwatch.ElapsedTicks}ticks"); } stopwatch.Stop(); //Console.WriteLine($"Draw Calls per Frame: {drawCalls}"); return; } private static Color GetColor(CellState cell) { return cell.isAlive ? Color.White : Color.Black; } public static void OnClosedEvent(object sender, EventArgs e) { ((Window)sender).Close(); // ((Window)sender).Dispose(); } public static void OnKeyPressedEvent(object sender, KeyEventArgs e) { if (e.Code == Keyboard.Key.Escape) { ((Window)sender).Close(); } if (e.Code == Keyboard.Key.R) { game.SetRandomStart(WindowHeight, WindowWidth, mod: 4); } if (e.Code == Keyboard.Key.P) { paused = !paused; } if (e.Code == Keyboard.Key.D) { dmsg = !dmsg; } } public void InitWindow() { } } }