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

Jonathan Dearborn <[email protected]> Sun, 26 Mar 2017 07:58:02 -0400
Newsgroups gmane.comp.lib.sdl
Message-ID <CA+DSiHY_ufkH6_6e4OiiMbYY4RaiEej-Eqm46f0BU7g0znBCnA@mail.gmail.com>
--===============5731740205822010482==
Content-Type: multipart/alternative; boundary=001a1134fc268d9968054ba0f176

--001a1134fc268d9968054ba0f176
Content-Type: text/plain; charset=UTF-8

That's a bit closer.  Here's a little edit to give you an idea of what I
was talking about:

static void readKeyboard(InputState * inputState)
{
   const Uint8 *state = SDL_GetKeyboardState(NULL);

   inputState->fireButtonDown = state[SDL_SCANCODE_SPACE];
   inputState->leftButtonDown = state[SDL_SCANCODE_LEFT];
   inputState->rightButtonDown = state[SDL_SCANCODE_RIGHT];
   inputState->upButtonDown = state[SDL_SCANCODE_UP];
   inputState->downButtonDown = state[SDL_SCANCODE_DOWN];
   inputState->rotateLeftButtonDown = state[SDL_SCANCODE_Z];
   inputState->rotateRightButtonDown = state[SDL_SCANCODE_X];
}

void handleEvents(InputState * inputState)
{
   SDL_Event event;

   while (SDL_PollEvent(&event))
   {
      switch (event.type)
      {
         case SDL_QUIT:
            inputState->quitButtonPressed = SDL_TRUE;
            break;
         case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_ESCAPE)
            {
                gameState->doQuit();  // Do whatever you need to quit the
app
                break;
            }
      }
   }

   readKeyboard(inputState);
}


On Sat, Mar 25, 2017 at 3:07 PM, capehill <[email protected]> wrote:

> Here is something that I use in a puzzle game. First I poll for events
> which triggers the event pump, and then I check the keyboard state. This
> happens at 60 Hz rate, each time game logic is ran.
>
>
>
>
> Code:
>
>
> static void readKeyboard(InputState * inputState)
> {
>    const Uint8 *state = SDL_GetKeyboardState(NULL);
>
>    inputState->fireButtonPressed = state[SDL_SCANCODE_SPACE];
>    inputState->leftButtonPressed = state[SDL_SCANCODE_LEFT];
>    inputState->rightButtonPressed = state[SDL_SCANCODE_RIGHT];
>    inputState->upButtonPressed = state[SDL_SCANCODE_UP];
>    inputState->downButtonPressed = state[SDL_SCANCODE_DOWN];
>    inputState->rotateLeftButtonPressed = state[SDL_SCANCODE_Z];
>    inputState->rotateRightButtonPressed = state[SDL_SCANCODE_X];
>
>    if (state[SDL_SCANCODE_ESCAPE])
>    {
>       inputState->quitButtonPressed = SDL_TRUE;
>    }
> }
>
> void handleEvents(InputState * inputState)
> {
>    SDL_Event event;
>
>    while (SDL_PollEvent(&event))
>    {
>       switch (event.type)
>       {
>          case SDL_QUIT:
>             inputState->quitButtonPressed = SDL_TRUE;
>             break;
>       }
>    }
>
>    readKeyboard(inputState);
> }
>
>
>
> _______________________________________________
> SDL mailing list
> [email protected]
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>

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

