aboutsummaryrefslogtreecommitdiff
path: root/Unicity.Renderer
diff options
context:
space:
mode:
Diffstat (limited to 'Unicity.Renderer')
-rw-r--r--Unicity.Renderer/GraphicsRenderer.cs63
-rw-r--r--Unicity.Renderer/Properties/AssemblyInfo.cs36
-rw-r--r--Unicity.Renderer/RenderWindow.cs120
-rw-r--r--Unicity.Renderer/Shapes/Shape.cs17
-rw-r--r--Unicity.Renderer/Shapes/Triangle.cs43
-rw-r--r--Unicity.Renderer/Unicity.Renderer.csproj72
-rw-r--r--Unicity.Renderer/WindowCreationFailedException.cs10
-rw-r--r--Unicity.Renderer/glfw.dllbin0 -> 206848 bytes
-rw-r--r--Unicity.Renderer/packages.config5
9 files changed, 366 insertions, 0 deletions
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <RootNamespace>Unicity.Renderer</RootNamespace>
+ <AssemblyName>Unicity.Renderer</AssemblyName>
+ <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+ <Deterministic>true</Deterministic>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup>
+ <StartupObject />
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="GLFW.NET, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
+ <HintPath>..\packages\glfw-net.3.3.1\lib\netstandard2.0\GLFW.NET.dll</HintPath>
+ </Reference>
+ <Reference Include="SharpGL, Version=2.4.1.2, Culture=neutral, PublicKeyToken=27fc851303210b27, processorArchitecture=MSIL">
+ <HintPath>..\packages\SharpGL.2.4.1.2\lib\net40-client\SharpGL.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="GraphicsRenderer.cs" />
+ <Compile Include="RenderWindow.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Shapes\Shape.cs" />
+ <Compile Include="Shapes\Triangle.cs" />
+ <Compile Include="WindowCreationFailedException.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="glfw.dll">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project> \ 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
--- /dev/null
+++ b/Unicity.Renderer/glfw.dll
Binary files 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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="glfw-net" version="3.3.1" targetFramework="net472" />
+ <package id="SharpGL" version="2.4.1.2" targetFramework="net472" />
+</packages> \ No newline at end of file