aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/windows/gl.cpp
blob: 07ef19b1b472a6d887f0d2c85d6b80bbc19ea504 (plain)
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
// 31 march 2019
#include "uipriv_windows.hpp"
#include "area.hpp"

#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/wglext.h>

struct uiGLContext
{
    uiArea* a;

    HWND hwnd;
    HDC dc;
    HGLRC rc;

    unsigned int version;
};


uiGLContext* createGLContext(uiArea* a, int vermajor, int verminor)
{
    uiGLContext* ctx;
    BOOL res;

    ctx = uiNew(uiGLContext);

    ctx->a = a;
    ctx->hwnd = a->hwnd;

    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cAlphaBits = 8;
    pfd.cDepthBits = 24;
    pfd.cStencilBits = 8;
    pfd.iLayerType = PFD_MAIN_PLANE;

    ctx->dc = GetDC(ctx->hwnd);
    if (!ctx->dc)
    {
        uiFree(ctx);
        return NULL;
    }

    int pixelformat = ChoosePixelFormat(ctx->dc, &pfd);
    res = SetPixelFormat(ctx->dc, pixelformat, &pfd);
    if (!res)
    {
        ReleaseDC(ctx->hwnd, ctx->dc);
        uiFree(ctx);
        return NULL;
    }

    ctx->rc = wglCreateContext(ctx->dc);
    if (!ctx->rc)
    {
        ReleaseDC(ctx->hwnd, ctx->dc);
        uiFree(ctx);
        return NULL;
    }

    wglMakeCurrent(ctx->dc, ctx->rc);

    if (vermajor >= 3)
    {
        HGLRC (*wglCreateContextAttribsARB)(HDC,HGLRC,const int*);
        HGLRC rc_better = NULL;

        wglCreateContextAttribsARB = (HGLRC(*)(HDC,HGLRC,const int*))wglGetProcAddress("wglCreateContextAttribsARB");
        if (wglCreateContextAttribsARB)
        {
            int attribs[15];
            int i = 0;

            attribs[i++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
            attribs[i++] = vermajor;
            attribs[i++] = WGL_CONTEXT_MINOR_VERSION_ARB;
            attribs[i++] = verminor;

            attribs[i] = 0;
            rc_better = wglCreateContextAttribsARB(ctx->dc, NULL, attribs);
        }

        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(ctx->rc);

        if (!rc_better)
        {
            ReleaseDC(ctx->hwnd, ctx->dc);
            uiFree(ctx);
            return NULL;
        }

        ctx->version = uiGLVersion(vermajor, verminor);
        ctx->rc = rc_better;
        wglMakeCurrent(ctx->dc, ctx->rc);
    }

    return ctx;
}

void freeGLContext(uiGLContext* ctx)
{
    if (ctx == NULL) return;
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(ctx->rc);
    ReleaseDC(ctx->hwnd, ctx->dc);
    uiFree(ctx);
}

void uiGLMakeContextCurrent(uiGLContext* ctx)
{
    if (ctx == NULL)
    {
        wglMakeCurrent(NULL, NULL);
        return;
    }

    if (wglGetCurrentContext() == ctx->rc) return;
    int res = wglMakeCurrent(ctx->dc, ctx->rc);
}

unsigned int uiGLGetVersion(uiGLContext* ctx)
{
    if (ctx == NULL) return 0;
    return ctx->version;
}

void *uiGLGetProcAddress(const char* proc)
{
    return (void*)wglGetProcAddress(proc);
}

void uiGLBegin(uiGLContext* ctx)
{
}

void uiGLEnd(uiGLContext* ctx)
{
}

void uiGLSwapBuffers(uiGLContext* ctx)
{
    if (ctx == NULL) return;
    SwapBuffers(ctx->dc);
}

int uiGLGetFramebuffer(uiGLContext* ctx)
{
    return 0;
}

float uiGLGetFramebufferScale(uiGLContext* ctx)
{
    // TODO
    return 1;
}

void uiGLSetVSync(int sync)
{
	static PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT = NULL;
	static bool symloaded = false;

	if (!symloaded)
    {
        PFNGLGETSTRINGIPROC _glGetStringi = (PFNGLGETSTRINGIPROC)wglGetProcAddress("glGetStringi");
        if (_glGetStringi == NULL) return;

        GLint numext;
        glGetIntegerv(GL_NUM_EXTENSIONS, &numext);

        bool hasswapctrl = false;
        for (GLint i = 0; i < numext; i++)
        {
            const char* ext = (const char*)_glGetStringi(GL_EXTENSIONS, i);
            if (!stricmp(ext, "WGL_EXT_swap_control"))
            {
                hasswapctrl = true;
                break;
            }
        }

        if (hasswapctrl)
            _wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");

        symloaded = true;
    }

    if (_wglSwapIntervalEXT)
        _wglSwapIntervalEXT(sync);
}