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;
}
2
Answers
Try SDL_WINDOW_SHOWN ~
I just tested using your code and sample
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.