experimental ddraw code
authornotaz <notasas@gmail.com>
Tue, 29 Jan 2008 19:25:35 +0000 (19:25 +0000)
committernotaz <notasas@gmail.com>
Tue, 29 Jan 2008 19:25:35 +0000 (19:25 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive@340 be3aeb3a-fb24-0410-a615-afba39da0efa

platform/win32/GenaDrive/Direct.cpp
platform/win32/GenaDrive/Emu.cpp
platform/win32/GenaDrive/Makefile.vc
platform/win32/GenaDrive/TexScreen.cpp
platform/win32/GenaDrive/app.h

index 8277bce..320c278 100644 (file)
@@ -1,6 +1,7 @@
 \r
 #include "app.h"\r
 \r
+// d3d\r
 static IDirect3D8 *Direct3D=NULL;\r
 IDirect3DDevice8 *Device=NULL;\r
 IDirect3DSurface8 *DirectBack=NULL; // Back Buffer\r
@@ -17,11 +18,95 @@ struct CustomVertex
 \r
 static CustomVertex VertexList[4];\r
 \r
+// ddraw\r
+#include <ddraw.h>\r
+\r
+LPDIRECTDRAW7        m_pDD;\r
+LPDIRECTDRAWSURFACE7 m_pddsFrontBuffer;\r
+LPDIRECTDRAWSURFACE7 m_pddsBackBuffer;\r
+\r
+// quick and dirty stuff..\r
+static int DirectDrawInit()\r
+{\r
+  HRESULT ret;\r
+\r
+  ret = DirectDrawCreateEx(NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL);\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  // Set cooperative level\r
+  ret = m_pDD->SetCooperativeLevel( FrameWnd, DDSCL_NORMAL );\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  // Create the primary surface\r
+  DDSURFACEDESC2 ddsd;\r
+  ZeroMemory( &ddsd, sizeof( ddsd ) );\r
+  ddsd.dwSize         = sizeof( ddsd );\r
+  ddsd.dwFlags        = DDSD_CAPS;\r
+  ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;\r
+\r
+  ret = m_pDD->CreateSurface( &ddsd, &m_pddsFrontBuffer, NULL );\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  // Create the backbuffer surface\r
+  ddsd.dwFlags        = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;    \r
+  ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;\r
+  ddsd.dwWidth        = 320;\r
+  ddsd.dwHeight       = 240;\r
+\r
+  ret = m_pDD->CreateSurface( &ddsd, &m_pddsBackBuffer, NULL );\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  // clipper\r
+  LPDIRECTDRAWCLIPPER pcClipper = NULL;\r
+  ret = m_pDD->CreateClipper( 0, &pcClipper, NULL );\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  ret = pcClipper->SetHWnd( 0, FrameWnd );\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  ret = m_pddsFrontBuffer->SetClipper( pcClipper );\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  RELEASE(pcClipper);\r
+\r
+#if 0\r
+  DDSURFACEDESC2 sd;\r
+  memset(&sd, 0, sizeof(sd));\r
+  sd.dwSize = sizeof(sd);\r
+  ret = m_pddsBackBuffer->Lock(NULL, &sd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT|DDLOCK_WRITEONLY, NULL);\r
+  if (ret) { LOGFAIL(); return 1; }\r
+\r
+  memset(sd.lpSurface, 0xcc, 200*200);\r
+\r
+  ret = m_pddsBackBuffer->Unlock(NULL);\r
+  if (ret) { LOGFAIL(); return 1; }\r
+#else\r
+    DDBLTFX ddbltfx;\r
+    ZeroMemory( &ddbltfx, sizeof(ddbltfx) );\r
+    ddbltfx.dwSize      = sizeof(ddbltfx);\r
+    ddbltfx.dwFillColor = 0xff00;\r
+\r
+    ret = m_pddsBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );\r
+#endif\r
+\r
+  ret = m_pddsFrontBuffer->Blt(NULL, m_pddsBackBuffer, NULL, DDBLT_WAIT, NULL);\r
+  if (ret) { LOGFAIL(); return 1; }\r
+Sleep(2000);\r
+/*   Sleep(500);\r
+  ret = m_pddsFrontBuffer->Blt(NULL, m_pddsBackBuffer, NULL, DDBLT_WAIT, NULL);\r
+  if (ret) { LOGFAIL(); return 1; }\r
+   Sleep(500);\r
+  ret = m_pddsFrontBuffer->Blt(NULL, m_pddsBackBuffer, NULL, DDBLT_WAIT, NULL);\r
+  if (ret) { LOGFAIL(); return 1; }\r
+*/\r
+  return 0;\r
+}\r
+\r
+\r
 int DirectInit()\r
 {\r
   D3DPRESENT_PARAMETERS d3dpp; \r
   D3DDISPLAYMODE mode;\r
-  D3DDEVTYPE dt = D3DDEVTYPE_HAL;\r
   int i,u,ret=0;\r
 \r
   memset(&d3dpp,0,sizeof(d3dpp));\r
@@ -45,32 +130,46 @@ int DirectInit()
 #endif\r
 \r
   // Try to create a device with hardware vertex processing:\r
-  for (u=0;u<2;u++)\r
+  for (i=0;i<3;i++)\r
   {\r
-    for (i=0;i<4;i++)\r
-    {\r
-      int behave=D3DCREATE_HARDWARE_VERTEXPROCESSING;\r
+    int behave=D3DCREATE_HARDWARE_VERTEXPROCESSING;\r
 \r
-      // Try software vertex processing:\r
-      if (i==1) behave=D3DCREATE_MIXED_VERTEXPROCESSING;\r
-      if (i==2) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING;\r
+    // Try software vertex processing:\r
+    if (i==1) behave=D3DCREATE_MIXED_VERTEXPROCESSING;\r
+    if (i==2) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING;\r
 \r
-      Direct3D->CreateDevice(D3DADAPTER_DEFAULT,dt,FrameWnd,behave,&d3dpp,&Device);\r
-      if (Device) break;\r
-    }\r
+    Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,FrameWnd,\r
+        behave|D3DCREATE_MULTITHREADED,&d3dpp,&Device);\r
     if (Device) break;\r
-    dt = D3DDEVTYPE_REF;\r
   }\r
 \r
