////////////////////////////////////////////////////////////////////////////////
// DirectXInput.cpp
//
//  DirectDraw, sprites and DirectInput.

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

#include "ddutil.h"

////////////////////////////////////////////////////////////////////////////////
// DirectInput definitions

// {BD0E8406-FB56-4322-8BC6-BD6E481F5564}
GUID g_guidApp = 
{ 0xbd0e8406, 0xfb56, 0x4322, { 0x8b, 0xc6, 0xbd, 0x6e, 0x48, 0x1f, 0x55, 0x64 } };
DWORD   g_dwGenre = DIVIRTUAL_ARCADE_SIDE2SIDE;
LPCTSTR g_tszActionMapName = _T("Sample");

enum Actions 
{
    ACTIONS_LEFTRIGHT,
    ACTIONS_LEFT,
    ACTIONS_RIGHT,
    ACTIONS_QUIT,
    ACTIONS_NONE
};

bool    g_bQuit;
bool    g_bLeft, g_bRight;

DIACTION g_rgActions[] =
{
    // Genre-defined virtual axes
    { ACTIONS_LEFTRIGHT,    DIAXIS_ARCADES_LATERAL,         0, "Left/Right" },

    // Actions mapped to keys as well as to virtual controls
    { ACTIONS_LEFT,         DIKEYBOARD_LEFT,                0, "Left" },
    { ACTIONS_RIGHT,        DIKEYBOARD_RIGHT,               0, "Right" },

    // Actions mapped to keys
    { ACTIONS_QUIT,         DIKEYBOARD_ESCAPE,              0, "Quit" },
};

DWORD g_nActions = sizeof(g_rgActions) / sizeof(DIACTION);

// Functions declared in input.cpp
BOOL InitDirectInput();
void ExitDirectInput();
void AcquireDevices();
void UnacquireDevices();
void CheckInput();

////////////////////////////////////////////////////////////////////////////////
// Graphics support

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

// Surface containing our background.
IDirectDrawSurface7 *lpA;
// Surface containing our sprites.
IDirectDrawSurface7 *lpSprites;

// 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);

    // Load our background.
    lpA = DDLoadBitmap(pDD, "a.bmp", 0, 0);

    // Load the sprites.
    lpSprites = DDLoadBitmap(pDD, "sprites.bmp", 0, 0);
    DDSetColorKey(lpSprites, RGB(255, 0, 255));
}

void ExitDirectDraw()
{
    // Release our sprites surface.
    lpSprites->Release();
    lpSprites = NULL;

    // Release our background surface.
    lpA->Release();
    lpA = NULL;

    // 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:
        UnacquireDevices();
        ExitDirectDraw();
        ExitDirectInput();
        DestroyWindow(hWnd);
        return 0;

    // Clear the cursor while active.
    case WM_SETCURSOR:
        SetCursor(NULL);
        return 0;

    case WM_ACTIVATE:
        if (wParam == WA_INACTIVE)
            UnacquireDevices();
        else
            AcquireDevices();
        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);
}

////////////////////////////////////////////////////////////////////////////////
// Input handling

#define AXIS_THRESHOLD  20

void HandleAction(UINT nAction, DWORD dwData)
{
    int nAxisPos = (int)dwData;

    switch (nAction)
    {
    case ACTIONS_LEFTRIGHT:
        if (nAxisPos < -AXIS_THRESHOLD)
        {
            g_bLeft = true;
            g_bRight = false;
        }
        else if (nAxisPos > AXIS_THRESHOLD)
        {
            g_bRight = true;
            g_bLeft = false;
        }
        else
        {
            g_bLeft = g_bRight = false;
        }
        break;
    
    case ACTIONS_LEFT:
        g_bLeft = (dwData != 0);
        break;

    case ACTIONS_RIGHT:
        g_bRight = (dwData != 0);
        break;

    case ACTIONS_QUIT:
        g_bQuit = true;
        break;
    
    default:
        break;
    }
}

////////////////////////////////////////////////////////////////////////////////
// Idle-time processing function.

void OnIdle(void)
{
    if (lpPrimary == NULL)
        return;

    CheckInput();

    if (g_bQuit)
    {
        PostMessage(hWndMain, WM_CLOSE, 0, 0);
        return;
    }

    RECT    r;
    
    // Draw the background to the screen.
    r.left = 0;
    r.top = 0;
    r.right = 640;
    r.bottom = 480;
    lpBackBuffer->BltFast(0, 0, lpA, &r, 
        DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);

    // Draw a sprite in the middle of the screen.
    r.left = 128;
    r.top = 0;
    r.right = 192;
    r.bottom = 64;
    lpBackBuffer->BltFast(288, 208, lpSprites, &r, 
        DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

    // Move a sprite around automatically.
    static int x;

    x++;
    if (x > 100)
        x = -100;
    
    r.left = 0;
    r.right = 64;
    lpBackBuffer->BltFast(288 + x, 100, lpSprites, &r, 
        DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

    // Move a sprite around with the input.
    static int xSprite;

    if (g_bLeft)
        xSprite--;
    if (g_bRight)
        xSprite++;
    r.left = 64;
    r.right = 128;
    lpBackBuffer->BltFast(288 + xSprite, 150, lpSprites, &r, 
        DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT); 

    lpPrimary->Flip(NULL, DDFLIP_WAIT);
}

// 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 Sprites Sample", WS_POPUP,
        0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
        NULL, NULL, hInstance, NULL);
    
    ShowWindow(hWndMain, SW_SHOW);

    InitDirectInput();
    AcquireDevices();
    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;
}