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);
217 mi.cbSize =
sizeof(mi);
218 GetMonitorInfo(MonitorFromWindow(0, MONITOR_DEFAULTTOPRIMARY), &mi);
220 int x = (mi.rcWork.right - mi.rcWork.left - w) / 2;
221 int y = (mi.rcWork.bottom - mi.rcWork.top - h) / 2;
223 char window_title[64];
224 seprintf(window_title,
lastof(window_title),
"OpenTTD %s", _openttd_revision);
226 this->
main_wnd = CreateWindow(L
"OTTD",
OTTD2FS(window_title).c_str(), style, x, y, w, h, 0, 0, GetModuleHandle(
nullptr),
this);
228 ShowWindow(this->
main_wnd, showstyle);
241 static WChar prev_char = 0;
245 if (prev_char != 0)
Debug(driver, 1,
"Got two UTF-16 lead surrogates, dropping the first one");
246 prev_char = charcode;
251 if (prev_char != 0) {
255 Debug(driver, 1,
"Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
268 return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
274 HIMC hIMC = ImmGetContext(hwnd);
277 cf.dwStyle = CFS_POINT;
282 cf.ptCurrentPos.x = _focused_window->
left + pt.x;
283 cf.ptCurrentPos.y = _focused_window->
top + pt.y;
285 cf.ptCurrentPos.x = 0;
286 cf.ptCurrentPos.y = 0;
288 ImmSetCompositionWindow(hIMC, &cf);
290 ImmReleaseContext(hwnd, hIMC);
296 HIMC hIMC = ImmGetContext(hwnd);
300 cf.dwStyle = CFS_EXCLUDE;
304 cf.ptCurrentPos.x = _focused_window->
left + pt.x;
305 cf.ptCurrentPos.y = _focused_window->
top + pt.y;
307 cf.rcArea.left = _focused_window->
left;
308 cf.rcArea.top = _focused_window->
top;
309 cf.rcArea.right = _focused_window->
left + _focused_window->
width;
310 cf.rcArea.bottom = _focused_window->
top + _focused_window->
height;
318 cf.ptCurrentPos.x = 0;
319 cf.ptCurrentPos.y = 0;
320 SetRectEmpty(&cf.rcArea);
322 ImmSetCandidateWindow(hIMC, &cf);
324 ImmReleaseContext(hwnd, hIMC);
330 HIMC hIMC = ImmGetContext(hwnd);
333 if (lParam & GCS_RESULTSTR) {
335 LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR,
nullptr, 0);
336 wchar_t *str = (
wchar_t *)_alloca(len +
sizeof(
wchar_t));
337 len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len);
338 str[len /
sizeof(wchar_t)] =
'\0';
348 lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
353 LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR,
nullptr, 0);
354 wchar_t *str = (
wchar_t *)_alloca(len +
sizeof(
wchar_t));
355 len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len);
356 str[len /
sizeof(wchar_t)] =
'\0';
359 static char utf8_buf[1024];
363 LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS,
nullptr, 0);
364 const char *caret = utf8_buf;
365 for (
const wchar_t *c = str; *c !=
'\0' && *caret !=
'\0' && caret_bytes > 0; c++, caret_bytes--) {
379 lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
382 ImmReleaseContext(hwnd, hIMC);
384 return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
390 HIMC hIMC = ImmGetContext(hwnd);
391 if (hIMC != NULL) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
392 ImmReleaseContext(hwnd, hIMC);
397 LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
399 static uint32 keycode = 0;
400 static bool console =
false;
406 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)((LPCREATESTRUCT)lParam)->lpCreateParams);
414 GetUpdateRect(hwnd, &r, FALSE);
415 video_driver->
MakeDirty(r.left, r.top, r.right - r.left, r.bottom - r.top);
417 ValidateRect(hwnd,
nullptr);
421 case WM_PALETTECHANGED:
422 if ((HWND)wParam == hwnd)
return 0;
425 case WM_QUERYNEWPALETTE:
430 HandleExitGameRequest();
471 int x = (int16)LOWORD(lParam);
472 int y = (int16)HIWORD(lParam);
480 tme.cbSize =
sizeof(tme);
481 tme.dwFlags = TME_LEAVE;
482 tme.hwndTrack = hwnd;
484 TrackMouseEvent(&tme);
491 while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
492 x = (int16)LOWORD(m.lParam);
493 y = (int16)HIWORD(m.lParam);
499 pt.x = _cursor.
pos.x;
500 pt.y = _cursor.
pos.y;
501 ClientToScreen(hwnd, &pt);
502 SetCursorPos(pt.x, pt.y);
509 case WM_INPUTLANGCHANGE:
513 case WM_IME_SETCONTEXT:
518 case WM_IME_STARTCOMPOSITION:
523 case WM_IME_COMPOSITION:
526 case WM_IME_ENDCOMPOSITION:
537 console =
GB(lParam, 16, 8) == 41;
541 uint scancode =
GB(lParam, 16, 8);
542 uint charcode = wParam;
546 if (console && scancode == 41) {
553 uint cur_keycode = keycode;
561 uint scancode =
GB(lParam, 16, 8);
562 keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
564 uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
575 if (
HasBit(charcode, 31) && !console) {
576 if (scancode == 41) {
585 uint cur_keycode = keycode;
615 if (wParam != SIZE_MINIMIZED) {
618 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
619 if (_window_maximize || _fullscreen) _bck_resolution =
_cur_resolution;
620 video_driver->ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
625 RECT *r = (RECT*)lParam;
629 SetRect(&r2, 0, 0, 0, 0);
630 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
632 w = r->right - r->left - (r2.right - r2.left);
633 h = r->bottom - r->top - (r2.bottom - r2.top);
636 SetRect(&r2, 0, 0, w, h);
638 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
639 w = r2.right - r2.left;
640 h = r2.bottom - r2.top;
644 r->bottom = r->top + h;
647 case WMSZ_BOTTOMLEFT:
648 r->bottom = r->top + h;
649 r->left = r->right - w;
652 case WMSZ_BOTTOMRIGHT:
653 r->bottom = r->top + h;
654 r->right = r->left + w;
658 r->left = r->right - w;
662 r->right = r->left + w;
666 r->top = r->bottom - h;
670 r->top = r->bottom - h;
671 r->left = r->right - w;
675 r->top = r->bottom - h;
676 r->right = r->left + w;
682 case WM_DPICHANGED: {
686 RECT *prcNewWindow = (RECT *)lParam;
691 prcNewWindow->right - prcNewWindow->left,
692 prcNewWindow->bottom - prcNewWindow->top,
693 SWP_NOZORDER | SWP_NOACTIVATE);
701 #if !defined(WM_MOUSEWHEEL)
702 # define WM_MOUSEWHEEL 0x020A
704 #if !defined(GET_WHEEL_DELTA_WPARAM)
705 # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
708 case WM_MOUSEWHEEL: {
709 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
713 }
else if (delta > 0) {
731 if (_exit_game)
break;
733 bool active = (LOWORD(wParam) != WA_INACTIVE);
734 bool minimized = (HIWORD(wParam) != 0);
736 if (active && minimized) {
739 ShowWindow(hwnd, SW_RESTORE);
742 }
else if (!active && !minimized) {
744 ShowWindow(hwnd, SW_MINIMIZE);
745 ChangeDisplaySettings(
nullptr, 0);
752 return DefWindowProc(hwnd, msg, wParam, lParam);
755 static void RegisterWndClass()
757 static bool registered =
false;
759 if (registered)
return;
761 HINSTANCE hinst = GetModuleHandle(
nullptr);
768 LoadIcon(hinst, MAKEINTRESOURCE(100)),
769 LoadCursor(
nullptr, IDC_ARROW),
776 if (!RegisterClass(&wnd))
usererror(
"RegisterClass failed");
779 static const Dimension default_resolutions[] = {
793 static void FindResolutions(uint8 bpp)
798 for (uint i = 0; EnumDisplaySettings(
nullptr, i, &dm) != 0; i++) {
799 if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480)
continue;
801 _resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
806 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
812 void VideoDriver_Win32Base::Initialize()
830 if (this->
fullscreen) ChangeDisplaySettings(
nullptr, 0);
842 this->
MakeDirty(0, 0, _screen.width, _screen.height);
863 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
864 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
865 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
866 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
878 if (!PeekMessage(&mesg,
nullptr, 0, 0, PM_REMOVE))
return false;
882 DispatchMessage(&mesg);
892 if (_exit_game)
break;
901 void VideoDriver_Win32Base::ClientSizeChanged(
int w,
int h,
bool force)
915 if (_window_maximize) ShowWindow(this->
main_wnd, SW_SHOWNORMAL);
938 static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hDC, LPRECT rc, LPARAM data)
940 auto &list = *
reinterpret_cast<std::vector<int>*
>(data);
942 MONITORINFOEX monitorInfo = {};
943 monitorInfo.cbSize =
sizeof(MONITORINFOEX);
944 GetMonitorInfo(hMonitor, &monitorInfo);
946 DEVMODE devMode = {};
947 devMode.dmSize =
sizeof(DEVMODE);
948 devMode.dmDriverExtra = 0;
949 EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
951 if (devMode.dmDisplayFrequency != 0) list.push_back(devMode.dmDisplayFrequency);
957 std::vector<int> rates = {};
958 EnumDisplayMonitors(
nullptr,
nullptr, MonitorEnumProc,
reinterpret_cast<LPARAM
>(&rates));
964 return {
static_cast<uint
>(GetSystemMetrics(SM_CXSCREEN)),
static_cast<uint
>(GetSystemMetrics(SM_CYSCREEN)) };
969 typedef UINT (WINAPI *PFNGETDPIFORWINDOW)(HWND hwnd);
970 typedef UINT (WINAPI *PFNGETDPIFORSYSTEM)(VOID);
971 typedef HRESULT (WINAPI *PFNGETDPIFORMONITOR)(HMONITOR hMonitor,
int dpiType, UINT *dpiX, UINT *dpiY);
973 static PFNGETDPIFORWINDOW _GetDpiForWindow =
nullptr;
974 static PFNGETDPIFORSYSTEM _GetDpiForSystem =
nullptr;
975 static PFNGETDPIFORMONITOR _GetDpiForMonitor =
nullptr;
977 static bool init_done =
false;
982 _GetDpiForWindow = _user32.GetProcAddress(
"GetDpiForWindow");
983 _GetDpiForSystem = _user32.GetProcAddress(
"GetDpiForSystem");
984 _GetDpiForMonitor = _shcore.GetProcAddress(
"GetDpiForMonitor");
989 if (cur_dpi == 0 && _GetDpiForWindow !=
nullptr && this->
main_wnd !=
nullptr) {
991 cur_dpi = _GetDpiForWindow(this->
main_wnd);
993 if (cur_dpi == 0 && _GetDpiForMonitor !=
nullptr && this->
main_wnd !=
nullptr) {
996 if (SUCCEEDED(_GetDpiForMonitor(MonitorFromWindow(this->
main_wnd, MONITOR_DEFAULTTOPRIMARY), 0 , &dpiX, &dpiY))) {
1000 if (cur_dpi == 0 && _GetDpiForSystem !=
nullptr) {
1002 cur_dpi = _GetDpiForSystem();
1005 return cur_dpi > 0 ? cur_dpi / 96.0f : 1.0f;
1014 assert(_screen.dst_ptr !=
nullptr);
1021 assert(_screen.dst_ptr !=
nullptr);
1022 if (_screen.dst_ptr !=
nullptr) {
1025 _screen.dst_ptr =
nullptr;
1040 this->MakePalette();
1063 w = std::max(w, 64);
1064 h = std::max(h, 64);
1066 if (!force && w == _screen.width && h == _screen.height)
return false;
1068 BITMAPINFO *bi = (BITMAPINFO *)alloca(
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * 256);
1069 memset(bi, 0,
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * 256);
1070 bi->bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
1072 bi->bmiHeader.biWidth = this->
width = w;
1073 bi->bmiHeader.biHeight = -(this->
height = h);
1075 bi->bmiHeader.biPlanes = 1;
1076 bi->bmiHeader.biBitCount = bpp;
1077 bi->bmiHeader.biCompression = BI_RGB;
1082 this->
dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID **)&this->
buffer_bits,
nullptr, 0);
1087 _screen.pitch = (bpp == 8) ?
Align(w, 4) : w;
1100 void VideoDriver_Win32GDI::MakePalette()
1104 LOGPALETTE *pal = (LOGPALETTE*)alloca(
sizeof(LOGPALETTE) + (256 - 1) *
sizeof(PALETTEENTRY));
1106 pal->palVersion = 0x300;
1107 pal->palNumEntries = 256;
1109 for (uint i = 0; i != 256; i++) {
1113 pal->palPalEntry[i].peFlags = 0;
1120 void VideoDriver_Win32GDI::UpdatePalette(HDC dc, uint start, uint count)
1124 for (uint i = 0; i != count; i++) {
1128 rgb[i].rgbReserved = 0;
1131 SetDIBColorTable(dc, start, count, rgb);
1136 HDC hDC = GetWindowDC(hWnd);
1137 HPALETTE hOldPalette = SelectPalette(hDC, this->
gdi_palette, FALSE);
1138 UINT nChanged = RealizePalette(hDC);
1140 SelectPalette(hDC, hOldPalette, TRUE);
1141 ReleaseDC(hWnd, hDC);
1142 if (nChanged != 0) this->
MakeDirty(0, 0, _screen.width, _screen.height);
1152 HDC dc2 = CreateCompatibleDC(dc);
1154 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, this->
dib_sect);
1155 HPALETTE old_palette = SelectPalette(dc, this->
gdi_palette, FALSE);
1179 BitBlt(dc, 0, 0, this->
width, this->
height, dc2, 0, 0, SRCCOPY);
1180 SelectPalette(dc, old_palette, TRUE);
1181 SelectObject(dc2, old_bmp);
1192 int VideoDriver_Win32GDI::RedrawScreenDebug()
1211 #include "../3rdparty/opengl/glext.h"
1212 #include "../3rdparty/opengl/wglext.h"
1215 #ifndef PFD_SUPPORT_COMPOSITION
1216 # define PFD_SUPPORT_COMPOSITION 0x00008000
1219 static PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB =
nullptr;
1220 static PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT =
nullptr;
1221 static bool _hasWGLARBCreateContextProfile =
false;
1226 OGLProc ret =
reinterpret_cast<OGLProc
>(wglGetProcAddress(proc));
1227 if (ret ==
nullptr) {
1229 ret =
reinterpret_cast<OGLProc
>(GetProcAddress(GetModuleHandle(L
"opengl32"), proc));
1240 static const char *SelectPixelFormat(HDC dc,
bool fullscreen)
1242 PIXELFORMATDESCRIPTOR pfd = {
1243 sizeof(PIXELFORMATDESCRIPTOR),
1245 PFD_DRAW_TO_WINDOW |
1246 PFD_SUPPORT_OPENGL |
1251 0, 0, 0, 0, 0, 0, 0, 0,
1259 if (IsWindowsVistaOrGreater()) pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
1262 int format = ChoosePixelFormat(dc, &pfd);
1263 if (format == 0)
return "No suitable pixel format found";
1264 if (!SetPixelFormat(dc, format, &pfd))
return "Can't set pixel format";
1270 static void LoadWGLExtensions()
1277 HWND wnd = CreateWindow(_T(
"STATIC"), _T(
"dummy"), WS_OVERLAPPEDWINDOW, 0, 0, 0, 0,
nullptr,
nullptr, GetModuleHandle(
nullptr),
nullptr);
1278 HDC dc = GetDC(wnd);
1281 if (SelectPixelFormat(dc,
false) ==
nullptr) {
1283 HGLRC rc = wglCreateContext(dc);
1284 if (rc !=
nullptr) {
1285 wglMakeCurrent(dc, rc);
1289 #pragma GCC diagnostic push
1290 #pragma GCC diagnostic ignored "-Wcast-function-type"
1294 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress(
"wglGetExtensionsStringARB");
1295 if (wglGetExtensionsStringARB !=
nullptr) {
1296 const char *wgl_exts = wglGetExtensionsStringARB(dc);
1299 _wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress(
"wglCreateContextAttribsARB");
1303 _wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress(
"wglSwapIntervalEXT");
1308 #pragma GCC diagnostic pop
1310 wglMakeCurrent(
nullptr,
nullptr);
1311 wglDeleteContext(rc);
1319 static FVideoDriver_Win32OpenGL iFVideoDriver_Win32OpenGL;
1321 const char *VideoDriver_Win32OpenGL::Start(
const StringList ¶m)
1327 LoadWGLExtensions();
1330 this->MakeWindow(_fullscreen);
1333 const char *err = this->AllocateContext();
1334 if (err !=
nullptr) {
1340 this->driver_info = GetName();
1341 this->driver_info +=
" (";
1343 this->driver_info +=
")";
1345 this->ClientSizeChanged(this->width, this->height,
true);
1347 if (_screen.dst_ptr ==
nullptr) {
1350 return "Can't get pointer to screen buffer";
1353 this->ReleaseVideoPointer();
1362 void VideoDriver_Win32OpenGL::Stop()
1364 this->DestroyContext();
1368 void VideoDriver_Win32OpenGL::DestroyContext()
1372 wglMakeCurrent(
nullptr,
nullptr);
1373 if (this->gl_rc !=
nullptr) {
1374 wglDeleteContext(this->gl_rc);
1375 this->gl_rc =
nullptr;
1377 if (this->dc !=
nullptr) {
1378 ReleaseDC(this->main_wnd, this->dc);
1383 void VideoDriver_Win32OpenGL::ToggleVsync(
bool vsync)
1385 if (_wglSwapIntervalEXT !=
nullptr) {
1386 _wglSwapIntervalEXT(vsync);
1388 Debug(driver, 0,
"OpenGL: Vsync requested, but not supported by driver");
1392 const char *VideoDriver_Win32OpenGL::AllocateContext()
1394 this->dc = GetDC(this->main_wnd);
1396 const char *err = SelectPixelFormat(this->dc, this->fullscreen);
1397 if (err !=
nullptr)
return err;
1402 if (_wglCreateContextAttribsARB !=
nullptr) {
1405 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
1406 WGL_CONTEXT_MINOR_VERSION_ARB, 5,
1407 WGL_CONTEXT_FLAGS_ARB, _debug_driver_level >= 8 ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
1408 _hasWGLARBCreateContextProfile ? WGL_CONTEXT_PROFILE_MASK_ARB : 0, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
1411 rc = _wglCreateContextAttribsARB(this->dc,
nullptr, attribs);
1413 if (rc ==
nullptr) {
1417 rc = _wglCreateContextAttribsARB(this->dc,
nullptr, attribs);
1421 if (rc ==
nullptr) {
1423 rc = wglCreateContext(this->dc);
1424 if (rc ==
nullptr)
return "Can't create OpenGL context";
1426 if (!wglMakeCurrent(this->dc, rc))
return "Can't active GL context";
1434 bool VideoDriver_Win32OpenGL::ToggleFullscreen(
bool full_screen)
1436 if (_screen.dst_ptr !=
nullptr) this->ReleaseVideoPointer();
1437 this->DestroyContext();
1439 res &= this->AllocateContext() ==
nullptr;
1440 this->ClientSizeChanged(this->width, this->height,
true);
1444 bool VideoDriver_Win32OpenGL::AfterBlitterChange()
1447 this->ClientSizeChanged(this->width, this->height,
true);
1451 void VideoDriver_Win32OpenGL::PopulateSystemSprites()
1456 void VideoDriver_Win32OpenGL::ClearSystemSprites()
1461 bool VideoDriver_Win32OpenGL::AllocateBackingStore(
int w,
int h,
bool force)
1463 if (!force && w == _screen.width && h == _screen.height)
return false;
1465 this->width = w = std::max(w, 64);
1466 this->height = h = std::max(h, 64);
1468 if (this->gl_rc ==
nullptr)
return false;
1470 if (_screen.dst_ptr !=
nullptr) this->ReleaseVideoPointer();
1472 this->dirty_rect = {};
1474 SwapBuffers(this->dc);
1475 _screen.dst_ptr = this->GetVideoPointer();
1480 void *VideoDriver_Win32OpenGL::GetVideoPointer()
1488 void VideoDriver_Win32OpenGL::ReleaseVideoPointer()
1492 this->dirty_rect = {};
1493 _screen.dst_ptr =
nullptr;
1494 this->anim_buffer =
nullptr;
1497 void VideoDriver_Win32OpenGL::Paint()
1516 SwapBuffers(this->dc);