www.pudn.com > zfxcengine-0.1.0.zip > ceInputDevice_SDL.cpp


/* $Id */ 
//////////////////////////////////////////////////////////////////////////////// 
// 
//	Module:		Input 
//	File:		ceInputDevice_SDL.cpp 
//	Created:	25.08.2005 02:17:18 
//	Author:		Florian Bauer aka Florianx 
// 
//	Last Change:	25.08.2005 02:17:18 
// 
//////////////////////////////////////////////////////////////////////////////// 
 
#include "Core/ceDef.h" 
#include "Core/ceExceptions.h" 
#include "Core/ceDebug.h" 
#include "Input/InputSDL/ceInputDevice_SDL.h" 
#include  
 
using namespace ZFXCE; 
 
Input::ceInputDeviceSDL::ceInputDeviceSDL(ceInputDeviceSDL** devicePointer) 
: m_DevicePointer(devicePointer), m_RefCounter(1) 
{ 
	PUSH_FUNCTION; 
 
	AddTriggerableEvent(ceKeyboardButtonDownEvent); 
	AddTriggerableEvent(ceKeyboardButtonUpEvent); 
	AddTriggerableEvent(ceMouseButtonDownEvent); 
	AddTriggerableEvent(ceMouseButtonUpEvent); 
	AddTriggerableEvent(ceMouseMoveEvent); 
	AddTriggerableEvent(ceJoystickButtonDownEvent); 
	AddTriggerableEvent(ceJoystickButtonUpEvent); 
	AddTriggerableEvent(ceJoystickAxisMoveEvent); 
	AddTriggerableEvent(ceJoystickHatMoveEvent); 
	AddTriggerableEvent(ceJoystickBallMoveEvent); 
	AddTriggerableEvent(ceQuitEvent); 
 
	if(SDL_WasInit(SDL_INIT_EVERYTHING)==0) 
		SDL_Init(SDL_INIT_EVERYTHING); 
 
	if(SDL_WasInit(SDL_INIT_EVENTTHREAD)==0) 
		SDL_InitSubSystem(SDL_INIT_EVENTTHREAD); 
 
	if(SDL_WasInit(SDL_INIT_JOYSTICK)==0) 
		SDL_InitSubSystem(SDL_INIT_JOYSTICK); 
 
	SDL_EnableUNICODE(1); 
	SDL_JoystickEventState(SDL_ENABLE); 
 
	int n = SDL_NumJoysticks(); 
	m_JoystickStates.reserve(n); 
	for(int i=0; i 16) state.numAxes = 16; 
		if(state.numButtons > 16) state.numButtons = 16; 
		if(state.numHats > 8) state.numHats = 8; 
		m_JoystickStates.push_back(std::pair(state, joy)); 
	} 
} 
 
Input::ceInputDeviceSDL::~ceInputDeviceSDL() 
{ 
	PUSH_FUNCTION; 
 
	std::vector >::iterator it; 
	for(it = m_JoystickStates.begin(); it != m_JoystickStates.end(); it++) 
		SDL_JoystickClose(it->second); 
} 
 
void Input::ceInputDeviceSDL::AddRef() 
{ 
	m_RefCounter++; 
} 
 
void Input::ceInputDeviceSDL::Release() 
{ 
	PUSH_FUNCTION; 
 
	if(--m_RefCounter == 0) 
	{ 
		*m_DevicePointer = 0; 
		delete this; 
	} 
} 
 
const unsigned char* Input::ceInputDeviceSDL::GetKeyboardState() 
{ 
	PUSH_FUNCTION; 
 
	SDL_PumpEvents(); 
	int size; 
	const unsigned char* d = SDL_GetKeyState(&size); 
	ce_assert(size >= CE_KEY_LAST); 
	return d; 
} 
 
const Input::ceMouseState& Input::ceInputDeviceSDL::GetMouseState() 
{ 
	PUSH_FUNCTION; 
 
	SDL_PumpEvents(); 
	unsigned char state = SDL_GetMouseState(&m_MouseState.x, &m_MouseState.y); 
	m_MouseState.buttons[0] = (state & (1<<0)) != 0; 
	m_MouseState.buttons[1] = (state & (1<<1)) != 0; 
	m_MouseState.buttons[2] = (state & (1<<2)) != 0; 
	m_MouseState.buttons[3] = (state & (1<<3)) != 0; 
	m_MouseState.buttons[4] = (state & (1<<4)) != 0; 
	m_MouseState.buttons[5] = (state & (1<<5)) != 0; 
	m_MouseState.buttons[6] = (state & (1<<6)) != 0; 
	m_MouseState.buttons[7] = (state & (1<<7)) != 0; 
	return m_MouseState; 
} 
 
unsigned int Input::ceInputDeviceSDL::GetNumJoysticks() const 
{ 
	PUSH_FUNCTION; 
 
	return m_JoystickStates.size(); 
} 
 
const Input::ceJoystickState& Input::ceInputDeviceSDL::GetJoystickState(unsigned int index) 
{ 
	PUSH_FUNCTION; 
 
	ce_assert(index