Home > Net >  SDL Window will not show even though I have a working event loop on Linux/Ubuntu
SDL Window will not show even though I have a working event loop on Linux/Ubuntu

Time:10-16

I really don't know what I'm doing wrong here. When I look up my issue, almost all the answers are solved by adding an event loop. I have an event loop already and I know its working because I used cout in my input function and it just filled up the terminal. I almost feel like the issue is related to the Ubuntu I'm running, maybe because I'm running it on a Gen 1 Surface Book. That being said, I updated my Mesa to 20.3.0 and my computer is compatible with OpenGL 4.6 so maybe that's not it.

Anyways this is the code I have. The program compiles with no issues, and it runs. The only issue is that the window does not show up on the screen. PS, I've also already tried calling SDL_SetMainReady(); right before the SDL_Init()

#include <SDL2/SDL.h>
#include <iostream>

// Globals
int gScreenWidth = 640;
int gScreenHeight = 480;
SDL_Window* gWindow = nullptr;
SDL_GLContext gContext = nullptr;

bool gQuit = false; // if true we quit

void InitializeProgram(){
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        std::cout << "SDL2 could not initialize video subsystem" << std::endl;
        exit(1);
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    gWindow = SDL_CreateWindow("OpenGL First Program", 0, 0, gScreenWidth, gScreenHeight, SDL_WINDOW_OPENGL);
    if(gWindow == nullptr){
        std::cout << "SDL Window could not be created" << std::endl;
        exit(1);
    }

    gContext = SDL_GL_CreateContext(gWindow);
    if(gContext == nullptr){
        std::cout << "OpenGL Context could not be created" << std::endl;
        exit(1);
    }
}

void Input(){
    SDL_Event e;
    while(SDL_PollEvent(&e)){
        if(e.type == SDL_QUIT){
            std::cout << "Goodbye!" << std::endl;
            gQuit = true;
        }
    }
}

void PreDraw(){

}

void Draw(){

}

void MainLoop(){
    while(!gQuit){
        Input();
        PreDraw();
        Draw();
        SDL_GL_SwapWindow(gWindow); // Updates the screen
    }
}

void CleanUp(){
    SDL_DestroyWindow(gWindow);
    SDL_Quit();
}

int main(){
    InitializeProgram();

    MainLoop();

    CleanUp();

    return 0;
}

CodePudding user response:

Try SDL_WINDOW_SHOWN ~

 SDL_Window *window = SDL_CreateWindow("Basic C   SDL project",
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SCREEN_WIDTH, SCREEN_HEIGHT,
                                          SDL_WINDOW_SHOWN);

CodePudding user response:

I just tested using your code and sample

  • Window pop ups
  • Used a triangle sample from source and combined all into main

On my machine major & minor versions failed even though they re correct - failed to create context so removed.

SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL made the window visible.

#include <SDL.h>
#include <SDL_opengl.h>
#include <iostream>

// Define MAX and MIN macros
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))

// Define screen dimensions
#define SCREEN_WIDTH    800
#define SCREEN_HEIGHT   600


// Globals
int gScreenWidth = 640;
int gScreenHeight = 480;
SDL_Window* gWindow = nullptr;
SDL_GLContext gContext = nullptr;

bool gQuit = false; // if true we quit


int main(){
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        std::cout << "SDL2 could not initialize video subsystem" << std::endl;
        exit(1);
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    gWindow = SDL_CreateWindow("OpenGL First Program", 0, 0, 
            gScreenWidth, gScreenHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

    gContext = SDL_GL_CreateContext(gWindow);

    glMatrixMode(GL_PROJECTION|GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(-320, 320, 240, -240, 0, 1);

    if(gWindow == nullptr){
        std::cout << "SDL Window could not be created" << std::endl;
        exit(1);
    }

    if(gContext == nullptr){
        std::cout << "OpenGL Context could not be created" << std::endl;
        exit(1);
    }

    SDL_Event e;
    float x = 0.0, y = 30.0;

    while (!gQuit)
    {
        SDL_PollEvent(&e);

        if (e.type == SDL_QUIT)
            gQuit = true;

        glClearColor(0,0,0,1);          // Draw with OpenGL.
        glClear(GL_COLOR_BUFFER_BIT);   
        glRotatef(10.0,0.0,0.0,1.0);     
        // Note that the glBegin() ... glEnd() OpenGL style used below is actually 
        // obsolete, but it will do for example purposes. For more information, see
        // SDL_GL_GetProcAddress() or find an OpenGL extension loading library.
        glBegin(GL_TRIANGLES);          
          glColor3f(1.0,0.0,0.0); glVertex2f(x, y 90.0);
          glColor3f(0.0,1.0,0.0); glVertex2f(x 90.0, y-90.0);
          glColor3f(0.0,0.0,1.0); glVertex2f(x-90.0, y-90.0);
        glEnd();

        SDL_GL_SwapWindow(gWindow);
        SDL_Delay(10);
    }

    SDL_GL_DeleteContext(gContext);
    SDL_DestroyWindow(gWindow);

    SDL_Quit();

    return 0;
}

  • Related