Skip to content

Commit c1d1303

Browse files
feat: adding an error log system
1 parent 489c282 commit c1d1303

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

FreeFrame/Helper.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using OpenTK.Graphics.OpenGL4;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace FreeFrame
11+
{
12+
static class Helper
13+
{
14+
private static DebugProc _debugProcCallback = DebugCallback;
15+
static public void CheckErrors()
16+
{
17+
ErrorCode errorCode = GL.GetError();
18+
19+
while (errorCode != ErrorCode.NoError)
20+
{
21+
Console.WriteLine(errorCode.ToString());
22+
errorCode = GL.GetError();
23+
}
24+
}
25+
[DebuggerStepThrough]
26+
static private void DebugCallback(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr message, IntPtr userParam)
27+
{
28+
string messageString = Marshal.PtrToStringAnsi(message, length); // Retrieve the string from the pointer
29+
30+
Console.WriteLine($"{severity} {type} | {messageString}");
31+
32+
if (type == DebugType.DebugTypeError)
33+
throw new Exception("OpenGL error");
34+
}
35+
static public void DebugMode()
36+
{
37+
GL.DebugMessageCallback(_debugProcCallback, IntPtr.Zero);
38+
GL.Enable(EnableCap.DebugOutput);
39+
GL.Enable(EnableCap.DebugOutputSynchronous);
40+
}
41+
42+
}
43+
}

FreeFrame/Shader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public Shader(string uriVertexShader, string uriFragementShader)
4545
GL.DeleteShader(vertexShader);
4646
GL.DeleteShader(fragmentShader);
4747
}
48+
public int GetUniformLocation(string uniform) => GL.GetUniformLocation(_program, uniform);
4849
public void Use() => GL.UseProgram(_program);
4950
~Shader() => GL.DeleteProgram(_program);
5051
}

FreeFrame/Shaders/shader.frag

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
layout(location = 0) out vec4 color;
44

5+
uniform vec4 u_Color;
6+
57
void main()
68
{
7-
color = vec4(0.5, 0.5, 0.5, 1.0);
9+
color = u_Color;
810
}

FreeFrame/Window.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ protected override void OnLoad()
3939
{
4040
base.OnLoad();
4141

42+
Helper.DebugMode();
43+
4244
GL.ClearColor(0.1f, 0.1f, 0.1f, 1.0f);
4345

4446
// Vertex Buffer Object
@@ -95,8 +97,12 @@ protected override void OnRenderFrame(FrameEventArgs e)
9597
_shader.Use(); // Select current shader
9698
GL.BindVertexArray(_vertexArray); // Bind VAO for workspace (because ImGui binds another)
9799

100+
int uColorLocation = _shader.GetUniformLocation("u_Color");
101+
GL.Uniform4(uColorLocation, 0.2f, 1.0f, 0.5f, 1.0f);
102+
98103
GL.DrawElements(PrimitiveType.Triangles, _indices.Length, DrawElementsType.UnsignedInt, 0);
99104

105+
100106
_ImGuiController.Update(this, (float)e.Time); // TODO: Explain what's the point of this. Also explain why this order is necessary
101107
//ImGui.ShowDemoWindow();
102108
UI.Show(this);

0 commit comments

Comments
 (0)