Tricks of the Windows Game Programming Gurus:The Windows Programming Model

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Windows 98

In mid-1998, Windows 98 was released. It’s more of an evolutionary step than a revolutionary one, like Windows 95, but it’s important nonetheless. Windows 98 is like a hot rod—it’s sleek, fast, and kicks ass! It’s totally 32-bit, has support for everything you can think of, and is open-ended for expansion. And it has DirectX, multimedia, 3D graphics, networking, and the Internet all integrated into it very nicely.

Windows 98 is also very robust compared to Windows 95. Sure, Windows 98 still crashes and tweaks out, but believe me, there is a lot less of that. Furthermore, plug-and-play actually works, and works well—it’s about time!

#### Windows NT

Now we can talk about Windows NT. At the time of this writing, Windows NT is currently on release 5.0. And as far as I can tell, ultimately it’s going to replace Windows 9X as the OS of choice for everyone. NT is simply a lot tighter than Windows 9X; furthermore, most game programmers develop on NT and then release on Windows 9X. The cool thing about NT 5.0 is that it has full plug-and-play support, along with Win32/DirectX, so applications written for Windows 9X with DirectX will work on NT 5.0+. This is great news, since now game developers who write PC games have the largest market potential in history.

So what’s the bottom line? If you write a Win32 application with DirectX (or not), it will work on Windows 95, 98, and NT 5.0+. This is a good thing. Hence, everything you learn in this book is applicable to at least three operating systems, and maybe more when NT along with DirectX is on other machines, like DEC Alphas. And don’t forget Windows CE—DirectX and a subset of Win32 work on that system, too!

#### Basic Windows Architecture: Win9X/NT

Windows, unlike DOS, is a multitasking operating system designed to allow a number of applications and/or smaller processes to run at the same time, using the hardware to its fullest. This means that Windows is a shared environment—one application can’t take over the entire system. Although Windows 95, 98, and NT are similar, there are a number of technical differences. However, as far as we are concerned here, we can generalize without too much drama. I will refer to the Windows machine as a Win 9X/NT or Windows box most of the time. So let’s get started!

### Multitasking and Multithreading

As I said, Windows allows a number of different applications to be executed simultaneously in a round-robin fashion, where each application gets a small time slice to run in and then the next application takes its turn. As you can see in Figure 2.1, the CPU is shared among a number of different applications in a circular manner. Figuring out the exact methodology that selects the next application, and the amount of time allotted to each application, is the job of the scheduler.

Figure 2.1 Multiprocessing in action with a single processor.

The scheduler may be very simple, running each application for a fixed number of milliseconds, or it may be very complex, giving applications various levels of priority and preempting applications or events with lower priority. In the case of Win 9X/NT, the scheduler is priority-based with preemption. This means that some applications can have more processor time than others, but if an application needs the CPU, the current task can be blocked or preempted while another task runs.

However, you don’t need to worry much about this unless you’re writing OS or real-time code, where exact details matter. In most cases, Windows will run and schedule your application, and you will have nothing to do with it.

Taking a closer look at Windows, we see that not only is it multitasking, but it’s multithreaded. This means that programs are really composed of a number of simpler threads of execution. These threads are scheduled just like heavier-weight processes, such as programs. In fact, right now there are probably 30 to 50 threads running on your machine, performing various tasks. So in reality, you may have a single program running that consists of one or more threads of execution.

Take a look at Figure 2.2 to see a more realistic multithreaded view of Windows. As you can see, each program actually consists of a number of worker threads in addition to the main thread.

Figure 2.2 A more realistic multithreaded view of Windows.

#### Getting Info on the Threads

For some fun, let’s see how many threads are running on your machine right now. On your Windows machine, press Ctrl+Alt+Delete to pop up the Active Program Task Manager, which displays all of the running tasks (or processes). This isn’t exactly what we want, but it’s close. What we really want is a tool or applet that displays the actual threads that are executing. A number of shareware and commercial utilities do this, but Windows comes with a couple of them built in.

Within the directory that Windows was installed in (`WINDOWS\`, in most cases), you will find an executable named `SYSMON.EXE` (Windows 95/98) or `PERFMON.EXE` (Windows NT). Figure 2.3 depicts `SYSMON.EXE` running on my Windows 98 machine. As you can see, there is a wealth of information in addition to the number of threads running, such as memory use and processor load. In fact, I like to keep `SYSMON.EXE` running as I develop so I can see what’s going on and how the system is loaded.

Figure 2.3 Running `SYSMON.EXE.`

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.