All pastes #348253 Raw Edit

Stuff

public text v1 · immutable
·published 2007-02-10 01:06 UTC
rendered paste body
#include &ltwindows.h>
#include &ltddraw.h>

// dissect globals
HINSTANCE   g_hinstance;
HWND        g_hwnd;
bool        g_running;
bool        g_active;
const char* g_appName = "My DirectDraw Program";

void update() { /* Update game */ }
bool createWindow();
LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);

// dissect WinMain
int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE,
                     LPSTR cmdLine, int)
{
    g_hinstance = hinstance; // Make a copy so we can use it later

    if(!createWindow())
        return -1; // Failed to create a window

    SetFocus(g_hwnd);
    ShowWindow(g_hwnd, SW_SHOWNORMAL);
    UpdateWindow(g_hwnd);

    // Set up DirectDraw (we'll do this later)
    
    g_running = true;

    while(true)
    {
        MSG msg;
        if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                return msg.wParam;

            // Send messages to WindowProc
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else if(g_running && g_active)
            update();
    }

    // Prevent compiler warning even though we never get here
    return -1;
}

// dissect createWindow
bool createWindow()
{
    WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(wc));
    
    wc.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.hCursor          = LoadCursor(0, IDC_ARROW);
    wc.hIcon            = LoadIcon(0, IDI_APPLICATION);
    wc.hIconSm          = LoadIcon(0, IDI_WINLOGO);
    wc.hInstance        = g_hinstance;
    wc.lpfnWndProc      = wndProc;
    wc.lpszClassName    = g_appName;
    wc.cbSize           = sizeof(wc);
    wc.style            = CS_VREDRAW | CS_HREDRAW;
    
    if(RegisterClassEx(&wc))
    {
        g_hwnd = CreateWindowEx(
            WS_EX_TOPMOST,
            g_appName,
            g_appName,
            WS_POPUP | WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT,
            0, 0, g_hinstance, 0
        );
        return g_hwnd != 0;
    }

    return false;
}

// dissect wndProc
LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
    switch(msg)
    {
    case WM_DESTROY:
        // Clean up (we'll do this later)
        PostQuitMessage(0);
        return 0;
    case WM_ACTIVATE:
        g_active = (LOWORD(w) != WA_INACTIVE) && !((BOOL)HIWORD(w));
        break;
    }

    return DefWindowProc(hwnd, msg, w, l);
}