Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Templates for Windowing, OpenGL #748

Merged
merged 16 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions templates/Silk.NET.Templates/Silk.NET.Templates.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageType>Template</PackageType>
<PackageVersion>1.0</PackageVersion>
<PackageId>Silk.NET.Templates</PackageId>
<Title>Silk.NET Project Templates</Title>
<Authors>.NET Foundation and Contributors</Authors>
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
<Description>A package with template projects for Silk.NET.</Description>
Perksey marked this conversation as resolved.
Show resolved Hide resolved

<TargetFramework>netstandard2.0</TargetFramework>

<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<NoWarn>$(NoWarn);NU5128</NoWarn>
</PropertyGroup>

<ItemGroup>
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" />
<Compile Remove="**\*" />
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkGLGame",
"name": "Silk.NET Game (OpenGL)",
"shortName": "silkglgame",
Perksey marked this conversation as resolved.
Show resolved Hide resolved
"description": "A Silk.NET Window with user input and an OpenGL Context.",
"tags": {
"language": "C#",
"type": "project"
}
}
41 changes: 41 additions & 0 deletions templates/Silk.NET.Templates/templates/silkglgame/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Silk.NET.Windowing;
using Silk.NET.Input;
using Silk.NET.OpenGL;

WindowOptions windowOptions = WindowOptions.Default;
windowOptions.Title = "My Silk.NET Window";
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
using IWindow window = Window.Create(windowOptions);
IInputContext inputContext = null!;
GL gl = null!;

window.Load += () =>
{
// ran on first startup - use this event to initialize stuff.
gl = window.CreateOpenGL();
inputContext = window.CreateInput();
Perksey marked this conversation as resolved.
Show resolved Hide resolved
};

window.Update += deltaSeconds =>
{
// ran every frame but before render - use this event to update data (e.g. physics).
};

window.Render += deltaSeconds =>
{
// ran every frame but after update - use this event to draw.
};

window.FramebufferResize += newSize =>
{
// ran when the window is resized - usually used to update the viewport and, in 3D apps, view matrices.
gl.Viewport(newSize);
};

window.Closing += () =>
{
// ran just before the window closes.
};

window.Run();
gl.Dispose();
inputContext.Dispose();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Silk.NET.Windowing" Version="2.*" />
<PackageReference Include="Silk.NET.Input" Version="2.*" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.*" />
Perksey marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkGLTriangle",
"name": "Silk.NET Triangle (OpenGL)",
"shortName": "silkgltriangle",
"description": "A Silk.NET Window with an OpenGL Context drawing a triangle.",
"tags": {
"language": "C#",
"type": "project"
}
}
135 changes: 135 additions & 0 deletions templates/Silk.NET.Templates/templates/silkgltriangle/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System;
using System.Numerics;
using Silk.NET.Windowing;
using Silk.NET.OpenGL;

WindowOptions windowOptions = WindowOptions.Default;
windowOptions.Title = "My Silk.NET Triangle";
windowOptions.PreferredDepthBufferBits = 0;
windowOptions.PreferredStencilBufferBits = 0;
windowOptions.API = new GraphicsAPI(ContextAPI.OpenGL, new APIVersion(3, 3));
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
using IWindow window = Window.Create(windowOptions);
GL gl = null!;

uint vbo = 0;
uint vao = 0;
uint shader = 0;
Perksey marked this conversation as resolved.
Show resolved Hide resolved

const string VertexShaderCode = @"
#version 330 core
layout (location = 0) in vec3 vPosition;
layout (location = 1) in vec4 vColor;

out vec4 fColor;

void main() {
gl_Position = vec4(vPosition, 1.0);
fColor = vColor;
}";

const string FragmentShaderCode = @"
#version 330 core
in vec4 fColor;

out vec4 FragColor;

void main() {
FragColor = fColor;
}";

window.Load += () =>
{
// ran on first startup - use this event to initialize stuff.
gl = window.CreateOpenGL();

vbo = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);

Span<Vertex> vertexData = stackalloc Vertex[]
{
new Vertex(new Vector3(-0.5f, -0.5f, 0), 255, 0, 0, 255),
new Vertex(new Vector3(0, 0.5f, 0), 0, 255, 0, 255),
new Vertex(new Vector3(0.5f, -0.5f, 0), 0, 0, 255, 255)
};

gl.BufferData<Vertex>(BufferTargetARB.ArrayBuffer, vertexData, BufferUsageARB.StaticDraw);

