Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
23 KB
Referenced Files
None
Subscribers
None
diff --git a/include/r-tech1/debug.h b/include/r-tech1/debug.h
index 9aae072c..3995902a 100644
--- a/include/r-tech1/debug.h
+++ b/include/r-tech1/debug.h
@@ -1,115 +1,116 @@
#ifndef _rtech1_debug_h
#define _rtech1_debug_h
#include <ostream>
#include <sstream>
#include <stdint.h>
#define PAINTOWN_DEBUG_CONTEXT Global::debug_context(__FILE__, __LINE__)
+#define _xdebug Global::debug(0, PAINTOWN_DEBUG_CONTEXT)
/* Enable this if you can't get regular debug output but have networking
*/
// #define NETWORK_DEBUG
namespace Global{
#ifdef ANDROID
class android_ostream: public std::ostream {
public:
android_ostream(bool enabled = true);
static android_ostream stream;
/* make these private at some point */
public:
bool enabled;
std::ostringstream buffer;
};
typedef android_ostream stream_type;
android_ostream & operator<<(android_ostream & stream, const std::string & input);
android_ostream & operator<<(android_ostream & stream, const char * input);
android_ostream & operator<<(android_ostream & stream, const char);
android_ostream & operator<<(android_ostream & stream, const double);
android_ostream & operator<<(android_ostream & stream, const int);
android_ostream & operator<<(android_ostream & stream, const short int);
android_ostream & operator<<(android_ostream & stream, const short unsigned int);
android_ostream & operator<<(android_ostream & stream, const unsigned int);
android_ostream & operator<<(android_ostream & stream, const bool);
android_ostream & operator<<(android_ostream & stream, const long int);
android_ostream & operator<<(android_ostream & stream, const unsigned long int);
android_ostream & operator<<(android_ostream & stream, const void *);
android_ostream & operator<<(android_ostream & stream, std::ostream & (*f)(std::ostream &));
#elif defined(WII) && defined(DEBUG)
class wii_ostream: public std::ostream {
public:
wii_ostream(bool enabled = true);
static wii_ostream stream;
/* make these private at some point */
public:
bool enabled;
std::ostringstream buffer;
};
typedef wii_ostream stream_type;
wii_ostream & operator<<(wii_ostream & stream, const std::string & input);
wii_ostream & operator<<(wii_ostream & stream, const char * input);
wii_ostream & operator<<(wii_ostream & stream, const char);
wii_ostream & operator<<(wii_ostream & stream, const double);
wii_ostream & operator<<(wii_ostream & stream, const int);
wii_ostream & operator<<(wii_ostream & stream, const short int);
wii_ostream & operator<<(wii_ostream & stream, const short unsigned int);
wii_ostream & operator<<(wii_ostream & stream, const unsigned int);
wii_ostream & operator<<(wii_ostream & stream, const bool);
wii_ostream & operator<<(wii_ostream & stream, const long int);
wii_ostream & operator<<(wii_ostream & stream, const unsigned long int);
wii_ostream & operator<<(wii_ostream & stream, const void *);
wii_ostream & operator<<(wii_ostream & stream, uint64_t);
wii_ostream & operator<<(wii_ostream & stream, std::ostream & (*f)(std::ostream &));
#elif defined(NETWORK_DEBUG)
class network_ostream: public std::ostream {
public:
network_ostream(const std::string & host, int port, bool enabled = true);
static network_ostream stream;
/* make these private at some point */
public:
std::string host;
int port;
bool enabled;
std::ostringstream buffer;
};
typedef network_ostream stream_type;
stream_type & operator<<(stream_type & stream, const std::string & input);
stream_type & operator<<(stream_type & stream, const char * input);
stream_type & operator<<(stream_type & stream, const char);
stream_type & operator<<(stream_type & stream, const double);
stream_type & operator<<(stream_type & stream, const int);
stream_type & operator<<(stream_type & stream, const short int);
stream_type & operator<<(stream_type & stream, const short unsigned int);
stream_type & operator<<(stream_type & stream, const unsigned int);
stream_type & operator<<(stream_type & stream, const bool);
stream_type & operator<<(stream_type & stream, const long int);
#ifndef PS3
stream_type & operator<<(stream_type & stream, const unsigned long int);
#endif
stream_type & operator<<(stream_type & stream, const void *);
stream_type & operator<<(stream_type & stream, uint64_t);
stream_type & operator<<(stream_type & stream, std::ostream & (*f)(std::ostream &));
#else
typedef std::ostream stream_type;
#endif
void logToFile();
void closeLog();
void setDebug(int i);
int getDebug();
void setLogFile(const std::string &);
const std::string & getLogFile();
extern std::string defaultDebugContext;
void setDefaultDebugContext(const std::string & context);
stream_type & debug(int i, const std::string & context = defaultDebugContext);
std::string debug_context(const char * file, int line);
}
#endif
diff --git a/include/r-tech1/input/input-manager.h b/include/r-tech1/input/input-manager.h
index 0c206260..5ef8328e 100644
--- a/include/r-tech1/input/input-manager.h
+++ b/include/r-tech1/input/input-manager.h
@@ -1,316 +1,318 @@
#ifndef _rtech1_input_manager
#define _rtech1_input_manager
#include <vector>
#include <algorithm>
#include "input.h"
#include "input-map.h"
#include "input-source.h"
+#include "touch.h"
#include "r-tech1/funcs.h"
#include "r-tech1/pointer.h"
#include "r-tech1/events.h"
#include "keyboard.h"
#include "joystick.h"
#include "r-tech1/exceptions/exception.h"
class Configuration;
class InputSource;
template <class Output>
class InputHandler{
public:
InputHandler(){
}
virtual ~InputHandler(){
}
virtual void press(const Output & out, Keyboard::unicode_t unicode) = 0;
virtual void release(const Output & out, Keyboard::unicode_t unicode) = 0;
};
/* handles keyboard/joystick/whatever input during the game */
class InputManager{
public:
/* main has one instance of this and thats it.
* should the janitor have the reference instead?
*/
friend int rtech_main(int, char**);
friend int main(int, char **);
/* returns true if any input device is activated (keys pressed, joystick button */
static bool anyInput();
// static std::vector<Input::PaintownInput> getInput(const Configuration & configuration, const int facing);
static void poll();
/*
static void enableBufferInput();
static void disableBufferInput();
*/
static void waitForKeys(int key1, int key2, const InputSource & source);
static void waitForKeys(int key1, const InputSource & source);
static int readKey();
static void waitForClear();
static void deferResizeEvents(bool defer);
static const std::map<int, Util::ReferenceCount<Joystick> > & getJoysticks();
/*
template <class X>
static void observeKeyboard(InputMap<X> & input){
manager->keyboard.addObserver(InputMap<X>::observeKey, &input);
}
template <class X>
static void unObserveKeyboard(InputMap<X> & input){
manager->keyboard.removeObserver(InputMap<X>::observeKey, &input);
}
*/
static std::vector<Keyboard::unicode_t> readText(){
return manager->keyboard.readText();
}
template <typename X>
static std::vector<Keyboard::unicode_t> readText(InputMap<X> & input, typename InputMap<X>::Output & output){
return manager->_readText(input, output);
}
#if 0
template <typename X>
static typename InputMap<X>::Output getMap(InputMap<X> & input){
if (manager){
return manager->_getMap(input);
}
/* just crash hard.. who cares */
throw Exception::Base(__FILE__, __LINE__);
/* make the compiler happy about returning something */
return *(typename InputMap<X>::Output*)1;
}
#endif
template <typename X>
static typename std::vector<typename InputMap<X>::InputEvent> getEvents(InputMap<X> & input, const InputSource & source){
return manager->_getEvents(input, source);
}
template <typename X>
static void handleEvents(InputMap<X> & input, const InputSource & source, InputHandler<X> & handler){
typename std::vector<typename InputMap<X>::InputEvent> events = getEvents(input, source);
for (typename std::vector<typename InputMap<X>::InputEvent>::iterator it = events.begin(); it != events.end(); it++){
const typename InputMap<X>::InputEvent & event = *it;
if (event.enabled){
handler.press(event.out, event.unicode);
} else {
handler.release(event.out, event.unicode);
}
}
}
template <typename X>
static bool pressed(InputMap<X> & input, const InputSource & source, X out){
if (manager){
return manager->_pressed(input, source, out);
}
return false;
}
/* wait for a key to be released
* really this waits for all inputs that would result in `out'
* being generated to stop.
*/
template <typename X>
static void waitForRelease(InputMap<X> & input, const InputSource & source, X out){
while (InputManager::pressed(input, source, out)){
Util::rest(1);
InputManager::poll();
}
}
template <typename X>
static void waitForPress(InputMap<X> & input, const InputSource & source, X out){
while (!InputManager::pressed(input, source, out)){
Util::rest(1);
InputManager::poll();
}
}
template <typename X>
static void captureInput(InputMap<X> & input){
manager->_captureInput(input);
}
template <typename X>
static void releaseInput(InputMap<X> & input){
manager->_releaseInput(input);
}
protected:
InputManager();
virtual ~InputManager();
virtual bool _anyInput();
virtual int _readKey();
// virtual std::vector<Input::PaintownInput> _getInput(const Configuration & configuration, const int facing);
template <typename X>
void _captureInput(InputMap<X> & input){
capture = (void*) &input;
}
template <typename X>
void _releaseInput(InputMap<X> & input){
if (capture == (void*) &input){
capture = 0;
}
}
template <typename X>
std::vector<Keyboard::unicode_t> _readText(InputMap<X> & input, typename InputMap<X>::Output & output){
std::vector<Keyboard::unicode_t> text;
std::vector<Keyboard::KeyData> all = keyboard.readData();
for (std::vector<Keyboard::KeyData>::iterator it = all.begin(); it != all.end(); it++){
const Keyboard::KeyData & data = *it;
KeyState<X> * state = input.getState(data.key);
if (state != NULL && output[state->out]){
text.push_back(data.unicode);
}
}
return text;
}
void removeDuplicates(std::vector<int> & storage){
std::vector<int> output;
int last = -1;
for (std::vector<int>::iterator it = storage.begin(); it != storage.end(); it++){
if (*it != last){
output.push_back(*it);
last = *it;
}
}
storage = output;
}
template <typename X>
typename std::vector<typename InputMap<X>::InputEvent> _getEvents(InputMap<X> & input, const InputSource & source){
/* FIXME: get events from the source */
std::vector<typename InputMap<X>::InputEvent> events;
if (capture != NULL && capture != &input){
return events;
}
if (source.useKeyboard()){
const std::vector<typename Keyboard::KeyData> & buffer = keyboard.getBufferedKeys();
for (std::vector<Keyboard::KeyData>::const_iterator it = buffer.begin(); it != buffer.end(); it++){
const Keyboard::KeyData & data = *it;
Util::ReferenceCount<KeyState<X> > state = input.getState(data.key);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, data.unicode, data.enabled));
}
}
}
for (std::vector<int>::const_iterator it = source.getJoystick().begin(); it != source.getJoystick().end(); it++){
int config = *it;
if (config >= 0 && config < (int) joysticks.size()){
Util::ReferenceCount<Joystick> joystick = joysticks[config];
if (joystick != NULL){
const std::vector<typename Joystick::Event> & joystickEvents = joystick->getEvents();
for (std::vector<Joystick::Event>::const_iterator it = joystickEvents.begin(); it != joystickEvents.end(); it++){
Joystick::Event event = *it;
Util::ReferenceCount<JoystickState<X> > state = input.getJoystickState(event.key);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, -1, event.enabled));
}
}
}
}
}
return events;
}
#if 0
template <typename X>
typename InputMap<X>::Output _getMap(InputMap<X> & input){
typename InputMap<X>::Output output;
if (capture != 0 && capture != &input){
return output;
}
std::vector<int> all_keys;
keyboard.readKeys(all_keys);
keyboard.readBufferedKeys(all_keys);
// all_keys.insert(all_keys.end(), bufferedKeys.begin(), bufferedKeys.end());
// all_keys.insert(all_keys.end(), keyboard.currentKeys().begin(), keyboard.currentKeys.end());
std::sort(all_keys.begin(), all_keys.end());
removeDuplicates(all_keys);
// std::unique(all_keys.begin(), all_keys.end());
// bufferedKeys.clear();
input.read(all_keys, &output);
/*
if (joystick != NULL){
JoystickInput all_joystick = joystick->readAll();
input.read(all_joystick, &output);
}
*/
/* just bumps an internal counter */
input.update();
return output;
}
#endif
template <typename X>
bool _pressed(InputMap<X> & input, const InputSource & source, X result){
/* FIXME: use the input source, luke */
if (capture != 0 && capture != &input){
return false;
}
std::vector<int> all_keys;
keyboard.readKeys(all_keys);
// all_keys.insert(all_keys.end(), bufferedKeys.begin(), bufferedKeys.end());
std::sort(all_keys.begin(), all_keys.end());
removeDuplicates(all_keys);
// bufferedKeys.clear();
bool out = false;
out = input.pressed(all_keys, result);
/*
if (joystick != NULL){
JoystickInput all_joystick = joystick->readAll();
out |= input.pressed(all_joystick, result);
}
*/
return out;
}
virtual void _poll();
protected:
void installJoysticks();
void checkJoysticks();
private:
static InputManager * manager;
void * capture;
std::map<int, Util::ReferenceCount<Joystick> > joysticks;
Keyboard keyboard;
+ Util::ReferenceCount<DeviceInput::Touch> touch;
Util::EventManager eventManager;
// std::vector<int> bufferedKeys;
// bool bufferKeys;
};
#endif
diff --git a/include/r-tech1/input/touch.h b/include/r-tech1/input/touch.h
index 3a2e157a..9c94c6dd 100644
--- a/include/r-tech1/input/touch.h
+++ b/include/r-tech1/input/touch.h
@@ -1,20 +1,21 @@
#ifndef _rtech1_touch
#define _rtech1_touch
#include "../pointer.h"
/* Manages devices that respond to touch input, such as android/ios */
-namespace Input{
+namespace DeviceInput{
class Touch{
public:
Touch();
+ virtual void poll() = 0;
virtual ~Touch();
};
Util::ReferenceCount<Touch> getTouchDevice();
}
#endif
diff --git a/src/input/allegro5/touch.cpp b/src/input/allegro5/touch.cpp
index 380c9b19..e1b91470 100644
--- a/src/input/allegro5/touch.cpp
+++ b/src/input/allegro5/touch.cpp
@@ -1,35 +1,68 @@
#ifdef USE_ALLEGRO5
#include "r-tech1/input/touch.h"
#include "r-tech1/pointer.h"
+#include "r-tech1/debug.h"
#include <allegro5/allegro.h>
-namespace Input{
+namespace DeviceInput{
class Allegro5Touch: public Touch {
public:
Allegro5Touch();
+ virtual void poll();
virtual ~Allegro5Touch();
protected:
ALLEGRO_EVENT_QUEUE * queue;
};
Allegro5Touch::Allegro5Touch():
queue(NULL){
- queue = al_create_event_queue();
- al_register_event_source(queue, al_get_touch_input_event_source());
+ if (al_install_touch_input()){
+ queue = al_create_event_queue();
+ if (queue != NULL){
+ al_register_event_source(queue, al_get_touch_input_event_source());
+ }
+ }
+}
+
+void Allegro5Touch::poll(){
+ if (queue == NULL){
+ return;
+ }
+ ALLEGRO_EVENT event;
+ while (al_get_next_event(queue, &event)){
+ switch (event.type){
+ case ALLEGRO_EVENT_TOUCH_BEGIN: {
+ Global::debug(0) << "Touch begin id (" << event.touch.id << ") x (" << (double) event.touch.x << ") y (" << (double) event.touch.y << ")" << std::endl;
+ break;
+ }
+ case ALLEGRO_EVENT_TOUCH_END: {
+ Global::debug(0) << "Touch end id (" << event.touch.id << ") x (" << (double) event.touch.x << ") y (" << (double) event.touch.y << ")" << std::endl;
+ break;
+ }
+ case ALLEGRO_EVENT_TOUCH_MOVE: {
+ Global::debug(0) << "Touch move id (" << event.touch.id << ") x (" << (double) event.touch.x << ") y (" << (double) event.touch.y << ")" << std::endl;
+ break;
+ }
+ case ALLEGRO_EVENT_TOUCH_CANCEL: {
+ Global::debug(0) << "Touch cancel id (" << event.touch.id << ") x (" << (double) event.touch.x << ") y (" << (double) event.touch.y << ")" << std::endl;
+ break;
+ }
+ }
+ }
}
Allegro5Touch::~Allegro5Touch(){
al_destroy_event_queue(queue);
queue = NULL;
}
Util::ReferenceCount<Touch> getTouchDevice(){
return Util::ReferenceCount<Touch>(new Allegro5Touch());
}
}
#endif
diff --git a/src/input/input-manager.cpp b/src/input/input-manager.cpp
index 380f7a51..6b10fe85 100644
--- a/src/input/input-manager.cpp
+++ b/src/input/input-manager.cpp
@@ -1,152 +1,155 @@
#include "r-tech1/input/input-manager.h"
#include "r-tech1/configuration.h"
#include "r-tech1/input/joystick.h"
#include "r-tech1/events.h"
#include "r-tech1/debug.h"
#include <stdlib.h>
#include <vector>
using namespace std;
InputManager * InputManager::manager = 0;
InputManager::InputManager():
capture(0){
manager = this;
if (Configuration::isJoystickEnabled()){
installJoysticks();
}
+
+ touch = DeviceInput::getTouchDevice();
}
void InputManager::installJoysticks(){
joysticks.clear();
for (int i = 0; i < Joystick::numberOfJoysticks(); i++){
joysticks[i] = Joystick::create(i);
}
}
const std::map<int, Util::ReferenceCount<Joystick> > & InputManager::getJoysticks(){
if (manager != NULL){
return manager->joysticks;
}
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
#ifdef PS3
#include <io/pad.h>
extern "C" int SDL_JoystickInit();
extern "C" int SDL_JoystickQuit();
#endif
static bool needJoystickUpdate(){
#ifdef PS3
padInfo pad;
if (ioPadGetInfo(&pad) == 0){
/* re-initialize the joystick stuff if we have a different
* number of joysticks according to lv2
*/
if (pad.connected != (unsigned) SDL_NumJoysticks()){
SDL_JoystickQuit();
SDL_JoystickInit();
return true;
}
}
#endif
return false;
}
void InputManager::checkJoysticks(){
if (needJoystickUpdate()){
installJoysticks();
}
}
InputManager::~InputManager(){
}
void InputManager::deferResizeEvents(bool defer){
if (manager != NULL){
manager->eventManager.deferResizeEvents(defer);
}
}
bool InputManager::anyInput(){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_anyInput();
}
bool InputManager::_anyInput(){
if (keyboard.keypressed()){
return true;
}
for (map<int, Util::ReferenceCount<Joystick> >::iterator it = joysticks.begin(); it != joysticks.end(); it++){
Util::ReferenceCount<Joystick> joystick = it->second;
if (joystick != NULL){
return joystick->pressed();
}
}
return false;
}
void InputManager::waitForClear(){
manager->keyboard.clear();
while (anyInput()){
poll();
Util::rest(1);
}
}
void InputManager::waitForKeys(int key1, int key2, const InputSource & source){
InputMap<int> wait;
wait.set(key1, 0, false, 1);
wait.set(key2, 0, false, 1);
InputManager::waitForRelease(wait, source, 1);
InputManager::waitForPress(wait, source, 1);
InputManager::waitForRelease(wait, source, 1);
}
void InputManager::waitForKeys(int key, const InputSource & source){
InputMap<int> wait;
wait.set(key, 0, false, 1);
InputManager::waitForRelease(wait, source, 1);
InputManager::waitForPress(wait, source, 1);
InputManager::waitForRelease(wait, source, 1);
}
void InputManager::poll(){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_poll();
}
int InputManager::readKey(){
return manager->_readKey();
}
int InputManager::_readKey(){
std::vector<int> keys;
do{
keyboard.readKeys(keys);
if (keys.size() == 0){
Util::rest(1);
poll();
}
} while (keys.size() == 0);
return keys.front();
}
void InputManager::_poll(){
#ifdef PS3
checkJoysticks();
#endif
+ touch->poll();
eventManager.run(keyboard, joysticks);
}
diff --git a/src/input/touch.cpp b/src/input/touch.cpp
index e9f8519d..8c698ea1 100644
--- a/src/input/touch.cpp
+++ b/src/input/touch.cpp
@@ -1,11 +1,11 @@
#include "r-tech1/input/touch.h"
-namespace Input{
+namespace DeviceInput{
Touch::Touch(){
}
Touch::~Touch(){
}
}
diff --git a/src/system/allegro5/init.cpp b/src/system/allegro5/init.cpp
index 42baf524..524cdd97 100644
--- a/src/system/allegro5/init.cpp
+++ b/src/system/allegro5/init.cpp
@@ -1,28 +1,29 @@
#ifdef USE_ALLEGRO5
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include "r-tech1/system/init.h"
#include "r-tech1/debug.h"
namespace System{
void initSystem(const Global::InitConditions & conditions, Global::stream_type & out){
out << "Allegro5 initialize " << (al_init() ? "Ok" : "Failed") << std::endl;
uint32_t version = al_get_allegro_version();
int major = version >> 24;
int minor = (version >> 16) & 255;
int revision = (version >> 8) & 255;
int release = version & 255;
out << "Allegro5 version " << major << "." << minor << "." << revision << "." << release << std::endl;
out << "Init image: " << (al_init_image_addon() ? "Ok" : "Failed") << std::endl;
out << "Init primitives " << (al_init_primitives_addon() ? "Ok" : "Failed") << std::endl;
out << "Init keyboard " << (al_install_keyboard() ? "Ok" : "Failed") << std::endl;
out << "Init joystick " << (al_install_joystick() ? "Ok" : "Failed") << std::endl;
+ out << "Init touch " << (al_install_touch_input() ? "Ok" : "Failed") << std::endl;
al_set_app_name("Paintown");
}
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:22 AM (2 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
71086
Default Alt Text
(23 KB)

Event Timeline