Skip to content

Commit

Permalink
v0.2.0-beta.6
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikja163 committed Apr 13, 2024
1 parent 4519e25 commit 3711ea5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>3D Game Engine for C# written in C# using OpenTK bindings for OpenGL.</Description>
<PackageLicenseExpression>MIT-0</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>0.2.0-beta.5</Version>
<Version>0.2.0-beta.6</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 14 additions & 8 deletions JAngine/Rendering/Glfw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,28 +353,28 @@ public enum OpenGL
public static extern IntPtr GetProcAddress([MarshalAs(UnmanagedType.LPStr)] string procName);

[DllImport(DllName, EntryPoint = "glfwMakeContextCurrent", CallingConvention = CallingConvention.Cdecl)]
public static extern void MakeContextCurrent(Glfw.Window window);
public static extern void MakeContextCurrent(Window window);

[DllImport(DllName, EntryPoint = "glfwSwapBuffers", CallingConvention = CallingConvention.Cdecl)]
public static extern void SwapBuffers(Glfw.Window window);
public static extern void SwapBuffers(Window window);

[DllImport(DllName, EntryPoint = "glfwWindowShouldClose", CallingConvention = CallingConvention.Cdecl)]
public static extern bool WindowShouldClose(Glfw.Window window);
public static extern bool WindowShouldClose(Window window);

[DllImport(DllName, EntryPoint = "glfwSetWindowShouldClose", CallingConvention = CallingConvention.Cdecl)]
public static extern void WindowSetShouldClose(Glfw.Window window, bool value);
public static extern void WindowSetShouldClose(Window window, bool value);

[DllImport(DllName, EntryPoint = "glfwSetWindowSize", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetWindowSize(Glfw.Window window, int width, int height);
public static extern void SetWindowSize(Window window, int width, int height);

[DllImport(DllName, EntryPoint = "glfwGetWindowSize", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetWindowSize(Glfw.Window window, out int width, out int height);
public static extern void GetWindowSize(Window window, out int width, out int height);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void WindowSizeCallback(Window window, int width, int height);

[DllImport(DllName, EntryPoint = "glfwSetWindowSizeCallback", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetWindowSizeCallback(Glfw.Window window, WindowSizeCallback windowSizeCallback);
public static extern void SetWindowSizeCallback(Window window, WindowSizeCallback windowSizeCallback);

[DllImport(DllName, EntryPoint = "glfwWindowHint", CallingConvention = CallingConvention.Cdecl)]
public static extern void WindowHint(Hint hint, int value);
Expand All @@ -385,7 +385,7 @@ public enum OpenGL
public static extern void WindowHint(Hint hint, [MarshalAs(UnmanagedType.LPStr)] string value);

[DllImport(DllName, EntryPoint = "glfwCreateWindow", CallingConvention = CallingConvention.Cdecl)]
public static extern Glfw.Window CreateWindow(int width, int height, [MarshalAs(UnmanagedType.LPStr)] string title, Monitor monitor, Window share);
public static extern Window CreateWindow(int width, int height, [MarshalAs(UnmanagedType.LPStr)] string title, Monitor monitor, Window share);

[DllImport(DllName, EntryPoint = "glfwDestroyWindow", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyWindow(Window window);
Expand All @@ -402,6 +402,12 @@ public enum OpenGL
[DllImport(DllName, EntryPoint = "glfwSetKeyCallback", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetKeyCallback(Window window, KeyCallback keyCallback);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CharacterCallback(Window window, int codepoint);

[DllImport(DllName, EntryPoint = "glfwSetCharCallback", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetCharCallback(Window window, CharacterCallback keyCallback);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MouseCallback(Window window, MouseButton button, Action action, Mods mods);

Expand Down
22 changes: 22 additions & 0 deletions JAngine/Rendering/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace JAngine.Rendering;

public delegate void WindowResizeDelegate(Window window, int width, int height);
public delegate void MouseMovedDelegate(Window window, Vector2 mouseDelta);
public delegate void TextTypedDelegate(Window window, char c);

/// <summary>
/// A window used for drawing to the screen and as a context to a renderer.
Expand All @@ -24,6 +25,7 @@ public sealed class Window : IDisposable, IGuiElement
private readonly Thread _renderingThread;
private WindowResizeDelegate? _onWindowResize;
private MouseMovedDelegate? _onMouseMoved;
private TextTypedDelegate? _onTextTyped;
private int _viewportWidth;
private int _viewportHeight;
private Matrix4x4 _guiMatrix;
Expand All @@ -32,6 +34,7 @@ public sealed class Window : IDisposable, IGuiElement

private readonly Glfw.WindowSizeCallback _sizeCallback;
private readonly Glfw.KeyCallback _keyCallback;
private readonly Glfw.CharacterCallback _characterCallback;
private readonly Glfw.MouseCallback _mouseCallback;
private readonly Glfw.CursorPositionCallback _cursorPositionCallback;

Expand All @@ -57,21 +60,34 @@ static Window()
public Window(string title, int width, int height)
{
_handle = Glfw.CreateWindow(width, height, title, Glfw.Monitor.Null, Glfw.Window.Null);

_keyCallback = KeyCallback;
Glfw.SetKeyCallback(_handle, _keyCallback);
_characterCallback = CharacterCallback;
Glfw.SetCharCallback(_handle, _characterCallback);

_mouseCallback = MouseCallback;
Glfw.SetMouseCallback(_handle, _mouseCallback);
_cursorPositionCallback = CursorPositionCallback;
Glfw.SetCursorPosCallback(_handle, _cursorPositionCallback);
Glfw.GetCursorPos(_handle, out double x, out double y);
CursorPosition = new Vector2((float)x, (float)y);

_sizeCallback = SizeCallback;
Glfw.SetWindowSizeCallback(_handle, _sizeCallback);

_renderingThread = new Thread(RenderThread);
_renderingThread.IsBackground = true;
_renderingThread.Start();

s_windows.Add(this);
}

private void CharacterCallback(Glfw.Window window, int codepoint)
{
_onTextTyped?.Invoke(this, (char)codepoint);
}

public int Width { get; private set; }
public int Height { get; private set; }

Expand All @@ -96,6 +112,12 @@ public event MouseMovedDelegate? OnMouseMoved
remove => _onMouseMoved -= value;
}

public TextTypedDelegate? OnTextTyped
{
get => _onTextTyped;
set => _onTextTyped = value;
}

/// <summary>
/// Holds the thread and runs while windows are still open.
/// Checks for events for each of the windows.
Expand Down

0 comments on commit 3711ea5

Please sign in to comment.