Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include "Window.h"
  4. #include "d3d9_manager.h"
  5.  
  6. Window* mainWindow = NULL;
  7. d3d9_manager* directX = NULL;
  8.  
  9. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  10. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  11. {
  12.     MSG uMsg;
  13.     mainWindow = new Window(480, 640, "Test D3D9", hInstance, WindowProc);
  14.  
  15.     ShowWindow(mainWindow->getHandle(), nCmdShow);
  16.  
  17.     while (GetMessage(&uMsg, mainWindow->getHandle(), NULL, NULL))
  18.     {
  19.         TranslateMessage(&uMsg);
  20.         DispatchMessage(&uMsg);
  21.     }
  22.  
  23.     return uMsg.wParam;
  24. }
  25.  
  26. typedef HRESULT (WINAPI* oPresent) (LPDIRECT3DDEVICE9, CONST RECT* ,CONST RECT* , HWND, CONST RGNDATA* );
  27. oPresent pPresent = NULL;
  28.  
  29. HRESULT myPresent(LPDIRECT3DDEVICE9 pDevice, CONST RECT* pSourceRect,CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
  30. {
  31.     return pPresent(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
  32. }
  33.  
  34. typedef HRESULT (WINAPI* tEndScene)(LPDIRECT3DDEVICE9 pDevice);
  35. tEndScene oEndScene = NULL;
  36.  
  37. HRESULT WINAPI myEndScene (LPDIRECT3DDEVICE9 pDevice)
  38. {
  39.     return oEndScene(pDevice);
  40. }
  41.  
  42. PBYTE HookVTableFunction(PDWORD* dwVTable, PBYTE dwHook, INT Index )
  43. {
  44.     DWORD dwOld = 0;
  45.     VirtualProtect((void*)((*dwVTable) + (Index*4) ), 4, PAGE_EXECUTE_READWRITE, &dwOld);
  46.  
  47.     PBYTE pOrig = ((PBYTE)(*dwVTable)[Index]);
  48.     (*dwVTable)[Index] = (DWORD)dwHook;
  49.  
  50.     VirtualProtect((void*)((*dwVTable) + (Index*4)), 4, dwOld, &dwOld);
  51.  
  52.     return pOrig;
  53. }
  54.  
  55.  
  56. LPDIRECT3DDEVICE9 pDevice;
  57. DWORD* vTable;
  58.  
  59. DWORD WINAPI Hook_Thread (LPVOID)
  60. {
  61.     for(;;Sleep(75))
  62.     {
  63.         if (GetAsyncKeyState(VK_NUMPAD1))
  64.             HookVTableFunction((PDWORD*)pDevice, (PBYTE)myEndScene, 42);
  65.     }
  66. }
  67.  
  68. DWORD WINAPI Draw_Thread( LPVOID)
  69. {
  70.     for(;;Sleep(1000/60))
  71.     {
  72.         HRESULT hr;
  73.         hr = directX->testDevice();
  74.         if(SUCCEEDED(hr))
  75.         {
  76.             directX->Clear(0);
  77.             directX->startDrawing();
  78.             directX->getFont("Arial1")->DrawTextA(NULL, "Hello world!", -1, NULL, NULL, D3DCOLOR_ARGB(255, 0, 255, 0));
  79.             LPD3DXLINE menuLine = directX->getLine("Line1");
  80.  
  81.             D3DXVECTOR2 dLine[5];
  82.  
  83.             dLine[0].x = 50; dLine[0].y = 50;
  84.             dLine[1].x = 350; dLine[1].y = 50;
  85.             dLine[2].x = 350; dLine[2].y = 400;
  86.             dLine[3].x = 50; dLine[3].y = 400;
  87.             dLine[4].x = 50; dLine[4].y = 50;
  88.  
  89.             menuLine->Begin();
  90.             menuLine->Draw(dLine, 5, D3DCOLOR_XRGB(255,255,255));
  91.             menuLine->End();
  92.  
  93.             directX->stopDrawing();
  94.             directX->toScreen();
  95.         }
  96.         if(hr == D3DERR_DEVICELOST)
  97.         {
  98.             Sleep(500);
  99.         }
  100.         else if (hr == D3DERR_DEVICENOTRESET)
  101.         {
  102.             hr = directX->Reset();
  103.             if(SUCCEEDED(hr))
  104.             {
  105.                 // creating objects again
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  112. {
  113.     switch (uMsg)
  114.     {
  115.     case WM_CREATE:
  116.         {
  117.         directX = new d3d9_manager(hWnd);
  118.         pDevice = directX->getDevice();
  119.         DWORD* pVTable = (DWORD*)pDevice;
  120.         vTable = (DWORD*)pVTable[0];
  121.         directX->createFont("Arial1");
  122.         directX->createLine("Line1");
  123.         CreateThread(NULL, NULL, Hook_Thread, NULL, NULL, NULL);
  124.         CreateThread(NULL, NULL, Draw_Thread, NULL, NULL, NULL);
  125.         }
  126.         break;
  127.     case WM_CLOSE:
  128.         delete directX;
  129.         delete mainWindow;
  130.         ExitProcess(0);
  131.         break;
  132.     default:
  133.         return DefWindowProc(hWnd, uMsg, wParam, lParam);
  134.         break;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement