Tuesday, December 20, 2011

Free Stuff

This title should get a lot of attention.

 A list of free game libraries : Free Game Libraries 

You are still here instead of going to the link.
Even though the list hasnt been updated in a long time, its still the most complete list I have found. There are a ton of entries, and I would say it is pretty comprehensive. It would be nice, if it mentioned if the link/library was still active but you cant win them all. This site breaks entries down into their general category ( 3D Graphics, 2D Graphics, Sound/Music, Networking, Video, Compression, Artificial Intelligence, Math/Physics, Scripting, etc. ), each entry contains a link to the library as well as the license it was released under

Monday, December 12, 2011

Control Control Control

I recently started coding a generic UI library which could be easily added to Mpong or to any other Game that I decided to make in the future.
So far, I have added the following controls

  • A base control from which all the controls will inherit. This will obviously contain all the common code
  • Window Control : A basic container for all the other controls
  • Label Control - For all those static pieces of text that just need to be displayed
  • Button Control - Press Me, Press Me
  • TextBox Control - User can finally enter text. Yay
  • CheckBox Control - Am I true or not?
These seemed to be the basic controls that I needed.  Currently, they are working fine in my test project. Hopefully, they will continue to work as planned once I have integrated them into the main project

I am still deciding, whether I want to spend time adding more controls like ListBoxes etc or not at this time.
I will be detailing out the workings of the controls over the next few updates.

Thursday, October 20, 2011

Tutorial 6: Using Mouse and DirectInput

The 2nd part of the user input tutorial deals with using the mouse.
   1: LPDIRECTINPUTDEVICE8    m_pdInputMouse;
2: bool m_bPressedButtons[4];
3: DIMOUSESTATE m_MouseState;
4: long m_lPosX;
5: long m_lPosY;
Most of the variables are self explanatory.

Like in the last tutorial, we will have to make sure the DirectInput Interface is created before we can bind the mouse.
1: void cInput::CreateMouse()
2: {
3: // create the mouse device
4: m_pdInput->CreateDevice(GUID_SysMouse,&m_pdInputMouse, NULL);
5: 
6: // set the data format to mouse format
7: m_pdInputMouse->SetDataFormat(&c_dfDIMouse);
8: 
9: // set the control over the mouse
10: m_pdInputMouse->SetCooperativeLevel(m_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
11: 
12: // get the current position of the cursor
13: POINT pt;
14: GetCursorPos( &pt );
15: ScreenToClient( m_hWnd, &pt );
16: m_lPosX = pt.x;
17: m_lPosY = pt.y;
18: 
19: DetectMouseMovement();
20: }
The CreateMouse is used to get control over the mouse. We first create the mouse device by calling CreateDevice. We set the data format to mouse format. To get control over the mouse we now make a call to SetCooperativeLevel and set the flags DISCL_EXCLUSIVE and DISCL_FOREGROUND. This basically sets the access level to exclusive (i.e other applications can not access the mouse) and foreground access. By foreground access we mean that the device is automatically un-acquired when the game loses focus. We then get the current position of the cursor and use the ScreenToClient function to convert the screen coordinates to client-area coordinates.
1: void cInput::DetectMouseMovement()
2: {
3: m_pdInputMouse->Acquire();
4:
5: m_pdInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), LPVOID(&m_MouseState));
6: 
7: m_lPosX += GetMouseXDelta();
8: m_lPosY += GetMouseYDelta();
9: 
10: ConstrainMouseCursor();
11: 
12: // Get pressed keys
13: for ( int i = 0; i < 4; i++ )
14: {
15: if ( m_MouseState.rgbButtons[i] &0x80 )
16: {
17: m_bPressedButtons[i] = TRUE;
18: 
19: }
20: else
21: {
22: m_bPressedButtons[i] = FALSE;
23: 
24: }
25:
26: }
27: }
The DetectMouseMovement function detects the mouse movement and which buttons have been pressed on the mouse. For this we first acquire the mouse and get the device state. We update the current position of the cursor using the deltas of the mousestate. We constrain the mouseposition to the client area.  Finally, we run through all the buttons and check which ones have been pressed.
1: void cInput::Cleanup()
2: {
3: m_pdInputMouse->Unacquire();
4: m_pdInputMouse->Release();
5: }
Lastly, we have a Cleanup function, which releases the input devices That's all that we need to use the mouse in our game.