<div dir=3D"ltr"><div>That&#39;s a bit closer.=C2=A0 Here&#39;s a little ed=
it to give you an idea of what I was talking about:<div><br></div><div><spa=
n style=3D"font-size:12.8px">static void readKeyboard(InputState * inputSta=
te)</span><br style=3D"font-size:12.8px"><span style=3D"font-size:12.8px">{=
</span><br style=3D"font-size:12.8px"><span style=3D"font-size:12.8px">=C2=
=A0 =C2=A0const Uint8 *state =3D SDL_GetKeyboardState(NULL);</span><br styl=
e=3D"font-size:12.8px"><br style=3D"font-size:12.8px"><span style=3D"font-s=
ize:12.8px">=C2=A0 =C2=A0inputState-&gt;fireButtonDown =3D state[SDL_SCANCO=
DE_SPACE];</span><br style=3D"font-size:12.8px"><span style=3D"font-size:12=
.8px">=C2=A0 =C2=A0inputState-&gt;leftButton</span><span style=3D"font-size=
:12.8px">Down</span><span style=3D"font-size:12.8px">=C2=A0=3D state[SDL_SC=
ANCODE_LEFT];</span><br style=3D"font-size:12.8px"><span style=3D"font-size=
:12.8px">=C2=A0 =C2=A0inputState-&gt;</span><wbr style=3D"font-size:12.8px"=
><span style=3D"font-size:12.8px">rightButton</span><span style=3D"font-siz=
e:12.8px">Down</span><span style=3D"font-size:12.8px">=C2=A0=3D state[SDL_S=
CANCODE_RIGHT];</span><br style=3D"font-size:12.8px"><span style=3D"font-si=
ze:12.8px">=C2=A0 =C2=A0inputState-&gt;upButton</span><span style=3D"font-s=
ize:12.8px">Down</span><span style=3D"font-size:12.8px">=C2=A0=3D state[SDL=
_SCANCODE_UP];</span><br style=3D"font-size:12.8px"><span style=3D"font-siz=
e:12.8px">=C2=A0 =C2=A0inputState-&gt;downButton</span><span style=3D"font-=
size:12.8px">Down</span><span style=3D"font-size:12.8px">=C2=A0=3D state[SD=
L_SCANCODE_DOWN];</span><br style=3D"font-size:12.8px"><span style=3D"font-=
size:12.8px">=C2=A0 =C2=A0inputState-&gt;</span><wbr style=3D"font-size:12.=
8px"><span style=3D"font-size:12.8px">rotateLeftButton</span><span style=3D=
"font-size:12.8px">Down</span><span style=3D"font-size:12.8px">=C2=A0=3D st=
ate[SDL_SCANCODE_Z];</span><br style=3D"font-size:12.8px"><span style=3D"fo=
nt-size:12.8px">=C2=A0 =C2=A0inputState-&gt;</span><wbr style=3D"font-size:=
12.8px"><span style=3D"font-size:12.8px">rotateRightButton</span><span styl=
e=3D"font-size:12.8px">Down</span><span style=3D"font-size:12.8px">=C2=A0=
=3D state[SDL_SCANCODE_X];</span><br style=3D"font-size:12.8px"><span style=
=3D"font-size:12.8px">}</span><br style=3D"font-size:12.8px"><br style=3D"f=
ont-size:12.8px"><span style=3D"font-size:12.8px">void handleEvents(InputSt=
ate * inputState)</span><br style=3D"font-size:12.8px"><span style=3D"font-=
size:12.8px">{</span><br style=3D"font-size:12.8px"><span style=3D"font-siz=
e:12.8px">=C2=A0 =C2=A0SDL_Event event;</span><br style=3D"font-size:12.8px=
"><br style=3D"font-size:12.8px"><span style=3D"font-size:12.8px">=C2=A0 =
=C2=A0while (SDL_PollEvent(&amp;event))</span><br style=3D"font-size:12.8px=
"><span style=3D"font-size:12.8px">=C2=A0 =C2=A0{</span><br style=3D"font-s=
ize:12.8px"><span style=3D"font-size:12.8px">=C2=A0 =C2=A0=C2=A0 =C2=A0swit=
ch (event.type)</span><br style=3D"font-size:12.8px"><span style=3D"font-si=
ze:12.8px">=C2=A0 =C2=A0=C2=A0 =C2=A0{</span><br style=3D"font-size:12.8px"=
><span style=3D"font-size:12.8px">=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0ca=
se SDL_QUIT:</span><br style=3D"font-size:12.8px"><span style=3D"font-size:=
12.8px">=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0inputState-&gt;=
quitButtonPressed =3D SDL_TRUE;</span><br style=3D"font-size:12.8px"><span =
style=3D"font-size:12.8px">=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =
=C2=A0break;</span></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0case SDL_KE=
YDOWN:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if(event.key.key=
sym.sym =3D=3D SDLK_ESCAPE)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
gameState-&gt;doQuit(); =C2=A0// Do whatever you need to quit the app</div>=
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br><span style=3D"font-size:=
12.8px">=C2=A0 =C2=A0=C2=A0 =C2=A0}</span><br style=3D"font-size:12.8px"><s=
pan style=3D"font-size:12.8px">=C2=A0 =C2=A0}</span><br style=3D"font-size:=
12.8px"><br style=3D"font-size:12.8px"><span style=3D"font-size:12.8px">=C2=
=A0 =C2=A0readKeyboard(inputState);</span><br style=3D"font-size:12.8px"><s=
pan style=3D"font-size:12.8px">}</span></div></div><div><span style=3D"font=
-size:12.8px"><br></span></div><div class=3D"gmail_extra"><br><div class=3D=
"gmail_quote">On Sat, Mar 25, 2017 at 3:07 PM, capehill <span dir=3D"ltr">&=
lt;<a href=3D"mailto:[email protected]" target=3D"_blank">juha.niemi=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pad=
ding-left:1ex"><u></u>




	<div>

	<div class=3D"gmail-m_-7375135370771642229m_-3143947921912619915postbody">=
