Question

Hi guys Ive been at this issue for a few days now and cant find the answer. After a successful build of SDL2 my projects hang and dont respond. They dont accept input and hang indefinitely unless I close them using the command prompt window. Even the 'X' on the application window doesnt respond. As far as I can tell this issue seems to be related to the window itself as the program can draw to the renderer. Please help.

I am using Windows 7, MinGW32, Eclipse Europa and SDL2

See below for example of the problem....

Internal Builder is used for build

gcc -O0 -g3 -Wall -c -fmessage-length=0 -osrc\CTestProject.o ..\src\CTestProject.c

gcc -oCTestProject.exe src\CTestProject.o -lmingw32 -lSDL2main -lSDL2

Build complete for project CTestProject

Time consumed: 562 ms.

SDL2 Not Responding

Here is the example program

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

int main(int argc, char* args[]) {
    
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Event event;
    SDL_Window* sdlWindow = SDL_CreateWindow("test",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,300,300,SDL_WINDOW_OPENGL);
    SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    
    while(event.type != SDL_QUIT){
        SDL_RenderClear(sdlRenderer);
        SDL_SetRenderDrawColor(sdlRenderer, 150, 0, 0, 255);
        SDL_RenderPresent(sdlRenderer);         
    }
    
    SDL_DestroyRenderer(sdlRenderer);
    SDL_DestroyWindow(sdlWindow);
    SDL_Quit(); 
    
    return EXIT_SUCCESS;
}
Was it helpful?

Solution

   while(event.type != SDL_QUIT)
   {
        SDL_RenderClear(sdlRenderer);
        SDL_SetRenderDrawColor(sdlRenderer, 150, 0, 0, 255);
        SDL_RenderPresent(sdlRenderer);         
    }

You are comparing a variable that doesn't change in your while loop. You need to update it in every iteration of loop using SDL_PollEvent(&event) Something like this:

bool quit = false;
SDL_Event event;

// Loop while user hasn't quit
while ( !quit )
{
    //  Check all new event to see if a SDL_QUIT event has come in...
    while (SDL_PollEvent(&event) )
    {
        // SDL_QUIT event has come in, quit. 
        if ( event.type == SDL_QUIT )
        {
            quit = true;
        }
    }

    SDL_RenderClear(sdlRenderer);
    SDL_SetRenderDrawColor(sdlRenderer, 150, 0, 0, 255);
    SDL_RenderPresent(sdlRenderer);     
}

Also I don't think you need SDL2_main anymore. At least I don't use it in my code. Nor do you need #include <stdio.h> and #include <stdlib.h> in your specific example.


Tutorials

SDL2 is fairly new, so there aren't that much tutorials around. The only ones I know is TwinklebearDev.But in most cases SDL1.3 and SDL2 are quite similar. So in most cases you can use SDL1.3 code with SDL_Texture, SDL_Renderer and SDL_Window.You can have a look here for more information on porting from 1.3 to 2.0. For SDL1.3 I have used LazyFoo's tutorials.

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