3 #include "../common/lprintf.h"
\r
8 #define EmuHeight 240
\r
10 #define RELEASE(x) if (x) x->Release(); x=NULL;
\r
11 #define LOGFAIL() lprintf("fail: %s %s:%i\n", __FUNCTION__, __FILE__, __LINE__)
\r
13 static LPDIRECTDRAW7 m_pDD = NULL;
\r
14 static LPDIRECTDRAWSURFACE7 m_pddsFrontBuffer = NULL;
\r
15 static LPDIRECTDRAWSURFACE7 m_pddsBackBuffer = NULL;
\r
17 // quick and dirty stuff..
\r
18 void DirectExit(void)
\r
20 RELEASE(m_pddsBackBuffer);
\r
21 RELEASE(m_pddsFrontBuffer);
\r
25 int DirectInit(void)
\r
28 LPDIRECTDRAWCLIPPER pcClipper = NULL;
\r
29 DDSURFACEDESC2 ddsd;
\r
31 ret = DirectDrawCreateEx(NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL);
\r
32 if (ret) { LOGFAIL(); return 1; }
\r
34 // Set cooperative level
\r
35 ret = m_pDD->SetCooperativeLevel( FrameWnd, DDSCL_NORMAL );
\r
36 if (ret) { LOGFAIL(); goto fail; }
\r
38 // Create the primary surface
\r
39 ZeroMemory( &ddsd, sizeof( ddsd ) );
\r
40 ddsd.dwSize = sizeof( ddsd );
\r
41 ddsd.dwFlags = DDSD_CAPS;
\r
42 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
\r
44 ret = m_pDD->CreateSurface( &ddsd, &m_pddsFrontBuffer, NULL );
\r
45 if (ret) { LOGFAIL(); goto fail; }
\r
47 // Create the backbuffer surface
\r
48 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
\r
49 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
\r
50 ddsd.dwWidth = EmuWidth;
\r
51 ddsd.dwHeight = EmuHeight;
\r
53 ret = m_pDD->CreateSurface( &ddsd, &m_pddsBackBuffer, NULL );
\r
54 if (ret) { LOGFAIL(); goto fail; }
\r
57 ret = m_pDD->CreateClipper( 0, &pcClipper, NULL );
\r
58 if (ret) { LOGFAIL(); goto fail; }
\r
60 ret = pcClipper->SetHWnd( 0, FrameWnd );
\r
61 if (ret) { LOGFAIL(); goto fail; }
\r
63 ret = m_pddsFrontBuffer->SetClipper( pcClipper );
\r
64 if (ret) { LOGFAIL(); goto fail; }
\r
75 int DirectScreen(const void *emu_screen)
\r
77 const unsigned short *ps = (const unsigned short *)emu_screen;
\r
81 memset(&sd, 0, sizeof(sd));
\r
82 sd.dwSize = sizeof(sd);
\r
83 ret = m_pddsBackBuffer->Lock(NULL, &sd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT|DDLOCK_WRITEONLY, NULL);
\r
84 if (ret) { LOGFAIL(); return 1; }
\r
86 //lprintf("w: %i h: %i pi: %i pf: %i\n", sd.dwWidth, sd.dwHeight, sd.lPitch, sd.ddpfPixelFormat.dwRGBBitCount);
\r
88 if (sd.ddpfPixelFormat.dwRGBBitCount == 32)
\r
90 int *dst = (int *)sd.lpSurface;
\r
91 for (y = 0; y < EmuHeight; y++)
\r
93 for (x = 0; x < EmuWidth; x++)
\r
96 dst[x] = ((s&0xf800)<<8) | ((s&0x07e0)<<5) | ((s&0x001f)<<3);
\r
98 dst = (int *)((char *)dst + sd.lPitch);
\r
101 else if (sd.ddpfPixelFormat.dwRGBBitCount == 24) /* wine uses this for me */
\r
103 void *dst = sd.lpSurface;
\r
104 for (y = 0; y < EmuHeight; y++)
\r
106 unsigned char *dst1 = (unsigned char *) dst;
\r
107 for (x = 0; x < EmuWidth; x++, dst1 += 3)
\r
110 dst1[2] = (s&0xf800)>>8; dst1[1] = (s&0x07e0)>>3; dst1[0] = s<<3; // BGR
\r
112 dst = (void *)((char *)dst + sd.lPitch);
\r
115 else if (sd.ddpfPixelFormat.dwRGBBitCount == 16)
\r
117 unsigned short *dst = (unsigned short *)sd.lpSurface;
\r
118 for (y = 0; y < EmuHeight; y++)
\r
120 memcpy(dst, ps, EmuWidth*2);
\r
122 dst = (unsigned short *)((char *)dst + sd.lPitch);
\r
130 ret = m_pddsBackBuffer->Unlock(NULL);
\r
131 if (ret) { LOGFAIL(); return 1; }
\r
135 int DirectClear(unsigned int colour)
\r
139 ZeroMemory( &ddbltfx, sizeof(ddbltfx) );
\r
140 ddbltfx.dwSize = sizeof(ddbltfx);
\r
141 ddbltfx.dwFillColor = colour;
\r
143 if (m_pddsBackBuffer != NULL)
\r
144 ret = m_pddsBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );
\r
145 if (ret) { LOGFAIL(); return 1; }
\r
149 int DirectPresent(void)
\r
152 if (FrameRectMy.right - FrameRectMy.left > 0 && FrameRectMy.bottom - FrameRectMy.top > 0)
\r
153 ret = m_pddsFrontBuffer->Blt(&FrameRectMy, m_pddsBackBuffer, &EmuScreenRect, DDBLT_WAIT, NULL);
\r
154 if (ret) { LOGFAIL(); return 1; }
\r
161 static IDirect3D8 *Direct3D=NULL;
\r
162 IDirect3DDevice8 *Device=NULL;
\r
163 IDirect3DSurface8 *DirectBack=NULL; // Back Buffer
\r
165 static IDirect3DVertexBuffer8 *VertexBuffer=NULL;
\r
167 struct CustomVertex
\r
169 float x,y,z; // Vertex cordinates
\r
170 unsigned int colour;
\r
171 float u,v; // Texture coordinates
\r
173 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
\r
175 static CustomVertex VertexList[4];
\r
179 D3DPRESENT_PARAMETERS d3dpp;
\r
180 D3DDISPLAYMODE mode;
\r
183 memset(&d3dpp,0,sizeof(d3dpp));
\r
184 memset(&mode,0,sizeof(mode));
\r
186 Direct3D=Direct3DCreate8(D3D_SDK_VERSION); if (Direct3D==NULL) return 1;
\r
188 // Set up the structure used to create the D3D device:
\r
189 d3dpp.BackBufferWidth =MainWidth;
\r
190 d3dpp.BackBufferHeight=MainHeight;
\r
191 d3dpp.BackBufferCount =1;
\r
192 d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
\r
193 d3dpp.MultiSampleType =D3DMULTISAMPLE_NONE;
\r
196 d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;
\r
197 d3dpp.FullScreen_RefreshRateInHz=60;
\r
199 Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&mode);
\r
200 d3dpp.BackBufferFormat=mode.Format;
\r
202 d3dpp.hDeviceWindow=FrameWnd;
\r
205 // Try to create a device with hardware vertex processing:
\r
208 int behave=D3DCREATE_HARDWARE_VERTEXPROCESSING;
\r
210 // Try software vertex processing:
\r
211 if (i==1) behave=D3DCREATE_MIXED_VERTEXPROCESSING;
\r
212 if (i==2) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
\r
214 Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,FrameWnd,
\r
215 behave|D3DCREATE_MULTITHREADED,&d3dpp,&Device);
\r
223 Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,FrameWnd,
\r
224 D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&d3dpp,&Device);
\r
225 if (Device==NULL) goto fail0;
\r
226 HMODULE test = LoadLibrary("d3d8d.dll");
\r
227 if (test != NULL) FreeLibrary(test);
\r
229 error("Sorry, but this program requires Direct3D with hardware acceleration.\n\n"
\r
230 "You can try using Direct3D software emulation, but you have to install "
\r
231 "DirectX SDK for it to work\n(it seems to be missing now).");
\r
239 Device->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&DirectBack);
\r
240 if (DirectBack==NULL) goto fail1;
\r
242 Device->CreateVertexBuffer(sizeof(VertexList),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&VertexBuffer);
\r
243 if (VertexBuffer==NULL) goto fail2;
\r
245 ret=TexScreenInit(); if (ret) goto fail3;
\r
249 Device->SetRenderState(D3DRS_LIGHTING,0); // Turn off lighting
\r
251 // Set up texture modes:
\r
252 Device->SetTextureStageState(0,D3DTSS_ADDRESSU,D3DTADDRESS_CLAMP);
\r
253 Device->SetTextureStageState(0,D3DTSS_ADDRESSV,D3DTADDRESS_CLAMP);
\r
258 RELEASE(VertexBuffer)
\r
260 RELEASE(DirectBack)
\r
274 RELEASE(VertexBuffer)
\r
275 RELEASE(DirectBack)
\r
280 int DirectClear(unsigned int colour)
\r
282 if (Device != NULL) {
\r
283 Device->Clear(0,NULL,D3DCLEAR_TARGET,colour,1.0f,0);
\r
290 int DirectPresent()
\r
292 if (Device != NULL) {
\r
293 Device->Present(NULL,NULL,NULL,NULL);
\r
300 #define PI 3.14159265f
\r
302 static int MakeVertexList()
\r
304 struct CustomVertex *vert=NULL,*pv=NULL;
\r
306 float scalex=0.0f,scaley=0.0f;
\r
307 unsigned int colour=0xffffff;
\r
308 float right=0.0f,bottom=0.0f;
\r
310 if (LoopMode!=8) colour=0x102040;
\r
312 dist=10.0f; scalex=dist*1.3333f; scaley=dist;
\r
314 scalex*=640.0f/(float)MainWidth;
\r
315 scaley*=448.0f/(float)MainHeight;
\r
319 // Put the vertices for the corners of the screen:
\r
322 pv->x=-scalex; pv->y=scaley;
\r
323 pv->colour=colour; pv++;
\r
325 *pv=vert[0]; pv->x= scalex; pv->y= scaley; pv++;
\r
326 *pv=vert[0]; pv->x=-scalex; pv->y=-scaley; pv++;
\r
327 *pv=vert[0]; pv->x= scalex; pv->y=-scaley; pv++;
\r
329 // Find where the screen images ends on the texture
\r
330 right =(float)EmuWidth /(float)TexWidth;
\r
331 bottom=(float)EmuHeight/(float)TexHeight;
\r
333 // Write texture coordinates:
\r
335 pv->u=0.0f; pv->v=0.00f; pv++;
\r
336 pv->u=right; pv->v=0.00f; pv++;
\r
337 pv->u=0.0f; pv->v=bottom; pv++;
\r
338 pv->u=right; pv->v=bottom; pv++;
\r
343 static int SetupMatrices()
\r
345 D3DXVECTOR3 eye ( 0.0f, 0.0f, 0.0f );
\r
346 D3DXVECTOR3 look( 0.0f, 0.0f, 0.0f );
\r
347 D3DXVECTOR3 up ( 0.0f, 1.0f, 0.0f );
\r
349 float nudgex=0.0f,nudgey=0.0f;
\r
351 memset(&mat,0,sizeof(mat));
\r
353 mat.m[0][0]=mat.m[1][1]=mat.m[2][2]=mat.m[3][3]=1.0f;
\r
354 Device->SetTransform(D3DTS_WORLD,&mat);
\r
356 look.x=(float)Inp.axis[2]/2457.6f;
\r
357 look.y=(float)Inp.axis[3]/2457.6f;
\r
360 // Nudge pixels to the centre of each screen pixel:
\r
361 nudgex=13.3333f/(float)(MainWidth <<1);
\r
362 nudgey=10.0000f/(float)(MainHeight<<1);
\r
363 eye.x +=nudgex; eye.y +=nudgey;
\r
364 look.x+=nudgex; look.y+=nudgey;
\r
366 D3DXMatrixLookAtLH(&mat,&eye,&look,&up);
\r
367 Device->SetTransform(D3DTS_VIEW,&mat);
\r
369 D3DXMatrixPerspectiveFovLH(&mat, 0.5f*PI, 1.3333f, 0.2f, 1000.0f);
\r
370 Device->SetTransform(D3DTS_PROJECTION,&mat);
\r
376 unsigned char *lock=NULL;
\r
379 if (Device == NULL)
\r
380 return DirectScreenDDraw();
\r
382 // Copy the screen to the screen texture:
\r
384 TexScreenSwizzle();
\r
386 ret=TexScreenLinear();
\r
387 if (ret) lprintf("TexScreenLinear failed\n");
\r
394 // Copy vertices in:
\r
395 VertexBuffer->Lock(0,sizeof(VertexList),&lock,0);
\r
396 if (lock==NULL) { lprintf("VertexBuffer->Lock failed\n"); return 1; }
\r
397 memcpy(lock,VertexList,sizeof(VertexList));
\r
398 VertexBuffer->Unlock();
\r
400 Device->BeginScene();
\r
401 Device->SetTexture(0,TexScreen);
\r
402 Device->SetStreamSource(0,VertexBuffer,sizeof(CustomVertex));
\r
403 Device->SetVertexShader(D3DFVF_CUSTOMVERTEX);
\r
404 Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
\r
405 Device->EndScene();
\r