Skip to content

Commit

Permalink
Input example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vercidium committed Dec 29, 2024
1 parent 21a0b1c commit 02477e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
26 changes: 21 additions & 5 deletions Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public Client()
// Precalculate input stuff
inputContext = window.CreateInput();
keyboard = inputContext.Keyboards[0];
mouse = inputContext.Mice[0];
mouse.DoubleClickTime = 1;
keyboard.KeyDown += OnKeyDown;
keyboard.KeyUp += OnKeyUp;
};

window.Render += (_) => Render();
Expand All @@ -37,14 +37,12 @@ public Client()
window.UpdatesPerSecond = 144;
window.VSync = false;

// Initialise OpenGL and input context
// Initialise OpenGL and the input context
window.Initialize();
}


// Silk
IWindow window;
IMouse mouse;
IKeyboard keyboard;
IInputContext inputContext;

Expand All @@ -66,6 +64,24 @@ void OnDidCreateOpenGLContext()
Console.WriteLine($"OpenGL Version: {version}");
}

public void OnKeyDown(IKeyboard keyboard, Key key, int something)
{
OnKeyEvent(new KeyEvent()
{
IsPress = true,
KeyCode = key,
});
}

public void OnKeyUp(IKeyboard keyboard, Key key, int something)
{
OnKeyEvent(new KeyEvent()
{
IsPress = false,
KeyCode = key,
});
}

void Render()
{
// Prepare OpenGL
Expand Down
24 changes: 21 additions & 3 deletions ClientDrawing.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
using SkiaSharp;
using Silk.NET.Input;
using SkiaSharp;

namespace drawing;

public unsafe partial class Client
{
public void Draw()
SKRect rectangle = new(100, 100, 400, 400);

void OnKeyEvent(KeyEvent key)
{
// Move the rectangle 10px to the right when pressing the right key
if (key.IsPress && key.KeyCode == Key.Right)
{
var offset = 10;

// Move faster if shift is held
if (keyboard.IsKeyPressed(Key.ShiftLeft))
offset *= 5;

rectangle.Offset(offset, 0);
}
}

void Draw()
{
// Colours
var red = new SKColor(255, 0, 0);
Expand All @@ -17,7 +35,7 @@ public void Draw()


// Blue rectangle
bitmap.FillRectangle(new SKRect(100, 100, 400, 400), blue);
bitmap.FillRectangle(rectangle, blue);


// Red sin wave
Expand Down
9 changes: 9 additions & 0 deletions KeyEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Silk.NET.Input;

namespace drawing;

internal class KeyEvent
{
public bool IsPress;
public Key KeyCode;
}

0 comments on commit 02477e1

Please sign in to comment.