1 #include <stdio.h>
2 #include "main.h"
3 
4 
5 // TODO:
6 //
7 // - free memory
8 // - Check if we ran out of memory (HeapAlloc == NULL)
9   HDC hdc;
10   DDRAWI_DIRECTDRAW_GBL mDDrawGlobal;
11   DDRAWI_DIRECTDRAW_LCL mDDrawLocal;
12   DDHALINFO mHALInfo;
13 
14   DDHAL_CALLBACKS mCallbacks;
15   DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks;
16   D3DHAL_CALLBACKS mD3dCallbacks;
17   D3DHAL_GLOBALDRIVERDATA mD3dDriverData;
18 
19   UINT mcModeInfos;
20   DDHALMODEINFO *mpModeInfos;
21 
22   UINT mcvmList;
23   VIDMEM *mpvmList;
24 
25   UINT mcFourCC;
26   DWORD *mpFourCC;
27 
28   UINT mcTextures;
29   DDSURFACEDESC *mpTextures;
30 
31   /* Surface */
32   DDRAWI_DDRAWSURFACE_GBL mPrimaryGlobal;
33   DDRAWI_DDRAWSURFACE_MORE mPrimaryMore;
34   DDRAWI_DDRAWSURFACE_LCL mPrimaryLocal;
35   DDRAWI_DDRAWSURFACE_LCL *mpPrimaryLocals[1];
36   DDRAWI_DDRAWCLIPPER_LCL mPrimaryClipperLocal;
37   DDRAWI_DDRAWCLIPPER_GBL mPrimaryClipperGlobal;
38   //DDRAWI_DDRAWCLIPPER_INT mPrimaryClipperInterface;
39   DDSURFACEDESC mddsdPrimary;
40   DDSURFACEDESC mddsdOverlay;
41 
42   DDRAWI_DDRAWSURFACE_GBL mOverlayGlobal;
43   DDRAWI_DDRAWSURFACE_LCL mOverlayLocal[6];
44   DDRAWI_DDRAWSURFACE_LCL *mpOverlayLocals[6];
45   DDRAWI_DDRAWSURFACE_MORE mOverlayMore[6];
46 
47   DDHAL_BLTDATA                mDdBlt;
48 
49 
50 typedef struct _DD_GETDRIVERINFODATA {
51   VOID  *dhpdev;
52   DWORD  dwSize;
53   DWORD  dwFlags;
54   GUID  guidInfo;
55   DWORD  dwExpectedSize;
56   PVOID  lpvData;
57   DWORD  dwActualSize;
58   HRESULT  ddRVal;
59   ULONG_PTR  dwContext;
60 } DD_GETDRIVERINFODATA, *PDD_GETDRIVERINFODATA;
61 
62 
63 typedef struct _DD_MISCELLANEOUSCALLBACKS {
64   DWORD  dwSize;
65   DWORD  dwFlags;
66   PVOID  GetAvailDriverMemory;
67 } DD_MISCELLANEOUSCALLBACKS;
68 
69 
70 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
71 					LPSTR lpCmdLine, int nCmdShow)
72 {
73     /* HAL Startup process */
74     DEVMODE devmode;
75     HBITMAP hbmp;
76     const UINT bmiSize = sizeof(BITMAPINFOHEADER) + 0x10;
77     UCHAR *pbmiData;
78     BITMAPINFO *pbmi;
79 
80     DWORD *pMasks;
81     //BOOL newmode = FALSE;
82 	//DWORD Status; /* for create surface */
83 	UINT i;
84 	UINT j;
85 	UINT cSurfaces;
86 
87 	DDHAL_CANCREATESURFACEDATA mDdCanCreateSurface;
88 	DDHAL_UPDATEOVERLAYDATA mDdUpdateOverlay;
89 	DDRAWI_DDRAWSURFACE_LCL *pDDSurface;
90 	DDHAL_CREATESURFACEDATA mDdCreateSurface;
91 
92 
93 	/* get the functions we need */
94 //	DD_GETDRIVERINFODATA drv;
95 	HMODULE lib = LoadLibrary("gdi32.dll");
96 	DdCreateDirectDrawObject = (BOOL (APIENTRY*)(LPDDRAWI_DIRECTDRAW_GBL,   HDC))GetProcAddress(lib, "GdiEntry1");
97 	DdQueryDirectDrawObject  = (BOOL (APIENTRY*)(LPDDRAWI_DIRECTDRAW_GBL,   LPDDHALINFO,LPDDHAL_DDCALLBACKS,LPDDHAL_DDSURFACECALLBACKS,LPDDHAL_DDPALETTECALLBACKS,LPD3DHAL_CALLBACKS,LPD3DHAL_GLOBALDRIVERDATA,LPDDHAL_DDEXEBUFCALLBACKS,LPDDSURFACEDESC,LPDWORD,LPVIDMEM))GetProcAddress(lib, "GdiEntry2");
98     DdAttachSurface          = (BOOL (APIENTRY*)(LPDDRAWI_DDRAWSURFACE_LCL, LPDDRAWI_DDRAWSURFACE_LCL))GetProcAddress(lib, "GdiEntry11");
99     DdResetVisrgn            = (BOOL (APIENTRY*)(LPDDRAWI_DDRAWSURFACE_LCL, HWND))GetProcAddress(lib, "GdiEntry6");
100 
101 	printf("This apps showing how to start up directx draw/d3d interface and some other as well\n");
102 	printf("This code have been releae to some close applactons with my premtions, if any company\n");
103     printf("want use part or whole code, you need contact the orginal author to ask for premtions\n");
104     printf("This code are release under alot of diffent licen\n");
105     printf("All GPL and LGPL project have right use and studing this code.\n");
106     printf("This code maybe need more comment to known how stuff working and maybe looking bit mesy\n");
107 	printf("Bestreagds Magnus Olsen magnus@greatlord.com or greatlord@reactos.org\n");
108     printf("Copyright 2006 by Magnus Olsen\n\n");
109     printf("This demo showing how to  start dx draw hal and create a primary surface,\n");
110 	printf("and a overlay sufrace and blt to the primary surface\n");
111 
112 
113     /*
114        Get and Create mode info
115     */
116     mcModeInfos = 1;
117     mpModeInfos = (DDHALMODEINFO*)HeapAlloc(GetProcessHeap(),
118                                                   HEAP_ZERO_MEMORY,
119                                                   mcModeInfos * sizeof(DDHALMODEINFO));
120 
121     if (mpModeInfos == NULL)
122     {
123        printf("Fail to alloc mpModeInfos\n");
124        return DD_FALSE;
125     }
126 
127 
128     EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devmode);
129 
130     mpModeInfos[0].dwWidth      = devmode.dmPelsWidth;
131     mpModeInfos[0].dwHeight     = devmode.dmPelsHeight;
132     mpModeInfos[0].dwBPP        = devmode.dmBitsPerPel;
133     mpModeInfos[0].lPitch       = (devmode.dmPelsWidth*devmode.dmBitsPerPel)/8;
134     mpModeInfos[0].wRefreshRate = (WORD)devmode.dmDisplayFrequency;
135 
136     /*
137        Setup HDC and mDDrawGlobal right
138     */
139     hdc = CreateDCW(L"DISPLAY",L"DISPLAY",NULL,NULL);
140 
141     if (hdc == NULL)
142     {
143 	  printf("Fail to create HDC\n");
144       return DD_FALSE;
145     }
146 
147     /*
148       Dectect RGB bit mask
149     */
150     hbmp = CreateCompatibleBitmap(hdc, 1, 1);
151     if (hbmp==NULL)
152     {
153        HeapFree(GetProcessHeap(), 0, mpModeInfos);
154        DeleteDC(hdc);
155 	   printf("Fail to Create Compatible Bitmap\n");
156        return DD_FALSE;
157     }
158 
159     pbmiData = (UCHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bmiSize);
160     pbmi = (BITMAPINFO*)pbmiData;
161 
162     if (pbmiData==NULL)
163     {
164        HeapFree(GetProcessHeap(), 0, mpModeInfos);
165        free(mpModeInfos);
166        DeleteDC(hdc);
167        DeleteObject(hbmp);
168 	   printf("Fail to Alloc  pbmiData\n");
169        return DDERR_UNSUPPORTED;
170     }
171 
172     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
173     pbmi->bmiHeader.biBitCount = (WORD)devmode.dmBitsPerPel;
174     pbmi->bmiHeader.biCompression = BI_BITFIELDS;
175     pbmi->bmiHeader.biWidth = 1;
176     pbmi->bmiHeader.biHeight = 1;
177 
178     GetDIBits(hdc, hbmp, 0, 0, NULL, pbmi, 0);
179     DeleteObject(hbmp);
180 
181     pMasks = (DWORD*)(pbmiData + sizeof(BITMAPINFOHEADER));
182     mpModeInfos[0].dwRBitMask = pMasks[0];
183     mpModeInfos[0].dwGBitMask = pMasks[1];
184     mpModeInfos[0].dwBBitMask = pMasks[2];
185     mpModeInfos[0].dwAlphaBitMask = pMasks[3];
186 
187     HeapFree(GetProcessHeap(), 0, pbmiData);
188 
189     /*
190       prepare start up the DX Draw HAL interface now
191     */
192 
193     memset(&mDDrawGlobal, 0, sizeof(DDRAWI_DIRECTDRAW_GBL));
194     memset(&mHALInfo,     0, sizeof(DDHALINFO));
195     memset(&mCallbacks,   0, sizeof(DDHAL_CALLBACKS));
196 
197     /*
198       Startup DX HAL step one of three
199     */
200     if (!DdCreateDirectDrawObject(&mDDrawGlobal, hdc))
201     {
202        HeapFree(GetProcessHeap(), 0, mpModeInfos);
203        DeleteDC(hdc);
204        DeleteObject(hbmp);
205 	   printf("Fail to Create Direct DrawObject\n");
206        return DD_FALSE;
207     }
208 
209 	mDDrawGlobal.dwRefCnt = 1; //addref / remove ref
210 
211     // Do not relase HDC it have been map in kernel mode
212     // DeleteDC(hdc);
213 
214     /* we need reanable it if screen res have changes, and some bad drv need be reanble very few
215 	   to contiune */
216 	/*
217     if (!DdReenableDirectDrawObject(&mDDrawGlobal, &newmode))
218     {
219       HeapFree(GetProcessHeap(), 0, mpModeInfos);
220       DeleteDC(hdc);
221       DeleteObject(hbmp);
222       return DD_FALSE;
223     }*/
224 
225     /*
226       Setup the DirectDraw Local
227     */
228 
229     mDDrawLocal.lpDDCB = &mCallbacks;
230     mDDrawLocal.lpGbl = &mDDrawGlobal;
231     mDDrawLocal.dwProcessId = GetCurrentProcessId();
232 
233     mDDrawGlobal.lpDDCBtmp = &mCallbacks;
234     mDDrawGlobal.lpExclusiveOwner = &mDDrawLocal;
235 	//mDDrawLocal.dwLocalFlags = DDRAWILCL_DIRECTDRAW7;
236 
237 
238     /*
239        Startup DX HAL step two of three
240     */
241 
242     if (!DdQueryDirectDrawObject(&mDDrawGlobal,
243                                  &mHALInfo,
244                                  &mCallbacks.HALDD,
245                                  &mCallbacks.HALDDSurface,
246                                  &mCallbacks.HALDDPalette,
247                                  &mD3dCallbacks,
248                                  &mD3dDriverData,
249                                  &mD3dBufferCallbacks,
250                                  NULL,
251                                  NULL,
252                                  NULL))
253     {
254       HeapFree(GetProcessHeap(), 0, mpModeInfos);
255       DeleteDC(hdc);
256       DeleteObject(hbmp);
257       // FIXME Close DX fristcall and second call
258       return DD_FALSE;
259     }
260 
261     mcvmList = mHALInfo.vmiData.dwNumHeaps;
262     mpvmList = (VIDMEM*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VIDMEM) * mcvmList);
263     if (mpvmList == NULL)
264     {
265       HeapFree(GetProcessHeap(), 0, mpModeInfos);
266       DeleteDC(hdc);
267       DeleteObject(hbmp);
268       // FIXME Close DX fristcall and second call
269 	  printf("Fail to QueryDirect Draw Object frist pass\n");
270       return DD_FALSE;
271     }
272 
273     mcFourCC = mHALInfo.ddCaps.dwNumFourCCCodes;
274     mpFourCC = (DWORD *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * mcFourCC);
275     if (mpFourCC == NULL)
276     {
277       HeapFree(GetProcessHeap(), 0, mpvmList);
278       HeapFree(GetProcessHeap(), 0, mpModeInfos);
279       DeleteDC(hdc);
280       DeleteObject(hbmp);
281       // FIXME Close DX fristcall and second call
282       return DD_FALSE;
283     }
284 
285     mcTextures = mD3dDriverData.dwNumTextureFormats;
286     mpTextures = (DDSURFACEDESC*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDSURFACEDESC) * mcTextures);
287     if (mpTextures == NULL)
288     {
289       HeapFree(GetProcessHeap(), 0, mpFourCC);
290       HeapFree(GetProcessHeap(), 0, mpvmList);
291       HeapFree(GetProcessHeap(), 0, mpModeInfos);
292       DeleteDC(hdc);
293       DeleteObject(hbmp);
294       // FIXME Close DX fristcall and second call
295 	  printf("Fail QueryDirect Draw Object to Alloc mpTextures \n");
296       return DD_FALSE;
297     }
298 
299     mHALInfo.vmiData.pvmList = mpvmList;
300     mHALInfo.lpdwFourCC = mpFourCC;
301     mD3dDriverData.lpTextureFormats = mpTextures;
302 
303     if (!DdQueryDirectDrawObject(
304                                     &mDDrawGlobal,
305                                     &mHALInfo,
306                                     &mCallbacks.HALDD,
307                                     &mCallbacks.HALDDSurface,
308                                     &mCallbacks.HALDDPalette,
309                                     &mD3dCallbacks,
310                                     &mD3dDriverData,
311                                     &mCallbacks.HALDDExeBuf,
312                                     mpTextures,
313                                     mpFourCC,
314                                     mpvmList))
315 
316     {
317       HeapFree(GetProcessHeap(), 0, mpTextures);
318       HeapFree(GetProcessHeap(), 0, mpFourCC);
319       HeapFree(GetProcessHeap(), 0, mpvmList);
320       HeapFree(GetProcessHeap(), 0, mpModeInfos);
321       DeleteDC(hdc);
322       DeleteObject(hbmp);
323 	  printf("Fail to QueryDirect Draw Object second pass\n");
324       return DD_FALSE;
325     }
326 
327    /*
328       Copy over from HalInfo to DirectDrawGlobal
329    */
330 
331   // this is wrong, cDriverName need be in ASC code not UNICODE
332   //memcpy(mDDrawGlobal.cDriverName, mDisplayAdapter, sizeof(wchar)*MAX_DRIVER_NAME);
333 
334   memcpy(&mDDrawGlobal.vmiData, &mHALInfo.vmiData,sizeof(VIDMEMINFO));
335   memcpy(&mDDrawGlobal.ddCaps,  &mHALInfo.ddCaps,sizeof(DDCORECAPS));
336 
337   mHALInfo.dwNumModes = mcModeInfos;
338   mHALInfo.lpModeInfo = mpModeInfos;
339   mHALInfo.dwMonitorFrequency = mpModeInfos[0].wRefreshRate;
340 
341   mDDrawGlobal.dwMonitorFrequency = mHALInfo.dwMonitorFrequency;
342   mDDrawGlobal.dwModeIndex        = mHALInfo.dwModeIndex;
343   mDDrawGlobal.dwNumModes         = mHALInfo.dwNumModes;
344   mDDrawGlobal.lpModeInfo         = mHALInfo.lpModeInfo;
345   mDDrawGlobal.hInstance          = mHALInfo.hInstance;
346 
347   mDDrawGlobal.lp16DD = &mDDrawGlobal;
348 
349 
350 
351   /* Hal insate is down now */
352 
353   /* cleare surface code now*/
354 
355   //  memset(&mGlobal, 0, sizeof(DDRAWI_DDRAWSURFACE_GBL));
356   //  memset(&mMore,   0, sizeof(DDRAWI_DDRAWSURFACE_MORE));
357 
358    /* mLocal.lpSurfMore = &mMore;
359    memset(mMore, 0, sizeof(DDRAWI_DDRAWSURFACE_MORE));
360    mMore.dwSize = sizeof(DDRAWI_DDRAWSURFACE_MORE);*/
361 
362    mPrimaryLocal.lpSurfMore = &mPrimaryMore;
363 
364   /* cleare surface ends now */
365 
366   /* create primare surface now */
367 
368    memset(&mddsdPrimary,   0, sizeof(DDSURFACEDESC));
369    mddsdPrimary.dwSize      = sizeof(DDSURFACEDESC);
370    mddsdPrimary.dwFlags     = DDSD_CAPS;
371    mddsdPrimary.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_VIDEOMEMORY | DDSCAPS_VISIBLE;
372 
373    mDdCanCreateSurface.lpDD = &mDDrawGlobal;
374    mDdCanCreateSurface.CanCreateSurface = mCallbacks.HALDD.CanCreateSurface;
375    mDdCanCreateSurface.bIsDifferentPixelFormat = FALSE; //isDifferentPixelFormat;
376    mDdCanCreateSurface.lpDDSurfaceDesc = &mddsdPrimary; // pDDSD;
377 
378    if (mHALInfo.lpDDCallbacks->CanCreateSurface(&mDdCanCreateSurface)== DDHAL_DRIVER_NOTHANDLED)
379    {
380      printf("Fail to mDdCanCreateSurface DDHAL_DRIVER_NOTHANDLED\n");
381      return DDERR_NOTINITIALIZED;
382    }
383 
384    if (mDdCanCreateSurface.ddRVal != DD_OK)
385    {
386 	   printf("Fail to mDdCanCreateSurface mDdCanCreateSurface.ddRVal = %d:%s\n",(int)mDdCanCreateSurface.ddRVal,DDErrorString(mDdCanCreateSurface.ddRVal));
387        return DDERR_NOTINITIALIZED;
388    }
389 
390   memset(&mPrimaryGlobal, 0, sizeof(DDRAWI_DDRAWSURFACE_GBL));
391   mPrimaryGlobal.dwGlobalFlags = DDRAWISURFGBL_ISGDISURFACE;
392   mPrimaryGlobal.lpDD       = &mDDrawGlobal;
393   mPrimaryGlobal.lpDDHandle = &mDDrawGlobal;
394   mPrimaryGlobal.wWidth  = (WORD)mpModeInfos[0].dwWidth;
395   mPrimaryGlobal.wHeight = (WORD)mpModeInfos[0].dwHeight;
396   mPrimaryGlobal.lPitch  = mpModeInfos[0].lPitch;
397 
398   memset(&mPrimaryMore,   0, sizeof(DDRAWI_DDRAWSURFACE_MORE));
399   mPrimaryMore.dwSize = sizeof(DDRAWI_DDRAWSURFACE_MORE);
400 
401 
402   //mPrimaryMore. = mpr
403 
404   memset(&mPrimaryLocal,  0, sizeof(DDRAWI_DDRAWSURFACE_LCL));
405   mPrimaryLocal.lpGbl = &mPrimaryGlobal;
406   mPrimaryLocal.lpSurfMore = &mPrimaryMore;
407   mPrimaryLocal.dwProcessId = GetCurrentProcessId();
408   mPrimaryLocal.dwFlags = DDRAWISURF_PARTOFPRIMARYCHAIN|DDRAWISURF_HASOVERLAYDATA;
409   mPrimaryLocal.ddsCaps.dwCaps = mddsdPrimary.ddsCaps.dwCaps;
410 
411   mpPrimaryLocals[0] = &mPrimaryLocal;
412 
413   mDdCreateSurface.lpDD = &mDDrawGlobal;
414   mDdCreateSurface.CreateSurface = mCallbacks.HALDD.CreateSurface;
415   mDdCreateSurface.lpDDSurfaceDesc = &mddsdPrimary;//pDDSD;
416   mDdCreateSurface.lplpSList = mpPrimaryLocals; //cSurfaces;
417   mDdCreateSurface.dwSCnt = 1 ;  //ppSurfaces;
418 
419   if (mHALInfo.lpDDCallbacks->CreateSurface(&mDdCreateSurface) == DDHAL_DRIVER_NOTHANDLED)
420   {
421     printf("Fail to mDdCreateSurface  DDHAL_DRIVER_NOTHANDLED \n");
422 	return DDERR_NOTINITIALIZED;
423   }
424 
425 
426   if (mDdCreateSurface.ddRVal != DD_OK)
427   {
428     printf("Fail to mDdCanCreateSurface mDdCreateSurface.ddRVal = %d:%s\n",(int)mDdCreateSurface.ddRVal,DDErrorString(mDdCreateSurface.ddRVal));
429     return mDdCreateSurface.ddRVal;
430   }
431 
432   // -- Setup Clipper ---------------------------------------------------------
433   memset(&mPrimaryClipperGlobal, 0, sizeof(DDRAWI_DDRAWCLIPPER_GBL));
434   mPrimaryClipperGlobal.dwFlags = DDRAWICLIP_ISINITIALIZED;
435   mPrimaryClipperGlobal.dwProcessId = GetCurrentProcessId();
436   //mPrimaryClipperGlobal.hWnd = (ULONG_PTR)hwnd;
437   mPrimaryClipperGlobal.hWnd = (ULONG_PTR)GetDesktopWindow();
438   mPrimaryClipperGlobal.lpDD = &mDDrawGlobal;
439   mPrimaryClipperGlobal.lpStaticClipList = NULL;
440 
441   memset(&mPrimaryClipperLocal, 0, sizeof(DDRAWI_DDRAWCLIPPER_LCL));
442   mPrimaryClipperLocal.lpGbl = &mPrimaryClipperGlobal;
443 
444   //memset(&mPrimaryClipperInterface, 0, sizeof(DDRAWI_DDRAWCLIPPER_INT));
445   //mPrimaryClipperInterface.lpLcl = &mPrimaryClipperLocal;
446   //mPrimaryClipperInterface.dwIntRefCnt = 1;
447   //mPrimaryClipperInterface.lpLink = null;
448   //mPrimaryClipperInterface.lpVtbl = null;
449 
450   mPrimaryLocal.lpDDClipper = &mPrimaryClipperLocal;
451   //mPrimaryMore.lpDDIClipper = &mPrimaryClipperInterface;
452 
453   mDdBlt.lpDDDestSurface = mpPrimaryLocals[0];
454 
455 
456   /* create primare surface is down now */
457 
458   /* create overlay surface now */
459 
460   memset(&mddsdOverlay, 0, sizeof(DDSURFACEDESC));
461   mddsdOverlay.dwSize = sizeof(DDSURFACEDESC);
462   mddsdOverlay.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_BACKBUFFERCOUNT | DDSD_WIDTH | DDSD_HEIGHT;
463 
464   mddsdOverlay.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
465 
466   mddsdOverlay.dwWidth = 100;  //pels;
467   mddsdOverlay.dwHeight = 100; // lines;
468   mddsdOverlay.dwBackBufferCount = 1; //cBuffers;
469 
470   mddsdOverlay.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
471   mddsdOverlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
472   mddsdOverlay.ddpfPixelFormat.dwRGBBitCount = 32;
473 
474 
475    //DDHAL_CANCREATESURFACEDATA   mDdCanCreateSurface;
476    mDdCanCreateSurface.lpDD = &mDDrawGlobal;
477    mDdCanCreateSurface.CanCreateSurface = mCallbacks.HALDD.CanCreateSurface;
478    mDdCanCreateSurface.bIsDifferentPixelFormat = TRUE; //isDifferentPixelFormat;
479    mDdCanCreateSurface.lpDDSurfaceDesc = &mddsdOverlay; // pDDSD;
480 
481 
482    if (mHALInfo.lpDDCallbacks->CanCreateSurface(&mDdCanCreateSurface)== DDHAL_DRIVER_NOTHANDLED)
483    {
484     // derr(L"DirectDrawImpl[%08x]::__createPrimary Cannot create primary [%08x]", this, rv);
485 
486 	 printf("Fail to mDdCanCreateSurface DDHAL_DRIVER_NOTHANDLED \n");
487     return DDERR_NOTINITIALIZED;
488    }
489 
490    if (mDdCanCreateSurface.ddRVal != DD_OK)
491    {
492 	 printf("Fail to mDdCanCreateSurface mDdCanCreateSurface.ddRVal = %d:%s\n",(int)mDdCanCreateSurface.ddRVal,DDErrorString(mDdCanCreateSurface.ddRVal));
493      return DDERR_NOTINITIALIZED;
494    }
495 
496 
497   memset(&mOverlayGlobal, 0, sizeof(DDRAWI_DDRAWSURFACE_GBL));
498   mOverlayGlobal.dwGlobalFlags = 0;
499   mOverlayGlobal.lpDD       = &mDDrawGlobal;
500   mOverlayGlobal.lpDDHandle = &mDDrawGlobal;
501   mOverlayGlobal.wWidth  = (WORD)mddsdOverlay.dwWidth;
502   mOverlayGlobal.wHeight = (WORD)mddsdOverlay.dwHeight;
503   mOverlayGlobal.lPitch  = -1;
504   mOverlayGlobal.ddpfSurface = mddsdOverlay.ddpfPixelFormat;
505 
506   // setup front- and backbuffer surfaces
507   cSurfaces = mddsdOverlay.dwBackBufferCount + 1;
508   for ( i = 0; i < cSurfaces; i++)
509   {
510      memset(&mOverlayMore[i], 0, sizeof(DDRAWI_DDRAWSURFACE_MORE));
511      mOverlayMore[i].dwSize = sizeof(DDRAWI_DDRAWSURFACE_MORE);
512 
513      memset(&mOverlayLocal[i],  0, sizeof(DDRAWI_DDRAWSURFACE_LCL));
514     mOverlayLocal[i].lpGbl = &mOverlayGlobal;
515     mOverlayLocal[i].lpSurfMore = &mOverlayMore[i];
516     mOverlayLocal[i].dwProcessId = GetCurrentProcessId();
517     mOverlayLocal[i].dwFlags = (i == 0) ?
518       (DDRAWISURF_IMPLICITROOT|DDRAWISURF_FRONTBUFFER):
519       (DDRAWISURF_IMPLICITCREATE|DDRAWISURF_BACKBUFFER);
520 
521     mOverlayLocal[i].dwFlags |=
522       DDRAWISURF_ATTACHED|DDRAWISURF_ATTACHED_FROM|
523       DDRAWISURF_HASPIXELFORMAT|
524       DDRAWISURF_HASOVERLAYDATA;
525 
526     mOverlayLocal[i].ddsCaps.dwCaps = mddsdOverlay.ddsCaps.dwCaps;
527     mpOverlayLocals[i] = &mOverlayLocal[i];
528   }
529 
530   for ( i = 0; i < cSurfaces; i++)
531   {
532     j = (i + 1) % cSurfaces;
533 
534 
535     /*if (!mHALInfo.lpDDSurfaceCallbacks->AddAttachedSurface(mpOverlayLocals[i], mpOverlayLocals[j]))
536 	{
537      // derr(L"DirectDrawImpl[%08x]::__setupDevice DdAttachSurface(%d, %d) failed", this, i, j);
538       return DD_FALSE;
539     }*/
540 
541 	if (!DdAttachSurface(mpOverlayLocals[i], mpOverlayLocals[j]))
542 	{
543      // derr(L"DirectDrawImpl[%08x]::__setupDevice DdAttachSurface(%d, %d) failed", this, i, j);
544 		printf("Fail to DdAttachSurface (%d:%d)\n", i, j);
545       return DD_FALSE;
546     }
547 
548   }
549 
550 
551   // DDHAL_CREATESURFACEDATA      mDdCreateSurface;
552   mDdCreateSurface.lpDD = &mDDrawGlobal;
553   mDdCreateSurface.CreateSurface = mCallbacks.HALDD.CreateSurface;
554   mDdCreateSurface.lpDDSurfaceDesc = &mddsdOverlay;//pDDSD;
555   mDdCreateSurface.lplpSList = mpOverlayLocals; //cSurfaces;
556   mDdCreateSurface.dwSCnt = 1 ;  //ppSurfaces;
557 
558  if (mHALInfo.lpDDCallbacks->CreateSurface(&mDdCreateSurface) == DDHAL_DRIVER_NOTHANDLED)
559   {
560 	printf("Fail to mDdCreateSurface = DDHAL_DRIVER_HANDLED\n");
561 	return DDERR_NOTINITIALIZED;
562   }
563 
564 
565   if (mDdCreateSurface.ddRVal != DD_OK)
566   {
567 	 printf("Fail to mDdCreateSurface mDdCreateSurface.ddRVal = %d:%s\n",(int)mDdCreateSurface.ddRVal,DDErrorString(mDdCreateSurface.ddRVal));
568      return mDdCreateSurface.ddRVal;
569   }
570 
571 
572   mDdUpdateOverlay.lpDD = &mDDrawGlobal;
573   mDdUpdateOverlay.UpdateOverlay = mCallbacks.HALDDSurface.UpdateOverlay;
574   mDdUpdateOverlay.lpDDDestSurface = mpPrimaryLocals[0];
575   mDdUpdateOverlay.lpDDSrcSurface = mpOverlayLocals[0];//pDDSurface;
576   mDdUpdateOverlay.dwFlags = DDOVER_SHOW;
577 
578  /* if (flags & DDOVER_DDFX)
579     mDdUpdateOverlay.overlayFX = *pFx;
580   copyRect(&mDdUpdateOverlay.rDest, pdst);
581   copyRect(&mDdUpdateOverlay.rSrc, psrc);
582 */
583 
584   mDdUpdateOverlay.rDest.top = 0;
585   mDdUpdateOverlay.rDest.left = 0;
586   mDdUpdateOverlay.rDest.right = 50;
587   mDdUpdateOverlay.rDest.bottom = 50;
588 
589    mDdUpdateOverlay.rSrc.top = 0;
590   mDdUpdateOverlay.rSrc.left = 0;
591   mDdUpdateOverlay.rSrc.right = 50;
592   mDdUpdateOverlay.rSrc.bottom = 50;
593 
594 
595 
596 
597   if ( mDdUpdateOverlay.UpdateOverlay(&mDdUpdateOverlay) == DDHAL_DRIVER_NOTHANDLED)
598   {
599 	 printf("Fail to mDdBlt = DDHAL_DRIVER_HANDLED\n");
600 	 return DDERR_NOTINITIALIZED;
601   }
602 
603 
604   if (mDdUpdateOverlay.ddRVal != DD_OK)
605   {
606 	  printf("Fail to mDdUpdateOverlay mDdUpdateOverlay.ddRVal = %d:%s\n",(int)mDdUpdateOverlay.ddRVal,DDErrorString(mDdUpdateOverlay.ddRVal));
607       return mDdUpdateOverlay.ddRVal;
608   }
609 
610   /* blt */
611 
612 
613   pDDSurface = mpPrimaryLocals[0];
614 
615   if (!DdResetVisrgn(pDDSurface, NULL))
616   {
617    // derr(L"DirectDrawImpl[%08x]::_clear DdResetVisrgn failed", this);
618   }
619 
620 
621   memset(&mDdBlt, 0, sizeof(DDHAL_BLTDATA));
622   memset(&mDdBlt.bltFX, 0, sizeof(DDBLTFX));
623   mDdBlt.bltFX.dwSize = sizeof(DDBLTFX);
624 
625   mDdBlt.lpDD = &mDDrawGlobal;
626   mDdBlt.Blt = mCallbacks.HALDDSurface.Blt;
627   mDdBlt.lpDDDestSurface = mpPrimaryLocals[0];
628 
629   mpPrimaryLocals[0]->hDC = (ULONG_PTR)GetDC(GetDesktopWindow());
630   mDdBlt.rDest.top = 50;
631   mDdBlt.rDest.bottom = 100;
632   mDdBlt.rDest.left = 0;
633   mDdBlt.rDest.right = 100;
634   mDdBlt.lpDDSrcSurface = NULL;
635   mDdBlt.IsClipped = FALSE;
636   mDdBlt.bltFX.dwFillColor = 0xFFFF00;
637   mDdBlt.dwFlags = DDBLT_COLORFILL | DDBLT_WAIT;
638  // mDdBlt.IsClipped = TRUE;
639 
640     if (mDdBlt.Blt(&mDdBlt) != DDHAL_DRIVER_HANDLED)
641 	{
642       printf("Fail to mDdBlt = DDHAL_DRIVER_HANDLED\n");
643 	  return DDHAL_DRIVER_HANDLED;
644     }
645 
646 
647 
648     if (mDdBlt.ddRVal!=DD_OK)
649 	{
650 		printf("Fail to mDdBlt mDdBlt.ddRVal = %d:%s\n",(int)mDdBlt.ddRVal,DDErrorString(mDdBlt.ddRVal));
651 		return mDdBlt.ddRVal;
652     }
653 
654   mDdUpdateOverlay.rDest.right = 100;
655    if ( mDdUpdateOverlay.UpdateOverlay(&mDdUpdateOverlay) == DDHAL_DRIVER_NOTHANDLED)
656   {
657     printf("Fail to mDdUpdateOverlay = DDERR_NOTINITIALIZED\n");
658 	return DDERR_NOTINITIALIZED;
659   }
660 
661   while(TRUE);
662 
663   return DD_OK;
664 
665 
666 }
667 
668 
669