-  if (Device==NULL) return 1;\r
+  if (Device==NULL)\r
+  {\r
+#if 0\r
+    // try ref\r
+    Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,FrameWnd,\r
+        D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&d3dpp,&Device);\r
+    if (Device==NULL) goto fail0;\r
+    HMODULE test = LoadLibrary("d3d8d.dll");\r
+    if (test != NULL) FreeLibrary(test);\r
+    else {\r
+      error("Sorry, but this program requires Direct3D with hardware acceleration.\n\n"\r
+            "You can try using Direct3D software emulation, but you have to install "\r
+            "DirectX SDK for it to work\n(it seems to be missing now).");\r
+      goto fail1;\r
+    }\r
+#else\r
+    goto fail1;\r
+#endif\r
+  }\r
 \r
   Device->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&DirectBack);\r
-  if (DirectBack==NULL) return 1;\r
+  if (DirectBack==NULL) goto fail1;\r
 \r
   Device->CreateVertexBuffer(sizeof(VertexList),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&VertexBuffer);\r
-  if (VertexBuffer==NULL) return 1;\r
+  if (VertexBuffer==NULL) goto fail2;\r
 \r
-  ret=TexScreenInit(); if (ret) return 1;\r
+  ret=TexScreenInit(); if (ret) goto fail3;\r
 \r
   //FontInit();\r
 \r
