////////////////////////////////////////////////////////////////////////////////
// DirectDrawBones.cpp
//
//  DirectDraw bare-bones application.

#include <windows.h>
#include <ddraw.h>

HWND hWndMain;
IDirectDraw7        *pDD;
IDirectDrawSurface7 *lpPrimary, *lpBackBuffer;

// Initialise DirectDraw and go to full screen mode.
void InitDirectDraw()
{
    // Create the DirectDraw object, through which we will create surfaces.
    DirectDrawCreateEx(NULL, (void **)&pDD, IID_IDirectDraw7, NULL);

    // Set the co-operative level to exclusive and full-screen.
    pDD->SetCooperativeLevel(hWndMain, 
        DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);

    // Set the display mode to 640x480, 16-bit colour, default refresh rate.
    pDD->SetDisplayMode(640, 480, 16, 0, 0);

    DDSURFACEDESC2  ddsd;

    // Create the primary surface with two back buffers.
    ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
    ddsd.dwSize = sizeof(DDSURFACEDESC2);
    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
            DDSCAPS_FLIP |
            DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount = 2;
    pDD->CreateSurface(&ddsd, &lpPrimary, NULL);
    
    // Get the back buffer pointer, to which we will do all drawing.
    ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
    lpPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpBackBuffer);
}

void ExitDirectDraw()
{
    // Release the primary surface. Note that the back buffer is automatically released 
    //  (since it was created at the same time).
    lpPrimary->Release();
    lpPrimary = NULL;

    // Release the DirectDraw object.
    pDD->Release();
    pDD = NULL;
}

// Handle all messages for the main window.
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_CLOSE:
        ExitDirectDraw();
        DestroyWindow(hWnd);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// Register the window class for the main window.
void RegisterWindowClass()
{
    WNDCLASSEX  wcx;

    ZeroMemory(&wcx, sizeof(WNDCLASSEX));
    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.lpfnWndProc = MainWindowProc;
    wcx.hInstance = GetModuleHandle(NULL);
    // Windows-default icon. Replace with your own.
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcx.hCursor = NULL;
    // Black background for the window.
    wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcx.lpszMenuName = NULL;
    wcx.lpszClassName = "SampleWindowClass";
    RegisterClassEx(&wcx);
}

// Idle-time processing function.

void OnIdle(void)
{
}

// Program entry point.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) 
{
    MSG msg;

    RegisterWindowClass();

    // Create a window that fills the screen.
    hWndMain = CreateWindowEx(WS_EX_APPWINDOW,
        "SampleWindowClass", "DirectDraw Bare-Bones Sample", WS_POPUP,
        0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
        NULL, NULL, hInstance, NULL);
    
    ShowWindow(hWndMain, SW_SHOW);

    InitDirectDraw();

    // Message loop. Note that this has been modified to allow
    //  us to execute even if no messages are being processed.
    for (;;)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (!GetMessage(&msg, NULL, 0, 0))
                break;
            
            // If you want WM_CHAR messages, add
            //  TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        // Idle-time processing
        OnIdle();
    }

    return 0;
}