简体   繁体   English

我无法使用 sdl2 在 opengl 中创建两个上下文

[英]I can't create two contexts in opengl using sdl2

i need a debug window, in which i could better observe the scenario the possible changes, and change in real time, using sdl2 and opengl 3.3 i created the second window, changed the event system to close the window using multiple windows, but glContext is buggy, once I create a second context, and as if the first one seems to exist, thus breaking the rendering of one of the windows, would it be possible to use multiple opengl contexts using sdl2? i need a debug window, in which i could better observe the scenario the possible changes, and change in real time, using sdl2 and opengl 3.3 i created the second window, changed the event system to close the window using multiple windows, but glContext is越野车,一旦我创建了第二个上下文,并且好像第一个似乎存在,从而破坏了 windows 之一的渲染,是否可以使用 sdl2 使用多个 opengl 上下文?

this->window=SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | (resize ? SDL_WINDOW_RESIZABLE : SDL_WINDOW_SHOWN));
SDL_GLContext windowContext=SDL_GL_CreateContext(this->window);
this->debug=SDL_CreateWindow("debug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext debugContext=SDL_GL_CreateContext(this->debug);

Here's an example of two windows with separate contexts:下面是两个具有不同上下文的 windows 的示例:

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <stdio.h>

int main(int argc, char **argv) {
    (void)argc, (void)argv;
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Window *windows[2];
    SDL_GLContext contexts[2];
    for(int i = 0; i != sizeof(windows)/sizeof(windows[0]); ++i) {
        char title[32];
        snprintf(title, sizeof(title), "window%d", i);
        windows[i] = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                640, 480, SDL_WINDOW_OPENGL);
        if(!windows[i]) {
            fprintf(stderr, "SDL_CreateWindow error: %s\n", SDL_GetError());
            return 1;
        }
        contexts[i] = SDL_GL_CreateContext(windows[i]);
        if(!contexts[i]) {
            fprintf(stderr, "SDL_GL_CreateContext error: %s\n", SDL_GetError());
            return 1;
        }
    }

    int running = 1;
    while(running) {
        SDL_Event ev;
        while(SDL_PollEvent(&ev)) {
            if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_q) {
                running = 0;
                break;
            }
        }

        SDL_GL_MakeCurrent(windows[0], contexts[0]);
        glClearColor(1, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(windows[0]);

        SDL_GL_MakeCurrent(windows[1], contexts[1]);
        glClearColor(0, 1, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(windows[1]);
    }

    return 0;
}

But separate context for each window is not a requirement.但是每个 window 的单独上下文不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM