16.8 Input


16.8 Input

Input is data given by a user. It often occurs through the mouse or keyboard, such as a mouse click or keyboard press. Input is important if a game intends to respond to user commands. ClanLib offers two classes to read input: a keyboard class and a mouse class.

16.8.1 Reading Input from the Keyboard

To read information from the keyboard, such as which key the user is currently holding down or which key is not being pressed, the CL_Keyboard class is used. Specifically, to determine the status of any key, the get_keycode method should be called. This method returns true or false, depending on whether or not a key is held down. True is returned if the key is pressed, and false otherwise.

      //if Escape is not being pressed...      If(!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))      {           //Do something here      } 

16.8.2 Reading Input from the Mouse

Aside from the mouse scroll wheel, there are essentially two fields of information that can be read from the mouse: cursor position and button status. Cursor position refers to the (x, y) coordinate of the mouse pointer on the screen. The button status refers to whether the left, middle (if there is one), or right mouse button is pressed. The mouse is encapsulated into the CL_Mouse class.

16.8.2.1 Determine Cursor Position

To retrieve the x and y locations of the cursor, the get_x and get_y methods can be called. Consider the following code:

      int Xpos = CL_Mouse::get_x(),      int YPos = CL_Mouse::get_y(); 

16.8.2.2 Determine Button Status

Testing whether or not a specified mouse button is pressed is much like determining whether a key on the keyboard is pressed. Here, the get_keycode method is called.

      //If left mouse button pressed      if(CL_Mouse::get_keycode(CL_MOUSE_LEFT))      {         //do something here      } 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net