-1

I'm following a tutorial on SDL_net and am quite confused on why my program is crashing. You can find the tutorial here. My client crashes after running it with the server open.

I'm just wondering what this error means (using SDLNet_GetError()):

no opengl context has been made

I'm just working off core SDL with SDL_TTF and have no idea what this means. The tutorial example I followed used OpenGL so if I were to guess the error is to do with me not using it? I just assumed it only required SDL.

If you want a look at the code you can find the client code here, and the server code here. I'm not going to put it all in this question as it's rather long and I'd be fine with just a simple explanation of the error, I don't want to ask for a full explanation to what's wrong with my code.

Okay, so my initialization code is done through a 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;
}

Which is running a screen, this is where the network runs:

#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);
}
genfy
  • 83
  • 3
  • 4
  • 18

1 Answers1

4

You are not creating an OpenGL context anywhere and call SDL_GL_SetSwapInterval(1); which according to the 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 after SDL_CreateWindow.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • I assumed that this would fix the crash I get, I was being naive. Any idea what could be behind the crash? I certainly don't. – genfy Jun 09 '16 at 21:13
  • Well, to be honest mostly I try and help out but at the moment I don't have any time to devote to this. Your code has a lot of `new` calls and pointers and looking at it takes time. I don't wan't to sound impolite, but why can't you use a debugger? At least a stack trace of the point of crash would help. – Rudolfs Bundulis Jun 09 '16 at 21:18
  • @genfy I took a quick look at the client code you posted and well - you need to rework it a lot. Sorry for the criticism but it is terrible - you are using a global `tmp` (assuming global since there is no declaration or definition) which is being printed to and accessed in weird ways like `tmp[strlen(tmp - 1)]` (I am calling this weird because you will actually pass one byte before the start of `tmp` to `strlen`). There are just too many things that could go wrong. – Rudolfs Bundulis Jun 09 '16 at 21:32