Here is something that I use in a puzzle game. First I poll for events whic=
h triggers the event pump, and then I check the keyboard state. This happen=
s at 60 Hz rate, each time game logic is ran.<br>
<br>
<div align=3D"center"><br><br><table width=3D"90%" align=3D"center" cellspa=
cing=3D"1" cellpadding=3D"2" border=3D"0" class=3D"gmail-m_-737513537077164=
2229m_-3143947921912619915code"><tbody><tr><td><br>
</td></tr><tr>=20
	<td class=3D"gmail-m_-7375135370771642229m_-3143947921912619915code_header=
" align=3D"left">Code:</td><td><br>
</td></tr><tr><td><br>
</td></tr><tr>
	<td class=3D"gmail-m_-7375135370771642229m_-3143947921912619915code" align=
=3D"left"><br>
static void readKeyboard(InputState * inputState)<br>
{<br>
=C2=A0 =C2=A0const Uint8 *state =3D SDL_GetKeyboardState(NULL);<br>
<br>
=C2=A0 =C2=A0inputState-&gt;fireButtonPressed =3D state[SDL_SCANCODE_SPACE]=
;<br>
=C2=A0 =C2=A0inputState-&gt;leftButtonPressed =3D state[SDL_SCANCODE_LEFT];=
<br>
=C2=A0 =C2=A0inputState-&gt;rightButtonPresse<wbr>d =3D state[SDL_SCANCODE_=
RIGHT];<br>
=C2=A0 =C2=A0inputState-&gt;upButtonPressed =3D state[SDL_SCANCODE_UP];<br>
=C2=A0 =C2=A0inputState-&gt;downButtonPressed =3D state[SDL_SCANCODE_DOWN];=
<br>
=C2=A0 =C2=A0inputState-&gt;rotateLeftButtonP<wbr>ressed =3D state[SDL_SCAN=
CODE_Z];<br>
=C2=A0 =C2=A0inputState-&gt;rotateRightButton<wbr>Pressed =3D state[SDL_SCA=
NCODE_X];<br>
<br>
=C2=A0 =C2=A0if (state[SDL_SCANCODE_ESCAPE])<br>
=C2=A0 =C2=A0{<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0inputState-&gt;quitButtonPressed =3D SDL_TRUE;<br=
>
=C2=A0 =C2=A0}<br>
}<br>
<br>
void handleEvents(InputState * inputState)<br>
{<br>
=C2=A0 =C2=A0SDL_Event event;<br>
<br>
=C2=A0 =C2=A0while (SDL_PollEvent(&amp;event))<br>
=C2=A0 =C2=A0{<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0switch (event.type)<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0{<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0case SDL_QUIT:<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0inputState-&gt;quitButt=
onPressed =3D SDL_TRUE;<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0break;<br>
=C2=A0 =C2=A0=C2=A0 =C2=A0}<br>
=C2=A0 =C2=A0}<br>
<br>
=C2=A0 =C2=A0readKeyboard(inputState);<br>
}<br>
</td><td><br>
	</td></tr><tr><td><br>
</td></tr></tbody></table></div></div>
	</div>

<br>______________________________<wbr>_________________<br>
SDL mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected].=
org</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/listin<wbr>fo.cgi/sdl-lib=
sdl.org</a><br>
<br></blockquote></div><br></div></div>

--001a1134fc268d9968054ba0f176--

--===============5731740205822010482==
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

--===============5731740205822010482==--