5 static IDirect3D8 *Direct3D=NULL;
\r
6 IDirect3DDevice8 *Device=NULL;
\r
7 IDirect3DSurface8 *DirectBack=NULL; // Back Buffer
\r
9 static IDirect3DVertexBuffer8 *VertexBuffer=NULL;
\r
13 float x,y,z; // Vertex cordinates
\r
14 unsigned int colour;
\r
15 float u,v; // Texture coordinates
\r
17 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
\r
19 static CustomVertex VertexList[4];
\r
25 LPDIRECTDRAW7 m_pDD = NULL;
\r
26 LPDIRECTDRAWSURFACE7 m_pddsFrontBuffer = NULL;
\r
27 LPDIRECTDRAWSURFACE7 m_pddsBackBuffer = NULL;
\r
29 // quick and dirty stuff..
\r
30 static void DirectExitDDraw()
\r
32 RELEASE(m_pddsBackBuffer);
\r
33 RELEASE(m_pddsFrontBuffer);
\r
37 static int DirectDrawInit()
\r
40 LPDIRECTDRAWCLIPPER pcClipper = NULL;
\r
41 DDSURFACEDESC2 ddsd;
\r
43 ret = DirectDrawCreateEx(NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL);
\r
44 if (ret) { LOGFAIL(); return 1; }
\r
46 // Set cooperative level
\r
47 ret = m_pDD->SetCooperativeLevel( FrameWnd, DDSCL_NORMAL );
\r
48 if (ret) { LOGFAIL(); goto fail; }
\r
50 // Create the primary surface
\r
51 ZeroMemory( &ddsd, sizeof( ddsd ) );
\r
52 ddsd.dwSize = sizeof( ddsd );
\r
53 ddsd.dwFlags = DDSD_CAPS;
\r
54 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
\r
56 ret = m_pDD->CreateSurface( &ddsd, &m_pddsFrontBuffer, NULL );
\r
57 if (ret) { LOGFAIL(); goto fail; }
\r
59 // Create the backbuffer surface
\r
60 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
\r
61 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
\r
62 ddsd.dwWidth = EmuWidth;
\r
63 ddsd.dwHeight = EmuHeight;
\r
65 ret = m_pDD->CreateSurface( &ddsd, &m_pddsBackBuffer, NULL );
\r
66 if (ret) { LOGFAIL(); goto fail; }
\r
69 ret = m_pDD->CreateClipper( 0, &pcClipper, NULL );
\r
70 if (ret) { LOGFAIL(); goto fail; }
\r
72 ret = pcClipper->SetHWnd( 0, FrameWnd );
\r
73 if (ret) { LOGFAIL(); goto fail; }
\r
75 ret = m_pddsFrontBuffer->SetClipper( pcClipper );
\r
76 if (ret) { LOGFAIL(); goto fail; }
\r
87 static int DirectScreenDDraw()
\r
90 unsigned short *ps=EmuScreen;
\r
93 memset(&sd, 0, sizeof(sd));
\r
94 sd.dwSize = sizeof(sd);
\r
95 ret = m_pddsBackBuffer->Lock(NULL, &sd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT|DDLOCK_WRITEONLY, NULL);
\r
96 if (ret) { LOGFAIL(); return 1; }
\r
98 //lprintf("w: %i h: %i pi: %i pf: %i\n", sd.dwWidth, sd.dwHeight, sd.lPitch, sd.ddpfPixelFormat.dwRGBBitCount);
\r
100 if (sd.ddpfPixelFormat.dwRGBBitCount == 32)
\r
102 int *dst = (int *)sd.lpSurface;
\r
103 for (y = 0; y < EmuHeight; y++)
\r
105 for (x = 0; x < EmuWidth; x++)
\r
108 dst[x] = ((s&0xf800)<<8) | ((s&0x07e0)<<5) | ((s&0x001f)<<3);
\r
110 dst = (int *)((char *)dst + sd.lPitch);
\r
113 else if (sd.ddpfPixelFormat.dwRGBBitCount == 24) /* wine uses this for me */
\r
115 void *dst = sd.lpSurface;
\r
116 for (y = 0; y < EmuHeight; y++)
\r
118 unsigned char *dst1 = (unsigned char *) dst;
\r
119 for (x = 0; x < EmuWidth; x++, dst1 += 3)
\r
122 dst1[2] = (s&0xf800)>>8; dst1[1] = (s&0x07e0)>>3; dst1[0] = s<<3; // BGR
\r
124 dst = (void *)((char *)dst + sd.lPitch);
\r
127 else if (sd.ddpfPixelFormat.dwRGBBitCount == 16)
\r
129 unsigned short *dst = (unsigned short *)sd.lpSurface;
\r
130 for (y = 0; y < EmuHeight; y++)
\r
132 memcpy(dst, ps, EmuWidth*2);
\r
134 dst = (unsigned short *)((char *)dst + sd.lPitch);
\r
142 ret = m_pddsBackBuffer->Unlock(NULL);
\r
143 if (ret) { LOGFAIL(); return 1; }
\r
147 static int DirectClearDDraw(unsigned int colour)
\r
151 ZeroMemory( &ddbltfx, sizeof(ddbltfx) );
\r
152 ddbltfx.dwSize = sizeof(ddbltfx);
\r
153 ddbltfx.dwFillColor = colour;
\r
155 if (m_pddsBackBuffer != NULL)
\r
156 ret = m_pddsBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );
\r
157 if (ret) { LOGFAIL(); return 1; }
\r
161 static int DirectPresentDDraw()
\r
164 if (FrameRectMy.right - FrameRectMy.left > 0 && FrameRectMy.bottom - FrameRectMy.top > 0)
\r
165 ret = m_pddsFrontBuffer->Blt(&FrameRectMy, m_pddsBackBuffer, &EmuScreenRect, DDBLT_WAIT, NULL);
\r
166 if (ret) { LOGFAIL(); return 1; }
\r
176 D3DPRESENT_PARAMETERS d3dpp;
\r
177 D3DDISPLAYMODE mode;
\r
180 memset(&d3dpp,0,sizeof(d3dpp));
\r
181 memset(&mode,0,sizeof(mode));
\r
183 Direct3D=Direct3DCreate8(D3D_SDK_VERSION); if (Direct3D==NULL) return 1;
\r
185 // Set up the structure used to create the D3D device:
\r
186 d3dpp.BackBufferWidth =MainWidth;
\r
187 d3dpp.BackBufferHeight=MainHeight;
\r
188 d3dpp.BackBufferCount =1;
\r
189 d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
\r
190 d3dpp.MultiSampleType =D3DMULTISAMPLE_NONE;
\r
193 d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;
\r
194 d3dpp.FullScreen_RefreshRateInHz=60;
\r
196 Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&mode);
\r
197 d3dpp.BackBufferFormat=mode.Format;
\r
199 d3dpp.hDeviceWindow=FrameWnd;
\r
202 // Try to create a device with hardware vertex processing:
\r
205 int behave=D3DCREATE_HARDWARE_VERTEXPROCESSING;
\r
207 // Try software vertex processing:
\r
208 if (i==1) behave=D3DCREATE_MIXED_VERTEXPROCESSING;
\r
209 if (i==2) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
\r
211 Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,FrameWnd,
\r
212 behave|D3DCREATE_MULTITHREADED,&d3dpp,&Device);
\r
220 Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,FrameWnd,
\r
221 D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&d3dpp,&Device);
\r
222 if (Device==NULL) goto fail0;
\r
223 HMODULE test = LoadLibrary("d3d8d.dll");
\r
224 if (test != NULL) FreeLibrary(test);
\r
226 error("Sorry, but this program requires Direct3D with hardware acceleration.\n\n"
\r
227 "You can try using Direct3D software emulation, but you have to install "
\r
228 "DirectX SDK for it to work\n(it seems to be missing now).");
\r
236 Device->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&DirectBack);
\r
237 if (DirectBack==NULL) goto fail1;
\r
239 Device->CreateVertexBuffer(sizeof(VertexList),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&VertexBuffer);
\r
240 if (VertexBuffer==NULL) goto fail2;
\r
242 ret=TexScreenInit(); if (ret) goto fail3;
\r
246 Device->SetRenderState(D3DRS_LIGHTING,0); // Turn off lighting
\r
248 // Set up texture modes:
\r
249 Device->SetTextureStageState(0,D3DTSS_ADDRESSU,D3DTADDRESS_CLAMP);
\r
250 Device->SetTextureStageState(0,D3DTSS_ADDRESSV,D3DTADDRESS_CLAMP);
\r
255 RELEASE(VertexBuffer)
\r
257 RELEASE(DirectBack)
\r
263 // error("Failed to use Direct3D, trying DirectDraw..");
\r
266 return DirectDrawInit();
\r
275 RELEASE(VertexBuffer)
\r
276 RELEASE(DirectBack)
\r
283 int DirectClear(unsigned int colour)
\r
286 if (Device != NULL) {
\r
287 Device->Clear(0,NULL,D3DCLEAR_TARGET,colour,1.0f,0);
\r
292 return DirectClearDDraw(colour);
\r
295 int DirectPresent()
\r
298 if (Device != NULL) {
\r
299 Device->Present(NULL,NULL,NULL,NULL);
\r
304 return DirectPresentDDraw();
\r
308 static int MakeVertexList()
\r
310 struct CustomVertex *vert=NULL,*pv=NULL;
\r
312 float scalex=0.0f,scaley=0.0f;
\r
313 unsigned int colour=0xffffff;
\r
314 float right=0.0f,bottom=0.0f;
\r
316 if (LoopMode!=8) colour=0x102040;
\r
318 dist=10.0f; scalex=dist*1.3333f; scaley=dist;
\r
320 scalex*=640.0f/(float)MainWidth;
\r
321 scaley*=448.0f/(float)MainHeight;
\r
325 // Put the vertices for the corners of the screen:
\r
328 pv->x=-scalex; pv->y=scaley;
\r
329 pv->colour=colour; pv++;
\r
331 *pv=vert[0]; pv->x= scalex; pv->y= scaley; pv++;
\r
332 *pv=vert[0]; pv->x=-scalex; pv->y=-scaley; pv++;
\r
333 *pv=vert[0]; pv->x= scalex; pv->y=-scaley; pv++;
\r
335 // Find where the screen images ends on the texture
\r
336 right =(float)EmuWidth /(float)TexWidth;
\r
337 bottom=(float)EmuHeight/(float)TexHeight;
\r
339 // Write texture coordinates:
\r
341 pv->u=0.0f; pv->v=0.00f; pv++;
\r
342 pv->u=right; pv->v=0.00f; pv++;
\r
343 pv->u=0.0f; pv->v=bottom; pv++;
\r
344 pv->u=right; pv->v=bottom; pv++;
\r
349 static int SetupMatrices()
\r
351 D3DXVECTOR3 eye ( 0.0f, 0.0f, 0.0f );
\r
352 D3DXVECTOR3 look( 0.0f, 0.0f, 0.0f );
\r
353 D3DXVECTOR3 up ( 0.0f, 1.0f, 0.0f );
\r
355 float nudgex=0.0f,nudgey=0.0f;
\r
357 memset(&mat,0,sizeof(mat));
\r
359 mat.m[0][0]=mat.m[1][1]=mat.m[2][2]=mat.m[3][3]=1.0f;
\r
360 Device->SetTransform(D3DTS_WORLD,&mat);
\r
362 look.x=(float)Inp.axis[2]/2457.6f;
\r
363 look.y=(float)Inp.axis[3]/2457.6f;
\r
366 // Nudge pixels to the centre of each screen pixel:
\r
367 nudgex=13.3333f/(float)(MainWidth <<1);
\r
368 nudgey=10.0000f/(float)(MainHeight<<1);
\r
369 eye.x +=nudgex; eye.y +=nudgey;
\r
370 look.x+=nudgex; look.y+=nudgey;
\r
372 D3DXMatrixLookAtLH(&mat,&eye,&look,&up);
\r
373 Device->SetTransform(D3DTS_VIEW,&mat);
\r
375 D3DXMatrixPerspectiveFovLH(&mat, 0.5f*PI, 1.3333f, 0.2f, 1000.0f);
\r
376 Device->SetTransform(D3DTS_PROJECTION,&mat);
\r
382 unsigned char *lock=NULL;
\r
385 if (Device == NULL)
\r
386 return DirectScreenDDraw();
\r
388 // Copy the screen to the screen texture:
\r
390 TexScreenSwizzle();
\r
392 ret=TexScreenLinear();
\r
393 if (ret) lprintf("TexScreenLinear failed\n");
\r
400 // Copy vertices in:
\r
401 VertexBuffer->Lock(0,sizeof(VertexList),&lock,0);
\r
402 if (lock==NULL) { lprintf("VertexBuffer->Lock failed\n"); return 1; }
\r
403 memcpy(lock,VertexList,sizeof(VertexList));
\r
404 VertexBuffer->Unlock();
\r
406 Device->BeginScene();
\r
407 Device->SetTexture(0,TexScreen);
\r
408 Device->SetStreamSource(0,VertexBuffer,sizeof(CustomVertex));
\r
409 Device->SetVertexShader(D3DFVF_CUSTOMVERTEX);
\r
410 Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
\r
411 Device->EndScene();
\r
418 return DirectScreenDDraw();
\r