Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.42 KB | None | 0 0
  1. //#include "D3DExports.h"
  2. #include "Direct3D.h"
  3. #include "DirectInput.h"
  4. #define uint unsigned int
  5. extern HWND hWnd;
  6. extern bool bExtendedScreen;
  7. extern bool bDebugOutput;
  8. extern bool bExit;
  9. extern byte screen[64*128];
  10. extern uint StopDrawCalltimes;
  11. extern uint PC;
  12. extern uint StopPC;
  13. extern int curSelection;
  14. extern bool bRealQuit;
  15. extern DWORD dwPixelColor;
  16.  
  17. bool bRenderingText = false;
  18. int XMultiplier, YMultiplier;
  19. float BORDER2_START;
  20. float BORDER1_END;
  21. float BORDER2_START_X;
  22. float BORDER1_END_X;
  23. const uint iNumOfVertices = 50000;
  24. DWORD dwStartColor = 0x00FF00FF;
  25. DWORD dwStartColorB = 0xFF00FF00;
  26. D3DRECT rect;
  27. //CD3DFont* m_pFont[2];
  28. //DrawText_Info info;
  29. CUSTOMVERTEX vScreen[ 0x2000 * 6 ];
  30.  
  31. D3DPRESENT_PARAMETERS DeviceInfo;
  32. IDirect3D9* pD3D = NULL;
  33. IDirect3DDevice9* pd3dDevice = NULL;
  34. IDirect3DVertexBuffer9* pVB = NULL;
  35. IDirect3DVertexBuffer9* pVB2 = NULL;
  36. IDirect3DVertexBuffer9* pVB3 = NULL;
  37.  
  38. #define safe_release(p) if (p) p->Release(); p = NULL;
  39. struct textured_vertex{
  40.     float x, y, z, rhw;  // The transformed(screen space) position for the vertex.
  41.     float tu,tv;         // Texture coordinates
  42. };
  43. IDirect3DTexture9*              g_pOriginalTexture;
  44. void RenderScreen(int* _pixels,int number)
  45. {
  46.  
  47.     if (bExit) return;
  48.     //CUSTOMVERTEX vFiller[4];
  49. //  CUSTOMVERTEX vFiller2[4];
  50.     int index = 0;
  51.     HRESULT hr;
  52.        struct COLORVERTEX
  53.     {
  54.         FLOAT Position[3];
  55.         DWORD Color;
  56.     };
  57.        for(int i = 0; i < sizeof(screen); i++) screen[i] = 1;
  58.     // Initialize the vertices that will fill out the borders of the screen
  59.     index = InitScreenVertices(vScreen,_pixels,number);
  60.     //InitFillerVertices(vFiller,vFiller2);
  61.     //if (index > 0)
  62.     //{
  63.         //pd3dDevice->BeginScene();
  64.         pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
  65.                              0xff00ffff, 1.0f, 0L );
  66.             D3DXMATRIX matView;
  67.             D3DXMatrixIdentity( &matView );
  68.         pd3dDevice->SetStreamSource(0, pVB, 0, sizeof(COLORVERTEX));
  69.         pd3dDevice->SetFVF(D3DFVF_NORMAL);
  70.         pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST,index/3,vScreen,sizeof(CUSTOMVERTEX));
  71.     //  pd3dDevice->EndScene();
  72.  
  73.         pd3dDevice->Present(0,0,0,0);
  74. //  }
  75. }
  76.  
  77. bool InitD3D()
  78. {
  79.     HRESULT hr = S_OK;
  80.        
  81.     pD3D = Direct3DCreate9( D3D_SDK_VERSION );
  82.    // if( pD3D == 0 )
  83. //        return E_FAIL;
  84.  
  85.     ZeroMemory(&DeviceInfo,sizeof(DeviceInfo));
  86.  
  87.     DeviceInfo.BackBufferWidth = 1280;
  88.     DeviceInfo.BackBufferHeight = 720;
  89.  
  90.     DeviceInfo.BackBufferFormat = D3DFMT_A8R8G8B8;
  91.     DeviceInfo.FrontBufferFormat = ( D3DFORMAT )MAKESRGBFMT( D3DFMT_LE_X8R8G8B8 );
  92.     DeviceInfo.BackBufferCount = 1;
  93.     DeviceInfo.MultiSampleType = D3DMULTISAMPLE_NONE;
  94.     DeviceInfo.EnableAutoDepthStencil = TRUE;
  95.     DeviceInfo.AutoDepthStencilFormat = D3DFMT_D24S8;
  96.     DeviceInfo.SwapEffect = D3DSWAPEFFECT_DISCARD;
  97.     DeviceInfo.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  98.  
  99.     hr = pD3D->CreateDevice( 0, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &DeviceInfo, &pd3dDevice );
  100.     if( FAILED( hr ) )
  101.     {
  102.       //  UninitD3D();
  103.     }
  104.  
  105.     return hr;
  106. #if 0
  107.     ZeroMemory(&DeviceInfo,sizeof(DeviceInfo));
  108.    
  109.     DeviceInfo.BackBufferFormat = D3DFMT_A8R8G8B8;
  110.     DeviceInfo.BackBufferCount = 1;
  111.     DeviceInfo.MultiSampleType = D3DMULTISAMPLE_NONE;
  112.     DeviceInfo.SwapEffect = D3DSWAPEFFECT_DISCARD;
  113.     DeviceInfo.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
  114.     DeviceInfo.BackBufferWidth = 1280;
  115.     DeviceInfo.BackBufferHeight = 720;
  116.  
  117.     HRESULT hr;
  118.     pD3D = Direct3DCreate9(D3D_SDK_VERSION);
  119.     if(pD3D == NULL)
  120.     {
  121.     //  MessageBox(hWnd,"Failed to initialize Direct3D!\nAre you sure you're using at least version 9.0?",emu_title,MB_ICONERROR);
  122.         return false;
  123.     }
  124.  
  125.     D3DCAPS9 card_info;
  126.     hr = pD3D->GetDeviceCaps(0,D3DDEVTYPE_HAL,&card_info);
  127.     if (hr == D3DERR_INVALIDDEVICE)
  128.     {
  129. //      MessageBox(hWnd,"Fatal error: gfx card does not support hardware acceleration!\nPlease upgrade your card before trying to run this application again.",emu_title,MB_ICONERROR);
  130.         return false;
  131.     }
  132.  
  133.     /*int CreateFlag[3] = { D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_PUREDEVICE,
  134.                           D3DCREATE_HARDWARE_VERTEXPROCESSING,
  135.         //                D3DCREATE_MIXED_VERTEXPROCESSING,
  136.                           D3DCREATE_SOFTWARE_VERTEXPROCESSING };*/
  137.  
  138.         hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
  139.                                 NULL,
  140.                                 D3DCREATE_HARDWARE_VERTEXPROCESSING,
  141.                                 &DeviceInfo, &pd3dDevice);
  142.  
  143. #endif
  144. /*  D3DFont_SetDevice(pd3dDevice);
  145.     for (int i=0; i<2; i++)
  146.     {
  147.         m_pFont[i] = D3DFont_InitFont("Monotype Corsiva",(i == 0 ? 23 : 46), D3DFONT_BOLD);
  148.         if (m_pFont[i] == NULL)
  149.         {
  150.             char msg[100];
  151.             wsprintf(msg,"Error when creating d3d font!\nReturned error: %s.", DXGetErrorString9( GetLastError() ) );
  152.             MessageBox(hWnd,msg,emu_title,MB_ICONERROR);
  153.             return false;
  154.         }
  155.     }
  156.     */
  157.     //hr = pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
  158.     //hr = pd3dDevice->SetDialogBoxMode(true);
  159.     //DeviceInfo->Clear( 0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0L );
  160.     //pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, 0xFF00FFFF, 1.0f, 0L );
  161.     //return true;
  162. }
  163. inline void InitFillerVertices(CUSTOMVERTEX* pFiller,CUSTOMVERTEX* pFiller2)
  164. {
  165.  
  166.     const float width = (float)DeviceInfo.BackBufferWidth;
  167.     const float height = (float)DeviceInfo.BackBufferHeight;
  168.     pFiller[0].x = 0;
  169.     pFiller[0].y = 0;
  170.     pFiller[1].x = width;
  171.     pFiller[1].y = 0;
  172.     pFiller[2].x = 0;
  173.     pFiller[2].y = BORDER1_END;
  174.     pFiller[3].x = width;
  175.     pFiller[3].y = BORDER1_END;
  176.  
  177.     pFiller2[0].x = 0;
  178.     pFiller2[0].y = BORDER2_START;
  179.     pFiller2[1].x = width;
  180.     pFiller2[1].y = BORDER2_START;
  181.     pFiller2[2].x = 0;
  182.     pFiller2[2].y = height;
  183.     pFiller2[3].x = width;
  184.     pFiller2[3].y = height;
  185.    
  186.     //filler2[0].color = 0xFF000000;
  187.     //filler2[1].color = 0x00FF0000;
  188.     //filler2[2].color = 0x0000FF00;
  189.     //filler2[3].color = 0x000000FF;
  190.     //filler[0].color = (dwStartColor & 0xFF) << 24;
  191.     //filler[1].color = (dwStartColor & 0xFF00) << 16;
  192.     //filler[2].color = (dwStartColor & 0xFF0000) << 8;
  193.     //filler[3].color = (dwStartColor & 0xFF000000);
  194.     //filler2[0].color = (dwStartColorB & 0xFF) << 24;
  195.     //filler2[1].color = (dwStartColorB & 0xFF00) << 16;
  196.     //filler2[2].color = (dwStartColorB & 0xFF0000) << 8;
  197.     //filler2[3].color = (dwStartColorB & 0xFF000000);
  198.  
  199.     //DWORD bit;
  200.     //int pos = 0;
  201.     ////DWORD temp;
  202.     //static bool bInvert = true;
  203.     /*bit = dwStartColor & 0x1;
  204.     dwStartColor >>= 1;
  205.     dwStartColor |= (bit << 31);*/
  206.    
  207. //#define Inc(_pos,number) pos = 0xFF * _pos + 0xFF; bit = number & pos; bit++; number = bit;
  208. //#define Dec(_pos,number) pos = 0xFF * _pos + 0xFF; bit = number & pos; bit--; number = bit;
  209. //  if (bInvert)
  210. //  {
  211. //      #define _pos 0
  212. //      int number = 0x00FF00FF;
  213. //      pos = 0xFF * _pos + 0xFF;
  214. //      bit = number & pos;
  215. //      bit--;
  216. //      pos = _pos * 4;
  217. //      number |= (bit << pos);
  218. //      Dec(0,dwStartColor);
  219. //      /*dStartColor[1]++;
  220. //      dStartColor[2]--;
  221. //      dStartColor[3]++;
  222. //      dStartColorB[0]++;
  223. //      dStartColorB[1]--;
  224. //      dStartColorB[2]++;
  225. //      dStartColorB[3]--;
  226. //      if (dStartColor[0] == 0) bInvert = false;*/
  227. //  } else {
  228. //      /*dStartColor[0]++;
  229. //      dStartColor[1]--;
  230. //      dStartColor[2]++;
  231. //      dStartColor[3]--;
  232. //      dStartColorB[0]--;
  233. //      dStartColorB[1]++;
  234. //      dStartColorB[2]--;
  235. //      dStartColorB[3]++;
  236. //      if (dStartColor[1] == 0) bInvert = true;*/
  237. //  }
  238.     //for (int i=0; i<4; i++)
  239.     //{
  240.     //  dStartColor[0] = ( ( (dStartColor[0] >> 20) + 1) << 20);
  241.     //  dStartColor[i]++;
  242.     //  dStartColorB[i]++;
  243.     //}
  244.  
  245.     for (int i=0; i<4; i++)
  246.     {
  247.         pFiller[i].rhw = 1.0;
  248.         pFiller[i].z = 0.5;
  249.         pFiller[i].color = 0x00000000;
  250.         pFiller2[i].rhw = 1.0;
  251.         pFiller2[i].z = 0.5;
  252.         pFiller2[i].color = 0x00000000;
  253.     }
  254.     /**pFiller = filler;
  255.     *pFiller2 = filler2;*/
  256.  
  257. }
  258.  
  259. inline int InitScreenVertices(CUSTOMVERTEX* pVertices,int* pixels,int number)
  260. {
  261.     int offsetx = 0;
  262.     int index = 0;
  263.     int xvalue = bExtendedScreen ? 128 : 64;
  264.     int yvalue = bExtendedScreen ? 64 : 32;
  265.  
  266.     //CUSTOMVERTEX* vertices = new CUSTOMVERTEX[ xvalue * yvalue * 3 ];
  267.     for (int y=0; y < yvalue; y++)
  268.     {
  269.         for (int x=0; x < xvalue; x++)
  270.         {
  271.             //if (screen[x+y*xvalue] != 1) continue;
  272. /*          if (bMonitorDrawOpcode)
  273.             {
  274.                 char msg[100];
  275.                 wsprintf(msg,"Pixel found at x: %i, y: %i.\n",x,y);
  276.                 DEBUGTRACE(msg);
  277.             }
  278.             */
  279. //#pragma warning(disable: 4244)
  280.             pVertices[index].x = x*XMultiplier;
  281.             pVertices[index].y = BORDER1_END + (y*YMultiplier);
  282.             pVertices[index+1].x = x*XMultiplier+XMultiplier;
  283.             pVertices[index+1].y = BORDER1_END + (y*YMultiplier);
  284.             pVertices[index+2].x = x*XMultiplier;
  285.             pVertices[index+2].y = BORDER1_END + (y*YMultiplier+YMultiplier);
  286.  
  287.             pVertices[index+3].x = x*XMultiplier+XMultiplier;
  288.             pVertices[index+3].y = BORDER1_END + (y*YMultiplier);
  289.             pVertices[index+4].x = x*XMultiplier+XMultiplier;
  290.             pVertices[index+4].y = BORDER1_END + (y*YMultiplier+YMultiplier);
  291.             pVertices[index+5].x = x*XMultiplier;
  292.             pVertices[index+5].y = BORDER1_END + (y*YMultiplier+YMultiplier);
  293. //#pragma warning(default: 4244)
  294.  
  295.             for (int i=0; i<6; i++)
  296.             {
  297.                 pVertices[index+i].z = 1.0;
  298.                 pVertices[index+i].rhw = 1.0;
  299.                 pVertices[index+i].color = dwPixelColor;
  300.             }      
  301.             index += 6;
  302.         }
  303.     }
  304.     //*pVertices = vertices;
  305.     return index;
  306. }
  307.  
  308. void UpdateInfo()
  309. {
  310.  
  311.     int xvalue = bExtendedScreen ? 128 : 64;
  312.     XMultiplier = DeviceInfo.BackBufferWidth / xvalue;
  313.     YMultiplier = XMultiplier;
  314.  
  315.     const float width = (float)DeviceInfo.BackBufferWidth;
  316.     const float height = (float)DeviceInfo.BackBufferHeight;
  317.     if (!bExtendedScreen)
  318.     {
  319.         BORDER1_END = (float)(height - (YMultiplier * 32)) / 2;
  320.         BORDER2_START = (float)(BORDER1_END + YMultiplier * 32 );
  321.     } else {
  322.         BORDER1_END = (float)(height - (YMultiplier * 64)) / 2;
  323.         BORDER2_START = (float)(BORDER1_END + YMultiplier * 64 );
  324.     }
  325.  
  326.     rect.x1 = 0;
  327.     rect.x2 = (long)width;
  328.     rect.y1 = (long)BORDER1_END;
  329.     rect.y2 = (long)BORDER2_START;
  330.  
  331. }
  332.  
  333. #pragma optimize("g",off)
  334. void RenderText(char* text,byte flags,int fontindex,DWORD dwColor)
  335. {
  336. #if 0
  337.     char msg[1000];
  338.     strcpy(msg,text);
  339.     if (flags & QUIT_EMULATOR)
  340.     {
  341.         strcat(msg,"\nPress any key to exit the emulator.");
  342.         bRenderingText = true;
  343.     }
  344.     ZeroMemory(&info,sizeof(info));
  345.     if (flags & USE_COLOR)
  346.         info.dwColor = dwColor;
  347.     else
  348.         info.dwColor = 0xFFFF0000;
  349.     info.flags = DT_CENTERTEXT|flags;
  350.     info.font = m_pFont[fontindex];
  351.     info.pCallback = DrawTextCallbackAdvanced;
  352.     info.strText = msg;
  353.    
  354.     if (flags & QUIT_EMULATOR)
  355.     {
  356.         HANDLE hThread = CreateThread(NULL,NULL,DrawTextCallback,NULL,CREATE_SUSPENDED,NULL);
  357.         SetThreadPriority(hThread,THREAD_PRIORITY_IDLE);
  358.         ResumeThread(hThread);
  359.         //WaitForSingleObject(hThread,INFINITE);
  360.     } else {
  361.         pd3dDevice->BeginScene();
  362.         D3DFont_DrawText(&info);
  363.         pd3dDevice->EndScene();
  364.         if (pd3dDevice->Present(0,0,0,0) == D3DERR_DEVICELOST)
  365.         {
  366.             for (int i=0; i<2; i++) D3DFont_Invalidate(m_pFont[i]);
  367.             safe_release(pVB);
  368.             while ( pd3dDevice->Reset(&DeviceInfo) == D3DERR_DEVICELOST );
  369.             for (int i=0; i<2; i++) D3DFont_Restore(m_pFont[i]);
  370.         }
  371.     }
  372. #endif
  373. }
  374. //#pragma optimize("",on)
  375.  
  376. void DestroyD3D()
  377. {
  378.  
  379.     safe_release(pVB);
  380.     safe_release(pd3dDevice);
  381.     safe_release(pD3D);
  382.     //for (int i=0; i<2; i++) D3DFont_Delete(m_pFont[i]);
  383.  
  384. }
  385.  
  386. DWORD WINAPI DrawTextCallback(LPVOID lParam)
  387. {
  388. #if 0
  389.     ClearKeys();
  390.     try
  391.     {
  392.         while (GetKeyboardPress() == -1)
  393.         {
  394.             pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,0,1.0f,0);
  395.             pd3dDevice->BeginScene();
  396.             D3DFont_DrawText(&info);
  397.             pd3dDevice->EndScene();
  398.             if (pd3dDevice->Present(0,0,0,0) == D3DERR_DEVICELOST)
  399.             {
  400.                 for (int i=0; i<2; i++) D3DFont_Invalidate(m_pFont[i]);
  401.                 safe_release(pVB);
  402.                 pd3dDevice->Reset(&DeviceInfo);
  403.                 for (int i=0; i<2; i++) D3DFont_Restore(m_pFont[i]);
  404.             }
  405.             Sleep(0);
  406.         }
  407.     }
  408.     catch(...) { }
  409.     bExit = true;
  410.     bRealQuit = true;
  411. #endif
  412.     return 0;
  413. }
  414.  
  415. void DrawTextCallbackAdvanced(DWORD& dwColor, int index)
  416. {
  417.     if (index == 1 && curSelection == 0)
  418.         dwColor = 0xFF00FF00;
  419.     if (index == 2 && curSelection == 1)
  420.         dwColor = 0xFF00FF00;
  421.     if (index == 3 && curSelection == 2)
  422.         dwColor = 0xFF00FF00;
  423.     if (index == 4 && curSelection == 3)
  424.         dwColor = 0xFF00FF00;
  425.     if (index == 5 && curSelection == 4)
  426.         dwColor = 0xFF00FF00;
  427.     if (index == 6 && curSelection == 5)
  428.         dwColor = 0xFF00FF00;
  429. }
  430.  
  431. //bool CreateVertex()
  432. //{
  433. //  HRESULT hr;
  434. //  hr = pd3dDevice->CreateVertexBuffer( (0x2000+1)*sizeof(CUSTOMVERTEX),
  435. //      D3DUSAGE_DYNAMIC|D3DUSAGE_DONOTCLIP|D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,
  436. //      D3DPOOL_DEFAULT, &pVB, NULL );
  437. //  if (FAILED(hr))
  438. //  {
  439. //      DXTRACE_ERR_MSGBOX("Vertex buffer creation failed!",hr);
  440. //      return false;
  441. //  }
  442. //  return true;
  443. //}
Add Comment
Please, Sign In to add comment