@@ -79,13 +178,28 @@ int DirectInit()
   // Set up texture modes:\r
   Device->SetTextureStageState(0,D3DTSS_ADDRESSU,D3DTADDRESS_CLAMP);\r
   Device->SetTextureStageState(0,D3DTSS_ADDRESSV,D3DTADDRESS_CLAMP);\r
+\r
   return 0;\r
+\r
+fail3:\r
+  RELEASE(VertexBuffer)\r
+fail2:\r
+  RELEASE(DirectBack)\r
+fail1:\r
+  RELEASE(Device)\r
+fail0:\r
+  RELEASE(Direct3D)\r
+\r
+  // error("Failed to use Direct3D, trying DirectDraw..");\r
+\r
+  // try DirectDraw\r
+  return DirectDrawInit();\r
 }\r
 \r
 void DirectExit()\r
 {\r
   //FontExit();\r
-  //TexScreenExit();\r
+  TexScreenExit();\r
 \r
   RELEASE(VertexBuffer)\r
   RELEASE(DirectBack)\r
@@ -201,6 +315,8 @@ int DirectScreen()
   memcpy(lock,VertexList,sizeof(VertexList));\r
   VertexBuffer->Unlock();\r
 \r
+  ret=Device->BeginScene();\r
+  if (ret) dprintf2("BeginScene failed\n");\r
   ret=Device->SetTexture(0,TexScreen);\r
   if (ret) dprintf2("SetTexture failed\n");\r
   ret=Device->SetStreamSource(0,VertexBuffer,sizeof(CustomVertex));\r
@@ -209,6 +325,8 @@ int DirectScreen()
   if (ret) dprintf2("SetVertexShader failed\n");\r
   ret=Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);\r
   if (ret) dprintf2("DrawPrimitive failed\n");\r
+  ret=Device->EndScene();\r
+  if (ret) dprintf2("EndScene failed\n");\r
 \r
   return 0;\r
 }\r
index d93abaf..79938b2 100644 (file)
@@ -47,7 +47,7 @@ static int EmuScan(unsigned int num, void *sdata)
   pd=EmuScreen+(num<<8)+(num<<6); end=pd+320;\r
   ps=(unsigned short *)sdata;\r
 \r
-  do { *pd++=0xf800;/**ps++;*/ } while (pd<end);\r
+  do { *pd++=*ps++; } while (pd<end);\r
   \r
   return 0;\r
 }\r
index 20b58e1..896b8b9 100644 (file)
@@ -51,7 +51,7 @@ OBJ = $(OBJ) $(R)cpu\cz80\cz80.obj
 ALL : PicoDrive.exe\r
 \r
 PicoDrive.exe : $(OBJ)\r
-       link.exe $(LDFLAGS) $(OBJ) gdi32.lib user32.lib advapi32.lib d3d8.lib d3dx8.lib dsound.lib comdlg32.lib\r
+       link.exe $(LDFLAGS) $(OBJ) gdi32.lib user32.lib advapi32.lib d3d8.lib d3dx8.lib dsound.lib comdlg32.lib ddraw.lib dxguid.lib\r
 \r
 clean:\r
        -del $(OBJ) PicoDrive.exe\r
index d656a87..1629dbc 100644 (file)
@@ -99,7 +99,7 @@ int TexScreenLinear()
 \r
     for (x=0; x<EmuWidth; x++,addr+=2)\r
     {\r
-      *(unsigned int *)(dest+addr)=0xf0f0f0f0;//*ps++;\r
+      *(unsigned int *)(dest+addr)=*ps++;\r
     }\r
   }\r
 \r
index 5fcf5b7..3113a29 100644 (file)
@@ -13,6 +13,8 @@
 \r
 #define RELEASE(x) if (x) x->Release();  x=NULL;\r
 \r
+#define LOGFAIL() dprintf2("fail: %s %s:%i\n", __FUNCTION__, __FILE__, __LINE__)\r
+\r
 \r
 // Emu.cpp\r
 extern unsigned short *EmuScreen;\r