// Checks for another instance of the current application // Returns: true if another instance is found
// false if this is the only one
//======================================================================
bool AnotherInstance() { HANDLE ourMutex;
// Attempt to create a mutex using our unique string ourMutex = CreateMutex(NULL, true,
"Use_a_different_string_here_for_each_program_48161-XYZZY");
if (GetLastError() == ERROR_ALREADY_EXISTS)
return true; // Another instance was found return false; // We are the only instance }
Listing 2.18. Using a mutex.
2.6 Multitasking in Windows
Windows is like a conductor of an orchestra. Everything that happens—every program, every process, every task—is controlled by Windows. For this discussion, we will use the term thread to describe any task that Windows is trying to perform. There may be other applications running at the same time as our game plus Windows has many processes of its own that are running. All of these programs and processes appear to run at the same time.
But how is this possible if there is only one CPU? The answer is time slicing, or preemptive multithreading, or whatever we want to call it. It works like this: Windows maintains a queue of threads that want to run. It starts one thread, lets it run for a very short amount of time, suspends the current thread, then starts the next. This process is repeated for all run-ning threads. The exact length of each time slice varies depending on a number of factors.
Times of 1 to 20 milliseconds per slice are typical.
In systems with multiple and/or multicored CPUs, it is possible to have more than one thread running at the same time; but even in a quad core system with hyperthreading, there is only room for eight threads. In a typical Windows system, there are dozens of threads running at any given time, so preemptive multithreading is still required.
The multitasking nature of Windows means our game will only be allowed to run in short time slices. We also have very limited control over when the next time slice will occur.
This will present some timing issues for games that need smooth animation. We will see how to deal with these issues in later chapters.
Chapter Review
In this chapter we created a Windows version of “Hello World.” Then we learned how to get input from the keyboard and how to prevent multiple instances of a program from running.
36 2. Windows Programming Fundamentals
We also discussed how Windows does multitasking. Here are the main points:
• We created and configured a project in Visual Studio.
• We learned that WinMain is the starting point for a Windows application.
• We learned about registering a Window class and the WNDCLASSEX structure.
• We saw how to use the CreateWindow function to create the window and what its parameters do.
• We learned about Windows messages, how to write a message loop, and how to use PeekMessage to check for new messages.
• We created a function named WinProc to process Windows messages.
• We learned how to use the WM_CHAR message to read character input.
• We learned how to use WM_KEYDOWN and WM_KEYUP messages to use a keyboard like a game controller.
• We wrote a function that can test for a currently running instance of our pro-gram.
• We learned that Windows uses multitasking to run programs in short time slices.
Review Questions
1. What is the starting point for a Windows application?
2. What is the role of the lpfnWndProc member of the WNDCLASSEX structure?
3. What style is most commonly used for the dsStyle parameter of the CreateWindow function for windowed games?
4. What style value is the dsStyle parameter set to for full-screen games?
5. If hwnd is equal to NULL, does the following if statement display true or false?
if(hwnd)
std::cout << "true";
else
std::cout << "false";
6. How does Windows communicate with our program?
7. When does the message loop end?
8. What happens to messages our program ignores?
37 Examples
9. What is the WM_CHAR message used for?
10. What is a virtual key code?
Exercises
1. Modify the “Hello World” program shown in Figure 2.1 so it displays “Hello World by:” followed by your name.
2. Which key presses result in a “beep” from the PC speaker from Listing 2.14?
3. Use the keyboard2 program shown in Figure 2.8 to test keyboard input.
a. What is the maximum number of simultaneous key presses allowed?
b. List key combinations that do not work (maximum of five).
Examples
All examples are available for download from www.programming2dgames.com.
• Hello World. A complete Windows application that displays “Hello World” in the title of a window.
– Demonstrates Windows programming fundamentals.
• Character Input. Displays the character typed on the keyboard.
– Uses the WM_CHAR message to get the keyboard input.
• Keys Down. Displays a grid that represents the current key press state of every key on the keyboard.
– Uses the WM_KEYDOWN and WM_KEYUP messages to determine the state of the keys on a keyboard.
• Prevent Multiple. A windows application that allows only one running instance of itself.
– Demonstrates using a mutex to prevent multiple instances of a program.
This page intentionally left blank This page intentionally left blank