Question

The SDL window that I have created with an OpenGL context has problems:

  1. It's transparent, you can see the console behind it. As you can see from the code below, I just wrote something to display red to the screen.

  2. It doesn't respond to anything. As soon as I hover my mouse over the screen, the loading cursor appears, and clicking does nothing.


#define main SDL_main
#include "stdafx.h"

int main(int argc, char **argv)
{
    bool quit = false;

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        SDL_Quit( );
        return 1;
    }

    SDL_Window* window = NULL;
    window = SDL_CreateWindow("SandVox", 100, 100, 600, 400, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    if (!window) {
        std::cout << "Unable to create window" << std::endl;
        SDL_DestroyWindow(window);
        return 0;
    }

    SDL_GLContext glcontext = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, glcontext);
    glewInit( );

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    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_ALPHA_SIZE, 8);
    SDL_GL_SetSwapInterval(1);

    SDL_Renderer* render = NULL;
    render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_Event *mainEvent = NULL;
    mainEvent = new SDL_Event( );

    while (!quit && mainEvent->type != SDL_QUIT) ;
    {
        SDL_PollEvent(mainEvent);
        /*
           SDL_RenderClear(render);
           SDL_RenderPresent(render);
         */
        glClearColor(1.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
    }


    SDL_GL_DeleteContext(glcontext);
    SDL_DestroyRenderer(render);
    SDL_DestroyWindow(window);
    delete mainEvent;
    SDL_Quit( );

    return 0;
}

stdafx.h doesn't have anything very exciting, just this:

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <GL/glew.h>
#include <GL/GL.h>
#include <GL/glxew.h>
#include <GL/wglew.h>
#include <GL/GLU.h>

EDIT: To preempt a question people might have, yes, I have OpenGL 4.0 installed on my computer, I've checked :)

Was it helpful?

Solution

Setting the pixel format properties after you create your context is pretty pointless. The pixel format of the default framebuffer is immutable once the context is created.

Move all your calls to SDL_GL_SetAttribute (...) so that they come before SDL_GL_CreateContext(window).

The swap interval can be set at any time, but everything else in this block of code needs to be moved:

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
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_ALPHA_SIZE, 8);
SDL_GL_SetSwapInterval(1);

Right now, you are probably getting a single-buffered pixel format. That does not jive well with modern window managers, which only update individual windows when you swap back/front buffers. Buffer swapping causes an implicit flush (whether or not it swaps buffers), so this problem is most likely due to a compositing window manager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top