Implemented some event handlers in SDL

This commit is contained in:
Tulioh 2015-01-11 13:24:54 -02:00
parent 15b3d439d6
commit fd97ccd402
7 changed files with 292 additions and 39 deletions

View File

@ -0,0 +1,35 @@
<?xml version="1.0"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.libsdl.app" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16"/>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:hardwareAccelerated="true" >
<activity android:name="SDLActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data android:name="android.app.lib_name" android:value="otclient"/>
</activity>
</application>
<!-- Android 2.3.3 -->
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="12"/>
<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000"/>
<!-- Allow writing to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- Allow make internet connections -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

View File

@ -45,6 +45,8 @@
#define BUILD_ARCH "x64"
#elif defined(__i386) || defined(_M_IX86) || defined(_X86_)
#define BUILD_ARCH "x86"
#elif defined(__arm__)
#define BUILD_ARCH "ARM"
#else
#define BUILD_ARCH "unknown"
#endif

View File

@ -177,8 +177,6 @@ std::string Application::getOs()
return "mac";
#elif __linux
return "linux";
#elif defined ANDROID
return "android";
#else
return "unknown";
#endif

View File

@ -27,12 +27,154 @@
SDLWindow::SDLWindow() {
m_minimumSize = Size(600,480);
m_size = Size(600,480);
m_window = 0;
//m_keyMap[SDLK_ESCAPE] = Fw::KeyEscape;
//m_keyMap[SDLK_TAB] = Fw::KeyTab;
m_keyMap[SDLK_RETURN] = Fw::KeyEnter;
m_keyMap[SDLK_BACKSPACE] = Fw::KeyBackspace;
m_keyMap[SDLK_SPACE] = Fw::KeySpace;
/*
m_keyMap[SDLK_PAGEUP] = Fw::KeyPageUp;
m_keyMap[SDLK_PAGEDOWN] = Fw::KeyPageDown;
m_keyMap[SDLK_HOME] = Fw::KeyHome;
m_keyMap[SDLK_END] = Fw::KeyEnd;
m_keyMap[SDLK_INSERT] = Fw::KeyInsert;
m_keyMap[SDLK_DELETE] = Fw::KeyDelete;
m_keyMap[SDLK_UP] = Fw::KeyUp;
m_keyMap[SDLK_DOWN] = Fw::KeyDown;
m_keyMap[SDLK_LEFT] = Fw::KeyLeft;
m_keyMap[SDLK_RIGHT] = Fw::KeyRight;
m_keyMap[SDLK_NUMLOCKCLEAR] = Fw::KeyNumLock;
m_keyMap[SDLK_SCROLLLOCK] = Fw::KeyScrollLock;
m_keyMap[SDLK_CAPSLOCK] = Fw::KeyCapsLock;
m_keyMap[SDLK_PRINTSCREEN] = Fw::KeyPrintScreen;
m_keyMap[SDLK_PAUSE] = Fw::KeyPause;
m_keyMap[SDLK_LCTRL] = Fw::KeyCtrl;
m_keyMap[SDLK_RCTRL] = Fw::KeyCtrl;
m_keyMap[SDLK_LSHIFT] = Fw::KeyShift;
m_keyMap[SDLK_RSHIFT] = Fw::KeyShift;
m_keyMap[SDLK_LALT] = Fw::KeyAlt;
m_keyMap[SDLK_RALT] = Fw::KeyAlt;
m_keyMap[SDLK_LGUI] = Fw::KeyMeta;
m_keyMap[SDLK_RGUI] = Fw::KeyMeta;
*/
// ascii characters
m_keyMap[SDLK_EXCLAIM] = Fw::KeyExclamation;
m_keyMap[SDLK_QUOTEDBL] = Fw::KeyQuote;
m_keyMap[SDLK_HASH] = Fw::KeyNumberSign;
m_keyMap[SDLK_DOLLAR] = Fw::KeyDollar;
m_keyMap[SDLK_PERCENT] = Fw::KeyPercent;
m_keyMap[SDLK_AMPERSAND] = Fw::KeyAmpersand;
m_keyMap[SDLK_QUOTE] = Fw::KeyApostrophe;
m_keyMap[SDLK_LEFTPAREN] = Fw::KeyLeftParen;
m_keyMap[SDLK_RIGHTPAREN] = Fw::KeyRightParen;
m_keyMap[SDLK_ASTERISK] = Fw::KeyAsterisk;
m_keyMap[SDLK_PLUS] = Fw::KeyPlus;
m_keyMap[SDLK_COMMA] = Fw::KeyComma;
m_keyMap[SDLK_MINUS] = Fw::KeyMinus;
m_keyMap[SDLK_PERIOD] = Fw::KeyPeriod;
m_keyMap[SDLK_SLASH] = Fw::KeySlash;
m_keyMap[SDLK_0] = Fw::Key0;
m_keyMap[SDLK_1] = Fw::Key1;
m_keyMap[SDLK_2] = Fw::Key2;
m_keyMap[SDLK_3] = Fw::Key3;
m_keyMap[SDLK_4] = Fw::Key4;
m_keyMap[SDLK_5] = Fw::Key5;
m_keyMap[SDLK_6] = Fw::Key6;
m_keyMap[SDLK_7] = Fw::Key7;
m_keyMap[SDLK_8] = Fw::Key8;
m_keyMap[SDLK_9] = Fw::Key9;
m_keyMap[SDLK_COLON] = Fw::KeyColon;
m_keyMap[SDLK_SEMICOLON] = Fw::KeySemicolon;
m_keyMap[SDLK_LESS] = Fw::KeyLess;
m_keyMap[SDLK_EQUALS] = Fw::KeyEqual;
m_keyMap[SDLK_GREATER] = Fw::KeyGreater;
m_keyMap[SDLK_QUESTION] = Fw::KeyQuestion;
m_keyMap[SDLK_AT] = Fw::KeyAtSign;
m_keyMap[SDLK_a] = Fw::KeyA;
m_keyMap[SDLK_b] = Fw::KeyB;
m_keyMap[SDLK_c] = Fw::KeyC;
m_keyMap[SDLK_d] = Fw::KeyD;
m_keyMap[SDLK_e] = Fw::KeyE;
m_keyMap[SDLK_f] = Fw::KeyF;
m_keyMap[SDLK_g] = Fw::KeyG;
m_keyMap[SDLK_h] = Fw::KeyH;
m_keyMap[SDLK_i] = Fw::KeyI;
m_keyMap[SDLK_j] = Fw::KeyJ;
m_keyMap[SDLK_k] = Fw::KeyK;
m_keyMap[SDLK_l] = Fw::KeyL;
m_keyMap[SDLK_m] = Fw::KeyM;
m_keyMap[SDLK_n] = Fw::KeyN;
m_keyMap[SDLK_o] = Fw::KeyO;
m_keyMap[SDLK_p] = Fw::KeyP;
m_keyMap[SDLK_q] = Fw::KeyQ;
m_keyMap[SDLK_r] = Fw::KeyR;
m_keyMap[SDLK_s] = Fw::KeyS;
m_keyMap[SDLK_t] = Fw::KeyT;
m_keyMap[SDLK_u] = Fw::KeyU;
m_keyMap[SDLK_v] = Fw::KeyV;
m_keyMap[SDLK_w] = Fw::KeyW;
m_keyMap[SDLK_x] = Fw::KeyX;
m_keyMap[SDLK_y] = Fw::KeyY;
m_keyMap[SDLK_z] = Fw::KeyZ;
m_keyMap[SDLK_LGUI] = Fw::KeyLeftBracket;
m_keyMap[SDLK_BACKSLASH] = Fw::KeyBackslash;
m_keyMap[SDLK_RIGHTBRACKET] = Fw::KeyRightBracket;
m_keyMap[SDLK_CARET] = Fw::KeyCaret;
m_keyMap[SDLK_UNDERSCORE] = Fw::KeyUnderscore;
m_keyMap[SDLK_BACKQUOTE] = Fw::KeyGrave;
//m_keyMap['{'] = Fw::KeyLeftCurly;
//m_keyMap['|'] = Fw::KeyBar;
//m_keyMap['}'] = Fw::KeyRightCurly;
//m_keyMap['~'] = Fw::KeyTilde;
// keypad
/*
m_keyMap[SDLK_KP_PLUS] = Fw::KeyPlus;
m_keyMap[SDLK_KP_MINUS] = Fw::KeyMinus;
m_keyMap[SDLK_KP_DECIMAL] = Fw::KeyPeriod;
m_keyMap[SDLK_KP_DIVIDE] = Fw::KeySlash;
m_keyMap[SDLK_KP_MULTIPLY] = Fw::KeyAsterisk;
m_keyMap[VK_OEM_1] = Fw::KeySemicolon;
m_keyMap[VK_OEM_2] = Fw::KeySlash;
m_keyMap[VK_OEM_3] = Fw::KeyGrave;
m_keyMap[VK_OEM_4] = Fw::KeyLeftBracket;
m_keyMap[VK_OEM_5] = Fw::KeyBackslash;
m_keyMap[VK_OEM_6] = Fw::KeyRightBracket;
m_keyMap[VK_OEM_7] = Fw::KeyApostrophe;
m_keyMap[VK_OEM_MINUS] = Fw::KeyMinus;
m_keyMap[VK_OEM_PLUS] = Fw::KeyEqual;
m_keyMap[SDLK_KP_COMMA] = Fw::KeyComma;
m_keyMap[VK_OEM_PERIOD] = Fw::KeyPeriod;
m_keyMap[SDLK_F1] = Fw::KeyF1;
m_keyMap[SDLK_F2] = Fw::KeyF2;
m_keyMap[SDLK_F3] = Fw::KeyF3;
m_keyMap[SDLK_F4] = Fw::KeyF4;
m_keyMap[SDLK_F5] = Fw::KeyF5;
m_keyMap[SDLK_F6] = Fw::KeyF6;
m_keyMap[SDLK_F7] = Fw::KeyF7;
m_keyMap[SDLK_F8] = Fw::KeyF8;
m_keyMap[SDLK_F9] = Fw::KeyF9;
m_keyMap[SDLK_F10] = Fw::KeyF10;
m_keyMap[SDLK_F11] = Fw::KeyF11;
m_keyMap[SDLK_F12] = Fw::KeyF12;
*/
}
void SDLWindow::init() {
window = 0;
gl = 0;
initializeSDL();
}
@ -44,18 +186,23 @@ void SDLWindow::initializeSDL() {
setSDLAttributes();
SDL_GetDisplayMode( 0, 0, &mode );
int width = mode.w;
int height = mode.h;
SDL_GetDisplayMode(0, 0, &m_mode);
int width = m_mode.w;
int height = m_mode.h;
window = SDL_CreateWindow( NULL, 0, 0, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE );
m_window = SDL_CreateWindow( NULL, 0, 0, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE );
if( window == 0 ) {
m_size = Size( width, height );
if( m_window == 0 ) {
g_logger.fatal("Failed to create window");
terminate();
}
gl = SDL_GL_CreateContext(window);
m_visible = true;
SDL_GL_CreateContext(m_window);
//SDL_StartTextInput();
}
void SDLWindow::setSDLAttributes() {
@ -74,18 +221,80 @@ void SDLWindow::setSDLAttributes() {
void SDLWindow::terminate() {
SDL_Quit();
}
void SDLWindow::hide() {
// TODO?
m_visible = false;
}
void SDLWindow::poll() {
// TODO
while(SDL_PollEvent(&m_event)) {
processKeydownOrKeyrelease();
if(hasRepeatedKey())
continue;
switch(m_event.type) {
case SDL_TEXTINPUT:
processTextInput();
break;
case SDL_FINGERDOWN:
case SDL_FINGERUP:
processFingerdownAndFingerup();
break;
}
if(m_inputEvent.type != Fw::NoInputEvent && m_onInputEvent)
m_onInputEvent(m_inputEvent);
}
fireKeysPress();
}
bool SDLWindow::hasRepeatedKey() {
return m_event.key.repeat != 0;
}
void SDLWindow::processKeydownOrKeyrelease() {
if(m_event.key.state == SDL_PRESSED || (m_event.key.state == SDL_RELEASED && !hasRepeatedKey())) {
Fw::Key keyCode = Fw::KeyUnknown;
SDL_Keycode keysym = m_event.key.keysym.sym;
if(m_keyMap.find(keysym) != m_keyMap.end())
keyCode = m_keyMap[keysym];
if(m_event.type == SDL_PRESSED)
processKeyDown(keyCode);
else if(m_event.type == SDL_RELEASED)
processKeyUp(keyCode);
}
}
void SDLWindow::processTextInput() {
std::string text = m_event.text.text;
SDL_Keycode keysym = m_event.key.keysym.sym;
if(text.length() == 0 || keysym == SDLK_BACKSPACE || keysym == SDLK_RETURN)
return;
if(m_onInputEvent) {
m_inputEvent.reset(Fw::KeyTextInputEvent);
m_inputEvent.keyText = text;
m_onInputEvent(m_inputEvent);
}
}
void SDLWindow::processFingerdownAndFingerup() {
m_inputEvent.reset();
m_inputEvent.type = (m_event.type == SDL_FINGERDOWN) ? Fw::MousePressInputEvent : Fw::MouseReleaseInputEvent;
m_inputEvent.mouseButton = Fw::MouseLeftButton;
m_mouseButtonStates[Fw::MouseLeftButton] = true;
Point newMousePos(m_event.tfinger.x * m_mode.w, m_event.tfinger.y * m_mode.h);
m_inputEvent.mouseMoved = newMousePos - m_inputEvent.mousePos;
m_inputEvent.mousePos = newMousePos;
if(m_onInputEvent)
m_onInputEvent(m_inputEvent);
}
void SDLWindow::swapBuffers() {
SDL_GL_SwapWindow(window);
SDL_GL_SwapWindow(m_window);
}
void SDLWindow::setVerticalSync(bool enable) {
@ -101,24 +310,28 @@ void SDLWindow::setClipboardText(const std::string& text) {
}
Size SDLWindow::getDisplaySize() {
Size size(mode.w, mode.h);
Size size(m_mode.w, m_mode.h);
return size;
}
std::string SDLWindow::getPlatformType() {
return "Android";
return "ANDROID_SDL2";
}
void SDLWindow::show() {
// android doesn't need to show activity, it's open automatically
// mobile devices doesn't need to show activity, it's open automatically
}
void SDLWindow::hide() {
// SDL make this for us
}
void SDLWindow::maximize() {
// android doesn't has window
// mobile devices doesn't has window
}
void SDLWindow::move(const Point& pos) {
// android doesn't has window
// mobile devices doesn't has window
}
void SDLWindow::resize(const Size& size) {
@ -126,40 +339,40 @@ void SDLWindow::resize(const Size& size) {
}
void SDLWindow::showMouse() {
// android doesn't has mouse
// mobile devices doesn't has mouse
}
void SDLWindow::hideMouse() {
// android doesn't has mouse
// mobile devices doesn't has mouse
}
int SDLWindow::internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) {
// android doesn't has mouse
// mobile devices doesn't has mouse
return 0;
}
void SDLWindow::setMouseCursor(int cursorId) {
// android doesn't has mouse
// mobile devices doesn't has mouse
}
void SDLWindow::restoreMouseCursor() {
// android doesn't has mouse
// mobile devices doesn't has mouse
}
void SDLWindow::setTitle(const std::string& title) {
// android doesn't need to set title, the app make it
// mobile devices doesn't need to set title, the app make it
}
void SDLWindow::setMinimumSize(const Size& minimumSize) {
// android doesn't has window
// mobile devices doesn't has window
}
void SDLWindow::setFullscreen(bool fullscreen) {
// android doesn't has window
// mobile devices doesn't has window
}
void SDLWindow::setIcon(const std::string& iconFile) {
// android doesn't has window
// mobile devices doesn't has icon
}
#endif // ANDROID

View File

@ -61,12 +61,17 @@ public:
void initializeSDL();
void setSDLAttributes();
bool hasRepeatedKey();
void processKeydownOrKeyrelease();
void processTextInput();
void processFingerdownAndFingerup();
protected:
int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot);
private:
SDL_Window* window;
SDL_GLContext gl;
SDL_DisplayMode mode;
SDL_Window* m_window;
SDL_DisplayMode m_mode;
SDL_Event m_event;
};
#endif

View File

@ -206,7 +206,7 @@ std::string Platform::traceback(const std::string& where, int level, int maxDept
return ss.str();
}
#else
std::string Platform::traceback(const std::string& where, int level, int maxDepth){ return "TODO"; }
std::string Platform::traceback(const std::string& where, int level, int maxDepth){ return " ANDROID TRACEBACK NEED TO DO"; }
#endif
#endif

View File

@ -590,15 +590,15 @@ void X11Window::poll()
XNextEvent(m_display, &event);
// check for repeated key releases
bool repatedKeyRelease = false;
bool repeatedKeyRelease = false;
if(event.type == KeyRelease && XPending(m_display)) {
XPeekEvent(m_display, &peekEvent);
if((peekEvent.type == KeyPress) && (peekEvent.xkey.keycode == event.xkey.keycode) && ((peekEvent.xkey.time-event.xkey.time) < 2))
repatedKeyRelease = true;
repeatedKeyRelease = true;
}
// process keydown and keyrelease events first
if(event.type == KeyPress || (event.type == KeyRelease && !repatedKeyRelease)) {
if(event.type == KeyPress || (event.type == KeyRelease && !repeatedKeyRelease)) {
// remove caps lock and shift maks
XKeyEvent xkey = event.xkey;
xkey.state &= ~(ShiftMask | LockMask);
@ -623,7 +623,7 @@ void X11Window::poll()
continue;
// discard repated key releases
if(repatedKeyRelease)
if(repeatedKeyRelease)
continue;
switch(event.type) {