SDL_net 没有创建 opengl 上下文

SDL_net no opengl context has been made

我正在学习 SDL_net 上的教程,但我对我的程序崩溃的原因感到很困惑。您可以找到教程 here. 我的客户端在服务器打开后 运行 崩溃。

我只是想知道这个错误是什么意思(使用 SDLNet_GetError()):

no opengl context has been made

我只是在使用 SDL_TTF 处理核心 SDL,不知道这意味着什么。我遵循的教程示例使用了 OpenGL,所以如果我猜错误是因为我没有使用它?我只是假设它只需要 SDL。

如果您想查看代码,可以找到 client code here, and the server code here。我不打算把它全部放在这个问题中,因为它相当长,我只需要对错误进行简单的解释就可以了,我不想要求对我的代码有什么问题的完整解释。

好的,所以我的初始化代码是通过 ScreenManager 完成的:

#include "ScreenManager.h"

ScreenManager &ScreenManager::GetInstance(){
    static ScreenManager instance;
    return instance;
}

void ScreenManager::init(){
    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();
    SDLNet_Init();

    window = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_BORDERLESS);
    renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);

    running = true;

    ResourceManager::GetInstance().loadTexture(renderer, "res/Human.bmp", "player");
    ResourceManager::GetInstance().loadTexture(renderer, "res/space.bmp", "background");
    ResourceManager::GetInstance().loadTexture(renderer, "res/ship.bmp", "ship");
    ResourceManager::GetInstance().loadTexture(renderer, "res/interior.bmp", "interior");

    SDL_GL_SetSwapInterval(1);

    GameManager::GetInstance().init();

    currentScreen = new MainScreen();
    currentScreen->init();
}

void ScreenManager::close(){
    ResourceManager::GetInstance().close();
    currentScreen->close();

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    SDL_Quit();
    SDLNet_Quit();
    TTF_Quit();
}

void ScreenManager::update(){
    currentScreen->update();
}

void ScreenManager::render(){
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    SDL_RenderClear(renderer);

    currentScreen->render(renderer);

    SDL_RenderPresent(renderer);
}

void ScreenManager::setScreen(GameScreen *screenToSet){
    currentScreen->close();
    currentScreen = screenToSet;
    currentScreen->init();
}

void ScreenManager::handleEvents(){
    SDL_Event e;
    while(SDL_PollEvent(&e) > 0){
        switch(e.type){
            case SDL_QUIT:
                running = false;
            break;
        }
    }
}

bool ScreenManager::isRunning(){
    return running;
}

这是运行一个屏幕,这是网络运行的地方:

#include "MainScreen.h"

void MainScreen::init(){
    Network = new network();
    Network->init();

    player = new Player((1920 / 2) - 32, (1080 / 2) - 32);
    background = new Background();
    entity_vector.push_back(new Ship(30, 30, 699*4, 444*4, 0, "ship", "interior"));
}

void MainScreen::close(){
    Network->close();
    for(unsigned i = 0; i < entity_vector.size(); i++){
        entity_vector[i]->close();
    }
    player->close();
}

void MainScreen::update(){
    for(unsigned i = 0; i < entity_vector.size(); i++){
        entity_vector[i]->update();
    }
    player->update((1920 / 2) - 32, (1080 / 2) - 32);

    GameManager::GetInstance().update();
    Network->recv(enemies, player);
    Network->send(player);
}

void MainScreen::render(SDL_Renderer *renderer){
    for(unsigned i = 0; i < entity_vector.size(); i++){
        entity_vector[i]->render(renderer);
    }
    for(unsigned i = 0; i < enemies.size(); i++){
        enemies[i]->render(renderer);
    }
    background->render(renderer);
    player->render(renderer);
}

您没有在任何地方创建 OpenGL 上下文并根据 SDL_CreateWindow 之后的 source sets the error to No OpenGL context has been made current. And then in your void network::send(Player *p) you call SDLNet_GetError() for no apparent reason and get that error. I assume you should call SDL_GL_CreateContext 调用 SDL_GL_SetSwapInterval(1);