• Complain

Mason McCuskey - Developing a GUI in C++ and DirectX

Here you can read online Mason McCuskey - Developing a GUI in C++ and DirectX full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Mason McCuskey Developing a GUI in C++ and DirectX
  • Book:
    Developing a GUI in C++ and DirectX
  • Author:
  • Genre:
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

Developing a GUI in C++ and DirectX: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Developing a GUI in C++ and DirectX" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

At first glance, it may seem like Im reinventing the wheel; Windows already comes with a very complex, very functional GUI. Unfortunately, while the Windows GUI is great for office apps, quite frequently, its not suited for many games. Games tend to want a more precise control over the GUI than Windows can provide (for example, games may want to use alpha-blending to implement partially transparent windows - easy if youve written your own GUI, but next to impossible using the Windows GUI). This article will walk you though how to create a GUI using C++ and DirectX. The series is divided into several parts, each dealing with a specific aspect of GUI programming: Part I: The Basics, and the Mouse Part II: Windows Part III: Controls Part IV: Resource Editors and Other Madness NOTE: This document was originally four separate articles on www.gamedev.net. Ive concatenated all four into one for the XGDC, but they remain otherwise unchanged. - Mason

Mason McCuskey: author's other books


Who wrote Developing a GUI in C++ and DirectX? Find out the surname, the name of the author of the book and a list of all author's works by series.

Developing a GUI in C++ and DirectX — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Developing a GUI in C++ and DirectX" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make

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.

The Mouse

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!

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Developing a GUI in C++ and DirectX»

Look at similar books to Developing a GUI in C++ and DirectX. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Developing a GUI in C++ and DirectX»

Discussion, reviews of the book Developing a GUI in C++ and DirectX and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.