@@ -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
@@ -121,6 +123,8 @@ protected override void OnRenderFrame(FrameEventArgs e)
121123 protected override void OnUpdateFrame ( FrameEventArgs e )
122124 {
123125 base . OnUpdateFrame ( e ) ;
126+
127+ ProcessInput ( ) ;
124128
125129 List < Vector3 > verts = new List < Vector3 > ( ) ;
126130 List < int > inds = new List < int > ( ) ;
@@ -191,67 +195,65 @@ protected override void OnUpdateFrame(FrameEventArgs e)
191195 // Buffer index data
192196 GL . BindBuffer ( BufferTarget . ElementArrayBuffer , ibo_elements ) ;
193197 GL . BufferData ( BufferTarget . ElementArrayBuffer , ( IntPtr ) ( indicedata . Length * sizeof ( int ) ) , indicedata , BufferUsageHint . StaticDraw ) ;
198+ }
194199
200+ protected override void OnFocusedChanged ( EventArgs e )
201+ {
202+ base . OnFocusedChanged ( e ) ;
203+ lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
204+ }
195205
196- // Reset mouse position
197- if ( Focused )
206+ /// <summary>
207+ /// Handles keyboard and mouse input
208+ /// </summary>
209+ private void ProcessInput ( )
210+ {
211+ /* We'll use the escape key to make the program easy to exit from.
212+ * Otherwise it's annoying since the mouse cursor is trapped inside.*/
213+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . Escape ) )
198214 {
199- Vector2 delta = lastMousePos - new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
200- lastMousePos += delta ;
215+ Exit ( ) ;
216+ }
201217
202- cam . AddRotation ( delta . X , delta . Y ) ;
203- ResetCursor ( ) ;
218+ /** Let's start by adding WASD input (feel free to change the keys if you want,
219+ * hopefully a later tutorial will have a proper input manager) for translating the camera. */
220+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . W ) )
221+ {
222+ cam . Move ( 0f , 0.1f , 0f ) ;
204223 }
205- }
206224
207- protected override void OnKeyPress ( KeyPressEventArgs e )
208- {
209- base . OnKeyPress ( e ) ;
225+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . S ) )
226+ {
227+ cam . Move ( 0f , - 0.1f , 0f ) ;
228+ }
210229
211- if ( e . KeyChar == 27 )
230+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . A ) )
212231 {
213- Exit ( ) ;
232+ cam . Move ( - 0.1f , 0f , 0f ) ;
214233 }
215234
216- switch ( e . KeyChar )
235+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . D ) )
217236 {
218- case 'w' :
219- cam . Move ( 0f , 0.1f , 0f ) ;
220- break ;
221- case 'a' :
222- cam . Move ( - 0.1f , 0f , 0f ) ;
223- break ;
224- case 's' :
225- cam . Move ( 0f , - 0.1f , 0f ) ;
226- break ;
227- case 'd' :
228- cam . Move ( 0.1f , 0f , 0f ) ;
229- break ;
230- case 'q' :
231- cam . Move ( 0f , 0f , 0.1f ) ;
232- break ;
233- case 'e' :
234- cam . Move ( 0f , 0f , - 0.1f ) ;
235- break ;
237+ cam . Move ( 0.1f , 0f , 0f ) ;
236238 }
237- }
238239
239- /// <summary>
240- /// Moves the mouse cursor to the center of the screen
241- /// </summary>
242- void ResetCursor ( )
243- {
244- OpenTK . Input . Mouse . SetPosition ( Bounds . Left + Bounds . Width / 2 , Bounds . Top + Bounds . Height / 2 ) ;
245- lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
246- }
240+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . Q ) )
241+ {
242+ cam . Move ( 0f , 0f , 0.1f ) ;
243+ }
247244
248- protected override void OnFocusedChanged ( EventArgs e )
249- {
250- base . OnFocusedChanged ( e ) ;
245+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . E ) )
246+ {
247+ cam . Move ( 0f , 0f , - 0.1f ) ;
248+ }
251249
252250 if ( Focused )
253251 {
254- ResetCursor ( ) ;
252+ Vector2 delta = lastMousePos - new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
253+ lastMousePos += delta ;
254+
255+ cam . AddRotation ( delta . X , delta . Y ) ;
256+ lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
255257 }
256258 }
257259
@@ -280,7 +282,7 @@ int loadImage(string filename)
280282 Bitmap file = new Bitmap ( filename ) ;
281283 return loadImage ( file ) ;
282284 }
283- catch ( FileNotFoundException e )
285+ catch ( FileNotFoundException )
284286 {
285287 return - 1 ;
286288 }
0 commit comments