4 static IDirect3D8 *Direct3D=NULL;
\r
5 IDirect3DDevice8 *Device=NULL;
\r
6 IDirect3DSurface8 *DirectBack=NULL; // Back Buffer
\r
8 static IDirect3DVertexBuffer8 *VertexBuffer=NULL;
\r
12 float x,y,z; // Vertex cordinates
\r
13 unsigned int colour;
\r
14 float u,v; // Texture coordinates
\r
16 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
\r
18 static CustomVertex VertexList[4];
\r
22 D3DPRESENT_PARAMETERS d3dpp;
\r
23 D3DDISPLAYMODE mode;
\r
26 memset(&d3dpp,0,sizeof(d3dpp));
\r
27 memset(&mode,0,sizeof(mode));
\r
29 Direct3D=Direct3DCreate8(D3D_SDK_VERSION); if (Direct3D==NULL) return 1;
\r
31 // Set up the structure used to create the D3D device:
\r
32 d3dpp.BackBufferWidth =MainWidth;
\r
33 d3dpp.BackBufferHeight=MainHeight;
\r
34 d3dpp.BackBufferCount =1;
\r
35 d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
\r
38 d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;
\r
39 d3dpp.FullScreen_RefreshRateInHz=60;
\r
41 Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&mode);
\r
42 d3dpp.BackBufferFormat=mode.Format;
\r
46 // Try to create a device with hardware vertex processing:
\r
49 int behave=D3DCREATE_HARDWARE_VERTEXPROCESSING;
\r
54 // If 60Hz didn't work, try PAL 50Hz instead:
\r
55 d3dpp.FullScreen_RefreshRateInHz=0;
\r
56 d3dpp.BackBufferHeight=MainHeight=576;
\r
60 // Try software vertex processing:
\r
61 if (i==2) behave=D3DCREATE_MIXED_VERTEXPROCESSING;
\r
62 if (i==3) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
\r
64 Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,FrameWnd,behave,&d3dpp,&Device);
\r
68 if (Device==NULL) return 1;
\r
70 Device->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&DirectBack);
\r
71 if (DirectBack==NULL) return 1;
\r
73 Device->CreateVertexBuffer(sizeof(VertexList),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&VertexBuffer);
\r
74 if (VertexBuffer==NULL) return 1;
\r
76 ret=TexScreenInit(); if (ret) return 1;
\r
80 Device->SetRenderState(D3DRS_LIGHTING,0); // Turn off lighting
\r
82 // Set up texture modes:
\r
83 Device->SetTextureStageState(0,D3DTSS_ADDRESSU,D3DTADDRESS_CLAMP);
\r
84 Device->SetTextureStageState(0,D3DTSS_ADDRESSV,D3DTADDRESS_CLAMP);
\r
93 RELEASE(VertexBuffer)
\r
100 static int MakeVertexList()
\r
102 struct CustomVertex *vert=NULL,*pv=NULL;
\r
104 float scalex=0.0f,scaley=0.0f;
\r
105 unsigned int colour=0xffffff;
\r
106 float right=0.0f,bottom=0.0f;
\r
108 if (LoopMode!=8) colour=0x102040;
\r
110 dist=10.0f; scalex=dist*1.3333f; scaley=dist;
\r
112 scalex*=640.0f/(float)MainWidth;
\r
113 scaley*=448.0f/(float)MainHeight;
\r
117 // Put the vertices for the corners of the screen:
\r
120 pv->x=-scalex; pv->y=scaley;
\r
121 pv->colour=colour; pv++;
\r
123 *pv=vert[0]; pv->x= scalex; pv->y= scaley; pv++;
\r
124 *pv=vert[0]; pv->x=-scalex; pv->y=-scaley; pv++;
\r
125 *pv=vert[0]; pv->x= scalex; pv->y=-scaley; pv++;
\r
127 // Find where the screen images ends on the texture
\r
128 right =(float)EmuWidth /(float)TexWidth;
\r
129 bottom=(float)EmuHeight/(float)TexHeight;
\r
131 // Write texture coordinates:
\r
133 pv->u=0.0f; pv->v=0.00f; pv++;
\r
134 pv->u=right; pv->v=0.00f; pv++;
\r
135 pv->u=0.0f; pv->v=bottom; pv++;
\r
136 pv->u=right; pv->v=bottom; pv++;
\r
141 int DirectClear(unsigned int colour)
\r
143 Device->Clear(0,NULL,D3DCLEAR_TARGET,colour,1.0f,0);
\r
147 int DirectPresent()
\r
149 Device->Present(NULL,NULL,NULL,NULL);
\r
153 static int SetupMatrices()
\r
155 D3DXVECTOR3 eye ( 0.0f, 0.0f, 0.0f );
\r
156 D3DXVECTOR3 look( 0.0f, 0.0f, 0.0f );
\r
157 D3DXVECTOR3 up ( 0.0f, 1.0f, 0.0f );
\r
159 float nudgex=0.0f,nudgey=0.0f;
\r
161 memset(&mat,0,sizeof(mat));
\r
163 mat.m[0][0]=mat.m[1][1]=mat.m[2][2]=mat.m[3][3]=1.0f;
\r
164 Device->SetTransform(D3DTS_WORLD,&mat);
\r
166 look.x=(float)Inp.axis[2]/2457.6f;
\r
167 look.y=(float)Inp.axis[3]/2457.6f;
\r
170 // Nudge pixels to the centre of each screen pixel:
\r
171 nudgex=13.3333f/(float)(MainWidth <<1);
\r
172 nudgey=10.0000f/(float)(MainHeight<<1);
\r
173 eye.x +=nudgex; eye.y +=nudgey;
\r
174 look.x+=nudgex; look.y+=nudgey;
\r
176 D3DXMatrixLookAtLH(&mat,&eye,&look,&up);
\r
177 Device->SetTransform(D3DTS_VIEW,&mat);
\r
179 D3DXMatrixPerspectiveFovLH(&mat, 0.5f*PI, 1.3333f, 0.2f, 1000.0f);
\r
180 Device->SetTransform(D3DTS_PROJECTION,&mat);
\r
186 unsigned char *lock=NULL;
\r
188 // Copy the screen to the screen texture:
\r
190 TexScreenSwizzle();
\r
199 // Copy vertices in:
\r
200 VertexBuffer->Lock(0,sizeof(VertexList),&lock,0); if (lock==NULL) return 1;
\r
201 memcpy(lock,VertexList,sizeof(VertexList));
\r
202 VertexBuffer->Unlock();
\r
204 Device->SetTexture(0,TexScreen);
\r
205 Device->SetStreamSource(0,VertexBuffer,sizeof(CustomVertex));
\r
206 Device->SetVertexShader(D3DFVF_CUSTOMVERTEX);
\r
207 Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
\r