qtcontextmanager.cpp 2.22 KB
Newer Older
1
#include "qtcontextmanager.h"
schultezub's avatar
schultezub committed
2

3
4
5
6
7
#include "tgt/assert.h"

namespace tgt {

    QtContextManager::QtContextManager()
8
        : GlContextManager()
9
10
11
12
13
14
15
    {
    }

    QtContextManager::~QtContextManager()
    {
    }

16
    GLCanvas* QtContextManager::createContext(const std::string& key, const std::string& title /*= ""*/, const ivec2& size /*= ivec2(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)*/, const GLCanvas::Buffers buffers /*= RGBADD*/, bool shared /*= true*/)
17
    {
18
19
        // FIXME: rethink this concept of unique IDs
        //tgtAssert(_contexts.find(key) == _contexts.end(), "A context with the same key already exists!");
20

21
        tbb::mutex::scoped_lock localLock(_localMutex);
22
        tbb::mutex::scoped_lock lock(_glMutex);
23
        QtThreadedCanvas* toReturn = new QtThreadedCanvas(title, size, buffers, 0, shared);
24
25
26
        _contexts.insert(std::make_pair(key, toReturn));

        toReturn->makeCurrent();
27
        _currentContext = toReturn;
28
        // Init GLEW for this context
29
30
31
32
33
34
35
        GLenum err = glewInit();
        if (err != GLEW_OK) {
            // Problem: glewInit failed, something is seriously wrong.
            tgtAssert(false, "glewInit failed");
            std::cerr << "glewInit failed, error: " << glewGetErrorString(err) << std::endl;
            exit(EXIT_FAILURE);
        }
36
        releaseCurrentContext();
37
38
39
40

        return toReturn;
    }

41
    void QtContextManager::setCurrent(GLCanvas* context) {
42
43
44
        if (_currentContext != context) {
            if (context == 0) {
                // explicitely release OpenGL context
45
                static_cast<QtThreadedCanvas*>(_currentContext)->doneCurrent();
46
47
48
                _currentContext = 0;
            }
            else {
49
                static_cast<QtThreadedCanvas*>(context)->makeCurrent();
50
51
52
53
54
55
                LGL_ERROR;
                _currentContext = context;
            }
        }
    }

56
57
58
    void QtContextManager::init() {
        tgtAssert( !singletonClass_, "singletonClass_ has already been initialized." );
        singletonClass_ = new QtContextManager();
59
60
    }

61
62
63
64
    void QtContextManager::deinit() {
        tgtAssert( singletonClass_ != 0, "singletonClass_ has already been deinitialized." );
        delete singletonClass_;
        singletonClass_ = 0;
65
66
67
    }


68
}