dxx-rebirth/arch/sdl/mouse.c

197 lines
3.9 KiB
C
Raw Normal View History

2006-03-20 17:12:09 +00:00
/*
*
* SDL mouse driver
2006-03-20 17:12:09 +00:00
*
*/
#ifdef HAVE_CONFIG_H
#include <conf.h>
#endif
#include <string.h>
#include <SDL/SDL.h>
2006-03-20 17:12:09 +00:00
#include "fix.h"
#include "timer.h"
#include "event.h"
#include "mouse.h"
struct mousebutton {
ubyte pressed;
fix time_went_down;
fix time_held_down;
uint num_downs;
uint num_ups;
};
static struct mouseinfo {
struct mousebutton buttons[MOUSE_MAX_BUTTONS];
int delta_x, delta_y, delta_z;
int x,y,z;
} Mouse;
void d_mouse_init(void)
{
memset(&Mouse,0,sizeof(Mouse));
}
void mouse_button_handler(SDL_MouseButtonEvent *mbe)
{
// to bad, SDL buttons use a different mapping as descent expects,
// this is at least true and tested for the first three buttons
int button_remap[17] = {
2007-06-11 15:54:09 +00:00
MBTN_LEFT,
MBTN_MIDDLE,
MBTN_RIGHT,
MBTN_Z_UP,
MBTN_Z_DOWN,
MBTN_PITCH_BACKWARD,
MBTN_PITCH_FORWARD,
MBTN_BANK_LEFT,
MBTN_BANK_RIGHT,
MBTN_HEAD_LEFT,
MBTN_HEAD_RIGHT,
MBTN_11,
MBTN_12,
MBTN_13,
MBTN_14,
MBTN_15,
MBTN_16
2006-03-20 17:12:09 +00:00
};
int button = button_remap[mbe->button - 1]; // -1 since SDL seems to start counting at 1
if (mbe->state == SDL_PRESSED) {
Mouse.buttons[button].pressed = 1;
Mouse.buttons[button].time_went_down = timer_get_fixed_seconds();
Mouse.buttons[button].num_downs++;
2007-06-11 15:54:09 +00:00
if (button == MBTN_Z_UP) {
2006-03-20 17:12:09 +00:00
Mouse.delta_z += Z_SENSITIVITY;
Mouse.z += Z_SENSITIVITY;
2007-06-11 15:54:09 +00:00
} else if (button == MBTN_Z_DOWN) {
2006-03-20 17:12:09 +00:00
Mouse.delta_z -= Z_SENSITIVITY;
Mouse.z -= Z_SENSITIVITY;
}
} else {
Mouse.buttons[button].pressed = 0;
Mouse.buttons[button].time_held_down += timer_get_fixed_seconds() - Mouse.buttons[button].time_went_down;
Mouse.buttons[button].num_ups++;
}
}
void mouse_motion_handler(SDL_MouseMotionEvent *mme)
{
Mouse.x += mme->xrel;
Mouse.y += mme->yrel;
}
void mouse_flush() // clears all mice events...
{
int i;
fix current_time;
event_poll();
current_time = timer_get_fixed_seconds();
for (i=0; i<MOUSE_MAX_BUTTONS; i++) {
Mouse.buttons[i].pressed=0;
Mouse.buttons[i].time_went_down=current_time;
Mouse.buttons[i].time_held_down=0;
Mouse.buttons[i].num_ups=0;
Mouse.buttons[i].num_downs=0;
}
Mouse.delta_x = 0;
Mouse.delta_y = 0;
Mouse.delta_z = 0;
Mouse.x = 0;
Mouse.y = 0;
Mouse.z = 0;
SDL_GetMouseState(&Mouse.x, &Mouse.y); // necessary because polling only gives us the delta.
}
//========================================================================
void mouse_get_pos( int *x, int *y, int *z )
2006-03-20 17:12:09 +00:00
{
event_poll();
*x=Mouse.x;
*y=Mouse.y;
*z=Mouse.z;
}
void mouse_get_delta( int *dx, int *dy, int *dz )
2006-03-20 17:12:09 +00:00
{
Giving credits function ability to use custom creditfile (again); Made laser-offset for laser exclusive so Prox mines won't go tru doors; Preventing cycling tru cockpit modes while dead, but allowing to load a state; Implemented D2X' lighting code to D1X (faster, better, sexier - weeee); Try to hop over some errors regarding walls/doors in levels instead of using -1 indexes for arrays; Made the briefing text ptr a bit more failsafe in case the file is corrupt/non-standard; Made scores use the menu screen even in GAME OVER; Fixed bug in neighbour fields of Weapon Keys table; Added the Weapon Keys stuff to TABLE_CREATION; Fixed bug where D2X did not recall applied resolution in the resolutions menu; Simpler check to create DEMO_DIR; Seperated X/Y sensitivity for mouse and joystick; Flush controls when Automap toggles so keypress won't deactivate it again; Made FrameCount in Demos aligned to the Dropframe condition; Added KEy to ttoggle playback text off; Gracefully exit demo code if demo is corrupt; Removed that new percent counter because many old demos seem to have corrupted last frames; Closing endlevel data file if IFF error so the mission still can be freed; Fixed Cruising for keyboard which was not aligned to FPS correctly; Used mouse delta scaling in kconfig.c instead of mouse.c to not screw up when delta is requested in non-ingame situations - it actually belongs to the controls IMHO; Now support up to 8 joysticks; Changed some leftover malloc's to d_malloc and free to d_free
2008-10-16 17:27:02 +00:00
static int old_delta_x = 0, old_delta_y = 0;
SDL_GetRelativeMouseState( &Mouse.delta_x, &Mouse.delta_y );
*dx = Mouse.delta_x;
*dy = Mouse.delta_y;
*dz = Mouse.delta_z;
// filter delta
Giving credits function ability to use custom creditfile (again); Made laser-offset for laser exclusive so Prox mines won't go tru doors; Preventing cycling tru cockpit modes while dead, but allowing to load a state; Implemented D2X' lighting code to D1X (faster, better, sexier - weeee); Try to hop over some errors regarding walls/doors in levels instead of using -1 indexes for arrays; Made the briefing text ptr a bit more failsafe in case the file is corrupt/non-standard; Made scores use the menu screen even in GAME OVER; Fixed bug in neighbour fields of Weapon Keys table; Added the Weapon Keys stuff to TABLE_CREATION; Fixed bug where D2X did not recall applied resolution in the resolutions menu; Simpler check to create DEMO_DIR; Seperated X/Y sensitivity for mouse and joystick; Flush controls when Automap toggles so keypress won't deactivate it again; Made FrameCount in Demos aligned to the Dropframe condition; Added KEy to ttoggle playback text off; Gracefully exit demo code if demo is corrupt; Removed that new percent counter because many old demos seem to have corrupted last frames; Closing endlevel data file if IFF error so the mission still can be freed; Fixed Cruising for keyboard which was not aligned to FPS correctly; Used mouse delta scaling in kconfig.c instead of mouse.c to not screw up when delta is requested in non-ingame situations - it actually belongs to the controls IMHO; Now support up to 8 joysticks; Changed some leftover malloc's to d_malloc and free to d_free
2008-10-16 17:27:02 +00:00
Mouse.delta_x = (*dx + old_delta_x) * 0.5;
Mouse.delta_y = (*dy + old_delta_y) * 0.5;
Giving credits function ability to use custom creditfile (again); Made laser-offset for laser exclusive so Prox mines won't go tru doors; Preventing cycling tru cockpit modes while dead, but allowing to load a state; Implemented D2X' lighting code to D1X (faster, better, sexier - weeee); Try to hop over some errors regarding walls/doors in levels instead of using -1 indexes for arrays; Made the briefing text ptr a bit more failsafe in case the file is corrupt/non-standard; Made scores use the menu screen even in GAME OVER; Fixed bug in neighbour fields of Weapon Keys table; Added the Weapon Keys stuff to TABLE_CREATION; Fixed bug where D2X did not recall applied resolution in the resolutions menu; Simpler check to create DEMO_DIR; Seperated X/Y sensitivity for mouse and joystick; Flush controls when Automap toggles so keypress won't deactivate it again; Made FrameCount in Demos aligned to the Dropframe condition; Added KEy to ttoggle playback text off; Gracefully exit demo code if demo is corrupt; Removed that new percent counter because many old demos seem to have corrupted last frames; Closing endlevel data file if IFF error so the mission still can be freed; Fixed Cruising for keyboard which was not aligned to FPS correctly; Used mouse delta scaling in kconfig.c instead of mouse.c to not screw up when delta is requested in non-ingame situations - it actually belongs to the controls IMHO; Now support up to 8 joysticks; Changed some leftover malloc's to d_malloc and free to d_free
2008-10-16 17:27:02 +00:00
old_delta_x = *dx;
old_delta_y = *dy;
Mouse.delta_x = 0;
Mouse.delta_y = 0;
Mouse.delta_y = 0;
2006-03-20 17:12:09 +00:00
}
int mouse_get_btns()
{
int i;
uint flag=1;
int status = 0;
event_poll();
for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
if (Mouse.buttons[i].pressed)
status |= flag;
flag <<= 1;
}
return status;
}
// Returns how long this button has been down since last call.
fix mouse_button_down_time(int button)
{
fix time_down, time;
event_poll();
if (!Mouse.buttons[button].pressed) {
time_down = Mouse.buttons[button].time_held_down;
Mouse.buttons[button].time_held_down = 0;
} else {
time = timer_get_fixed_seconds();
time_down = time - Mouse.buttons[button].time_held_down;
Mouse.buttons[button].time_held_down = time;
}
return time_down;
}
// Returns how many times this button has went down since last call
int mouse_button_down_count(int button)
{
int count;
event_poll();
count = Mouse.buttons[button].num_downs;
Mouse.buttons[button].num_downs = 0;
return count;
}
// Returns 1 if this button is currently down
int mouse_button_state(int button)
{
event_poll();
return Mouse.buttons[button].pressed;
}