Jiffis28
Jiffis28

Reputation: 65

Why does SDL window immediately close?

This is my C++ file:

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

int main(int argc, char* argv[]) {

   if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
     std::cout << "Something went wrong" << std::endl;
}
  else { 
   SDL_CreateWindow("Neptune's Limit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_OPENGL);
   }

   return 0;
}

When I run it, it flashes up for a half a second and then immediately closes. I have looked at the other posts about this, but the answer to them was about SDL_EVENT. I do not have that anywhere in my program.

What could be wrong?

Upvotes: 4

Views: 3772

Answers (1)

Zem
Zem

Reputation: 474

After create SDL window, use while loop for maintain SDL window. I learn basic SDL functions with this tutorial

SIMPLE EXAMPLE

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

int main(int argc, char* argv[]) {

   if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
     std::cout << "Something went wrong" << std::endl;
   } else { 
     SDL_CreateWindow("Neptune's Limit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_OPENGL);
     while( true ) {
       // SDL RUNNING
       // Poll Event Code Maybe here!
     }
   }

   return 0;
}

Maybe this code doesn't exit because there's no exit-SDL code.

For exit program, add SDL_Event:

SDL POLL EVENT EX)

SDL_Event e;
while(true) { // SDL loop
  while( SDL_PollEvent( &e ) != 0 ) {
    if( e.type == SDL_QUIT ) {
      // Ctrl + C in console !
    }
  } // end of handling event.
}

Hope this helps you.

Upvotes: 5

Related Questions