Browse Source

Merge branch 'next' of https://github.com/blackberry/GamePlay into next

Darryl Gough 13 years ago
parent
commit
5c89f7e7da
4 changed files with 142 additions and 39 deletions
  1. 1 1
      gameplay/src/Container.cpp
  2. 61 20
      gameplay/src/PlatformAndroid.cpp
  3. 36 5
      gameplay/src/PlatformQNX.cpp
  4. 44 13
      gameplay/src/TextBox.cpp

+ 1 - 1
gameplay/src/Container.cpp

@@ -601,7 +601,7 @@ bool Container::keyEvent(Keyboard::KeyEvent evt, int key)
             if (control->keyEvent(evt, key))
             {
                 release();
-                return _consumeInputEvents;
+                return true;
             }
             else if (evt == Keyboard::KEY_CHAR && key == Keyboard::KEY_TAB)
             {

+ 61 - 20
gameplay/src/PlatformAndroid.cpp

@@ -108,14 +108,26 @@ static EGLenum checkErrorEGL(const char* msg)
 // Initialized EGL resources.
 static bool initEGL()
 {
+	int samples = 0;
+	Properties* config = Game::getInstance()->getConfig()->getNamespace("window", true);
+	if (config)
+	{
+		samples = std::max(config->getInt("samples"), 0);
+	}
+
     // Hard-coded to 32-bit/OpenGL ES 2.0.
+    // NOTE: EGL_SAMPLE_BUFFERS, EGL_SAMPLES and EGL_DEPTH_SIZE MUST remain at the beginning of the attribute list
+    // since they are expected to be at indices 0-5 in config fallback code later.
+	// EGL_DEPTH_SIZE is also expected to
     EGLint eglConfigAttrs[] =
     {
+		EGL_SAMPLE_BUFFERS,     samples > 0 ? 1 : 0,
+		EGL_SAMPLES,            samples,
+		EGL_DEPTH_SIZE,         24,
         EGL_RED_SIZE,           8,
         EGL_GREEN_SIZE,         8,
         EGL_BLUE_SIZE,          8,
         EGL_ALPHA_SIZE,         8,
-        EGL_DEPTH_SIZE,         24,
         EGL_STENCIL_SIZE,       8,
         EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
         EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
@@ -144,30 +156,59 @@ static bool initEGL()
             checkErrorEGL("eglGetDisplay");
             goto error;
         }
-    
+
         if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE)
         {
             checkErrorEGL("eglInitialize");
             goto error;
         }
-    
-        if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE)
-        {
-            checkErrorEGL("eglChooseConfig");
-            goto error;
-        }
-        
-        if (eglConfigCount == 0)
-        {
-            // try 16 bit depth buffer instead
-            eglConfigAttrs[9] = 16;
-            if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0)
-            {
-                checkErrorEGL("eglChooseConfig");
-                goto error;
-            }
-        }
-    
+
+		// Try both 24 and 16-bit depth sizes since some hardware (i.e. Tegra) does not support 24-bit depth
+		bool validConfig = false;
+		EGLint depthSizes[] = { 24, 16 };
+		for (unsigned int i = 0; i < 2; ++i)
+		{
+			eglConfigAttrs[1] = samples > 0 ? 1 : 0;
+			eglConfigAttrs[3] = samples;
+			eglConfigAttrs[5] = depthSizes[i];
+
+			if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0)
+			{
+				validConfig = true;
+				break;
+			}
+
+			if (samples)
+			{
+				// Try lowering the MSAA sample size until we find a supported config
+				int sampleCount = samples;
+				while (sampleCount)
+				{
+					GP_WARN("No EGL config found for depth_size=%d and samples=%d. Trying samples=%d instead.", depthSizes[i], sampleCount, sampleCount / 2);
+					sampleCount /= 2;
+					eglConfigAttrs[1] = sampleCount > 0 ? 1 : 0;
+					eglConfigAttrs[3] = sampleCount;
+					if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0)
+					{
+						validConfig = true;
+						break;
+					}
+				}
+				if (validConfig)
+					break;
+			}
+			else
+			{
+				GP_WARN("No EGL config found for depth_size=%d.", depthSizes[i]);
+			}
+		}
+
+		if (!validConfig)
+		{
+			checkErrorEGL("eglChooseConfig");
+			goto error;
+		}
+
         __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs);
         if (__eglContext == EGL_NO_CONTEXT)
         {

+ 36 - 5
gameplay/src/PlatformQNX.cpp

@@ -421,7 +421,7 @@ EGLenum checkErrorEGL(const char* msg)
     GP_ASSERT(msg);
     static const char* errmsg[] =
     {
-        "EGL function succeeded",
+        "EGL function failed",
         "EGL is not initialized, or could not be initialized, for the specified display",
         "EGL cannot access a requested resource",
         "EGL failed to allocate resources for the requested operation",
@@ -544,6 +544,14 @@ Platform* Platform::create(Game* game, void* attachToWindow)
     FileSystem::setResourcePath("./app/native/");
     Platform* platform = new Platform(game);
 
+    // Query game config
+    int samples = 0;
+    Properties* config = Game::getInstance()->getConfig()->getNamespace("window", true);
+    if (config)
+    {
+        samples = std::max(config->getInt("samples"), 0);
+    }
+
     __gestureSet = gestures_set_alloc();
     swipe_gesture_alloc(NULL, gesture_callback, __gestureSet);
     pinch_gesture_alloc(NULL, gesture_callback, __gestureSet);
@@ -583,8 +591,12 @@ Platform* Platform::create(Game* game, void* attachToWindow)
     EGLint eglConfigCount;
 
     // Hard-coded to 32-bit/OpenGL ES 2.0.
-    const EGLint eglConfigAttrs[] =
+    // NOTE: EGL_SAMPLE_BUFFERS and EGL_SAMPLES MUST remain at the beginning of the attribute list
+    // since they are expected to be at indices 0-3 in config fallback code later.
+    EGLint eglConfigAttrs[] =
     {
+        EGL_SAMPLE_BUFFERS,     samples > 0 ? 1 : 0,
+        EGL_SAMPLES,            samples,
         EGL_RED_SIZE,           8,
         EGL_GREEN_SIZE,         8,
         EGL_BLUE_SIZE,          8,
@@ -766,8 +778,26 @@ Platform* Platform::create(Game* game, void* attachToWindow)
 
     if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0)
     {
-        checkErrorEGL("eglChooseConfig");
-        goto error;
+    	bool success = false;
+    	while (samples)
+    	{
+    		// Try lowering the MSAA sample count until we find a supported config
+    		GP_WARN("Failed to find a valid EGL configuration with EGL samples=%d. Trying samples=%d instead.", samples, samples/2);
+    		samples /= 2;
+    		eglConfigAttrs[1] = samples > 0 ? 1 : 0;
+    		eglConfigAttrs[3] = samples;
+    		if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0)
+    		{
+    			success = true;
+    			break;
+    		}
+    	}
+
+    	if (!success)
+    	{
+			checkErrorEGL("eglChooseConfig");
+			goto error;
+    	}
     }
 
     __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs);
