DirectX 8. Начинаем работу с DirectX Graphics (Ваткин, Dempski) - страница 28

>bool InitDD(HWND hWnd) {

> //dd init code

> g_pDisplay = new CDisplay();

> if (FAILED(g_pDisplay->CreateWindowedDisplay(hWnd, 640, 480))) {

>  MessageBox(NULL, "Failed to Initialize DirectDraw", "DirectDraw Initialization Failure", MB_OK | MB_ICONERROR);

>  return false;

> }

> return true;

>}

The infamous InitDD() function… but wait, it's only several lines! This is what the common libs were made for! We now have all the DirectDraw setup garbage out of the way, effortlessly! Again, you'll notice that it's done a lot for you. If you don't really care to know the exact procedure of what has been abstracted from you, at least get the gist of it. It will help out if you have to go back and change the cooperative level or whatnot. Note that this is a boolean function, so if you like, you can do error checking (which I, for some reason or another, decided to omit in this article).

>void CleanUp() {

> SAFE_DELETE(g_pDisplay);

>}

Simple enough. This function calls on the SAFE_DELETE macro defined in dxutil to delete our display object, and call the destructor.

>void MainLoop() {

> g_pDisplay->CreateSurfaceFromText(&g_pText, NULL, "DDraw using Common Files", RGB(0,0,0), RGB(0,255,0));

> g_pDisplay->Clear(0);

> g_pDisplay->Blt(0, 0, g_pText, 0);

> g_pDisplay->Present();

> g_pText->Destroy();

>}

This is where you'd want to put your game. In order to give you an example of how surface objects work, we've made a simple text surface and drawn some text onto it. Note that we destroy g_pText at the end, because it is recreated every cycle and not doing so would eventually eat up quite a bit of memory.

>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iShowCmd) {

> WNDCLASSEX wc;

> HWND hWnd;

> MSG lpMsg;

> wc.cbClsExtra=0;

> wc.cbSize=sizeof(WNDCLASSEX);

> wc.cbWndExtra=0;

> wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

> wc.hCursor=LoadCursor(NULL, IDC_ARROW);

> wc.hIcon=LoadIcon(NULL, IDI_APPLICATION);

> wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);

> wc.hInstance=hInstance;

> wc.lpfnWndProc=WndProc;

> wc.lpszClassName="wc";

> wc.lpszMenuName=0;

> wc.style=CS_HREDRAW | CS_VREDRAW;

> if (!RegisterClassEx(&wc)) {

>  MessageBox(NULL, "Couldn't Register Window Class", "Window Class Registration Failure", MB_OK | MB_ICONERROR);

>  return 0;

> }

> hWnd = CreateWindowEx(NULL, "wc", "DirectDraw Common Files in Action", WS_POPUPWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, hInstance, 0);

> if (hWnd == NULL) {

>  MessageBox(NULL, "Failed to Create Window", "Window Creation Failure", MB_OK | MB_ICONERROR);

>  return 0;