vao = gl.GenVertexArray();
gl.BindVertexArray(vao);
unsafe
{
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.Size, (void*)0);
gl.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, true, Vertex.Size, (void*)12);
}
gl.EnableVertexAttribArray(0);
gl.EnableVertexAttribArray(1);

uint vs = gl.CreateShader(ShaderType.VertexShader);
gl.ShaderSource(vs, VertexShaderCode);
gl.CompileShader(vs);

uint fs = gl.CreateShader(ShaderType.FragmentShader);
gl.ShaderSource(fs, FragmentShaderCode);
gl.CompileShader(fs);

shader = gl.CreateProgram();
gl.AttachShader(shader, vs);
gl.AttachShader(shader, fs);
gl.LinkProgram(shader);
gl.DetachShader(shader, vs);
gl.DetachShader(shader, fs);
gl.DeleteShader(vs);
gl.DeleteShader(fs);

gl.Viewport(window.Size);
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
};

window.Update += deltaSeconds =>
{
// ran every frame but before render - use this event to update data (e.g. physics).
};

window.Render += deltaSeconds =>
{
// ran every frame but after update - use this event to draw.
gl.ClearColor(0f, 0f, 0f, 1f);
gl.Clear(ClearBufferMask.ColorBufferBit);

gl.BindVertexArray(vao);
gl.UseProgram(shader);
gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
};

window.FramebufferResize += newSize =>
{
// ran when the window is resized - usually used to update the viewport and, in 3D apps, view matrices.
gl.Viewport(newSize);
};

window.Closing += () =>
{
gl.DeleteProgram(shader);
gl.DeleteVertexArray(vao);
gl.DeleteBuffer(vbo);
// ran just before the window closes.
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
};

window.Run();
gl.Dispose();

struct Vertex
{
public static uint Size = 3 * 4 + 4;

Vector3 Position;
byte R, G, B, A;

public Vertex(Vector3 position, byte r, byte g, byte b, byte a)
{
Position = position;
R = r;
G = g;
B = b;
A = a;
}
}
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Silk.NET.Windowing" Version="2.*" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkGLWindow",
"name": "Silk.NET Window (OpenGL)",
"shortName": "silkglwindow",
"description": "A Silk.NET Window with an OpenGL Context.",
"tags": {
"language": "C#",
"type": "project"
}
}
37 changes: 37 additions & 0 deletions templates/Silk.NET.Templates/templates/silkglwindow/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Silk.NET.Windowing;
using Silk.NET.OpenGL;

WindowOptions windowOptions = WindowOptions.Default;
windowOptions.Title = "My Silk.NET Window";
using IWindow window = Window.Create(windowOptions);
GL gl = null!;

window.Load += () =>
{
// ran on first startup - use this event to initialize stuff.
gl = window.CreateOpenGL();
};

window.Update += deltaSeconds =>
{
// ran every frame but before render - use this event to update data (e.g. physics).
};

window.Render += deltaSeconds =>
{
// ran every frame but after update - use this event to draw.
};

window.FramebufferResize += newSize =>
{
// ran when the window is resized - usually used to update the viewport and, in 3D apps, view matrices.
gl.Viewport(newSize);
};

window.Closing += () =>
{
// ran just before the window closes.
};

window.Run();
gl.Dispose();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
ThomasMiz marked this conversation as resolved.
Show resolved Hide resolved

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

<ItemGroup>
<PackageReference Include="Silk.NET.Windowing" Version="2.*" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/template",
"author": ".NET Foundation and Contributors",
"classifications": [ "Silk.NET" ],
"identity": "Silk.NET.Templates.SilkWindow",
"name": "Silk.NET Window (Empty)",
"shortName": "silkwindow",
"description": "An empty Silk.NET Window.",
"tags": {
"language": "C#",
"type": "project"
}
}
32 changes: 32 additions & 0 deletions templates/Silk.NET.Templates/templates/silkwindow/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Silk.NET.Windowing;

WindowOptions windowOptions = WindowOptions.Default;
windowOptions.Title = "My Silk.NET Window";
using IWindow window = Window.Create(windowOptions);

window.Load += () =>
{
// ran on first startup - use this event to initialize stuff.
};

window.Update += deltaSeconds =>
{
// ran every frame but before render - use this event to update data (e.g. physics).
};

window.Render += deltaSeconds =>
{
// ran every frame but after update - use this event to draw,.
};

window.FramebufferResize += newSize =>
{
// ran when the window is resized - usually used to update the viewport and, in 3D apps, view matrices.
};

window.Closing += () =>
{
// ran just before the window closes.
};

window.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Silk.NET.Windowing" Version="2.*" />
</ItemGroup>

</Project>