Arrow Keys, and Diagonals

Hello,

I have a quick question, I’m scrolling a sprite across the screen using
the arrow keys. What I want to do is have the sprite scroll at a
diagonal when the LEFT and UP or RIGHT and UP arrow keys are pressed at
the same time. I can’t seem to find any documentation about this.
Thanks for any help,

Max

I tried adding the keys like this:

if ( key_event.key.keysym.sym == SDLK_LEFT + SDLK_UP)
spritePosition_x -= 4, spritePosition_y -= 4;

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: text/enriched
Size: 601 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030821/772283ee/attachment.bin

Quoth Max Countryman , on Thu 21 Aug 2003:

I tried adding the keys like this:

if ( key_event.key.keysym.sym == SDLK_LEFT + SDLK_UP)
spritePosition_x -= 4, spritePosition_y -= 4;

Firstly, keysyms are opaque. They cannot be added together
arithmetically in any meaningful way.

Secondly, checking for keys being down, per se, is done with
SDL_GetKeyState, which returns a pointer to an array of Uint8, indexed
by keysym, indicating whether the given key is pressed (1) or not
pressed (0). It must be updated by use of SDL_PumpEvents (possibly
implicit).

Thirdly, if I understand your intent correctly, it would be much
better accomplished by checking for each key individually, with no
out-jumping between the checks; this would cause a combination of left
and up to go left, and up, in the same tick, which seems to be what
you desire.

Fourthly, it would not be wise IMO to use the key repeat mechanism for
this, as it appears from your snippet that you might be doing. Your
particular case would seem to require knowledge of the current state
of the keys more directly than it does knowledge of when they change
state (even though the two are theoretically equivalent). It would
thus seem to me to be a good idea to simply ignore the events
themselves (though they must still be pumped and discarded) and use
the SDL_GetKeyState array.

I apologize if this message appears overly harsh; I am simply
attempting to be as objective as possible.

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030821/f1be66e9/attachment.pgp

There’s no such keysym as the quantity “SDLK_LEFT + SDLK_UP”.
At the very least, if there WERE such a thing, it’d be OR’d together
(SDLK_LEFT | SDLK_UP).

Anyway, what you want to do is simply keep track of whether
UP, DOWN, LEFT and RIGHT are pushed or not.

What I do is:

if (event.type == SDL_KEYPRESS || event.type == SDL_KEYRELEASE)
{
key = event.key.keysym.sym;

if (key == SDLK_UP)
  up_flag = event.type;
else if (key == SDLK_DOWN)
  down_flag = event.type;
else if (key == SDLK_LEFT)
  left_flag = event.type;
else if (key == SDLK_RIGHT)
  right_flag = event.type;

}

(hopefully I’ve got all of the variable names right :wink: )

Then:

if (up_flag == SDL_KEYPRESS)
y = y - 1;
else if (down_flag == SDL_KEYPRESS)
y = y + 1;

if (left_flag == SDL_KEYPRESS)
x = x - 1;
else if (right_flag == SDL_KEYPRESS)
x = x + 1;

Or some-such.

Good luck!

-bill!On Thu, Aug 21, 2003 at 08:00:26PM -0700, Max Countryman wrote:

Hello,

I have a quick question, I’m scrolling a sprite across the screen using
the arrow keys. What I want to do is have the sprite scroll at a
diagonal when the LEFT and UP or RIGHT and UP arrow keys are pressed at
the same time. I can’t seem to find any documentation about this.
Thanks for any help,

Max

I tried adding the keys like this:

if ( key_event.key.keysym.sym == SDLK_LEFT + SDLK_UP)
spritePosition_x -= 4, spritePosition_y -= 4;


bill at newbreedsoftware.com Was I useful? Rate this message!
http://newbreedsoftware.com/bill http://svcs.affero.net/rm.php?r=billkendrick

Drake, some code snippets would greatly help me to better understand
what you mean, thanks,

MaxOn Thursday, August 21, 2003, at 04:31 PM, Drake Wilson wrote:

Quoth Max Countryman <@Max_Countryman>, on Thu 21 Aug 2003:

I tried adding the keys like this:

if ( key_event.key.keysym.sym == SDLK_LEFT + SDLK_UP)
spritePosition_x -= 4, spritePosition_y -= 4;

Firstly, keysyms are opaque. They cannot be added together
arithmetically in any meaningful way.

Secondly, checking for keys being down, per se, is done with
SDL_GetKeyState, which returns a pointer to an array of Uint8, indexed
by keysym, indicating whether the given key is pressed (1) or not
pressed (0). It must be updated by use of SDL_PumpEvents (possibly
implicit).

