From dab4c57284e15bdf3dfaf3ca4800a41c8ae3e3bb Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 14 Jan 2021 00:31:03 -0800 Subject: [PATCH] initial commit --- .gitignore | 3 ++ .gitmodules | 3 ++ Campari.csproj | 15 +++++++++ Campari.sln | 31 ++++++++++++++++++ RefreshDevice.cs | 52 ++++++++++++++++++++++++++++++ Texture.cs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/RefreshCS | 1 + 7 files changed, 187 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Campari.csproj create mode 100644 Campari.sln create mode 100644 RefreshDevice.cs create mode 100644 Texture.cs create mode 160000 lib/RefreshCS diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b82ccd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs/ +bin/ +obj/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2136974 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/RefreshCS"] + path = lib/RefreshCS + url = https://github.com/thatcosmonaut/RefreshCS.git diff --git a/Campari.csproj b/Campari.csproj new file mode 100644 index 0000000..9d7b99d --- /dev/null +++ b/Campari.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.0 + x64 + + + + $(DefaultItemExcludes);lib\**\* + + + + + + diff --git a/Campari.sln b/Campari.sln new file mode 100644 index 0000000..2e511bb --- /dev/null +++ b/Campari.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30717.126 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Campari", "Campari.csproj", "{55485ED3-E08A-4827-B7CF-5028287D4AE3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RefreshCS", "lib\RefreshCS\RefreshCS.csproj", "{2EFA491B-EDAF-4983-A3E4-A24D014E4B6E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {55485ED3-E08A-4827-B7CF-5028287D4AE3}.Debug|x64.ActiveCfg = Debug|x64 + {55485ED3-E08A-4827-B7CF-5028287D4AE3}.Debug|x64.Build.0 = Debug|x64 + {55485ED3-E08A-4827-B7CF-5028287D4AE3}.Release|x64.ActiveCfg = Release|x64 + {55485ED3-E08A-4827-B7CF-5028287D4AE3}.Release|x64.Build.0 = Release|x64 + {2EFA491B-EDAF-4983-A3E4-A24D014E4B6E}.Debug|x64.ActiveCfg = Debug|x64 + {2EFA491B-EDAF-4983-A3E4-A24D014E4B6E}.Debug|x64.Build.0 = Debug|x64 + {2EFA491B-EDAF-4983-A3E4-A24D014E4B6E}.Release|x64.ActiveCfg = Release|x64 + {2EFA491B-EDAF-4983-A3E4-A24D014E4B6E}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4F656977-F01D-4BBD-9E72-0FD6CD077717} + EndGlobalSection +EndGlobal diff --git a/RefreshDevice.cs b/RefreshDevice.cs new file mode 100644 index 0000000..0ddfdad --- /dev/null +++ b/RefreshDevice.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Text; +using RefreshCS; + +namespace Campari +{ + public class RefreshDevice : IDisposable + { + public IntPtr Handle { get; } + + public bool IsDisposed { get; private set; } + + public RefreshDevice( + Refresh.PresentationParameters presentationParameters, + bool debugMode + ) { + Handle = Refresh.Refresh_CreateDevice( + ref presentationParameters, + (byte) (debugMode ? 1 : 0) + ); + } + + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing) + { + // TODO: dispose managed state (managed objects) + } + + Refresh.Refresh_DestroyDevice(Handle); + IsDisposed = true; + } + } + + // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources + ~RefreshDevice() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: false); + } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +} diff --git a/Texture.cs b/Texture.cs new file mode 100644 index 0000000..287d516 --- /dev/null +++ b/Texture.cs @@ -0,0 +1,82 @@ +using System; +using RefreshCS; + +namespace Campari +{ + public class Texture : IDisposable + { + public RefreshDevice Device { get; } + public IntPtr Handle { get; } + public uint Height { get; } + public uint Width { get; } + + public bool IsDisposed { get; private set; } + + public static Texture Load(RefreshDevice device, string path) + { + var pixels = Refresh.Refresh_Image_Load(path, out var width, out var height, out var channels); + IntPtr textureHandle = Refresh.Refresh_CreateTexture2D( + device.Handle, + Refresh.ColorFormat.R8G8B8A8, + (uint) width, + (uint) height, + 1, + (uint) Refresh.TextureUsageFlagBits.SamplerBit + ); + + Refresh.TextureSlice textureSlice; + textureSlice.texture = textureHandle; + textureSlice.rectangle.x = 0; + textureSlice.rectangle.y = 0; + textureSlice.rectangle.w = width; + textureSlice.rectangle.h = height; + textureSlice.level = 0; + textureSlice.layer = 0; + textureSlice.depth = 0; + + Refresh.Refresh_SetTextureData( + device.Handle, + ref textureSlice, + pixels, + (uint) (width * height * 4) + ); + + return new Texture( + device, + textureHandle, + (uint) width, + (uint) height + ); + } + + public Texture(RefreshDevice device, IntPtr handle, uint width, uint height) + { + Device = device; + Handle = handle; + Width = width; + Height = height; + } + + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + Refresh.Refresh_QueueDestroyTexture(Device.Handle, Handle); + IsDisposed = true; + } + } + + ~Texture() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: false); + } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +} diff --git a/lib/RefreshCS b/lib/RefreshCS new file mode 160000 index 0000000..c33e62b --- /dev/null +++ b/lib/RefreshCS @@ -0,0 +1 @@ +Subproject commit c33e62b9e46f9c86c40fffe56bce461739b71bf8