Advertisement
Guest User

DirectX Cursor Window

a guest
Aug 7th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ConsoleApplication3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. // Includes
  7. #include <time.h>
  8. #include <d3d9.h>
  9. #include <d3dx9.h>
  10. #include <dwmapi.h>
  11.  
  12. #include <iostream>
  13.  
  14. // Import libraries to link with
  15. #pragma comment(lib, "d3d9.lib")
  16. #pragma comment(lib, "d3dx9.lib")
  17. #pragma comment(lib, "dwmapi.lib")
  18.  
  19. #define ARGB_TRANS   0x00000000 // 100% alpha
  20. #define SPRITE_SIZE 128
  21.  
  22. #define TEXTURE_PATH L"circle.png"
  23.  
  24. // +---------+
  25. // | Globals |
  26. // +---------+
  27. WCHAR                   *g_wcpAppName = L"D3DCursor";
  28. INT                     g_iWidth = 800;
  29. INT                     g_iHeight = 600;
  30. MARGINS                 g_mgDWMMargins = { -1, -1, -1, -1 };
  31. IDirect3D9Ex            *g_pD3D = NULL;
  32. IDirect3DDevice9Ex      *g_pD3DDevice = NULL;
  33. IDirect3DVertexBuffer9  *g_pVB = NULL;
  34. LPD3DXSPRITE g_sprite = NULL;
  35. LPDIRECT3DTEXTURE9 g_circle = NULL;
  36.  
  37. HWND       hWnd = NULL;
  38.  
  39. D3DXMATRIX Identity;
  40.  
  41. BOOL wait = false;
  42.  
  43. INT last_x = 0;
  44. INT last_y = 0;
  45. INT x = 0;
  46. INT y = 0;
  47.  
  48.  
  49. // +--------------+
  50. // | D3DStartup() |
  51. // +--------------+----------------------------------+
  52. // | Initialise Direct3D and perform once only tasks |
  53. // +-------------------------------------------------+
  54. HRESULT D3DStartup(HWND hWnd)
  55. {
  56.     BOOL                  bCompOk = FALSE;   // Is composition enabled?
  57.     D3DPRESENT_PARAMETERS pp;                            // Presentation prefs
  58.     DWORD                 msqAAQuality = 0;       // Non-maskable quality
  59.  
  60.     D3DXMATRIX Ortho2D;
  61.  
  62.     // Make sure that DWM composition is enabled
  63.     DwmIsCompositionEnabled(&bCompOk);
  64.     if (!bCompOk) return E_FAIL;
  65.  
  66.     // Create a Direct3D object
  67.     if (FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &g_pD3D))) return E_FAIL;
  68.  
  69.     // Setup presentation parameters
  70.     ZeroMemory(&pp, sizeof(pp));
  71.     pp.Windowed = TRUE;
  72.     pp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Required for multi sampling
  73.     pp.BackBufferFormat = D3DFMT_A8R8G8B8;       // Back buffer format with alpha channel
  74.     pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //Disables vsync
  75.    
  76.     // No AA
  77.     pp.MultiSampleType = D3DMULTISAMPLE_NONE;
  78.  
  79.     // Create a Direct3D device object
  80.     if (FAILED(g_pD3D->CreateDeviceEx(D3DADAPTER_DEFAULT,
  81.         D3DDEVTYPE_HAL,
  82.         hWnd,
  83.         D3DCREATE_HARDWARE_VERTEXPROCESSING,
  84.         &pp,
  85.         NULL,
  86.         &g_pD3DDevice
  87.         ))) return E_FAIL;
  88.  
  89.     // Configure the device state
  90.  
  91.     g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  92.    
  93.     g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  94.  
  95.     return S_OK;
  96. }
  97.  
  98. // +---------------+
  99. // | D3DShutdown() |
  100. // +---------------+----------------------+
  101. // | Release all created Direct3D objects |
  102. // +--------------------------------------+
  103. VOID D3DShutdown(VOID)
  104. {
  105.     if (g_pVB != NULL) g_pVB->Release();
  106.     if (g_pD3DDevice != NULL) g_pD3DDevice->Release();
  107.     if (g_pD3D != NULL) g_pD3D->Release();
  108. }
  109.  
  110. // +--------------+
  111. // | CreateCube() |
  112. // +--------------+------------------------------+
  113. // | Populates a vertex buffer with a cube shape |
  114. // +---------------------------------------------+
  115. HRESULT InitSprites(VOID)
  116. {
  117.     if (SUCCEEDED(D3DXCreateSprite(g_pD3DDevice, &g_sprite)))
  118.     {
  119.         // created OK
  120.     }
  121.  
  122.     D3DXCreateTextureFromFile(g_pD3DDevice, TEXTURE_PATH, &g_circle);
  123.  
  124.     return S_OK;
  125. }
  126.  
  127. // +----------+
  128. // | Render() |
  129. // +----------+-------------------------+
  130. // | Renders a scene to the back buffer |
  131. // +------------------------------------+
  132. VOID Render(VOID)
  133. {
  134.     if (!wait)
  135.     {
  136.        
  137.         // Sanity check
  138.         if (g_pD3DDevice == NULL) return;
  139.         if (g_sprite == NULL) return;
  140.  
  141.         D3DRECT clearRect;
  142.         clearRect.x1 = last_x - (SPRITE_SIZE / 2);
  143.         clearRect.x2 = last_x + (SPRITE_SIZE / 2);
  144.         clearRect.y1 = last_y - (SPRITE_SIZE / 2);
  145.         clearRect.y2 = last_y + (SPRITE_SIZE / 2);
  146.  
  147.         g_pD3DDevice->Clear(1, &clearRect, D3DCLEAR_TARGET, ARGB_TRANS, 1.0f, 0);
  148.  
  149.         // Render scene
  150.         if (SUCCEEDED(g_pD3DDevice->BeginScene()))
  151.         {
  152.             D3DXMATRIX mat;
  153.             if (SUCCEEDED(g_sprite->Begin(D3DXSPRITE_ALPHABLEND)))
  154.             {
  155.                 D3DXMatrixTranslation(&mat, x+0.0f-(SPRITE_SIZE/2), y+0.0f-(SPRITE_SIZE/2), 0.0f);
  156.                 g_sprite->SetTransform(&mat);
  157.                 g_sprite->Draw(g_circle, NULL, NULL, NULL, 0xff000000);
  158.                 g_sprite->End();
  159.             }
  160.  
  161.             g_pD3DDevice->EndScene();
  162.         }
  163.  
  164.         // Update display
  165.         g_pD3DDevice->PresentEx(NULL, NULL, NULL, NULL, NULL);
  166.     }
  167. }
  168.  
  169. // +--------------+
  170. // | WindowProc() |
  171. // +--------------+------------------+
  172. // | The main window message handler |
  173. // +---------------------------------+
  174. LRESULT WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  175. {
  176.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  177. }
  178.  
  179. // +-----------+
  180. // | WinMain() |
  181. // +-----------+---------+
  182. // | Program entry point |
  183. // +---------------------+
  184. INT WINAPI StartD3DCursorWindow(HINSTANCE hInstance, HWND hParent, int x, int y, int width, int height, bool topmost)
  185. {
  186.     MSG        uMsg;
  187.     WNDCLASSEX wc = { sizeof(WNDCLASSEX),              // cbSize
  188.         NULL,                            // style
  189.         WindowProc,                      // lpfnWndProc
  190.         NULL,                            // cbClsExtra
  191.         NULL,                            // cbWndExtra
  192.         hInstance,                       // hInstance
  193.         LoadIcon(NULL, IDI_APPLICATION), // hIcon
  194.         LoadCursor(NULL, IDC_ARROW),     // hCursor
  195.         NULL,                            // hbrBackground
  196.         NULL,                            // lpszMenuName
  197.         g_wcpAppName,                    // lpszClassName
  198.         LoadIcon(NULL, IDI_APPLICATION) };// hIconSm
  199.  
  200.     RegisterClassEx(&wc);
  201.  
  202.     g_iWidth = width;
  203.     g_iHeight = height;
  204.  
  205.     hWnd = CreateWindowEx(WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_TRANSPARENT,             // dwExStyle
  206.         g_wcpAppName,                 // lpClassName
  207.         g_wcpAppName,                 // lpWindowName
  208.         WS_POPUP,        // dwStyle
  209.         x, y, // x, y
  210.         g_iWidth, g_iHeight,          // nWidth, nHeight
  211.         hParent,                         // hWndParent
  212.         NULL,                         // hMenu
  213.         hInstance,                    // hInstance
  214.         NULL);                        // lpParam
  215.  
  216.     // Extend glass to cover whole window
  217.     DwmExtendFrameIntoClientArea(hWnd, &g_mgDWMMargins);
  218.  
  219.     // Initialise Direct3D
  220.     if (SUCCEEDED(D3DStartup(hWnd)))
  221.     {
  222.         if (SUCCEEDED(InitSprites()))
  223.         {
  224.             // Show the window
  225.             ShowWindow(hWnd, SW_SHOWDEFAULT);
  226.             UpdateWindow(hWnd);
  227.         }
  228.     }
  229.  
  230.     // Shutdown Direct3D
  231.  
  232.     // Exit application
  233.     return 0;
  234. }
  235.  
  236. int _tmain(int argc, _TCHAR* argv[])
  237. {
  238.     StartD3DCursorWindow(GetModuleHandle(NULL),0,0,0,2560,1440,true);
  239.  
  240.     while (true)
  241.     {
  242.         LPPOINT point = new POINT;
  243.         GetCursorPos(point);
  244.         last_x = x;
  245.         last_y = y;
  246.         x = point->x;
  247.         y = point->y;
  248.         Render();
  249.         Sleep(10);
  250.     }
  251.  
  252.     return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement