3

I have the following C code (for clarity, I know it's not complete code and it should handle events and stuff like that):

#!/usr/bin/tcc -run -L/usr/lib/x86_64-linux-gnu -D_REENTRANT -DSDL_MAIN_HANDLED -I/usr/include/SDL2 -lSDL2 -lGL -lGLEW

#include <GL/glew.h>
#include <SDL.h>

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    SDL_Window *win = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    SDL_GLContext glContext = SDL_GL_CreateContext(win);

    while (1)
    {
        glClearColor(1, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(win);
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(win);

    return 0;
}

If I compile this with GCC (without the shebang line), and start the executable, it works fine, but I like the simplicity of the compile and run functionality of TCC and its speed.

However, the compiled executable gives me the following console output and then hangs forever and can't even be killed with CTRL-C:

libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast

I added the SDL_MAIN_HANDLED as suggested here but that didn't change anything.

Anyone?

genpfault
  • 51,148
  • 11
  • 85
  • 139
scippie
  • 2,011
  • 1
  • 26
  • 42
  • Where's your `glewInit()` call? – genpfault Feb 05 '18 at 14:32
  • Yeah, forgot it, but it's not necessary as long as you don't use extension-based opengl functionality. Adding it doesn't help. – scippie Feb 05 '18 at 15:02
  • Did you compiled SDL on your own? If so, try using the same compiler for SDL and your app. – Ripi2 Feb 05 '18 at 17:05
  • @Ripi2: Ah, no, I think I just installed libSDL2-dev. Good point! Will try it out! – scippie Feb 05 '18 at 17:37
  • @Ripi2: I've been trying to figure this out (it's not my strongest side), but this is taking too much time for what it's worth. So I'm giving up on trying to compile SDL2 with TCC. – scippie Feb 06 '18 at 09:17

1 Answers1

1

How about proper handling of the possible errors of the SDL calls and also initializing glew?

#include <stdlib.h>
#include <SDL.h>
#include <GL/glew.h>


void exit_sdl_error(const char *message) {
    SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "%s failed with SDL error %s", message, SDL_GetError());
    exit(EXIT_FAILURE);
}

int main() {
    const Uint32 init_flags = SDL_INIT_VIDEO;
    const int gl_major = 4, gl_minor = 0;
    if (!SDL_WasInit(init_flags)) {
        if (SDL_Init(init_flags) != 0)
            exit_sdl_error("Unable to initialize SDL");
        if (SDL_InitSubSystem(init_flags) != 0)
            exit_sdl_error("Unable to initialize sub system of SDL");
    }

    if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, gl_major))
        exit_sdl_error("Unable to set gl major version");
    if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_minor))
        exit_sdl_error("Unable to set gl minor version");
    if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE))
        exit_sdl_error("Unable to set gl profile");


    SDL_Window *window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if (!window) {
        SDL_QuitSubSystem(init_flags);
        exit_sdl_error("Unable to create window");
    }

    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    if (!glContext) {
        SDL_DestroyWindow(window);
        SDL_QuitSubSystem(init_flags);
        exit_sdl_error("Unable to create context");
    }

    glewInit();
    while (1)
    {
        glClearColor(1, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_QuitSubSystem(init_flags);
}
Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • 1
    You're right, but because the code did work with GCC, I thought my minimal code should work with TCC. I have done error handling on my 'real' project. Anyway, the error I now get after the two libGL errors: CRITICAL: Unable to create window failed with SDL error Couldn't find matching GLX visual – scippie Feb 05 '18 at 14:58
  • By the way, if I search for that error, I find lots of info, mostly about bad opengl drivers or something like that, but my system (two different systems by the way) is perfectly fine and the above code does work with GCC. – scippie Feb 05 '18 at 15:07
  • @scippie That's what I expected: An actual error of SDL. Since the windows creation fails, I guess either your drivers are bad or you can't define a window like that with that plattform/compiler. I would try `SDL_WINDOWPOS_CENTERED` instead of `SDL_WINDOWPOS_UNDEFINED`. Otherwise this is a XY-Problem: http://mywiki.wooledge.org/XyProblem – Superlokkus Feb 05 '18 at 15:19
  • That doesn't help either. I don't think this has become an XY-problem. I could replace the code in my OP to your code and ask the same question: why does it work in GCC and doesn't it work with TCC while there are obviously no problems with my drivers as not only my software as other software works and I get the same problem on different computers. – scippie Feb 05 '18 at 15:22
  • 1
    Maybe you're right, could be a genuine SDL issue. But error from SDL suggests that it also could be a problem of your TCC toolchain (different opengl lib or something), your window creation, system/installation and much more. Hard to tell and reproduce, so you get an upvote from me. – Superlokkus Feb 05 '18 at 15:30