/cocos2d/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java

https://gitlab.com/gasabr/flappy-test · Java · 185 lines · 97 code · 31 blank · 57 comment · 4 complexity · 75a1403401f8e8e15679e257562f4b94 MD5 · raw file

  1. /****************************************************************************
  2. Copyright (c) 2010-2011 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. package org.cocos2dx.lib;
  21. import android.opengl.GLSurfaceView;
  22. import javax.microedition.khronos.egl.EGLConfig;
  23. import javax.microedition.khronos.opengles.GL10;
  24. public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
  25. // ===========================================================
  26. // Constants
  27. // ===========================================================
  28. private final static long NANOSECONDSPERSECOND = 1000000000L;
  29. private final static long NANOSECONDSPERMICROSECOND = 1000000;
  30. private static long sAnimationInterval = (long) (1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND);
  31. // ===========================================================
  32. // Fields
  33. // ===========================================================
  34. private long mLastTickInNanoSeconds;
  35. private int mScreenWidth;
  36. private int mScreenHeight;
  37. private boolean mNativeInitCompleted = false;
  38. // ===========================================================
  39. // Constructors
  40. // ===========================================================
  41. // ===========================================================
  42. // Getter & Setter
  43. // ===========================================================
  44. public static void setAnimationInterval(final float animationInterval) {
  45. Cocos2dxRenderer.sAnimationInterval = (long) (animationInterval * Cocos2dxRenderer.NANOSECONDSPERSECOND);
  46. }
  47. public void setScreenWidthAndHeight(final int surfaceWidth, final int surfaceHeight) {
  48. this.mScreenWidth = surfaceWidth;
  49. this.mScreenHeight = surfaceHeight;
  50. }
  51. // ===========================================================
  52. // Methods for/from SuperClass/Interfaces
  53. // ===========================================================
  54. @Override
  55. public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) {
  56. Cocos2dxRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight);
  57. this.mLastTickInNanoSeconds = System.nanoTime();
  58. mNativeInitCompleted = true;
  59. }
  60. @Override
  61. public void onSurfaceChanged(final GL10 GL10, final int width, final int height) {
  62. Cocos2dxRenderer.nativeOnSurfaceChanged(width, height);
  63. }
  64. @Override
  65. public void onDrawFrame(final GL10 gl) {
  66. /*
  67. * No need to use algorithm in default(60 FPS) situation,
  68. * since onDrawFrame() was called by system 60 times per second by default.
  69. */
  70. if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {
  71. Cocos2dxRenderer.nativeRender();
  72. } else {
  73. final long now = System.nanoTime();
  74. final long interval = now - this.mLastTickInNanoSeconds;
  75. if (interval < Cocos2dxRenderer.sAnimationInterval) {
  76. try {
  77. Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
  78. } catch (final Exception e) {
  79. }
  80. }
  81. /*
  82. * Render time MUST be counted in, or the FPS will slower than appointed.
  83. */
  84. this.mLastTickInNanoSeconds = System.nanoTime();
  85. Cocos2dxRenderer.nativeRender();
  86. }
  87. }
  88. // ===========================================================
  89. // Methods
  90. // ===========================================================
  91. private static native void nativeTouchesBegin(final int id, final float x, final float y);
  92. private static native void nativeTouchesEnd(final int id, final float x, final float y);
  93. private static native void nativeTouchesMove(final int[] ids, final float[] xs, final float[] ys);
  94. private static native void nativeTouchesCancel(final int[] ids, final float[] xs, final float[] ys);
  95. private static native boolean nativeKeyEvent(final int keyCode,boolean isPressed);
  96. private static native void nativeRender();
  97. private static native void nativeInit(final int width, final int height);
  98. private static native void nativeOnSurfaceChanged(final int width, final int height);
  99. private static native void nativeOnPause();
  100. private static native void nativeOnResume();
  101. public void handleActionDown(final int id, final float x, final float y) {
  102. Cocos2dxRenderer.nativeTouchesBegin(id, x, y);
  103. }
  104. public void handleActionUp(final int id, final float x, final float y) {
  105. Cocos2dxRenderer.nativeTouchesEnd(id, x, y);
  106. }
  107. public void handleActionCancel(final int[] ids, final float[] xs, final float[] ys) {
  108. Cocos2dxRenderer.nativeTouchesCancel(ids, xs, ys);
  109. }
  110. public void handleActionMove(final int[] ids, final float[] xs, final float[] ys) {
  111. Cocos2dxRenderer.nativeTouchesMove(ids, xs, ys);
  112. }
  113. public void handleKeyDown(final int keyCode) {
  114. Cocos2dxRenderer.nativeKeyEvent(keyCode, true);
  115. }
  116. public void handleKeyUp(final int keyCode) {
  117. Cocos2dxRenderer.nativeKeyEvent(keyCode, false);
  118. }
  119. public void handleOnPause() {
  120. /**
  121. * onPause may be invoked before onSurfaceCreated,
  122. * and engine will be initialized correctly after
  123. * onSurfaceCreated is invoked. Can not invoke any
  124. * native method before onSurfaceCreated is invoked
  125. */
  126. if (! mNativeInitCompleted)
  127. return;
  128. Cocos2dxHelper.onEnterBackground();
  129. Cocos2dxRenderer.nativeOnPause();
  130. }
  131. public void handleOnResume() {
  132. Cocos2dxHelper.onEnterForeground();
  133. Cocos2dxRenderer.nativeOnResume();
  134. }
  135. private static native void nativeInsertText(final String text);
  136. private static native void nativeDeleteBackward();
  137. private static native String nativeGetContentText();
  138. public void handleInsertText(final String text) {
  139. Cocos2dxRenderer.nativeInsertText(text);
  140. }
  141. public void handleDeleteBackward() {
  142. Cocos2dxRenderer.nativeDeleteBackward();
  143. }
  144. public String getContentText() {
  145. return Cocos2dxRenderer.nativeGetContentText();
  146. }
  147. // ===========================================================
  148. // Inner and Anonymous Classes
  149. // ===========================================================
  150. }