summaryrefslogtreecommitdiff
path: root/src/unix/sdl/glimp.c
blob: 5350b5131e36740df85d1d6a9fff0de22991349a (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
/*
Copyright (C) 2003-2012 Andrey Nazarov

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "video.h"

void VID_SDL_SurfaceChanged(void)
{
    int accel;

    SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &accel);
    if (accel) {
        sdl.flags |= QVF_ACCELERATED;
    }

#if USE_X11
    VID_GLX_SurfaceChanged();
#endif
}

qboolean VID_Init(void)
{
    cvar_t *gl_driver;
    cvar_t *gl_colorbits;
    cvar_t *gl_depthbits;
    cvar_t *gl_stencilbits;
    cvar_t *gl_multisamples;
    int colorbits;
    int depthbits;
    int stencilbits;
    int multisamples;

    if (!VID_SDL_Init()) {
        return qfalse;
    }

#if USE_FIXED_LIBGL
    gl_driver = Cvar_Get("gl_driver", LIBGL, CVAR_ROM);
    (void)gl_driver;
#else
    gl_driver = Cvar_Get("gl_driver", LIBGL, CVAR_REFRESH);

    // don't allow absolute or relative paths
    FS_SanitizeFilenameVariable(gl_driver);

    while (1) {
        char *s;

        // ugly hack to work around brain-dead servers that actively
        // check and enforce `gl_driver' cvar to `opengl32', unaware
        // of other systems than Windows
        s = gl_driver->string;
        if (!Q_stricmp(s, "opengl32") || !Q_stricmp(s, "opengl32.dll")) {
            Com_Printf("...attempting to load %s instead of %s\n",
                       gl_driver->default_string, s);
            s = gl_driver->default_string;
        }

        if (SDL_GL_LoadLibrary(s) == 0) {
            break;
        }

        Com_EPrintf("Couldn't load OpenGL library: %s\n", SDL_GetError());
        if (!strcmp(s, gl_driver->default_string)) {
            goto fail;
        }

        // attempt to recover
        Com_Printf("...falling back to %s\n", gl_driver->default_string);
        Cvar_Reset(gl_driver);
    }
#endif

    gl_colorbits = Cvar_Get("gl_colorbits", "0", CVAR_REFRESH);
    gl_depthbits = Cvar_Get("gl_depthbits", "0", CVAR_REFRESH);
    gl_stencilbits = Cvar_Get("gl_stencilbits", "8", CVAR_REFRESH);
    gl_multisamples = Cvar_Get("gl_multisamples", "0", CVAR_REFRESH);

    colorbits = Cvar_ClampInteger(gl_colorbits, 0, 32);
    depthbits = Cvar_ClampInteger(gl_depthbits, 0, 32);
    stencilbits = Cvar_ClampInteger(gl_stencilbits, 0, 8);
    multisamples = Cvar_ClampInteger(gl_multisamples, 0, 32);

    if (colorbits == 0)
        colorbits = 24;

    if (depthbits == 0)
        depthbits = colorbits > 16 ? 24 : 16;

    if (depthbits < 24)
        stencilbits = 0;

    if (colorbits > 16) {
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    } else {
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    }

    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depthbits);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencilbits);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    if (multisamples > 1) {
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisamples);
    }

    if (!VID_SDL_SetMode(SDL_OPENGL | SDL_RESIZABLE, 0)) {
        Com_EPrintf("Couldn't set video mode: %s\n", SDL_GetError());
        goto fail;
    }

    return qtrue;

fail:
    VID_SDL_Shutdown();
    return qfalse;
}

#if !USE_X11

void VID_Shutdown(void)
{
    VID_SDL_Shutdown();
}

void VID_VideoWait(void)
{
}

qboolean VID_VideoSync(void)
{
    return qtrue;
}

void VID_BeginFrame(void)
{
}

void VID_EndFrame(void)
{
    SDL_GL_SwapBuffers();
}

#endif  // !USE_X11

#if !USE_FIXED_LIBGL
void *VID_GetCoreAddr(const char *sym)
{
    void    *entry = SDL_GL_GetProcAddress(sym);

    if (!entry)
        Com_EPrintf("Couldn't get OpenGL entry point: %s\n", sym);

    return entry;
}
#endif

void *VID_GetProcAddr(const char *sym)
{
    void    *entry = SDL_GL_GetProcAddress(sym);

    if (!entry)
        Com_EPrintf("Couldn't get OpenGL entry point: %s\n", sym);

    return entry;
}