Advertisement
YaLTeR

DrawText issue

Jan 29th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <chrono>
  2. #include <string>
  3.  
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <Windows.h>
  6. #include <d3d9.h>
  7. #include <d3dx9.h>
  8. #pragma comment (lib, "d3d9.lib")
  9. #pragma comment (lib, "d3dx9.lib")
  10.  
  11. using namespace std;
  12. using namespace std::literals;
  13.  
  14. #define LOG(...) OutputDebugStringA(("MSG: <Test Program> "s + __FUNCTION__ + "(): "s + __VA_ARGS__ + "\n"s).c_str());
  15.  
  16. LPDIRECT3D9 d3d;
  17. LPDIRECT3DDEVICE9 d3ddev;
  18. LPD3DXFONT d3dfont;
  19.  
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21.  
  22. void render_frame();
  23.  
  24. int APIENTRY wWinMain(
  25.     HINSTANCE hInstance,
  26.     HINSTANCE hPrevInstance,
  27.     LPWSTR     lpCmdLine,
  28.     int         nCmdShow)
  29. {
  30.     UNREFERENCED_PARAMETER(hPrevInstance);
  31.     UNREFERENCED_PARAMETER(lpCmdLine);
  32.  
  33.     WNDCLASSEXW wcex;
  34.     wcex.cbSize = sizeof(wcex);
  35.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  36.     wcex.lpfnWndProc = WndProc;
  37.     wcex.cbClsExtra = 0;
  38.     wcex.cbWndExtra = 0;
  39.     wcex.hInstance = hInstance;
  40.     wcex.hIcon = nullptr;
  41.     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  42.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  43.     wcex.lpszMenuName = nullptr;
  44.     wcex.lpszClassName = L"DrawTextIssue";
  45.     wcex.hIconSm = nullptr;
  46.     RegisterClassExW(&wcex);
  47.    
  48.     auto window = CreateWindowW(
  49.         L"DrawTextIssue",
  50.         L"DrawTextIssue",
  51.         WS_OVERLAPPEDWINDOW,
  52.         CW_USEDEFAULT,
  53.         0,
  54.         800,
  55.         600,
  56.         nullptr,
  57.         nullptr,
  58.         hInstance,
  59.         nullptr);
  60.  
  61.     if (!window)
  62.         return FALSE;
  63.  
  64.     ShowWindow(window, nCmdShow);
  65.     UpdateWindow(window);
  66.  
  67.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  68.  
  69.     D3DPRESENT_PARAMETERS d3dpp;
  70.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  71.     d3dpp.Windowed = TRUE;
  72.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  73.     d3dpp.hDeviceWindow = window;
  74.     d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  75.  
  76.     d3d->CreateDevice(
  77.         D3DADAPTER_DEFAULT,
  78.         D3DDEVTYPE_HAL,
  79.         window,
  80.         D3DCREATE_HARDWARE_VERTEXPROCESSING,
  81.         &d3dpp,
  82.         &d3ddev);
  83.  
  84.     D3DXCreateFontW(
  85.         d3ddev,
  86.         20,
  87.         0,
  88.         FW_NORMAL,
  89.         1,
  90.         FALSE,
  91.         DEFAULT_CHARSET,
  92.         OUT_DEFAULT_PRECIS,
  93.         ANTIALIASED_QUALITY,
  94.         DEFAULT_PITCH | FF_DONTCARE,
  95.         L"Source Code Pro",
  96.         &d3dfont);
  97.  
  98.     MSG msg;
  99.     while (1)
  100.     {
  101.         while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
  102.         {
  103.             TranslateMessage(&msg);
  104.             DispatchMessage(&msg);
  105.         }
  106.  
  107.         if (msg.message == WM_QUIT)
  108.             break;
  109.  
  110.         render_frame();
  111.     }
  112.  
  113.     d3dfont->Release();
  114.     d3ddev->Release();
  115.     d3d->Release();
  116.  
  117.     return (int)msg.wParam;
  118. }
  119.  
  120. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  121. {
  122.     switch (message)
  123.     {
  124.     case WM_DESTROY:
  125.         PostQuitMessage(0);
  126.         break;
  127.     default:
  128.         return DefWindowProc(hWnd, message, wParam, lParam);
  129.     }
  130.     return 0;
  131. }
  132.  
  133. void render_frame()
  134. {
  135.     LOG("Start of the function."s);
  136.  
  137.     static auto last = chrono::steady_clock::now();
  138.     static int framesDrawn = 0;
  139.     static auto refreshFPSAt = last + 1s;
  140.     static wstring fps = L"0"s;
  141.     framesDrawn++;
  142.  
  143.     LOG("Calling chrono::steady_clock::now()"s);
  144.     auto now = chrono::steady_clock::now();
  145.     if (now > refreshFPSAt)
  146.     {
  147.         refreshFPSAt = now + 1s;
  148.         fps = to_wstring(framesDrawn);
  149.         LOG("FPS: "s + to_string(framesDrawn));
  150.         framesDrawn = 0;
  151.     }
  152.  
  153.     auto secondsPassed = chrono::duration_cast<chrono::nanoseconds>(now - last).count() / 1000000000.0;
  154.     last = now;
  155.     static auto color = 255.0;
  156.     color += secondsPassed * 127.0;
  157.     color = fmod(color, 512.0);
  158.  
  159.     LOG("d3ddev->Clear()"s);
  160.     d3ddev->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, static_cast<int>(color > 256.0 ? 512.0 - color : color)), 1.0f, 0);
  161.     LOG("d3ddev->BeginScene()"s);
  162.     d3ddev->BeginScene();
  163.  
  164.     RECT textRect{ 0, 0, 100, 100 };
  165.  
  166.     /*
  167.      * Uncomment that stuff below to trigger the issue.
  168.      */
  169.     //LOG("d3dfont->DrawTextW()"s);
  170.     //d3dfont->DrawTextW(
  171.     //  nullptr,
  172.     //  L"Hello",
  173.     //  -1,
  174.     //  &textRect,
  175.     //  DT_LEFT | DT_TOP,
  176.     //  D3DCOLOR_XRGB(255, 255, 255));
  177.  
  178.     LOG("d3ddev->EndScene()"s);
  179.     d3ddev->EndScene();
  180.     LOG("d3ddev->Present()"s);
  181.     d3ddev->Present(nullptr, nullptr, nullptr, nullptr);
  182.  
  183.     //Sleep(500);
  184.  
  185.     LOG("End of the function."s);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement