Re: esc detector wont run -- 'out of scope'

Jonathan Dearborn <[email protected]> Sat, 18 Mar 2017 13:32:55 -0400
Newsgroups gmane.comp.lib.sdl
Message-ID <CA+DSiHZYnDrgwR_atqf=N1u2B9AXEYXrY-iCfU_fN=ryA5mLvQ@mail.gmail.com>
--===============4077741468554853374==
Content-Type: multipart/alternative; boundary=94eb2c18c5a072c6b3054b04b053

--94eb2c18c5a072c6b3054b04b053
Content-Type: text/plain; charset=UTF-8

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

Jonny D


On Sat, Mar 18, 2017 at 1:23 PM, Clangray <[email protected]> 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
>   [email protected]
>
>
>
> 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" <[email protected]> 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
> [email protected]
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
> *_______________________________________________*
> SDL mailing list
> [email protected]
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>
>
> _______________________________________________
> SDL mailing list
> [email protected]
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>

--94eb2c18c5a072c6b3054b04b053
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">As typed, your `while(event.type =3D=3D SDL_KEYDOWN)` is a=
n infinite loop.<div><br></div><div>Jonny D</div><div><br></div></div><div =
class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Sat, Mar 18, 2017 a=
t 1:23 PM, Clangray <span dir=3D"ltr">&lt;<a href=3D"mailto:clangray@fastma=
il.com" target=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><b=
lockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px =
#ccc solid;padding-left:1ex"><u></u>




