The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Compiling SDL 2 to use gl* instead of wgl* under cygwin
Winestone


Joined: 23 Oct 2014
Posts: 1
I would like to compile SDL 2 using cygwin to use gl* rather than wgl* things so that it can create a gl* context. (hopefully I'm making sense here)

This is because I'm using GLEW as well and the cygwin port version of GLEW uses gl* while the mingw SDL 2 build (on the website) and the version I have compiled with make has appeared to create a wgl* context.

The code is compiled with
Code:
-std=c++11
and it is linked with
Code:
-lSDL2 -lSDL2main -lGLEW -lGLU -lGL  -lSDL2 -lSDL2main -lGLEW -lGLU -lGL
(twice to ensure that everything links).

Code:
#include <cstdio>

#include <chrono>
#include <thread>

//#include <windows.h>
//#include <windef.h>

#include <SDL2/SDL.h>

//#define GL_GLEXT_PROTOTYPES
//#define GLEW_STATIC
#include <GL/glew.h>

#include <SDL2/SDL_opengl.h>

#include <GL/glu.h>

const int width = 1000;
const int height = 500;
bool Running = true;

#undef main //Required to compile, otherwise  In function 'main': [Linker error] undefined reference to 'WinMain@16'
int main (int argc, char *argv[]) {
    FILE* cdebug = fopen("cdebug.txt", "w");
   
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(cdebug, "SDL could not initialize! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
    }
   
    #define setAttr(attr, value) \
        if (SDL_GL_SetAttribute(attr, value) < 0) { \
            fprintf(cdebug, "SDL failed to set %s to %s, SDL Error: %s\n", #attr, #value, SDL_GetError()); fflush(cdebug);\
        }
    setAttr(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    setAttr(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    setAttr(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
   
    setAttr(SDL_GL_RED_SIZE, 8);
    setAttr(SDL_GL_GREEN_SIZE, 8);
    setAttr(SDL_GL_BLUE_SIZE, 8);
    setAttr(SDL_GL_DEPTH_SIZE, 24);
    setAttr(SDL_GL_DOUBLEBUFFER, 1);
    #undef setAttr
    /*
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
   
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    */
    SDL_Window *window = SDL_CreateWindow(
            "test",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            640, 480,
            SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
    );
    if (window == NULL) {
        fprintf(cdebug, "Window could not be created! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
    }
   
    SDL_GLContext GLContext = SDL_GL_CreateContext(window);
    if (GLContext == NULL) {
        fprintf(cdebug, "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
    }
    if (SDL_GL_MakeCurrent(window, GLContext) < 0) {
        fprintf(cdebug, "OpenGL context could not be made current! SDL Error: %s\n", SDL_GetError()); fflush(cdebug);
    }
   
    fprintf(cdebug, "OpenGL Vendor: %s\n", glGetString(GL_VENDOR));
    fprintf(cdebug, "OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
    fprintf(cdebug, "OpenGL Shading Language Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
    fprintf(cdebug, "OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
    fflush(cdebug);
   
    glewExperimental = GL_TRUE;
    {
        GLenum glewError = glewInit();
        if (glewError != GLEW_OK) {
            fprintf(cdebug, "Error initializing GLEW! %s\n", glewGetErrorString(glewError)); fflush(cdebug);
        }
    }
   
    SDL_Event event;
    while (Running) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_KEYUP: {
                    switch (event.key.keysym.scancode) {
                        case SDL_SCANCODE_ESCAPE:
                            Running = false;
                            break;
                    }
                    break;
                }
            }
        }
       
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
       
        SDL_GL_SwapWindow(window);
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }
   
    SDL_GL_DeleteContext(GLContext);
   
    SDL_DestroyWindow(window);
    window = nullptr;
   
    SDL_Quit();
   
    printf("All done.\n");
    fflush(stdout);
   
    return 0;
}


With either the mingw SDL2.dll or my SDL2.dll in the .exe's directory, the output in cdebug.txt is:

Code:
OpenGL Vendor: (null)
OpenGL Renderer: (null)
OpenGL Shading Language Version: (null)
OpenGL Extensions: (null)
Error initializing GLEW! Missing GL version


No other files that should affect the .exe (no other .dll's), only source files.