/*
* src/sysevt.c
*
* Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
*
* The use and distribution terms for this software are contained in the file
* named README, which can be found in the root of this distribution. By
* using this software in any fashion, you are agreeing to be bound by the
* terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
#include <SDL.h>
#include "system.h"
#include "game.h"
#include "control.h"
#include "draw.h"
#define SYSJOY_RANGE 3280
static SDL_Event event;
extern U8 want_filter;
extern U8 want_fullscreen;
extern U8 recreate_screen;
/*
* Process an event
*/
static void processEvent (void)
{
static U8 alt_pressed = FALSE;
U16 key = event.key.keysym.scancode;
switch (event.type)
{
case SDL_KEYDOWN:
if (key == SDL_SCANCODE_LALT)
alt_pressed = TRUE;
else if (alt_pressed && key == SDL_SCANCODE_RETURN)
{
want_fullscreen ^= TRUE;
recreate_screen = TRUE;
}
else if (alt_pressed && key == SDL_SCANCODE_F4)
{
control_status |= CONTROL_EXIT;
control_last = CONTROL_EXIT;
}
else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
{
control_status |= CONTROL_UP;
control_last = CONTROL_UP;
}
else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
{
control_status |= CONTROL_DOWN;
control_last = CONTROL_DOWN;
}
else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
{
control_status |= CONTROL_LEFT;
control_last = CONTROL_LEFT;
}
else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
{
control_status |= CONTROL_RIGHT;
control_last = CONTROL_RIGHT;
}
else if (key == SDL_SCANCODE_PAUSE)
{
control_status |= CONTROL_PAUSE;
control_last = CONTROL_PAUSE;
}
else if (key == SDL_SCANCODE_END)
{
control_status |= CONTROL_END;
control_last = CONTROL_END;
}
else if (key == SDL_SCANCODE_ESCAPE)
{
control_status |= CONTROL_EXIT;
control_last = CONTROL_EXIT;
}
else if (key == SDL_SCANCODE_SPACE)
{
control_status |= CONTROL_FIRE;
control_last = CONTROL_FIRE;
}
else if (key == SDL_SCANCODE_F4)
syssnd_toggleMute();
else if (key == SDL_SCANCODE_F5)
syssnd_vol(-1);
else if (key == SDL_SCANCODE_F6)
syssnd_vol(+1);
else if (key == SDL_SCANCODE_F7)
want_filter ^= TRUE;
else if (key == SDL_SCANCODE_F10)
{
control_status &= ~CONTROL_EXIT;
control_last = CONTROL_EXIT;
}
break;
case SDL_KEYUP:
if (key == SDL_SCANCODE_LALT)
alt_pressed = FALSE;
else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
{
control_status &= ~CONTROL_UP;
control_last = CONTROL_UP;
}
else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
{
control_status &= ~CONTROL_DOWN;
control_last = CONTROL_DOWN;
}
else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
{
control_status &= ~CONTROL_LEFT;
control_last = CONTROL_LEFT;
}
else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
{
control_status &= ~CONTROL_RIGHT;
control_last = CONTROL_RIGHT;
}
else if (key == SDL_SCANCODE_PAUSE)
{
control_status &= ~CONTROL_PAUSE;
control_last = CONTROL_PAUSE;
}
else if (key == SDL_SCANCODE_END)
{
control_status &= ~CONTROL_END;
control_last = CONTROL_END;
}
else if (key == SDL_SCANCODE_ESCAPE)
{
control_status &= ~CONTROL_EXIT;
control_last = CONTROL_EXIT;
}
else if (key == SDL_SCANCODE_SPACE)
{
control_status &= ~CONTROL_FIRE;
control_last = CONTROL_FIRE;
}
break;
case SDL_QUIT:
/* player tries to close the window -- this is the same as pressing ESC */
control_status |= CONTROL_EXIT;
control_last = CONTROL_EXIT;
break;
case SDL_JOYAXISMOTION:
if (event.jaxis.axis == 0)
{
/* left-right */
if (event.jaxis.value < -SYSJOY_RANGE)
{ /* left */
control_status |= CONTROL_LEFT;
control_status &= ~CONTROL_RIGHT;
}
else if (event.jaxis.value > SYSJOY_RANGE)
{ /* right */
control_status |= CONTROL_RIGHT;
control_status &= ~CONTROL_LEFT;
}
else
{ /* center */
control_status &= ~CONTROL_RIGHT;
control_status &= ~CONTROL_LEFT;
}
}
if (event.jaxis.axis == 1)
{
/* up-down */
if (event.jaxis.value < -SYSJOY_RANGE)
{ /* up */
control_status |= CONTROL_UP;
control_status &= ~CONTROL_DOWN;
}
else if (event.jaxis.value > SYSJOY_RANGE)
{ /* down */
control_status |= CONTROL_DOWN;
control_status &= ~CONTROL_UP;
}
else
{ /* center */
control_status &= ~CONTROL_DOWN;
control_status &= ~CONTROL_UP;
}
}
break;
case SDL_JOYBUTTONDOWN:
control_status |= CONTROL_FIRE;
break;
case SDL_JOYBUTTONUP:
control_status &= ~CONTROL_FIRE;
break;
default:
break;
}
}
/*
* Process events, if any, then return
*/
void sysevt_poll (void)
{
while (SDL_PollEvent (&event))
processEvent ();
}
/*
* Wait for an event, then process it and return
*/
void sysevt_wait (void)
{
SDL_WaitEvent (&event);
processEvent ();
}