Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 130 additions & 99 deletions Silk.NET.sln

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions examples/Triangle.Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Triangle.OpenGL;

namespace Triangle.Desktop
{
class Program
{
static void Main(string[] args)
{
TriangleDesktop.Run();
}
}
}
12 changes: 12 additions & 0 deletions examples/Triangle.Desktop/Triangle.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Triangle\Triangle.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using Silk.NET.Windowing;
using Silk.NET.Windowing.Common;

namespace Triangle
namespace Triangle.OpenGL
{
public static class Program
public static class TriangleDesktop
{
private static readonly float[] _vertices =
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public static class Program
" gl_Position = vec4(aPosition, 1.0);\n" +
"}\n";

public static void Main(string[] args)
public static void Run()
{
_window = Window.Create(WindowOptions.Default);
_window.Load += Load;
Expand Down
124 changes: 124 additions & 0 deletions examples/Triangle/OpenGLES/TriangleMobile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Drawing;
using Silk.NET.Input;
using Silk.NET.Input.Common;
using Silk.NET.OpenGLES;
using Silk.NET.Windowing;
using Silk.NET.Windowing.Common;

namespace Triangle.OpenGLES
{
public static class TriangleMobile
{
private static readonly float[] _vertices =
{
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};

private static uint _vertexBufferObject;
private static uint _vertexArrayObject;
private static uint _shader;
private static GL _gl;
private static IInputContext _input;
private static IWindow _window;

private const string VertexShader = "#version 330\n\n" +

"out vec4 outputColor;\n\n" +

"void main()\n" +
"{\n" +
" outputColor = vec4(1.0, 1.0, 0.0, 1.0);\n" +
"}\n";

private const string FragmentShader = "#version 330 core\n\n" +

"layout(location = 0) in vec3 aPosition;\n\n" +

"void main(void)\n" +
"{\n" +
" gl_Position = vec4(aPosition, 1.0);\n" +
"}\n";

public static void Run()
{
_window = Window.Create(WindowOptions.Default);
_window.Load += Load;
_window.Render += RenderFrame;
_window.Update += UpdateFrame;
_window.Resize += Resize;
_window.Run();
End();
}

private static unsafe void Load()
{
_gl ??= GL.GetApi();
var vertShader = _gl.CreateShader(GLEnum.VertexShader);
var fragShader = _gl.CreateShader(GLEnum.FragmentShader);
_gl.ShaderSource(vertShader, VertexShader);
_gl.ShaderSource(fragShader, FragmentShader);
_gl.CompileShader(vertShader);
_gl.CompileShader(fragShader);
_shader = _gl.CreateProgram();
_gl.AttachShader(_shader, vertShader);
_gl.AttachShader(_shader, fragShader);
_gl.LinkProgram(_shader);
_gl.DetachShader(_shader, vertShader);
_gl.DetachShader(_shader, fragShader);
_gl.DeleteShader(fragShader);
_gl.DeleteShader(vertShader);
_gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
_vertexBufferObject = _gl.GenBuffer();
_gl.BindBuffer(GLEnum.ArrayBuffer, _vertexBufferObject);
fixed (void* vertices = _vertices)
{
_gl.BufferData(GLEnum.ArrayBuffer, (uint) _vertices.Length * sizeof(float), vertices, GLEnum.StaticDraw);
}

_vertexArrayObject = _gl.GenVertexArray();
_gl.BindVertexArray(_vertexArrayObject);
_gl.VertexAttribPointer(0, 3, GLEnum.Float, false, 3 * sizeof(float), 0);
_gl.EnableVertexAttribArray(0);
_gl.BindBuffer(GLEnum.ArrayBuffer, _vertexBufferObject);
Console.WriteLine("done load");
}


private static void RenderFrame(double delta)
{
_gl.Clear((uint)GLEnum.ColorBufferBit);
_gl.UseProgram(_shader);
_gl.BindVertexArray(_vertexArrayObject);
_gl.DrawArrays(GLEnum.Triangles, 0, 3);
}


private static void UpdateFrame(double delta)
{
_input ??= _window.GetInput();
if (_input.Keyboards[0].IsKeyPressed(Key.Escape))
{
_window.Close();
}
}

private static void Resize(Size size)
{
_gl.Viewport(0, 0, (uint) size.Width, (uint) size.Height);
Console.WriteLine("done resize");
}

private static void End()
{
_gl.BindBuffer(GLEnum.ArrayBuffer, 0);
_gl.BindVertexArray(0);
_gl.UseProgram(0);
_gl.DeleteBuffer(_vertexBufferObject);
_gl.DeleteVertexArray(_vertexArrayObject);
_gl.DeleteProgram(_shader);
}
}
}
4 changes: 2 additions & 2 deletions examples/Triangle/Triangle.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3</TargetFramework>
<TargetFrameworks>netstandard2</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>8</LangVersion>
</PropertyGroup>
Expand All @@ -11,6 +10,7 @@
<ProjectReference Include="..\..\src\Input\Silk.NET.Input.Common\Silk.NET.Input.Common.csproj" />
<ProjectReference Include="..\..\src\Input\Silk.NET.Input\Silk.NET.Input.csproj" />
<ProjectReference Include="..\..\src\OpenGL\Silk.NET.OpenGL\Silk.NET.OpenGL.csproj" />
<ProjectReference Include="..\..\src\OpenGL\Silk.NET.OpenGLES\Silk.NET.OpenGLES.csproj" />
<ProjectReference Include="..\..\src\Windowing\Silk.NET.Windowing\Silk.NET.Windowing.csproj" />
</ItemGroup>

Expand Down
24 changes: 24 additions & 0 deletions src/Windowing/Silk.NET.Windowing.Android/ANativeWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

namespace Silk.NET.Windowing.Android
{
/// <summary>
/// Opaque handle to an Android Native Window.
/// <remarks>
/// A pointer can be obtained using <see cref="Android.CreateNativeWindow"/>
/// </remarks>
/// </summary>
public struct ANativeWindow
{
/// <summary>
/// Struct that represents a window's buffer.
/// </summary>
public struct Buffer
{

}
}
}
11 changes: 11 additions & 0 deletions src/Windowing/Silk.NET.Windowing.Android/ARect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.
namespace Silk.NET.Windowing.Android
{
public struct ARect
{
public int Bottom, Left, Right, Top;
}
}
117 changes: 117 additions & 0 deletions src/Windowing/Silk.NET.Windowing.Android/Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Silk.NET.Windowing.Android
{
public static class Android
{
/// <summary>
/// Acquire a reference on the given <see cref="ANativeWindow"/> object. This prevents the object from being
/// deleted until the reference is removed.
/// </summary>
/// <param name="window"></param>
[DllImport("android", EntryPoint = "ANativeWindow_acquire")]
public static extern unsafe void Acquire(ANativeWindow* window);

/// <summary>
/// Return the ANativeWindow associated with a Java Surface object, for interacting with it through native code.
/// </summary>
/// <param name="jni">The JNI environment.</param>
/// <param name="surface">The Java surface.</param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_fromSurface")]
public static extern unsafe ANativeWindow* CreateNativeWindow(IntPtr jni, IntPtr surface);

/// <summary>
/// Get the dataspace of buffers in window.
/// </summary>
/// <param name="window">The window to get the dataspace of.</param>
/// <returns>The dataspace.</returns>
[DllImport("android", EntryPoint = "ANativeWindow_getBuffersDataSpace")]
public static extern unsafe int GetBuffersDataSpace(ANativeWindow* window);

/// <summary>
/// Gets the current pixel format of the window surface.
/// </summary>
/// <param name="anw"></param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_getFormat")]
public static extern unsafe int GetFormat(ANativeWindow* anw);

/// <summary>
/// Gets the current width in pixels of the window surface.
/// </summary>
/// <param name="anw"></param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_getWidth")]
public static extern unsafe int GetWidth(ANativeWindow* anw);

/// <summary>
/// Gets the current height in pixels of the window surface.
/// </summary>
/// <param name="anw"></param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_getHeight")]
public static extern unsafe int GetHeight(ANativeWindow* anw);

/// <summary>
/// Lock the window's next drawing surface for writing.
/// </summary>
/// <param name="anw">The window.</param>
/// <param name="outBuffer">The output buffer.</param>
/// <param name="inOutDirtyBounds">The region the caller intends to redraw.</param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_lock")]
public static extern unsafe int LockWindow(ANativeWindow* anw, ANativeWindow.Buffer* outBuffer, ARect* inOutDirtyBounds);

/// <summary>
/// Remove a reference that was previously acquired with <see cref="Acquire"/>.
/// </summary>
/// <param name="window"></param>
[DllImport("android", EntryPoint = "ANativeWindow_release")]
public static extern unsafe void ReleaseNativeWindow(ANativeWindow* window);

/// <summary>
/// All buffers queued after this call will be associated with the dataSpace parameter specified.
/// </summary>
/// <param name="window">The window.</param>
/// <param name="dataSpace">Data space of all buffers queued after this call.</param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_setBuffersDataSpace")]
public static extern unsafe int SetBuffersDataSpace(ANativeWindow* window, int dataSpace);

/// <summary>
/// Change the format and size of the window buffers.
/// </summary>
/// <param name="window">The window.</param>
/// <param name="width">The new width.</param>
/// <param name="height">The new height.</param>
/// <param name="format">The new format.</param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_setBuffersGeometry")]
public static extern unsafe int SetBuffersGeometry(ANativeWindow* window, int width, int height, int format);

/// <summary>
/// Set a transform that will be applied to future buffers posted to the window.
/// </summary>
/// <param name="window"></param>
/// <param name="transform"></param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_setBuffersTransform")]
public static extern unsafe int SetBuffersTransform(ANativeWindow* window, int transform);

/// <summary>
/// Unlock the window's drawing surface after previously locking it, posting the new buffer to the display.
/// </summary>
/// <param name="anw"></param>
/// <returns></returns>
[DllImport("android", EntryPoint = "ANativeWindow_unlockAndPost")]
public static extern unsafe int SwapBuffers(ANativeWindow* anw);
}
}
36 changes: 36 additions & 0 deletions src/Windowing/Silk.NET.Windowing.Android/AndroidPlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Android.App;
using Silk.NET.Windowing.Common;

namespace Silk.NET.Windowing.Android
{
public class AndroidPlatform : IWindowPlatform
{
public bool IsApplicable => Activity != null;
public static Activity Activity { get; set; }
public static AndroidWindow Current { get; set; }

public IWindow GetWindow(WindowOptions options)
{
if (Current != null)
{
throw new NotSupportedException("Multiple windows are not supported on Android.");
}

if (Activity == null)
{
throw new InvalidOperationException
(
"AndroidPlatform has not been initialized. To initialize it, pass the current Activity " +
"as an argument to Window.Init(Activity)"
);
}
return Current = new AndroidWindow(Activity, options);
}

public static void Init(Activity activity)
{
Activity = activity;
}
}
}
Loading