aboutsummaryrefslogtreecommitdiff
path: root/src/mgl.c
blob: 549121fb9843e4372053485621b176d190068b31 (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
#include "../include/mgl/mgl.h"
#include <X11/Xutil.h>
#include <stdio.h>
#ifndef NDEBUG
#include <stdlib.h>
#endif

static mgl_context context;
static int init_count = 0;
static XErrorHandler prev_xerror = NULL;

static int ignore_xerror(Display *display, XErrorEvent *ee) {
    (void)display;
    (void)ee;
    return 0;
}

static int glx_context_init() {
    const int attr[] = {
        GLX_RGBA,
        GLX_BUFFER_SIZE, 24,
        GLX_DEPTH_SIZE, 0,
        GLX_STENCIL_SIZE, 0,
        GLX_RED_SIZE, 8,
        GLX_GREEN_SIZE, 8,
        GLX_BLUE_SIZE, 8,
        GLX_ALPHA_SIZE, 0,
        GLX_ACCUM_RED_SIZE, 0,
        GLX_ACCUM_GREEN_SIZE, 0,
        GLX_ACCUM_BLUE_SIZE, 0,
        GLX_ACCUM_ALPHA_SIZE, 0,
        GLX_DOUBLEBUFFER, /* TODO: Add option to turn this off? */
        None
    };

    context.visual_info = context.gl.glXChooseVisual(context.connection, DefaultScreen(context.connection), (int*)attr);
    if(!context.visual_info) {
        fprintf(stderr, "glXChooseVisual failed, no appropriate visual found\n");
        return -1;
    }

    context.glx_context = context.gl.glXCreateContext(context.connection, context.visual_info, NULL, 1);
    if(!context.glx_context) {
        fprintf(stderr, "glXCreateContext failed\n");
        return -1;
    }

    return 0;
}

static void glx_context_deinit() {
    if(context.glx_context) {
        context.gl.glXDestroyContext(context.connection, context.glx_context);
        context.glx_context = NULL;
    }

    if(context.visual_info) {
        XFree(context.visual_info);
        context.visual_info = NULL;
    }
}

int mgl_init(void) {
    ++init_count;
    if(init_count == 1) {
        context.connection = XOpenDisplay(NULL);
        if(!context.connection) {
            fprintf(stderr, "XOpenDisplay(NULL) failed\n");
            mgl_deinit();
            return -1;
        }

        prev_xerror = XSetErrorHandler(ignore_xerror);

        if(mgl_gl_load(&context.gl) != 0) {
            mgl_deinit();
            return -1;
        }

        if(glx_context_init() != 0) {
            mgl_deinit();
            return -1;
        }
    }
    return 0;
}

void mgl_deinit(void) {
    if(init_count == 1) {
        glx_context_deinit();
        mgl_gl_unload(&context.gl);
        XSetErrorHandler(prev_xerror);
        if(context.connection) {
            XCloseDisplay(context.connection);
            context.connection = NULL;
        }
    }

    if(init_count > 0)
        --init_count;
}

mgl_context* mgl_get_context(void) {
#ifndef NDEBUG
    if(init_count == 0) {
        fprintf(stderr, "Error: mgl_get_context was called before mgl_init\n");
        abort();
    }
#endif
    return &context;
}