Skip to content

Commit c82cce8

Browse files
committed
Input improvements for tutorial 8-1
1 parent 80a4c8e commit c82cce8

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

OpenTKTutorial8-1/OpenTKTutorial8/Game.cs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OpenTK;
88
using OpenTK.Graphics;
99
using OpenTK.Graphics.OpenGL;
10+
using OpenTK.Input;
1011

1112
namespace OpenTKTutorial8
1213
{
@@ -42,6 +43,8 @@ public Game()
4243
void initProgram()
4344
{
4445
lastMousePos = new Vector2(Mouse.X, Mouse.Y);
46+
CursorVisible = false;
47+
cam.MouseSensitivity = 0.0025f;
4548

4649
GL.GenBuffers(1, out ibo_elements);
4750

@@ -119,6 +122,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
119122
{
120123
base.OnUpdateFrame(e);
121124

125+
ProcessInput();
126+
122127
List<Vector3> verts = new List<Vector3>();
123128
List<int> inds = new List<int>();
124129
List<Vector3> colors = new List<Vector3>();
@@ -198,69 +203,66 @@ protected override void OnUpdateFrame(FrameEventArgs e)
198203
// Buffer index data
199204
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
200205
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw);
206+
}
201207

208+
/// <summary>
209+
/// Handles keyboard and mouse input
210+
/// </summary>
211+
private void ProcessInput()
212+
{
213+
/* We'll use the escape key to make the program easy to exit from.
214+
* Otherwise it's annoying since the mouse cursor is trapped inside.*/
215+
if (Keyboard.GetState().IsKeyDown(Key.Escape))
216+
{
217+
Exit();
218+
}
202219

203-
// Reset mouse position
204-
if (Focused)
220+
/** Let's start by adding WASD input (feel free to change the keys if you want,
221+
* hopefully a later tutorial will have a proper input manager) for translating the camera. */
222+
if (Keyboard.GetState().IsKeyDown(Key.W))
205223
{
206-
Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
207-
lastMousePos += delta;
224+
cam.Move(0f, 0.1f, 0f);
225+
}
208226

209-
cam.AddRotation(delta.X, delta.Y);
210-
ResetCursor();
227+
if (Keyboard.GetState().IsKeyDown(Key.S))
228+
{
229+
cam.Move(0f, -0.1f, 0f);
211230
}
212-
}
213231

232+
if (Keyboard.GetState().IsKeyDown(Key.A))
233+
{
234+
cam.Move(-0.1f, 0f, 0f);
235+
}
214236

215-
protected override void OnKeyPress(KeyPressEventArgs e)
216-
{
217-
base.OnKeyPress(e);
237+
if (Keyboard.GetState().IsKeyDown(Key.D))
238+
{
239+
cam.Move(0.1f, 0f, 0f);
240+
}
218241

219-
if (e.KeyChar == 27)
242+
if (Keyboard.GetState().IsKeyDown(Key.Q))
220243
{
221-
Exit();
244+
cam.Move(0f, 0f, 0.1f);
222245
}
223246

224-
switch (e.KeyChar)
247+
if (Keyboard.GetState().IsKeyDown(Key.E))
225248
{
226-
case 'w':
227-
cam.Move(0f, 0.1f, 0f);
228-
break;
229-
case 'a':
230-
cam.Move(-0.1f, 0f, 0f);
231-
break;
232-
case 's':
233-
cam.Move(0f, -0.1f, 0f);
234-
break;
235-
case 'd':
236-
cam.Move(0.1f, 0f, 0f);
237-
break;
238-
case 'q':
239-
cam.Move(0f, 0f, 0.1f);
240-
break;
241-
case 'e':
242-
cam.Move(0f, 0f, -0.1f);
243-
break;
249+
cam.Move(0f, 0f, -0.1f);
244250
}
245-
}
246251

247-
/// <summary>
248-
/// Moves the mouse cursor to the center of the screen
249-
/// </summary>
250-
void ResetCursor()
251-
{
252-
OpenTK.Input.Mouse.SetPosition(Bounds.Left + Bounds.Width / 2, Bounds.Top + Bounds.Height / 2);
253-
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
252+
if (Focused)
253+
{
254+
Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
255+
lastMousePos += delta;
256+
257+
cam.AddRotation(delta.X, delta.Y);
258+
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
259+
}
254260
}
255261

256262
protected override void OnFocusedChanged(EventArgs e)
257263
{
258264
base.OnFocusedChanged(e);
259-
260-
if (Focused)
261-
{
262-
ResetCursor();
263-
}
265+
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
264266
}
265267

266268
int loadImage(Bitmap image)

OpenTKTutorial8-1/OpenTKTutorial8/ObjVolume.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public static ObjVolume LoadFromFile(string filename)
9898
obj = LoadFromString(reader.ReadToEnd());
9999
}
100100
}
101-
catch (FileNotFoundException e)
101+
catch (FileNotFoundException)
102102
{
103103
Console.WriteLine("File not found: {0}", filename);
104104
}
105-
catch (Exception e)
105+
catch (Exception)
106106
{
107107
Console.WriteLine("Error loading file: {0}", filename);
108108
}

0 commit comments

Comments
 (0)