<div><div>Ok, I introduced a window<br></div><span class=3D"">
<div><br></div>
<div>#include&quot;SDL.h&quot; <br></div>
<div>using namespace std;<br></div>
<div><br></div>
</span><span class=3D""><div>const char* WINDOW_TITLE =3D &quot;SDL Start&q=
uot;;<br></div>
<div><br></div>
<div>int main(int argc, char **argv)<br></div>
<div>{<br></div>
</span><div>SDL_Init(SDL_INIT_EVERYTHING);<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2=A0=C2=A0 SDL_WINDOW_SHOW=
N);<br></div>
<div><br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SDL_Delay(2000); // Delay s=
o that we can see the<br></div>
<div><br></div>
<div>SDL_Event event;<br></div>
<div><br></div>
<div>cout&lt;&lt;&quot;flag 0&quot;;<br></div><span class=3D"">
<div><br></div>
<div>=C2=A0=C2=A0 bool gameRunning =3D true;<br></div>
<div><br></div>
<div>=C2=A0=C2=A0 while (gameRunning)<br></div>
<div>=C2=A0=C2=A0 {<br></div>
</span><div>=C2=A0=C2=A0 cout&lt;&lt;&quot;flag 1&quot;&lt;&lt;endl;<br></d=
iv>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 while (SDL_PollEvent(&amp;event))<br></=
div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 {<br></div>
<div>=C2=A0=C2=A0=C2=A0 	=C2=A0 gameRunning =3D false;<br></div><span class=
=3D"">
<div>=C2=A0=C2=A0=C2=A0 	=C2=A0 cout&lt;&lt;&quot;flag 2&quot;;<br></div>
<div>=C2=A0=C2=A0=C2=A0 	=C2=A0 if (event.type =3D=3D SDL_QUIT)<br></div>
<div>=C2=A0=C2=A0=C2=A0 	=C2=A0 	=C2=A0 {<br></div>
<div>=C2=A0=C2=A0=C2=A0 		 cout&lt;&lt;&quot;flag 3&quot;;<br></div>
<div>=C2=A0=C2=A0=C2=A0 		 gameRunning =3D false;<br></div>
<div>=C2=A0=C2=A0=C2=A0 	=C2=A0 }<br></div>
<div><br></div>
</span><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 while (event.t=
ype =3D=3D SDL_KEYDOWN)<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 {<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 	 cout&lt;&lt;&quot;flag 2&=
quot;;<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SDL=
_Keycode keyPressed =3D event.key.keysym.sym;<br></div><span class=3D"">
<div><br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 swi=
tch (keyPressed)<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 {<b=
r></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 case SDLK_ESCAPE:<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 gameRunning =3D false;<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 break;<br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<b=
r></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br></div>
<div><br></div>
<div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br></div>
<div><br></div>
<div>=C2=A0=C2=A0 }<br></div>
<div><br></div>
</span><div>=C2=A0=C2=A0 SDL_Quit();<br></div>
<div><br></div>
<div>=C2=A0=C2=A0 cout&lt;&lt;endl&lt;&lt;&quot;end flag&quot;;<br></div>
<div>=C2=A0=C2=A0 return 0;<br></div>
<div><br></div>
<div><br></div>
<div><br></div>
<div>}<br></div>
<div><br></div>
<div>I&#39;m getting flag2 constantly.<br></div>
<div>Esc and other keys seem to have no effect</div>
<div><br></div>
<div>**if I substitute if for while (below)<br></div>
<div><br></div>
<div>change while back to if</div>
<div>if (SDL_PollEvent(&amp;event))<br></div>
<div><br></div>
<div>output:<br></div>
<div><br></div>
<div>flag 0flag 1 <br></div>
<div>flag 2<br></div>
<div>end flag<br></div>
<div><br></div>
<div><br></div>
<div id=3D"m_112665417914199088sig43414442"><div class=3D"m_112665417914199=
088signature">--<br></div>
<div class=3D"m_112665417914199088signature">=C2=A0 Gray Family<br></div>
<div class=3D"m_112665417914199088signature">=C2=A0 <a href=3D"mailto:clang=
[email protected]" target=3D"_blank">[email protected]</a><br></div>
<div class=3D"m_112665417914199088signature"><br></div>
</div><div><div class=3D"h5">
<div><br></div>
<div><br></div>
<div>On Sat, Mar 18, 2017, at 12:44 PM, Brian Puthuff wrote:<br></div>
<blockquote type=3D"cite"><div><div>I agree also. I thought there was some =
caveat that you needed a window open to receive input events.<br></div>
<div><div><br></div>
<div><div><br></div>
<div><div>On Mar 18, 2017 9:17 AM, &quot;DLudwig&quot; &lt;<a href=3D"mailt=
o:[email protected]" target=3D"_blank">[email protected]</a>&gt; wrote:<br>=
</div>
<blockquote style=3D"margin-top:0px;margin-right:0px;margin-bottom:0px;marg=
in-left:0.8ex;border-left-width:1px;border-left-style:solid;border-left-col=
or:rgb(204,204,204);padding-left:1ex"><div><u></u><br></div>
<div><div><div align=3D"center"><div><br></div>
<div><br></div>
<table width=3D"90%" align=3D"center" cellspacing=3D"1" cellpadding=3D"2" b=
order=3D"0"><tbody><tr><td><br></td></tr><tr><td align=3D"left">capehill wr=
ote:<br></td><td><br></td></tr><tr><td><br></td></tr><tr><td align=3D"left"=
>Do you have a window open? Does Control-C work?<br></td><td><br></td></tr>=
<tr><td><br></td></tr></tbody></table></div>
<div><br></div>
<div><br></div>
<div>I&#39;d like to second the above.<br></div>
<div> <br></div>
<div> Many platforms need a window open, before input can be received.  Usi=
ng either SDL_CreateWindowAndRenderer, or just SDL_CreateWindow, once befor=
ehand at app init, may be sufficient to fix this.<br></div>
<div> <br></div>
<div> -- David L.<br></div>
</div>
</div>
<div><br></div>
<div>______________________________<wbr>_________________<br></div>
<div> SDL mailing list<br></div>
<div> <a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]=
ibsdl.org</a><br></div>
<div> <a href=3D"http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org" targe=
t=3D"_blank">http://lists.libsdl.org/listin<wbr>fo.cgi/sdl-libsdl.org</a><b=
r></div>
<div> <br></div>
</blockquote></div>
</div>
</div>
</div>
<div><u>______________________________<wbr>_________________</u><br></div>
<div>SDL mailing list<br></div>
<div><a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]=
bsdl.org</a><br></div>
<div><a href=3D"http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org" target=
=3D"_blank">http://lists.libsdl.org/<wbr>listinfo.cgi/sdl-libsdl.org</a><br=
></div>
</blockquote><div><br></div>
</div></div></div>

<br>______________________________<wbr>_________________<br>
SDL mailing list<br>
<a href=3D"mailto:[email protected]">[email protected]</a><br>
<a href=3D"http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org" rel=3D"nore=
ferrer" target=3D"_blank">http://lists.libsdl.org/<wbr>listinfo.cgi/sdl-lib=
sdl.org</a><br>
<br></blockquote></div><br></div>

--94eb2c18c5a072c6b3054b04b053--

--===============4077741468554853374==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
SDL mailing list
[email protected]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

--===============4077741468554853374==--