Mason McCuskey
Developing a GUI in C++ and DirectX
Part I: The Basics, and the Mouse
Before I get started, I want to throw out a disclaimer: Im going to outline the approach I used when I created the GUI system for my upcoming title, Quaternion. Treat this text as one solution to a very intricate problem, nothing more. Im not saying that this way of making a GUI is the fastest or easiest way; Im simply outlining a solution that worked for me. Also, this text is not complete. Like one of those Bob-Vila TV episodes, it skips over the easy stuff and concentrates on the interesting. There is no attached source file; there are code snippets in the text, and thats it. In that code, Ive stripped out a lot of layers of indirection that arent relevant to what Im trying to show (i.e. the wrappers that youd probably have for your DirectX functions, non-relevant initialization and cleanup code, etc). Also, beware of bugs - Ive done lots of bug checking, but Im only human. If you find a bug, please let me know about it by emailing .
Im making several assumptions about your knowledge. Im assuming you know the basics of how event-driven programming works (message queues, etc), and Im assuming you have a strong grasp of PDL (the commenting language - if you dont know what this is, read Code Complete, by Steve McConnell), and C++. I used C++ to implement my GUI system, because Im a card-carrying member of the C++ fan club, and because the OOP of C++ work great for implementing window and control types. Shameless plug for the C++ language: Note the power of OOP in this solution, and ask yourself if you could do the same thing as easily in C.
Lets start by defining our scope. Its important to realize up front that were not remaking Windows 95, were just trying to get a simple GUI up for a game, so we dont have to implement every single control and GUI construct. We only need a few parts for this simple GUI: a mouse pointer, a generic window, and some dialog controls to place within that window. Were also going to need a resource editor, a program that will allow us to design dialogs by graphically dropping controls at various places.
Start with the basics - The Rendering Loop
Im going to start at the top, by defining a function that will calculate and draw one frame of our GUI system. Lets call this function RenderGUI(). In PDL, RenderGUI does something like this:
void CApplication::RenderGUI(void) {
// get position and button status of mouse cursor
// calculate mouse cursors effects on windows / send messages
// render all windows
// render mouse
// flip to screen
}
Pretty straightforward for now. Basically, we grab the new position and status of the mouse cursor, calculate any changes that are caused by the new position, render all our windows, render the mouse cursor, then push the whole thing to the screen.
Now that weve got a main function, were going to create a mouse class. This mouse class will initialize the rodent, and will be responsible for querying its position and storing the results. Heres the definition:
class CMouse {
public:
CMouse(); // boring
~CMouse(); // boring
int Init(LPDIRECTINPUT di); // well talk about this later
int Refresh(void); // well talk about this later
int GetButton(int index) {
if (index 0 || index NUMMOUSEBUTTONS) return(0);
return(m_button[index]);
}
CPoint GetPosition(void) { return(m_position); }
enum { NUMMOUSEBUTTONS = 3 }; // three button mouse
private:
LPDIRECTINPUTDEVICE m_mousedev;
char m_button[NUMMOUSEBUTTONS]; // state of buttons
CPoint m_position; // actual screen position
};
Pretty straightforward class definition. Weve got two data pieces, m_button and m_position, abstracted by two functions, GetButton and GetPosition(). Then weve got Init and Refresh functions, which initialize the mouse and Refresh its button and position information. The m_mousedev is an interface to our mouse device; we get this interface during Init(), and use it in Refresh to communicate with DirectInput.
So before we go any further with CMouse, lets look at the code to initialize DirectInput. Note that this code doesnt belong in our CMouse::Init() routine; the DirectInput pointer is used by the entire game, not just the mouse, so the code that inits DirectInput should go in your main init function - the same time you init DirectDraw, DirectSound, etc. A DirectInput interface pointer is different than a DirectInput device pointer; you use DirectInput pointers to get DirectInputDevice pointers.
Heres the code to initialize the master DirectInput interface pointer:
LPDIRECTINPUT di = NULL;
hr = DirectInputCreate(hinst, DIRECTINPUT_VERSION, &di, NULL);
if (FAILED(hr)) {
// error processing
}
That will put a valid DirectInput interface pointer into di. (Dont forget to Release() it when your game ends!)
Now that weve got a DirectInput interface, lets begin fleshing out our CMouse by implementing CMouse::Init().
bool CMouse::Init(LPDIRECTINPUT di) {
// Obtain an interface to the system mouse device.
hr = di-CreateDevice(GUID_SysMouse, (LPDIRECTINPUTDEVICE*)&di_mouse, NULL);
if (FAILED(hr)) {/* handle errors! */}
// Set the data format to "mouse format".
hr = m_mousedev-SetDataFormat(&c_dfDIMouse);
if (FAILED(hr)) {/* handle errors! */}
// Set the cooperativity level
hr = m_mousedev-SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
if (FAILED(hr)) {/* handle errors! */}
}
That code does three important things. First, it gets a valid DirectInput mouse device interface, and puts it in di_mouse. Next, it sets the data format and the cooperative level for the device, basically letting windows know that we want to query the device as if it were a mouse, and that we dont want to take exclusive ownership of it. (Exclusive ownership means that were the only app that can use the mouse - by specifying DISCL_NONEXCLUSIVE, weve told Windows that were going to be sharing the mouse with other applications.)
Now lets flesh out CMouse::Refresh(), the function responsible for updating the CMouses internal button state and position variables. Heres the code.
void CMouse::Refresh(void) {
C done = 0;
int q;
HRESULT hr;
POINT p;
DIMOUSESTATE dims;
if (!m_di) return;
// clear our struct - eventually, directinput will fill this in
memset(&dims, 0, sizeof(DIMOUSESTATE));
if (!m_mousedev) return; // we dont have a pointer! Bail!
while (!done) {
hr = m_mousedev-GetDeviceState(sizeof(DIMOUSESTATE), &dims);
if (FAILED(hr)) {
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
// device lost reacquire
hr = m_mousedev-Acquire();
if (FAILED(hr)) {
// houston, we have a problem clear & bail
clear();
done=1;
}
} else {
// its some other error - clear and bail!