Chapter 1 – Creating A Window

To program games on windows, you need to know something about the Windows Api. Firstly, you need to understand the windows message loop. To create a windows application you need to link three libraries. Namely,

  • gdi32
  • user32
  • kernel32

And include the header windows.h.

Here’s the code for creating a simple window:

//////////////////////////////////////////////
// Creating A Simple Window
// Author: Muhammad Ahmad Tirmazi
// Copyright 2012 Muhammad Ahmad Tirmazi
// License: BSD 4-Clause
//////////////////////////////////////////////


///////////////////
// Includes
///////////////////
#include <windows.h>
#include <iostream>

//////////////////////////////
// The Main Window Procedure
//////////////////////////////
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//////////////////////////
// The Class Name
//////////////////////////
static const char className[] = "Main Window";

/////////////////////////////////////////
// The WinMain entry-point function
////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {0};

    wc.lpszClassName = className;
    wc.lpszMenuName = className;

    wc.hInstance = hInstance;
    wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW);
    wc.lpfnWndProc = WndProc; // pointer to our window procedure

    // register the class
    if (!::RegisterClass(&wc))
    {
        std::cerr << "Could not register class: " << className << std::endl;
    }

    HWND hwnd = ::CreateWindow(
                        className, // The class name
                        className, // The window's title
                        WS_OVERLAPPEDWINDOW|WS_VISIBLE, // the window's style
                        CW_USEDEFAULT, CW_USEDEFAULT, // the windows position
                        320, 240, // the window's size
                        HWND_DESKTOP, // the parent
                        NULL, // the main menu
                        hInstance, // the program's instance
                        NULL // additional data
                        );

    // error check
    if (hwnd == NULL)
    {
        std::cerr << "Could not create window: " << className << std::endl;
    }

    MSG msg;

    // message loop
    while (::GetMessage(&msg, NULL, 0, 0))
    {
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    // handle the quit message
    case WM_DESTROY:
        ::PostQuitMessage(0);
        break;
    }

    // send unhandled messages to DefWindowProc
    return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

// That's All Folks!

Adapting The Window For Gaming

The above window needs to be modified a little to make it suitable for games. Firstly, the user shouldn’t be allowed to resize or maximize it. For that, you need to change the window’s style a bit. Instead of using

WS_OVERLAPPEDWINDOW|WS_VISIBLE

you should use:

WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s