[SDL] esc detector wont run -- 'out of scope'

Jonathan Dearborn grimfang4 at gmail.com
Sat Mar 18 10:32:55 PDT 2017


As typed, your `while(event.type == SDL_KEYDOWN)` is an infinite loop.

Jonny D


On Sat, Mar 18, 2017 at 1:23 PM, Clangray <clangray at fastmail.com> wrote:

> Ok, I introduced a window
>
> #include"SDL.h"
> using namespace std;
>
> const char* WINDOW_TITLE = "SDL Start";
>
> int main(int argc, char **argv)
> {
> SDL_Init(SDL_INIT_EVERYTHING);
>
> SDL_Window * window = nullptr;
>         window = SDL_CreateWindow("Hello World!",
>                                   SDL_WINDOWPOS_CENTERED,
>                                   SDL_WINDOWPOS_CENTERED,
>                                   640,480,
>                                   SDL_WINDOW_SHOWN);
>
>         SDL_Delay(2000); // Delay so that we can see the
>
> SDL_Event event;
>
> cout<<"flag 0";
>
>    bool gameRunning = true;
>
>    while (gameRunning)
>    {
>    cout<<"flag 1"<<endl;
>       while (SDL_PollEvent(&event))
>       {
>       gameRunning = false;
>       cout<<"flag 2";
>       if (event.type == SDL_QUIT)
>         {
>     cout<<"flag 3";
>     gameRunning = false;
>       }
>
>          while (event.type == SDL_KEYDOWN)
>          {
>         cout<<"flag 2";
>             SDL_Keycode keyPressed = event.key.keysym.sym;
>
>             switch (keyPressed)
>             {
>                case SDLK_ESCAPE:
>                   gameRunning = false;
>                   break;
>             }
>          }
>
>       }
>
>    }
>
>    SDL_Quit();
>
>    cout<<endl<<"end flag";
>    return 0;
>
>
>
> }
>
> I'm getting flag2 constantly.
> Esc and other keys seem to have no effect
>
> **if I substitute if for while (below)
>
> change while back to if
> if (SDL_PollEvent(&event))
>
> output:
>
> flag 0flag 1
> flag 2
> end flag
>
>
> --
>   Gray Family
>   clangray at fastmail.com
>
>
>
> On Sat, Mar 18, 2017, at 12:44 PM, Brian Puthuff wrote:
>
> I agree also. I thought there was some caveat that you needed a window
> open to receive input events.
>
>
> On Mar 18, 2017 9:17 AM, "DLudwig" <dludwig at pobox.com> wrote:
>
>
>
>
>
> capehill wrote:
>
>
> Do you have a window open? Does Control-C work?
>
>
>
>
> I'd like to second the above.
>
> Many platforms need a window open, before input can be received. Using
> either SDL_CreateWindowAndRenderer, or just SDL_CreateWindow, once
> beforehand at app init, may be sufficient to fix this.
>
> -- David L.
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
> *_______________________________________________*
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20170318/8584a77a/attachment-0001.htm>


More information about the SDL mailing list