|
@@ -1,7 +1,5 @@
|
|
|
#include "glplatform.h"
|
|
|
|
|
|
-#define GLPLATFORM_ENABLE_WGL_ARB_create_context
|
|
|
-#define GLPLATFORM_ENABLE_WGL_ARB_create_context_profile
|
|
|
#include "glplatform-wgl.h"
|
|
|
|
|
|
#include <wingdi.h>
|
|
@@ -17,11 +15,13 @@
|
|
|
#undef wglCreateContext
|
|
|
|
|
|
#include <windowsx.h>
|
|
|
+#include "glplatform_priv.h"
|
|
|
|
|
|
static LRESULT CALLBACK PlatformWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
|
|
|
|
|
|
static struct glplatform_win *g_win_list = NULL;
|
|
|
static int g_glplatform_win_count = 0;
|
|
|
+static DWORD g_context_tls;
|
|
|
|
|
|
static struct glplatform_win *find_glplatform_win(HWND hwnd)
|
|
|
{
|
|
@@ -60,14 +60,36 @@ static void register_glplatform_win(struct glplatform_win *win)
|
|
|
g_win_list = win;
|
|
|
}
|
|
|
|
|
|
-//TODO
|
|
|
-//bool glplatform_is_button_pressed(struct glplatform_win *win, int button)
|
|
|
+#define PRESSED_MASK ((SHORT)0x8000)
|
|
|
|
|
|
-//TODO
|
|
|
-//bool glplatform_is_shift_pressed(struct glplatform_win *win)
|
|
|
+bool glplatform_is_button_pressed(struct glplatform_win *win, int button)
|
|
|
+{
|
|
|
+ switch (button) {
|
|
|
+ case 0:
|
|
|
+ return GetKeyState(VK_LBUTTON) & PRESSED_MASK;
|
|
|
+ case 1:
|
|
|
+ return GetKeyState(VK_MBUTTON) & PRESSED_MASK;
|
|
|
+ case 2:
|
|
|
+ return GetKeyState(VK_RBUTTON) & PRESSED_MASK;
|
|
|
+ default:
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-//TODO
|
|
|
-//bool glplatform_is_control_pressed(struct glplatform_win *win)
|
|
|
+bool glplatform_is_shift_pressed(struct glplatform_win *win)
|
|
|
+{
|
|
|
+ return GetKeyState(VK_SHIFT) & PRESSED_MASK;
|
|
|
+}
|
|
|
+
|
|
|
+bool glplatform_is_control_pressed(struct glplatform_win *win)
|
|
|
+{
|
|
|
+ return GetKeyState(VK_CONTROL) & PRESSED_MASK;
|
|
|
+}
|
|
|
+
|
|
|
+struct glplatform_context *glplatform_get_context_priv()
|
|
|
+{
|
|
|
+ return (struct glplatform_context *)TlsGetValue(g_context_tls);
|
|
|
+}
|
|
|
|
|
|
bool glplatform_init()
|
|
|
{
|
|
@@ -82,17 +104,23 @@ bool glplatform_init()
|
|
|
wc.hIcon = NULL;
|
|
|
wc.hIconSm = NULL;
|
|
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
|
|
+ wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
|
|
- if (RegisterClassEx(&wc) == 0) {
|
|
|
+ if (RegisterClassEx(&wc) == 0)
|
|
|
return false;
|
|
|
- }
|
|
|
+
|
|
|
+ g_context_tls = TlsAlloc();
|
|
|
+
|
|
|
+ if (g_context_tls == TLS_OUT_OF_INDEXES)
|
|
|
+ return false;
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void glplatform_shutdown()
|
|
|
{
|
|
|
+ TlsFree(g_context_tls);
|
|
|
}
|
|
|
|
|
|
static LRESULT CALLBACK windows_event(struct glplatform_win *win, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
|
|
@@ -124,6 +152,16 @@ static LRESULT CALLBACK windows_event(struct glplatform_win *win, HWND hWnd, UIN
|
|
|
GetClientRect(hWnd, &cr);
|
|
|
|
|
|
switch (Msg) {
|
|
|
+ case WM_SETCURSOR: {
|
|
|
+ WORD ht = LOWORD(lParam);
|
|
|
+ ShowCursor(TRUE);
|
|
|
+ if (ht == HTCLIENT) {
|
|
|
+ SetCursor(NULL);
|
|
|
+ return TRUE;
|
|
|
+ } else {
|
|
|
+ return DefWindowProc(hWnd, Msg, wParam, lParam);
|
|
|
+ }
|
|
|
+ } break;
|
|
|
case WM_PAINT: {
|
|
|
if (win->callbacks.on_expose)
|
|
|
win->callbacks.on_expose(win);
|
|
@@ -155,7 +193,6 @@ static LRESULT CALLBACK windows_event(struct glplatform_win *win, HWND hWnd, UIN
|
|
|
pfd.cStencilBits = win->fbformat.stencil_bits;
|
|
|
pfd.cAccumBits = win->fbformat.accum_bits;
|
|
|
pfd.cDepthBits = win->fbformat.depth_bits;
|
|
|
-
|
|
|
win->pixel_format = ChoosePixelFormat(win->hdc, &pfd);
|
|
|
if (win->pixel_format == 0) {
|
|
|
return -1;
|
|
@@ -253,7 +290,7 @@ struct glplatform_win *glplatform_create_window(const char *title,
|
|
|
struct glplatform_win *win = (struct glplatform_win *) malloc(sizeof(struct glplatform_win));
|
|
|
win->fbformat = *fbformat;
|
|
|
win->callbacks = *callbacks;
|
|
|
-
|
|
|
+ win->fullscreen = false;
|
|
|
RECT wr = { 0, 0, width, height };
|
|
|
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
|
|
|
|
|
@@ -273,6 +310,7 @@ struct glplatform_win *glplatform_create_window(const char *title,
|
|
|
free(win);
|
|
|
return NULL;
|
|
|
}
|
|
|
+ win->hwnd = hwnd;
|
|
|
register_glplatform_win(win);
|
|
|
if (win->callbacks.on_create)
|
|
|
win->callbacks.on_create(win);
|
|
@@ -286,9 +324,16 @@ struct glplatform_win *glplatform_create_window(const char *title,
|
|
|
//TODO
|
|
|
//void glplatform_set_win_type(struct glplatform_win *win, enum glplatform_win_types type)
|
|
|
|
|
|
-void glplatform_make_current(struct glplatform_win *win, glplatform_gl_context_t context)
|
|
|
+
|
|
|
+void glplatform_make_current(struct glplatform_win *win, glplatform_gl_context_t ctx)
|
|
|
{
|
|
|
- wglMakeCurrent(win->hdc, (HGLRC)context);
|
|
|
+ struct glplatform_context *context = (struct glplatform_context*)ctx;
|
|
|
+
|
|
|
+ TlsSetValue(g_context_tls, context);
|
|
|
+ if (context)
|
|
|
+ wglMakeCurrent(win->hdc, context->rc);
|
|
|
+ else
|
|
|
+ wglMakeCurrent(win->hdc, 0);
|
|
|
}
|
|
|
|
|
|
glplatform_gl_context_t glplatform_create_context(struct glplatform_win *win, int maj_ver, int min_ver)
|
|
@@ -306,11 +351,37 @@ glplatform_gl_context_t glplatform_create_context(struct glplatform_win *win, in
|
|
|
};
|
|
|
|
|
|
HGLRC rc = wglCreateContextAttribsARB(win->hdc, 0, attribList);
|
|
|
- return (glplatform_gl_context_t)rc;
|
|
|
+
|
|
|
+ if (!rc)
|
|
|
+ return 0;
|
|
|
+ struct glplatform_context *context = calloc(1, sizeof(struct glplatform_context));
|
|
|
+ if (context)
|
|
|
+ context->rc = rc;
|
|
|
+ return (glplatform_gl_context_t)context;
|
|
|
}
|
|
|
|
|
|
-//TODO:
|
|
|
-//void glplatform_fullscreen_win(struct glplatform_win *win, bool fullscreen)
|
|
|
+void glplatform_fullscreen_win(struct glplatform_win *win, bool fullscreen)
|
|
|
+{
|
|
|
+ DWORD dwStyle = GetWindowLong(win->hwnd, GWL_STYLE);
|
|
|
+ if (fullscreen && !win->fullscreen) {
|
|
|
+ MONITORINFO mi = { sizeof(mi) };
|
|
|
+ GetWindowPlacement(win->hwnd, &win->prev_placement);
|
|
|
+ GetMonitorInfo(MonitorFromWindow(win->hwnd, MONITOR_DEFAULTTOPRIMARY), &mi);
|
|
|
+ SetWindowLong(win->hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
|
|
|
+ SetWindowPos(win->hwnd, HWND_TOP,
|
|
|
+ mi.rcMonitor.left, mi.rcMonitor.top,
|
|
|
+ mi.rcMonitor.right - mi.rcMonitor.left,
|
|
|
+ mi.rcMonitor.bottom - mi.rcMonitor.top,
|
|
|
+ SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
|
|
|
+ } else if (!fullscreen && win->fullscreen) {
|
|
|
+ SetWindowLong(win->hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
|
|
|
+ SetWindowPlacement(win->hwnd, &win->prev_placement);
|
|
|
+ SetWindowPos(win->hwnd, NULL, 0, 0, 0, 0,
|
|
|
+ SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
|
|
|
+ SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
|
|
|
+ }
|
|
|
+ win->fullscreen = fullscreen;
|
|
|
+}
|
|
|
|
|
|
int glplatform_get_events(bool block)
|
|
|
{
|