1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
internal bool
create_service_window()
{
WNDCLASSEX service_window_class = {};
service_window_class.style = CS_OWNDC;
service_window_class.cbSize = sizeof(service_window_class);
service_window_class.lpfnWndProc = &win32_service_window_proc;
service_window_class.hInstance = GetModuleHandle(NULL);
service_window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
service_window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
service_window_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
service_window_class.lpszClassName = "win32_service_window_class";
if (RegisterClassEx(&service_window_class) == 0)
{
printf("RegisterClassEx() failed\n");
return 0;
}
const char *service_window_name = "win32_service_window";
HWND service_window = CreateWindowEx(0,
service_window_class.lpszClassName,
service_window_name,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
service_window_class.hInstance,
0);
if (!service_window)
{
printf("CreateWindowEx() failed\n");
return 0;
}
/*
* Create fake gl context, so I can create window with modern opengl context.
* Doing this with the service window prevents creating another 'fake' window.
*/
HDC dc = GetDC(service_window);
if (!dc)
{
printf("GetDC() failed\n");
return false;
}
PIXELFORMATDESCRIPTOR pfd;
init_pfd(&pfd);
int pixel_format = ChoosePixelFormat(dc, &pfd);
if (pixel_format == 0)
{
printf("ChoosePixelFormat failed\n");
return false;
}
BOOL pixel_format_set = SetPixelFormat(dc, pixel_format, &pfd);
if (pixel_format_set == FALSE)
{
printf("SetPixelFormat() failed\n");
return false;
}
HGLRC glrc = wglCreateContext(dc);
if (!glrc)
{
printf("wglCreateContext() failed\n");
return false;
}
BOOL made_current = wglMakeCurrent(dc, glrc);
if (made_current == FALSE)
{
printf("wglMakeCurrent() failed\n");
return false;
}
// TODO: check if extensions are actually supported
// "WGL_ARB_pixel_format"
// "WGL_ARB_create_context"
// TODO: check all return values indicating invalid function from wglGetProcAddress
// msdn: 0 https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglgetprocaddress
// khronos: (void*){0,1,2,3,-1} https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions#Windows_2
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if (!wglChoosePixelFormatARB || !wglCreateContextAttribsARB)
{
printf("wgl functions to create context not received\n");
return false;
}
g_service_window = service_window;
return true;
}
Win32_Window* platform_create_window(const char *name, int width, int height)
{
Win32_Window *window = (Win32_Window*)malloc(sizeof(Platform_Window));
if (!window)
{
printf("out of memory to create window\n");
return 0;
}
// Note: SendMessage returns when execution is finished, so settings can be on stack
Win32_Window_Settings settings = {name, width, height};
HWND handle = (HWND)SendMessage(g_service_window, WIN32_CREATE_WINDOW, (WPARAM)&settings, 0);
if (!handle)
return 0;
HDC dc = GetDC(handle);
if (!dc)
{
printf("GetDC failed\n");
return 0;
}
const int pixel_format_attribs[] =
{
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
0, // End
};
int pixel_format;
UINT cnt_pixel_formats;
if (wglChoosePixelFormatARB(dc, pixel_format_attribs, NULL, 1, &pixel_format, &cnt_pixel_formats) == FALSE)
{
printf("wglChoosePixelFormat return false\n");
return 0;
}
PIXELFORMATDESCRIPTOR pfd;
init_pfd(&pfd);
if (SetPixelFormat(dc, pixel_format, &pfd) == FALSE)
{
printf("SetPixelFormat return false\n");
return 0;
}
int context_attribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0
};
HGLRC glrc = wglCreateContextAttribsARB(dc, 0, context_attribs);
if (!glrc)
{
printf("wglCreateContextAttribsARB failed\n");
return 0;
}
if (wglMakeCurrent(dc, glrc) == FALSE)
{
printf("wglMakeCurrent failed\n");
return 0;
}
window->window = handle;
window->dc = dc;
window->glrc = glrc;
return window;
}
bool platform_init_windowing()
{
g_main_thread_id = GetCurrentThreadId();
g_opengl_module = LoadLibraryA("opengl32.dll");
if (!g_opengl_module)
{
printf("can't open opengl32.dll\n");
return false;
}
DWORD tid;
HANDLE thread = CreateThread(0, 0, win32_service_window_thread, 0, 0, &tid);
if (!thread)
{
printf("error: CreateThread(...) failed\n");
return false;
}
// wait until service window is ready
for (;;)
{
MSG message;
GetMessageA(&message, 0, 0, 0);
if (message.message == WIN32_SERVICE_WINDOW_CREATED)
return true;
else
return false;
}
}
|