Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
109 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Introduction

Since Microsoft's introduction of DirectX, game developers have dealt with the challenge of encapsulating the Win32 windowing API.  The obvious choice, really the only ready-made choice aside from Borland's ObjectWindows, was MFC, but its large footprint and inefficient implementation led many developers to either develop their own in-house solution or use the Win32 API directly.  Today, however, there is an alternative to MFC known as the Windows Template Library that provides the desired encapsulation without incurring a large footprint or poor performance.

This is the first in a series of articles that intends to introduce developers to the use of this sparsely documented library in a game development context.  While development utilities are an obvious application for the WTL, and by the end of this series the reader should have gained enough experience to use it in such a manner, this series' true focus will be on using the WTL to aid in developing the actual game application.

Requirements

I make a few assumptions in this article.  First I assume that the reader has a basic understanding of Win32 programming.  He or she should be capable of creating a simple Win32 application.  No knowledge of MFC, ATL, or WTL will be required.  I also assume that the reader is using VC 6.0 SP 5 or VC .NET.  Other compilers may work but I cannot make any guarantees.  Before continuing with this article you'll also want to make sure you have the WTL 3.1 or 7.0 libraries and have added the WTL\Include path to your list of include directories.  The libraries can be obtained from the MSDN download site.

Active Template Library (ATL)

Before we get into using the WTL we'll need to define what the Active Template Library is and its relation to the WTL.  The Active Template Library is a set of template classes developed by Microsoft to aid in the development of ActiveX controls and COM objects.  Since many ActiveX controls interact with the user in some fashion, Microsoft implemented classes to encapsulate the Windows user interface elements.  ATL's classes were designed to be lightweight and fast making the ATL Window Classes (a.k.a WTL) a very attractive alternative to MFC and straight Win32 API programming.  With that out of the way we're ready to begin.

Getting Started

Let's start at the beginning.  Create an empty Win32 Application project and add a file to it called main.cpp. Make it a windows executable that simply returns.

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine, int nCmdShow)
{
  return 0;
}

Because the WTL is an extension of ATL and is actually intended to be used in conjunction with developing COM objects / COM servers, there are a few items that need to be added to main.cpp to appease the ATL library.  We'll need to include atlbase.h and atlapp.h and define a global variable called _Module. We'll also add initialization and termination calls for _Module.

The _Module variable is used by ATL to help encapsulate the functionality required by COM servers.  It's tightly coupled to the ATL libraries so it has to be defined, and its name can't be changed.  A typical ATL project would define _Module as an instance of a CComModule object.  Here we define its type as CAppModule, a derivative of CComModule that adds support for message loops.  _Module gives the ATL libraries access to the application instance as well as some information about the operating environment.  Let's not worry about the details of _Module just yet.  We'll cover it in more detail in another article.  Below is our new main.cpp file.

#include <windows.h>
#include <atlbase.h>
#include <atlapp.h>

CAppModule _Module;

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine, int nCmdShow)
{
  _Module.Init(NULL, hInstance);

  _Module.Term();

  return 0;
}

Having made these few changes, we're going to leave main.cpp alone for a bit.  At this point all that's missing is a window, its window procedure, and a message loop.  Let's move on and get to know the classes the WTL provides for wrapping and implementing windows and their window procedures.





Windows Class

Contents
  Introduction
  Windows Class
  Message Handling
  Conclusion

  Source code
  Printable version
  Discuss this article

The Series
  Getting Started
  Windowing