10 #include "../stdafx.h"
11 #include "../openttd.h"
12 #include "../gfx_func.h"
13 #include "../os/windows/win32.h"
15 #include "../blitter/factory.hpp"
16 #include "../core/geometry_func.hpp"
17 #include "../core/math_func.hpp"
18 #include "../core/random_func.hpp"
19 #include "../texteff.hpp"
20 #include "../thread.h"
21 #include "../progress.h"
22 #include "../window_gui.h"
23 #include "../window_func.h"
24 #include "../framerate_type.h"
28 #include <versionhelpers.h>
30 #include "../safeguards.h"
33 #ifndef MAPVK_VK_TO_CHAR
34 #define MAPVK_VK_TO_CHAR (2)
38 #define PM_QS_INPUT 0x20000
42 #define WM_DPICHANGED 0x02E0
45 bool _window_maximize;
51 bool VideoDriver_Win32Base::ClaimMousePointer()
53 MyShowCursor(
false,
true);
63 #define AS(x, z) {x, 0, z}
64 #define AM(x, y, z, w) {x, y - x, z}
68 AM(VK_PRIOR, VK_DOWN, WKC_PAGEUP, WKC_DOWN),
70 AM(
'A',
'Z',
'A',
'Z'),
71 AM(
'0',
'9',
'0',
'9'),
73 AS(VK_ESCAPE, WKC_ESC),
74 AS(VK_PAUSE, WKC_PAUSE),
75 AS(VK_BACK, WKC_BACKSPACE),
76 AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE),
78 AS(VK_SPACE, WKC_SPACE),
79 AS(VK_RETURN, WKC_RETURN),
83 AM(VK_F1, VK_F12, WKC_F1, WKC_F12),
86 AM(VK_NUMPAD0, VK_NUMPAD9,
'0',
'9'),
87 AS(VK_DIVIDE, WKC_NUM_DIV),
88 AS(VK_MULTIPLY, WKC_NUM_MUL),
89 AS(VK_SUBTRACT, WKC_NUM_MINUS),
90 AS(VK_ADD, WKC_NUM_PLUS),
91 AS(VK_DECIMAL, WKC_NUM_DECIMAL),
107 static uint MapWindowsKey(uint sym)
112 for (map = _vk_mapping; map !=
endof(_vk_mapping); ++map) {
113 if ((uint)(sym - map->vk_from) <= map->vk_count) {
114 key = sym - map->vk_from + map->map_to;
119 if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT;
120 if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL;
121 if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT;
142 _fullscreen = full_screen;
164 if (settings.dmBitsPerPel == 8 && ChangeDisplaySettings(&
settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
169 if (ChangeDisplaySettings(&
settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
171 GetWindowRect(GetDesktopWindow(), &r);
174 if ((
int)
settings.dmPelsWidth != r.right - r.left || (
int)
settings.dmPelsHeight != r.bottom - r.top) {
179 if (ChangeDisplaySettings(&
settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
185 ChangeDisplaySettings(
nullptr, 0);
187 this->
width = _bck_resolution.width;
188 this->
height = _bck_resolution.height;
193 DWORD style, showstyle;
196 showstyle = SW_SHOWNORMAL;
202 style = WS_OVERLAPPEDWINDOW;
204 if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
208 AdjustWindowRect(&r, style, FALSE);
209 w = r.right - r.left;
210 h = r.bottom - r.top;
213 if (!_window_maximize && resize) SetWindowPos(this->
main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
221 mi.cbSize =
sizeof(mi);
222 GetMonitorInfo(MonitorFromWindow(0, MONITOR_DEFAULTTOPRIMARY), &mi);
224 x = (mi.rcWork.right - mi.rcWork.left - w) / 2;
225 y = (mi.rcWork.bottom - mi.rcWork.top - h) / 2;
228 char window_title[64];
229 seprintf(window_title,
lastof(window_title),
"OpenTTD %s", _openttd_revision);
231 this->
main_wnd = CreateWindow(L
"OTTD",
OTTD2FS(window_title).c_str(), style, x, y, w, h, 0, 0, GetModuleHandle(
nullptr),
this);
233 ShowWindow(this->
main_wnd, showstyle);
246 static WChar prev_char = 0;
250 if (prev_char != 0)
Debug(driver, 1,
"Got two UTF-16 lead surrogates, dropping the first one");
251 prev_char = charcode;
256 if (prev_char != 0) {
260 Debug(driver, 1,
"Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
273 return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
279 HIMC hIMC = ImmGetContext(hwnd);
282 cf.dwStyle = CFS_POINT;
287 cf.ptCurrentPos.x = _focused_window->
left + pt.x;
288 cf.ptCurrentPos.y = _focused_window->
top + pt.y;
290 cf.ptCurrentPos.x = 0;
291 cf.ptCurrentPos.y = 0;
293 ImmSetCompositionWindow(hIMC, &cf);
295 ImmReleaseContext(hwnd, hIMC);
301 HIMC hIMC = ImmGetContext(hwnd);
305 cf.dwStyle = CFS_EXCLUDE;
309 cf.ptCurrentPos.x = _focused_window->
left + pt.x;
310 cf.ptCurrentPos.y = _focused_window->
top + pt.y;
312 cf.rcArea.left = _focused_window->
left;
313 cf.rcArea.top = _focused_window->
top;
314 cf.rcArea.right = _focused_window->
left + _focused_window->
width;
315 cf.rcArea.bottom = _focused_window->
top + _focused_window->
height;
323 cf.ptCurrentPos.x = 0;
324 cf.ptCurrentPos.y = 0;
325 SetRectEmpty(&cf.rcArea);
327 ImmSetCandidateWindow(hIMC, &cf);
329 ImmReleaseContext(hwnd, hIMC);
335 HIMC hIMC = ImmGetContext(hwnd);
338 if (lParam & GCS_RESULTSTR) {
340 LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR,
nullptr, 0);
341 wchar_t *str = (
wchar_t *)_alloca(len +
sizeof(
wchar_t));
342 len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len);
343 str[len /
sizeof(wchar_t)] =
'\0';
353 lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
358 LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR,
nullptr, 0);
359 wchar_t *str = (
wchar_t *)_alloca(len +
sizeof(
wchar_t));
360 len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len);
361 str[len /
sizeof(wchar_t)] =
'\0';
364 static char utf8_buf[1024];
368 LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS,
nullptr, 0);
369 const char *caret = utf8_buf;
370 for (
const wchar_t *c = str; *c !=
'\0' && *caret !=
'\0' && caret_bytes > 0; c++, caret_bytes--) {
384 lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
387 ImmReleaseContext(hwnd, hIMC);
389 return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
395 HIMC hIMC = ImmGetContext(hwnd);
396 if (hIMC != NULL) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
397 ImmReleaseContext(hwnd, hIMC);
402 LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
404 static uint32 keycode = 0;
405 static bool console =
false;
411 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)((LPCREATESTRUCT)lParam)->lpCreateParams);
419 GetUpdateRect(hwnd, &r, FALSE);
420 video_driver->
MakeDirty(r.left, r.top, r.right - r.left, r.bottom - r.top);
422 ValidateRect(hwnd,
nullptr);
426 case WM_PALETTECHANGED:
427 if ((HWND)wParam == hwnd)
return 0;
430 case WM_QUERYNEWPALETTE:
435 HandleExitGameRequest();
476 int x = (int16)LOWORD(lParam);
477 int y = (int16)HIWORD(lParam);
485 tme.cbSize =
sizeof(tme);
486 tme.dwFlags = TME_LEAVE;
487 tme.hwndTrack = hwnd;
489 TrackMouseEvent(&tme);
496 while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
497 x = (int16)LOWORD(m.lParam);
498 y = (int16)HIWORD(m.lParam);
504 pt.x = _cursor.
pos.x;
505 pt.y = _cursor.
pos.y;
506 ClientToScreen(hwnd, &pt);
507 SetCursorPos(pt.x, pt.y);
514 case WM_INPUTLANGCHANGE:
518 case WM_IME_SETCONTEXT:
523 case WM_IME_STARTCOMPOSITION:
528 case WM_IME_COMPOSITION:
531 case WM_IME_ENDCOMPOSITION:
542 console =
GB(lParam, 16, 8) == 41;
546 uint scancode =
GB(lParam, 16, 8);
547 uint charcode = wParam;
551 if (console && scancode == 41) {
558 uint cur_keycode = keycode;
566 uint scancode =
GB(lParam, 16, 8);
567 keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
569 uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
580 if (
HasBit(charcode, 31) && !console) {
581 if (scancode == 41) {
590 uint cur_keycode = keycode;
620 if (wParam != SIZE_MINIMIZED) {
623 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
624 if (_window_maximize || _fullscreen) _bck_resolution =
_cur_resolution;
625 video_driver->ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
630 RECT *r = (RECT*)lParam;
634 SetRect(&r2, 0, 0, 0, 0);
635 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
637 w = r->right - r->left - (r2.right - r2.left);
638 h = r->bottom - r->top - (r2.bottom - r2.top);
641 SetRect(&r2, 0, 0, w, h);
643 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
644 w = r2.right - r2.left;
645 h = r2.bottom - r2.top;
649 r->bottom = r->top + h;
652 case WMSZ_BOTTOMLEFT:
653 r->bottom = r->top + h;
654 r->left = r->right - w;
657 case WMSZ_BOTTOMRIGHT:
658 r->bottom = r->top + h;
659 r->right = r->left + w;
663 r->left = r->right - w;
667 r->right = r->left + w;
671 r->top = r->bottom - h;
675 r->top = r->bottom - h;
676 r->left = r->right - w;
680 r->top = r->bottom - h;
681 r->right = r->left + w;
687 case WM_DPICHANGED: {
691 RECT *prcNewWindow = (RECT *)lParam;
696 prcNewWindow->right - prcNewWindow->left,
697 prcNewWindow->bottom - prcNewWindow->top,
698 SWP_NOZORDER | SWP_NOACTIVATE);
706 #if !defined(WM_MOUSEWHEEL)
707 # define WM_MOUSEWHEEL 0x020A
709 #if !defined(GET_WHEEL_DELTA_WPARAM)
710 # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
713 case WM_MOUSEWHEEL: {
714 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
718 }
else if (delta > 0) {
736 if (_exit_game)
break;
738 bool active = (LOWORD(wParam) != WA_INACTIVE);
739 bool minimized = (HIWORD(wParam) != 0);
741 if (active && minimized) {
744 ShowWindow(hwnd, SW_RESTORE);
747 }
else if (!active && !minimized) {
749 ShowWindow(hwnd, SW_MINIMIZE);
750 ChangeDisplaySettings(
nullptr, 0);
757 return DefWindowProc(hwnd, msg, wParam, lParam);
760 static void RegisterWndClass()
762 static bool registered =
false;
764 if (registered)
return;
766 HINSTANCE hinst = GetModuleHandle(
nullptr);
773 LoadIcon(hinst, MAKEINTRESOURCE(100)),
774 LoadCursor(
nullptr, IDC_ARROW),
781 if (!RegisterClass(&wnd))
usererror(
"RegisterClass failed");
784 static const Dimension default_resolutions[] = {
798 static void FindResolutions(uint8 bpp)
803 for (uint i = 0; EnumDisplaySettings(
nullptr, i, &dm) != 0; i++) {
804 if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480)
continue;
806 _resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
811 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
817 void VideoDriver_Win32Base::Initialize()
835 if (this->
fullscreen) ChangeDisplaySettings(
nullptr, 0);
847 this->
MakeDirty(0, 0, _screen.width, _screen.height);
868 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
869 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
870 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
871 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
883 if (!PeekMessage(&mesg,
nullptr, 0, 0, PM_REMOVE))
return false;
887 DispatchMessage(&mesg);
897 if (_exit_game)
break;
906 void VideoDriver_Win32Base::ClientSizeChanged(
int w,
int h,
bool force)
920 if (_window_maximize) ShowWindow(this->
main_wnd, SW_SHOWNORMAL);
943 static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hDC, LPRECT rc, LPARAM data)
945 auto &list = *
reinterpret_cast<std::vector<int>*
>(data);
947 MONITORINFOEX monitorInfo = {};
948 monitorInfo.cbSize =
sizeof(MONITORINFOEX);
949 GetMonitorInfo(hMonitor, &monitorInfo);
951 DEVMODE devMode = {};
952 devMode.dmSize =
sizeof(DEVMODE);
953 devMode.dmDriverExtra = 0;
954 EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
956 if (devMode.dmDisplayFrequency != 0) list.push_back(devMode.dmDisplayFrequency);
962 std::vector<int> rates = {};
963 EnumDisplayMonitors(
nullptr,
nullptr, MonitorEnumProc,
reinterpret_cast<LPARAM
>(&rates));
969 return {
static_cast<uint
>(GetSystemMetrics(SM_CXSCREEN)),
static_cast<uint
>(GetSystemMetrics(SM_CYSCREEN)) };
974 typedef UINT (WINAPI *PFNGETDPIFORWINDOW)(HWND hwnd);
975 typedef UINT (WINAPI *PFNGETDPIFORSYSTEM)(VOID);
976 typedef HRESULT (WINAPI *PFNGETDPIFORMONITOR)(HMONITOR hMonitor,
int dpiType, UINT *dpiX, UINT *dpiY);
978 static PFNGETDPIFORWINDOW _GetDpiForWindow =
nullptr;
979 static PFNGETDPIFORSYSTEM _GetDpiForSystem =
nullptr;
980 static PFNGETDPIFORMONITOR _GetDpiForMonitor =
nullptr;
982 static bool init_done =
false;
987 _GetDpiForWindow = _user32.GetProcAddress(
"GetDpiForWindow");
988 _GetDpiForSystem = _user32.GetProcAddress(
"GetDpiForSystem");
989 _GetDpiForMonitor = _shcore.GetProcAddress(
"GetDpiForMonitor");
994 if (cur_dpi == 0 && _GetDpiForWindow !=
nullptr && this->
main_wnd !=
nullptr) {
996 cur_dpi = _GetDpiForWindow(this->
main_wnd);
998 if (cur_dpi == 0 && _GetDpiForMonitor !=
nullptr && this->
main_wnd !=
nullptr) {
1001 if (SUCCEEDED(_GetDpiForMonitor(MonitorFromWindow(this->
main_wnd, MONITOR_DEFAULTTOPRIMARY), 0 , &dpiX, &dpiY))) {
1005 if (cur_dpi == 0 && _GetDpiForSystem !=
nullptr) {
1007 cur_dpi = _GetDpiForSystem();
1010 return cur_dpi > 0 ? cur_dpi / 96.0f : 1.0f;
1019 assert(_screen.dst_ptr !=
nullptr);
1026 assert(_screen.dst_ptr !=
nullptr);
1027 if (_screen.dst_ptr !=
nullptr) {
1030 _screen.dst_ptr =
nullptr;
1045 this->MakePalette();
1068 w = std::max(w, 64);
1069 h = std::max(h, 64);
1071 if (!force && w == _screen.width && h == _screen.height)
return false;
1073 BITMAPINFO *bi = (BITMAPINFO *)alloca(
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * 256);
1074 memset(bi, 0,
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * 256);
1075 bi->bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
1077 bi->bmiHeader.biWidth = this->
width = w;
1078 bi->bmiHeader.biHeight = -(this->
height = h);
1080 bi->bmiHeader.biPlanes = 1;
1081 bi->bmiHeader.biBitCount = bpp;
1082 bi->bmiHeader.biCompression = BI_RGB;
1087 this->
dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID **)&this->
buffer_bits,
nullptr, 0);
1092 _screen.pitch = (bpp == 8) ?
Align(w, 4) : w;
1105 void VideoDriver_Win32GDI::MakePalette()
1109 LOGPALETTE *pal = (LOGPALETTE*)alloca(
sizeof(LOGPALETTE) + (256 - 1) *
sizeof(PALETTEENTRY));
1111 pal->palVersion = 0x300;
1112 pal->palNumEntries = 256;
1114 for (uint i = 0; i != 256; i++) {
1118 pal->palPalEntry[i].peFlags = 0;
1125 void VideoDriver_Win32GDI::UpdatePalette(HDC dc, uint start, uint count)
1129 for (uint i = 0; i != count; i++) {
1133 rgb[i].rgbReserved = 0;
1136 SetDIBColorTable(dc, start, count, rgb);
1141 HDC hDC = GetWindowDC(hWnd);
1142 HPALETTE hOldPalette = SelectPalette(hDC, this->
gdi_palette, FALSE);
1143 UINT nChanged = RealizePalette(hDC);
1145 SelectPalette(hDC, hOldPalette, TRUE);
1146 ReleaseDC(hWnd, hDC);
1147 if (nChanged != 0) this->
MakeDirty(0, 0, _screen.width, _screen.height);
1157 HDC dc2 = CreateCompatibleDC(dc);
1159 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, this->
dib_sect);
1160 HPALETTE old_palette = SelectPalette(dc, this->
gdi_palette, FALSE);
1184 BitBlt(dc, 0, 0, this->
width, this->
height, dc2, 0, 0, SRCCOPY);
1185 SelectPalette(dc, old_palette, TRUE);
1186 SelectObject(dc2, old_bmp);
1197 int VideoDriver_Win32GDI::RedrawScreenDebug()
1216 #include "../3rdparty/opengl/glext.h"
1217 #include "../3rdparty/opengl/wglext.h"
1220 #ifndef PFD_SUPPORT_COMPOSITION
1221 # define PFD_SUPPORT_COMPOSITION 0x00008000
1224 static PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB =
nullptr;
1225 static PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT =
nullptr;
1226 static bool _hasWGLARBCreateContextProfile =
false;
1231 OGLProc ret =
reinterpret_cast<OGLProc
>(wglGetProcAddress(proc));
1232 if (ret ==
nullptr) {
1234 ret =
reinterpret_cast<OGLProc
>(GetProcAddress(GetModuleHandle(L
"opengl32"), proc));
1245 static const char *SelectPixelFormat(HDC dc,
bool fullscreen)
1247 PIXELFORMATDESCRIPTOR pfd = {
1248 sizeof(PIXELFORMATDESCRIPTOR),
1250 PFD_DRAW_TO_WINDOW |
1251 PFD_SUPPORT_OPENGL |
1256 0, 0, 0, 0, 0, 0, 0, 0,
1264 if (IsWindowsVistaOrGreater()) pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
1267 int format = ChoosePixelFormat(dc, &pfd);
1268 if (format == 0)
return "No suitable pixel format found";
1269 if (!SetPixelFormat(dc, format, &pfd))
return "Can't set pixel format";
1275 static void LoadWGLExtensions()
1282 HWND wnd = CreateWindow(_T(
"STATIC"), _T(
"dummy"), WS_OVERLAPPEDWINDOW, 0, 0, 0, 0,
nullptr,
nullptr, GetModuleHandle(
nullptr),
nullptr);
1283 HDC dc = GetDC(wnd);
1286 if (SelectPixelFormat(dc,
false) ==
nullptr) {
1288 HGLRC rc = wglCreateContext(dc);
1289 if (rc !=
nullptr) {
1290 wglMakeCurrent(dc, rc);
1294 #pragma GCC diagnostic push
1295 #pragma GCC diagnostic ignored "-Wcast-function-type"
1299 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress(
"wglGetExtensionsStringARB");
1300 if (wglGetExtensionsStringARB !=
nullptr) {
1301 const char *wgl_exts = wglGetExtensionsStringARB(dc);
1304 _wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress(
"wglCreateContextAttribsARB");
1308 _wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress(
"wglSwapIntervalEXT");
1313 #pragma GCC diagnostic pop
1315 wglMakeCurrent(
nullptr,
nullptr);
1316 wglDeleteContext(rc);
1324 static FVideoDriver_Win32OpenGL iFVideoDriver_Win32OpenGL;
1326 const char *VideoDriver_Win32OpenGL::Start(
const StringList ¶m)
1332 LoadWGLExtensions();
1335 this->MakeWindow(_fullscreen);
1338 const char *err = this->AllocateContext();
1339 if (err !=
nullptr) {
1345 this->driver_info = GetName();
1346 this->driver_info +=
" (";
1348 this->driver_info +=
")";
1350 this->ClientSizeChanged(this->width, this->height,
true);
1352 if (_screen.dst_ptr ==
nullptr) {
1355 return "Can't get pointer to screen buffer";
1358 this->ReleaseVideoPointer();
1367 void VideoDriver_Win32OpenGL::Stop()
1369 this->DestroyContext();
1373 void VideoDriver_Win32OpenGL::DestroyContext()
1377 wglMakeCurrent(
nullptr,
nullptr);
1378 if (this->gl_rc !=
nullptr) {
1379 wglDeleteContext(this->gl_rc);
1380 this->gl_rc =
nullptr;
1382 if (this->dc !=
nullptr) {
1383 ReleaseDC(this->main_wnd, this->dc);
1388 void VideoDriver_Win32OpenGL::ToggleVsync(
bool vsync)
1390 if (_wglSwapIntervalEXT !=
nullptr) {
1391 _wglSwapIntervalEXT(vsync);
1393 Debug(driver, 0,
"OpenGL: Vsync requested, but not supported by driver");
1397 const char *VideoDriver_Win32OpenGL::AllocateContext()
1399 this->dc = GetDC(this->main_wnd);
1401 const char *err = SelectPixelFormat(this->dc, this->fullscreen);
1402 if (err !=
nullptr)
return err;
1407 if (_wglCreateContextAttribsARB !=
nullptr) {
1410 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
1411 WGL_CONTEXT_MINOR_VERSION_ARB, 5,
1412 WGL_CONTEXT_FLAGS_ARB, _debug_driver_level >= 8 ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
1413 _hasWGLARBCreateContextProfile ? WGL_CONTEXT_PROFILE_MASK_ARB : 0, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
1416 rc = _wglCreateContextAttribsARB(this->dc,
nullptr, attribs);
1418 if (rc ==
nullptr) {
1422 rc = _wglCreateContextAttribsARB(this->dc,
nullptr, attribs);
1426 if (rc ==
nullptr) {
1428 rc = wglCreateContext(this->dc);
1429 if (rc ==
nullptr)
return "Can't create OpenGL context";
1431 if (!wglMakeCurrent(this->dc, rc))
return "Can't active GL context";
1439 bool VideoDriver_Win32OpenGL::ToggleFullscreen(
bool full_screen)
1441 if (_screen.dst_ptr !=
nullptr) this->ReleaseVideoPointer();
1442 this->DestroyContext();
1444 res &= this->AllocateContext() ==
nullptr;
1445 this->ClientSizeChanged(this->width, this->height,
true);
1449 bool VideoDriver_Win32OpenGL::AfterBlitterChange()
1452 this->ClientSizeChanged(this->width, this->height,
true);
1456 void VideoDriver_Win32OpenGL::PopulateSystemSprites()
1461 void VideoDriver_Win32OpenGL::ClearSystemSprites()
1466 bool VideoDriver_Win32OpenGL::AllocateBackingStore(
int w,
int h,
bool force)
1468 if (!force && w == _screen.width && h == _screen.height)
return false;
1470 this->width = w = std::max(w, 64);
1471 this->height = h = std::max(h, 64);
1473 if (this->gl_rc ==
nullptr)
return false;
1475 if (_screen.dst_ptr !=
nullptr) this->ReleaseVideoPointer();
1477 this->dirty_rect = {};
1479 SwapBuffers(this->dc);
1480 _screen.dst_ptr = this->GetVideoPointer();
1485 void *VideoDriver_Win32OpenGL::GetVideoPointer()
1493 void VideoDriver_Win32OpenGL::ReleaseVideoPointer()
1497 this->dirty_rect = {};
1498 _screen.dst_ptr =
nullptr;
1499 this->anim_buffer =
nullptr;
1502 void VideoDriver_Win32OpenGL::Paint()
1521 SwapBuffers(this->dc);