Skip to content

Commit 981b554

Browse files
committed
use wglCreateContextAttribsARB
1 parent ee58b08 commit 981b554

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

rlawt_windows.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,44 @@ void rlawtThrow(JNIEnv *env, const char *msg) {
4545
}
4646
}
4747

48+
static HGLRC createContext(HDC hdc) {
49+
HGLRC base = wglCreateContext(hdc);
50+
if (!base) {
51+
return NULL;
52+
}
53+
54+
if (!wglMakeCurrent(hdc, base)) {
55+
wglDeleteContext(base);
56+
return NULL;
57+
}
58+
59+
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");
60+
if (wglCreateContextAttribsARB) {
61+
// The default values for WGL_CONTEXT_MAJOR_VERSION_ARB and WGL_CONTEXT_MINOR_VERSION_ARB are 1 and 0 respectively. In this case,
62+
// implementations will typically return the most recent version of OpenGL they support which is backwards compatible with OpenGL 1.0.
63+
const int attribs[] = {
64+
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
65+
0
66+
};
67+
HGLRC gl = wglCreateContextAttribsARB(hdc, 0, attribs);
68+
if (!gl) {
69+
wglDeleteContext(base);
70+
return NULL;
71+
}
72+
73+
if (!wglMakeCurrent(hdc, gl)) {
74+
wglDeleteContext(base);
75+
wglDeleteContext(gl);
76+
return NULL;
77+
}
78+
79+
wglDeleteContext(base);
80+
return gl;
81+
} else {
82+
return base;
83+
}
84+
}
85+
4886
static bool makeCurrent(JNIEnv *env, HDC dc, HGLRC context) {
4987
if (!wglMakeCurrent(dc, context)) {
5088
rlawtThrow(env, "unable to make current");
@@ -107,16 +145,12 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_createGLContext(JNIEnv
107145
goto unlock;
108146
}
109147

110-
ctx->context = wglCreateContext(ctx->dspi->hdc);
148+
ctx->context = createContext(ctx->dspi->hdc);
111149
if (!ctx->context) {
112150
rlawtThrow(env, "unable to create context");
113151
goto unlock;
114152
}
115153

116-
if (!makeCurrent(env, ctx->dspi->hdc, ctx->context)) {
117-
goto freeContext;
118-
}
119-
120154
PFNWGLGETEXTENSIONSSTRINGEXTPROC wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC) wglGetProcAddress("wglGetExtensionsStringEXT");
121155
if (wglGetExtensionsStringEXT) {
122156
const char *extensions = wglGetExtensionsStringEXT();
@@ -132,8 +166,6 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_createGLContext(JNIEnv
132166
ctx->contextCreated = true;
133167
return;
134168

135-
freeContext:
136-
wglDeleteContext(ctx->context);
137169
unlock:
138170
jthrowable exception = (*env)->ExceptionOccurred(env);
139171
ctx->ds->Unlock(ctx->ds);

0 commit comments

Comments
 (0)