From 5d4b4023bddd12e27c6756793aea2ff36d49e124 Mon Sep 17 00:00:00 2001 From: Adrian Ulbrich Date: Mon, 4 Nov 2019 00:36:44 +0100 Subject: Upload code --- Unicity.Renderer/GraphicsRenderer.cs | 63 ++++++++++++ Unicity.Renderer/Properties/AssemblyInfo.cs | 36 +++++++ Unicity.Renderer/RenderWindow.cs | 120 ++++++++++++++++++++++ Unicity.Renderer/Shapes/Shape.cs | 17 +++ Unicity.Renderer/Shapes/Triangle.cs | 43 ++++++++ Unicity.Renderer/Unicity.Renderer.csproj | 72 +++++++++++++ Unicity.Renderer/WindowCreationFailedException.cs | 10 ++ Unicity.Renderer/glfw.dll | Bin 0 -> 206848 bytes Unicity.Renderer/packages.config | 5 + 9 files changed, 366 insertions(+) create mode 100644 Unicity.Renderer/GraphicsRenderer.cs create mode 100644 Unicity.Renderer/Properties/AssemblyInfo.cs create mode 100644 Unicity.Renderer/RenderWindow.cs create mode 100644 Unicity.Renderer/Shapes/Shape.cs create mode 100644 Unicity.Renderer/Shapes/Triangle.cs create mode 100644 Unicity.Renderer/Unicity.Renderer.csproj create mode 100644 Unicity.Renderer/WindowCreationFailedException.cs create mode 100644 Unicity.Renderer/glfw.dll create mode 100644 Unicity.Renderer/packages.config (limited to 'Unicity.Renderer') diff --git a/Unicity.Renderer/GraphicsRenderer.cs b/Unicity.Renderer/GraphicsRenderer.cs new file mode 100644 index 0000000..f417e14 --- /dev/null +++ b/Unicity.Renderer/GraphicsRenderer.cs @@ -0,0 +1,63 @@ +using GLFW; +using SharpGL; +using System; +using Unicity.Renderer.Shapes; +using static SharpGL.OpenGL; + +namespace Unicity.Renderer +{ + public class GraphicsRenderer : IDisposable + { + internal static OpenGL GL = new OpenGL(); + + public RenderWindow window { get; } + + public GraphicsRenderer(RenderWindow window) + { + this.window = window; + } + + public void SetClearColor(float red, float green, float blue, float alpha) + { + GL.ClearColor(red, green, blue, alpha); + } + + public void ClearScreen() + { + Glfw.MakeContextCurrent(window.window); + GL.Clear(GL_COLOR_BUFFER_BIT); + } + + public void RenderShape(Shape shape) + { + shape.Render(); + } + + bool disposed = false; + protected virtual void Dispose(bool disposing) + { + // Return of already disposed + if (disposed) + { + return; + } + + if (disposing) + { + // Free managed objects here + } + + // Dispose of any unmanaged resources + window?.Dispose(); + + // Set disposed flag to true + disposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + } +} diff --git a/Unicity.Renderer/Properties/AssemblyInfo.cs b/Unicity.Renderer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b86f96f --- /dev/null +++ b/Unicity.Renderer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("Unicity Renderer")] +[assembly: AssemblyDescription("Renderer for the Unicity game")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Alee Productions")] +[assembly: AssemblyProduct("Unicity Renderer")] +[assembly: AssemblyCopyright("Copyright © 2019 Alee Productions")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("f6ebb1f8-fca9-4a8d-a807-e877cd047908")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Unicity.Renderer/RenderWindow.cs b/Unicity.Renderer/RenderWindow.cs new file mode 100644 index 0000000..840d4fb --- /dev/null +++ b/Unicity.Renderer/RenderWindow.cs @@ -0,0 +1,120 @@ +using System; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using GLFW; + +namespace Unicity.Renderer +{ + public class RenderWindow : IDisposable + { + const int FPS = 60; + + internal NativeWindow window = null; + + Stopwatch loopTimer = new Stopwatch(); + + public event EventHandler Init; + public event EventHandler Update; + public event EventHandler Render; + + public int Width + { + get => window.ClientSize.Width; + set => window.ClientSize = new Size(value, window.ClientSize.Height); + } + + public int Height + { + get => window.ClientSize.Height; + set => window.ClientSize = new Size(window.ClientSize.Width, value); + } + + public string Title + { + get => window.Title; + set => window.Title = value; + } + + public RenderWindow(int width, int height, string title) + { + if (!File.Exists(Glfw.LIBRARY + ".dll")) + { + throw new WindowCreationFailedException("A required library file is missing and operation cannot continue."); + } + + if (!Glfw.Init()) + { + throw new WindowCreationFailedException("Failed to initialize GLFW."); + } + + window = new NativeWindow(width, height, title); + window.SizeChanged += Window_SizeChanged; + } + + private void Window_SizeChanged(object sender, SizeChangeEventArgs e) + { + GraphicsRenderer.GL.Viewport(0, 0, Width, Height); + UpdateWindow(); + } + + public void StartUpdateLoop() + { + if (loopTimer.IsRunning) return; + + loopTimer.Start(); + + Init?.Invoke(this, EventArgs.Empty); + + while (!window.IsClosed) + { + Glfw.PollEvents(); + + if (!window.IsClosing) UpdateWindow(); + } + + loopTimer.Stop(); + } + + private void UpdateWindow() + { + if (loopTimer.Elapsed.TotalMilliseconds >= 1000 / FPS) + { + loopTimer.Restart(); + + Update?.Invoke(this, EventArgs.Empty); + Render?.Invoke(this, EventArgs.Empty); + + window.SwapBuffers(); + } + } + + bool disposed = false; + protected virtual void Dispose(bool disposing) + { + // Return of already disposed + if (disposed) + { + return; + } + + if (disposing) + { + // Free managed objects here + } + + // Dispose of any unmanaged resources + window?.Dispose(); + Glfw.Terminate(); + + // Set disposed flag to true + disposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + } +} diff --git a/Unicity.Renderer/Shapes/Shape.cs b/Unicity.Renderer/Shapes/Shape.cs new file mode 100644 index 0000000..b051a89 --- /dev/null +++ b/Unicity.Renderer/Shapes/Shape.cs @@ -0,0 +1,17 @@ +using System; +using static Unicity.Renderer.GraphicsRenderer; + +namespace Unicity.Renderer.Shapes +{ + public class Shape : IDisposable + { + protected uint[] buffers = new uint[1]; + + public void Dispose() + { + GL.DeleteBuffers(1, buffers); + } + + internal virtual void Render() { } + } +} diff --git a/Unicity.Renderer/Shapes/Triangle.cs b/Unicity.Renderer/Shapes/Triangle.cs new file mode 100644 index 0000000..2aec109 --- /dev/null +++ b/Unicity.Renderer/Shapes/Triangle.cs @@ -0,0 +1,43 @@ +using System; +using System.Drawing; +using static Unicity.Renderer.GraphicsRenderer; +using static SharpGL.OpenGL; + +namespace Unicity.Renderer.Shapes +{ + public class Triangle : Shape + { + public Triangle(PointF pos1, PointF pos2, PointF pos3) + { + Init(pos1.X, pos1.Y, pos2.X, pos2.Y, pos3.X, pos3.Y); + } + + public Triangle(float x1, float y1, float x2, float y2, float x3, float y3) + { + Init(x1, y1, x2, y2, x3, y3); + } + + private void Init(float x1, float y1, float x2, float y2, float x3, float y3) + { + float[] data = + { + x1, y1, + x2, y2, + x3, y3 + }; + + GL.GenBuffers(1, buffers); + GL.BindBuffer(GL_ARRAY_BUFFER, buffers[0]); + GL.BufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW); + } + + internal override void Render() + { + GL.EnableVertexAttribArray(0); + GL.BindBuffer(GL_ARRAY_BUFFER, buffers[0]); + GL.VertexAttribPointer(0, 2, GL_FLOAT, false, 0, IntPtr.Zero); + GL.DrawArrays(GL_TRIANGLES, 0, 3); + GL.DisableVertexAttribArray(0); + } + } +} diff --git a/Unicity.Renderer/Unicity.Renderer.csproj b/Unicity.Renderer/Unicity.Renderer.csproj new file mode 100644 index 0000000..4be8ec2 --- /dev/null +++ b/Unicity.Renderer/Unicity.Renderer.csproj @@ -0,0 +1,72 @@ + + + + + Debug + AnyCPU + {F6EBB1F8-FCA9-4A8D-A807-E877CD047908} + Library + Unicity.Renderer + Unicity.Renderer + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + ..\packages\glfw-net.3.3.1\lib\netstandard2.0\GLFW.NET.dll + + + ..\packages\SharpGL.2.4.1.2\lib\net40-client\SharpGL.dll + + + + + + + + + + + + + + + + + + + + + + + + + Always + + + + \ No newline at end of file diff --git a/Unicity.Renderer/WindowCreationFailedException.cs b/Unicity.Renderer/WindowCreationFailedException.cs new file mode 100644 index 0000000..380ffb6 --- /dev/null +++ b/Unicity.Renderer/WindowCreationFailedException.cs @@ -0,0 +1,10 @@ +namespace Unicity.Renderer +{ + class WindowCreationFailedException : System.Exception + { + public WindowCreationFailedException(string message) : base(message) + { + + } + } +} diff --git a/Unicity.Renderer/glfw.dll b/Unicity.Renderer/glfw.dll new file mode 100644 index 0000000..f9747a3 Binary files /dev/null and b/Unicity.Renderer/glfw.dll differ diff --git a/Unicity.Renderer/packages.config b/Unicity.Renderer/packages.config new file mode 100644 index 0000000..a52cd52 --- /dev/null +++ b/Unicity.Renderer/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file -- cgit v1.2.3