Thirdly, if I understand your intent correctly, it would be much
better accomplished by checking for each key individually, with no
out-jumping between the checks; this would cause a combination of left
and up to go left, and up, in the same tick, which seems to be what
you desire.

Fourthly, it would not be wise IMO to use the key repeat mechanism for
this, as it appears from your snippet that you might be doing. Your
particular case would seem to require knowledge of the current state
of the keys more directly than it does knowledge of when they change
state (even though the two are theoretically equivalent). It would
thus seem to me to be a good idea to simply ignore the events
themselves (though they must still be pumped and discarded) and use
the SDL_GetKeyState array.

I apologize if this message appears overly harsh; I am simply
attempting to be as objective as possible.

—> Drake Wilson

Quoth Max Countryman , on Fri 22 Aug 2003:

Drake, some code snippets would greatly help me to better understand
what you mean, thanks,

The following '’-delimited C++ source compiles into a small
application demonstrating what my suggestion(s); note that it is not
by any means optimal in all respects.

#include “SDL.h”

int main(int argc, char *argv[])
{
Uint8 *keystate;
bool quit = false;
SDL_Rect rect;
SDL_Surface *screen;
Uint32 color;

SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(400, 400, 16, SDL_SWSURFACE);
keystate = SDL_GetKeyState(NULL);
rect.x = rect.y = 200;
rect.w = rect.h = 2;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
color = SDL_MapRGB(screen->format, 255, 255, 255);

while (!quit) {
SDL_Event ev, pushed;

while (SDL_PollEvent(&ev)) {
  switch (ev.type) {
  case SDL_KEYDOWN:
switch (ev.key.keysym.sym) {
case SDLK_q:
  pushed.type = SDL_QUIT;
  SDL_PushEvent(&pushed);
  break;
}
break;
  case SDL_QUIT:
quit = true;
break;
  }
}
if (quit)
  break;

if (keystate[SDLK_UP])
  rect.y -= 2;
if (keystate[SDLK_DOWN])
  rect.y += 2;
if (keystate[SDLK_LEFT])
  rect.x -= 2;
if (keystate[SDLK_RIGHT])
  rect.x += 2;
SDL_FillRect(screen, &rect, color);
SDL_UpdateRect(screen, rect.x, rect.y, rect.w, rect.h);
SDL_Delay(1);

}

return 0;
}


   ---> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030821/70511361/attachment.pgp>

Thanks guys! I’ve got it to work thanks to your help. ;)On Thursday, August 21, 2003, at 10:48 PM, Drake Wilson wrote:

Quoth Max Countryman <@Max_Countryman>, on Fri 22 Aug 2003:

Drake, some code snippets would greatly help me to better understand
what you mean, thanks,

The following ‘~~~~’-delimited C++ source compiles into a small
application demonstrating what my suggestion(s); note that it is not
by any means optimal in all respects.

#include "SDL.h"

int main(int argc, char *argv[])
{
  Uint8 *keystate;
  bool quit = false;
  SDL_Rect rect;
  SDL_Surface *screen;
  Uint32 color;

  SDL_Init(SDL_INIT_VIDEO);
  screen = SDL_SetVideoMode(400, 400, 16, SDL_SWSURFACE);
  keystate = SDL_GetKeyState(NULL);
  rect.x = rect.y = 200;
  rect.w = rect.h = 2;
  SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  color = SDL_MapRGB(screen->format, 255, 255, 255);

  while (!quit) {
    SDL_Event ev, pushed;

    while (SDL_PollEvent(&ev)) {
      switch (ev.type) {
      case SDL_KEYDOWN:
	switch (ev.key.keysym.sym) {
	case SDLK_q:
	  pushed.type = SDL_QUIT;
	  SDL_PushEvent(&pushed);
	  break;
	}
	break;
      case SDL_QUIT:
	quit = true;
	break;
      }
    }
    if (quit)
      break;

    if (keystate[SDLK_UP])
      rect.y -= 2;
    if (keystate[SDLK_DOWN])
      rect.y += 2;
    if (keystate[SDLK_LEFT])
      rect.x -= 2;
    if (keystate[SDLK_RIGHT])
      rect.x += 2;
    SDL_FillRect(screen, &rect, color);
    SDL_UpdateRect(screen, rect.x, rect.y, rect.w, rect.h);
    SDL_Delay(1);
  }

  return 0;
}

—> Drake Wilson