前提・実現したいこと
以前の質問WGLの初期化 プロシージャの記法についての回答を踏まえコード全体の流れを理解し直しながら書き直しWGLでウィンドウにレンダリングまではうまくいったのですが実行時に最初白い画面が表示された後WGLによって描画された画面が表示されます.このラグをなくすためにはどうしたらいいでしょうか?
また画面サイズの取得はGetClientRect()
やWM_SIZE
のLPARAM
でもできますが取得方法で描画速度に影響は出てくるでしょうか?WM_SIZE
とWM_PAINT
の呼ばれるタイミング次第でしょうか?
該当のコード
C
1#define UNICODE 2 3#pragma comment(lib,"opengl32.lib") 4 5#include <windows.h> 6#include <gl/GL.h> 7 8 9LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM); 10 11const wchar_t CLASS_NAME[] = L"Class_Name"; 12 13int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR nCmdLine, int nCmdShow) { 14 UNREFERENCED_PARAMETER(hPrevInstance); 15 UNREFERENCED_PARAMETER(nCmdLine); 16 17 WNDCLASSEX wc = { 18 sizeof(WNDCLASSEX), CS_VREDRAW | CS_HREDRAW, WindowProc, 19 0, 0, hInstance, 20 NULL, (HCURSOR)LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), 21 NULL, CLASS_NAME, NULL 22 }; 23 24 if (!RegisterClassEx(&wc)) { 25 MessageBox(NULL, L"Register Failed", L"CAUTION", MB_OK); 26 return -1; 27 } 28 29 HWND hwnd = CreateWindowEx( 30 0, CLASS_NAME, L"Device Independent", WS_OVERLAPPEDWINDOW, 31 CW_USEDEFAULT, CW_USEDEFAULT, 960, 540, 32 NULL, NULL, hInstance, NULL 33 ); 34 35 if (hwnd == NULL) { 36 MessageBox(NULL, L"HWND Failed", L"CAUTION", MB_OK); 37 return -1; 38 } 39 40 ShowWindow(hwnd, nCmdShow); 41 UpdateWindow(hwnd); 42 43 MSG msg = {}; 44 45 while (GetMessage(&msg, hwnd, 0, 0) > 0) { 46 TranslateMessage(&msg); 47 DispatchMessage(&msg); 48 } 49 50 return (int)msg.wParam; 51} 52 53LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 54 static int iWidth; 55 static int iHeight; 56 static RECT rc; 57 switch (uMsg) { 58 case WM_ERASEBKGND: 59 return -1; 60 61 case WM_CREATE: 62 break; 63 64 case WM_PAINT: 65 { 66 GetClientRect(hwnd, &rc); 67 iWidth = rc.right - rc.left; 68 iHeight = rc.bottom - rc.top; 69 70 /* Make Memory Device Context */ 71 BITMAPINFO bmi = { 72 sizeof(BITMAPINFOHEADER), iWidth, iHeight, 1, 24, BI_RGB, 0, 0, 0, 0, 0, NULL 73 }; 74 75 HDC tmpDIB = CreateCompatibleDC(NULL); 76 77 if (tmpDIB == NULL)MessageBox(NULL, L"CCDC Failed", L"Caution", MB_OK); 78 79 void *pv; 80 81 HBITMAP hbmDIB = CreateDIBSection(tmpDIB, &bmi, DIB_RGB_COLORS, &pv, NULL, 0); 82 83 if (hbmDIB == NULL)MessageBox(NULL, L"DIBSec Failed", L"Caution", MB_OK); 84 85 HGDIOBJ test_a = SelectObject(tmpDIB, hbmDIB); 86 87 if (test_a == NULL)MessageBox(NULL, L"SelectObject Failed", L"Caution", MB_OK); 88 /* END Make Memory Device Context */ 89 /* SET PIXELFORMATDESCRIPTOR */ 90 PIXELFORMATDESCRIPTOR pfd = { 91 sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_SUPPORT_OPENGL | PFD_DRAW_TO_BITMAP, PFD_TYPE_RGBA, 24, 92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0, 93 }; 94 95 96 int format = ChoosePixelFormat(tmpDIB, &pfd); 97 if (format == 0) { 98 MessageBox(NULL, L"ChoosePixelFormat Failed", L"CAUTION", MB_OK); 99 GetLastError(); 100 } 101 if (SetPixelFormat(tmpDIB, format, &pfd) == FALSE)MessageBox(NULL, L"SetPixelFormat Failed", L"CAUTION", MB_OK); 102 103 HGLRC mglrc = wglCreateContext(tmpDIB); 104 if (mglrc == NULL)MessageBox(NULL, L"wglCreateContext Failed", L"CAUTION", MB_OK); 105 106 /* END PIXELFORMATDESCRIPTOR*/ 107 wglMakeCurrent(tmpDIB, mglrc); 108 /* GL Initialization */ 109 glEnable(GL_COLOR_MATERIAL); 110 glEnable(GL_BLEND); 111 112 113 glViewport(0, 0, iWidth, iHeight); 114 glMatrixMode(GL_PROJECTION); 115 glLoadIdentity(); 116 glMatrixMode(GL_MODELVIEW); 117 glLoadIdentity(); 118 /* END GL Initialization */ 119 /* GL Contents */ 120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 121 122 glPushMatrix(); 123 124 glColor3f(0, 1, 1); 125 glBegin(GL_TRIANGLES); 126 glColor3f(1.0f, 0.0f, 0.0f); 127 glVertex3f(0.0f, 1.0f, 0.0f); 128 glVertex3f(-1.0f, -1.0f, 0.0f); 129 glColor3f(0.0f, 0.0f, 1.0f); 130 glVertex3f(1.0f, -1.0f, 0.0f); 131 glEnd(); 132 133 glPopMatrix(); 134 glFlush(); 135 /* END GL Contents */ 136 /* PASS DC */ 137 138 PAINTSTRUCT ps; 139 HDC hdc = BeginPaint(hwnd, &ps); 140 BitBlt(hdc, 0, 0, iWidth, iHeight, tmpDIB, 0, 0, SRCCOPY); 141 EndPaint(hwnd, &ps); 142 143 DeleteDC(tmpDIB); 144 DeleteObject(mglrc); 145 } 146 break; 147 148 case WM_DESTROY: 149 break; 150 } 151 152 return DefWindowProc(hwnd, uMsg, wParam, lParam); 153}
補足情報
Windows10 Pro
VisualStudio2017 Community
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。