Friday, October 14, 2011

Tutorial 5: Using Keyboard and DirectInput

User Input is one of the most important aspects of a game. In this tutorial we will look at using the keyboard through DirectInput
   1: LPDIRECTINPUT8          m_pdInput;
2: LPDIRECTINPUTDEVICE8 m_pdInputKeyboard;
3: BYTE m_cKeyState[256];
4: bool m_bPressedKeys[256];
5: bool m_bLockedKeys[256];
6: HWND m_hWnd;
7: UINT m_iHeight;
8: UINT m_iWidth;
m_pdInput is the input object
m_pdInputKeyboard is the keyboard device
m_cKeyState[] stores the pressed keys on the keyboard
m_bPressedKeys[] is the array of pressed keys on the keyboard
m_bLockedKeys[256] is the array of locked keys on the keyboard
m_hWnd is the handle to the window
1: void cInput::Init( const HINSTANCE hInst,
2: const HWND hWnd,
3: const UINT iTableWidth /*= 0 */,
4: const UINT iTableHeight/*= 0*/ )
5: {
6: m_hWnd = hWnd;
7: 
8: // create the DirectInput Interface
9: DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pdInput, NULL);
10:
  11:     CreateKeyboard();
  12:     ...
  13: }
We first need to create the input object by calling DirectInput8Create()
   1: void cInput::CreateKeyboard()
   2: {
   3:     // create the keyboard device
   4:     m_pdInput->CreateDevice(GUID_SysKeyboard,&m_pdInputKeyboard, NULL);
   5:  
   6:     // set the data format to keyboard format
   7:     m_pdInputKeyboard->SetDataFormat(&c_dfDIKeyboard);
   8:  
   9:     // set the control over the keyboard
  10:     m_pdInputKeyboard->SetCooperativeLevel(m_hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
  11:     DetectKeys();
  12: }
The CreateKeyboard is used to get control over the keyboad. We first create the keyboard device by calling CreateDevice. We set the data format to keyboard format. To get control over the keyboard we now make a call to SetCooperativeLevel and set the flags DISCL_NONEXCLUSIVE and DISCL_FOREGROUND. This basically sets the access level to non-exclusive (i.e other applications can also access the keyboard) and foreground access. By foreground access we mean that the device is automatically un-acquired when the game loses focus.
   1: void cInput::LockKey( const DWORD dwKey )
   2: {
   3:     m_bLockedKeys[dwKey] = true;
   4: }
The LockKey function is used to lock a key on the keyboard so that it is read only once per key press
   1: void cInput::DetectKeys()
   2: {
   3:     m_pdInputKeyboard->Acquire();
   4:  
   5:     m_pdInputKeyboard->GetDeviceState(256, (LPVOID)m_cKeyState);
   6:  
   7:     for (unsigned int iKey=0;iKey<256;iKey++)
   8:     {
   9:         if (m_cKeyState[iKey] & 0x80)
  10:         {
  11:             // key is pressed if it isnt locked
  12:             m_bPressedKeys[iKey] = !(m_bLockedKeys[iKey]);
  13:         }
  14:         else
  15:         {
  16:             m_bPressedKeys[iKey] = false;
  17:             m_bLockedKeys[iKey] = false;
  18:         }
  19:     }
  20: }
The Detectkeys function detects which keys have been pressed. For this we first acquire the keyboard and get the device state. We check which keys have been pressed. A key is considered as pressed only if it is not locked
   1: void cInput::Cleanup()
   2: {
   3:     m_pdInputKeyboard->Unacquire();
   4:     m_pdInputKeyboard->Release();
   5:  
   6:     m_pdInput->Release();
   7: }
Lastly, we have a Cleanup function, which unacquires and releases the input devices

That's all that we need to use the keyboard in our game.The next tutorial will deal with getting mouse input.