Skip to content

Commit 48e1b54

Browse files
committed
Fix input code for tutorial 6
1 parent 498e1d7 commit 48e1b54

File tree

1 file changed

+46
-44
lines changed
  • OpenTKTutorial6/OpenTKTutorial6

1 file changed

+46
-44
lines changed

OpenTKTutorial6/OpenTKTutorial6/Game.cs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public Game()
3737
void initProgram()
3838
{
3939
lastMousePos = new Vector2(Mouse.X, Mouse.Y);
40+
CursorVisible = false;
41+
cam.MouseSensitivity = 0.0025f;
4042

4143
GL.GenBuffers(1, out ibo_elements);
4244

@@ -113,6 +115,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
113115
{
114116
base.OnUpdateFrame(e);
115117

118+
ProcessInput();
119+
116120
List<Vector3> verts = new List<Vector3>();
117121
List<int> inds = new List<int>();
118122
List<Vector3> colors = new List<Vector3>();
@@ -182,68 +186,66 @@ protected override void OnUpdateFrame(FrameEventArgs e)
182186
// Buffer index data
183187
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
184188
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw);
189+
}
185190

191+
/// <summary>
192+
/// Handles keyboard and mouse input
193+
/// </summary>
194+
private void ProcessInput()
195+
{
196+
/* We'll use the escape key to make the program easy to exit from.
197+
* Otherwise it's annoying since the mouse cursor is trapped inside.*/
198+
if (Keyboard.GetState().IsKeyDown(Key.Escape))
199+
{
200+
Exit();
201+
}
186202

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

193-
cam.AddRotation(delta.X, delta.Y);
194-
ResetCursor();
210+
if (Keyboard.GetState().IsKeyDown(Key.S))
211+
{
212+
cam.Move(0f, -0.1f, 0f);
195213
}
196-
}
197214

198-
protected override void OnKeyPress(KeyPressEventArgs e)
199-
{
200-
base.OnKeyPress(e);
215+
if (Keyboard.GetState().IsKeyDown(Key.A))
216+
{
217+
cam.Move(-0.1f, 0f, 0f);
218+
}
201219

202-
if (e.KeyChar == 27)
220+
if (Keyboard.GetState().IsKeyDown(Key.D))
203221
{
204-
Exit();
222+
cam.Move(0.1f, 0f, 0f);
205223
}
206224

207-
switch (e.KeyChar)
225+
if (Keyboard.GetState().IsKeyDown(Key.Q))
208226
{
209-
case 'w':
210-
cam.Move(0f, 0.1f, 0f);
211-
break;
212-
case 'a':
213-
cam.Move(-0.1f, 0f, 0f);
214-
break;
215-
case 's':
216-
cam.Move(0f, -0.1f, 0f);
217-
break;
218-
case 'd':
219-
cam.Move(0.1f, 0f, 0f);
220-
break;
221-
case 'q':
222-
cam.Move(0f, 0f, 0.1f);
223-
break;
224-
case 'e':
225-
cam.Move(0f, 0f, -0.1f);
226-
break;
227+
cam.Move(0f, 0f, 0.1f);
227228
}
228-
}
229229

230-
/// <summary>
231-
/// Moves the mouse cursor to the center of the screen
232-
/// </summary>
233-
void ResetCursor()
234-
{
235-
OpenTK.Input.Mouse.SetPosition(Bounds.Left + Bounds.Width / 2, Bounds.Top + Bounds.Height / 2);
236-
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
230+
if (Keyboard.GetState().IsKeyDown(Key.E))
231+
{
232+
cam.Move(0f, 0f, -0.1f);
233+
}
234+
235+
if (Focused)
236+
{
237+
Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
238+
lastMousePos += delta;
239+
240+
cam.AddRotation(delta.X, delta.Y);
241+
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
242+
}
237243
}
238244

239245
protected override void OnFocusedChanged(EventArgs e)
240246
{
241247
base.OnFocusedChanged(e);
242-
243-
if (Focused)
244-
{
245-
ResetCursor();
246-
}
248+
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
247249
}
248250

249251
int loadImage(Bitmap image)

0 commit comments

Comments
 (0)