@@ -1047,7 +1077,8 @@ int Platform::enterMessagePump()
                         break;
                     case NAVIGATOR_WINDOW_THUMBNAIL:
                     case NAVIGATOR_WINDOW_INVISIBLE:
-                        _game->pause();
+                        if (!suspended)
+                            _game->pause();
                         suspended = true;
                         break;
                     }

+ 44 - 13
gameplay/src/TextBox.cpp

@@ -64,8 +64,8 @@ bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
 
             if (_state == NORMAL)
                 Game::getInstance()->displayKeyboard(true);
-            else
-                setCaretLocation(x, y);
+
+            setCaretLocation(x, y);
 
             _state = ACTIVE;
             _dirty = true;
@@ -110,8 +110,6 @@ bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
 
 bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 {
-    bool consume = false;
-
     if (_state == FOCUS)
     {
         switch (evt)
@@ -149,7 +147,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
                             textAlignment, true, rightToLeft);
                         _dirty = true;
-                        consume = true;
                         notifyListeners(Listener::TEXT_CHANGED);
                         break;
                     }
@@ -166,7 +163,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
                             textAlignment, true, rightToLeft);
                         _dirty = true;
-                        consume = true;
                         break;
                     }
                     case Keyboard::KEY_RIGHT_ARROW:
@@ -182,7 +178,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
                             textAlignment, true, rightToLeft);
                         _dirty = true;
-                        consume = true;
                         break;
                     }
                     case Keyboard::KEY_UP_ARROW:
@@ -202,7 +197,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         }
 
                         _dirty = true;
-                        consume = true;
                         break;
                     }
                     case Keyboard::KEY_DOWN_ARROW:
@@ -222,7 +216,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         }
 
                         _dirty = true;
-                        consume = true;
                         break;
                     }
                 }
@@ -259,7 +252,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 
                             _dirty = true;
                         }
-                        consume = true;
                         break;
                     }
                     case Keyboard::KEY_RETURN:
@@ -273,7 +265,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                     {
                         // Insert character into string.
                         _text.insert(textIndex, 1, (char)key);
-                        consume = true;
 
                         // Get new location of caret.
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
@@ -325,7 +316,7 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 
     _lastKeypress = key;
 
-    return (consume & _consumeInputEvents);
+    return _consumeInputEvents;
 }
 
 void TextBox::update(const Control* container, const Vector2& offset)
@@ -374,7 +365,47 @@ void TextBox::setCaretLocation(int x, int y)
 
     if (index == -1)
     {
-        _caretLocation.set(_prevCaretLocation);
+        // Attempt to find the nearest valid caret location.
+        Rectangle textBounds;
+        font->measureText(_text.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
+
+        if (_caretLocation.x > textBounds.x + textBounds.width &&
+            _caretLocation.y > textBounds.y + textBounds.height)
+        {
+            font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, _text.length(),
+                textAlignment, true, rightToLeft);
+            return;
+        }
+
+        if (_caretLocation.x < textBounds.x)
+        {
+            _caretLocation.x = textBounds.x;
+        }
+        else if (_caretLocation.x > textBounds.x + textBounds.width)
+        {
+            _caretLocation.x = textBounds.x + textBounds.width;
+        }
+
+        if (_caretLocation.y < textBounds.y)
+        {
+            _caretLocation.y = textBounds.y;
+        }
+        else if (_caretLocation.y > textBounds.y + textBounds.height)
+        {
+            Font* font = getFont(_state);
+            GP_ASSERT(font);
+            unsigned int fontSize = getFontSize(_state);
+            _caretLocation.y = textBounds.y + textBounds.height - fontSize;
+        }
+
+        index = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
+            textAlignment, true, rightToLeft);
+
+        if (index == -1)
+        {
+            // We failed to find a valid location; just put the caret back to where it was.
+            _caretLocation.set(_prevCaretLocation);
+        }
     }
 }