Question

Why the window that created by SDL Game Library are automatically closed after specific time.
I know the reason is SDL_Delay() function, but if not used that function, the game will appear runtime error.

How can I create a window that continuously work without appear in specific period time ?

My code(Simplest code):

SDL_Window *window;
SDL_Renderer *render;

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

   if(SDL_Init(SDL_INIT_EVERYTHING) >= 0){
      window = SDL_CreateWindow("Simple game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
      if(window != 0){
         render = SDL_CreateRenderer(window, -1, 0);
      }

   }else{
      return 1;
   }

   SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
   SDL_RenderClear(render);
   SDL_RenderPresent(render);

   SDL_Delay(3000);     
   SDL_Quit();

   return 0
}
Was it helpful?

Solution

You need to loop forever and call SDL update screen functions. Read LazyFoo tutorials found here: http://lazyfoo.net/SDL_tutorials

Or here a short code to get you started:

#include <iostream>
#include "SDL/SDL.h"                    // basic SDL
#include <string>

const int       SCREEN_WIDTH    = 640;
const int       SCREEN_HEIGHT   = 480;
const int       SCREEN_BBP      = 32;   // bits per-pixel

SDL_Surface*    screen          = NULL; // display screen 
SDL_Event       event;                  // grab events


using namespace std;

bool init() {

    // initialize SDL
    if(SDL_Init( SDL_INIT_EVERYTHING ) == -1)
        return false;

    //the screen image
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 
                              SCREEN_BBP, SDL_SWSURFACE );
    if(!screen) {
        cout << "error creating screen" << endl;
        return false;
    }
    //Set the window caption 
    SDL_WM_SetCaption("Event Test", NULL );

    return true;
}

int main(int argc, char* argv[]) 
{   
    try
    {
        // make sure the program waits for a quit
        bool quit = false;

        cout << "Starting SDL..." << endl; 

        // Start SDL 
        if(!init()) {
            cout << "initialize error" << endl;
            return false;
        }

        // main loop
        while( quit == false )  
        {
            if (SDL_PollEvent(&event))  
            {                       
                // The x button click
                if(event.type == SDL_QUIT)  
                {
                    //quit the program
                    quit = true;
                }

            }

            // Fill the screen white 
            SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( 
                screen->format, 0xFF, 0xFF, 0xFF ) );


            //Update screen
            if(SDL_Flip(screen) == -1)
                return -1;
        }
    }
    catch (exception& e)
    {
        cerr << "exception caught: " << e.what() << endl;
        return -1;
    }

    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top