// linesminimum application

// horlines.cpp -- A DirectDraw application
// <F1> draws horizontal lines in the 8-bit default palette
// <ESC> exits program
// Uses the CDirDraw class defined in cdirdraw.cpp

// Be sure to add the ddraw.lib file to the link libraries to be used.
// Do this by "Project | Settings | Link Tab" and typing "ddraw.lib" 
// at the end of the "Object/library modules:" edit box. 
// Separator is a space.


#include <windows.h>
#include "cdirdraw.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT wMessage, 
                                    WPARAM wParam,LPARAM lParam);

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                                   LPSTR lpszCmdLine, int nCmdShow)
{
    WNDCLASS wndClass;
    HWND hWnd;
    MSG msg;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hInstance = hInstance;
    wndClass.hIcon = NULL; 
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "MyClass";
    RegisterClass(&wndClass);

    hWnd = CreateWindow("MyClass",
                        "Direct-X Horizontal Lines: <F1> draws lines, <ESC> exits",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        NULL, NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT wMessage, 
                                    WPARAM wParam, LPARAM lParam)
{
    static CDirDraw* pDirDraw;      // A pointer to an object derived from
                                    // our CDirDraw class
    DWORD t0, t1, t2, delt1, delt2;

    static char cBuf[]="                                     ";
    HDC hDC;

    switch(wMessage)
    {
        case WM_KEYDOWN:
            if (wParam == VK_ESCAPE)
                DestroyWindow(hWnd);
            if (wParam == VK_F1)
            {
             // create new DirectDraw object, get into 640X480X8 mode, 
             // and get primary/backbuffer surfaces
                pDirDraw = new CDirDraw(hWnd);
             // paint backbuffer surface black
                pDirDraw->ChangeColor(0);
                t0 = GetTickCount();
             // draw 256 horizontal lines on backbuffer
                pDirDraw->DrawLines();
                t1 = GetTickCount();
             // flip primary and back surfaces
                pDirDraw->m_pPrimarySurface->Flip(NULL, DDFLIP_WAIT);
                t2 = GetTickCount();
                delt1 = t1-t0;
                delt2 = t2-t1;
             // get a DC compatible with primary surface
                pDirDraw->m_pPrimarySurface->GetDC(&hDC);       
                SetBkColor(hDC, RGB(0,0,0));
                SetTextColor(hDC, RGB(255,255,255));
             // display time intervals
                wsprintf(cBuf, "DirectX drawing lines: %d msecs", delt1);                               
                TextOut(hDC, 200, 10, cBuf, lstrlen(cBuf)); 
                wsprintf(cBuf, "DirectX flipping buffers: %d msecs", delt2);                            
                TextOut(hDC, 200, 30, cBuf, lstrlen(cBuf));
             // get rid of DC
                pDirDraw->m_pPrimarySurface->ReleaseDC(hDC); 
            }
            break;
                        
        case WM_DESTROY:
            delete pDirDraw;  // get rid of all DirectDraw constructs
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, wMessage, wParam, lParam);
        }
        return 0;
}