Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

248 linhas
7.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. package net.lolengine;
  11. import android.app.Activity;
  12. import android.content.Context;
  13. import android.content.pm.ActivityInfo;
  14. import android.content.res.AssetManager; /* getAssets() */
  15. import android.content.res.Resources; /* getResources() */
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18. import android.opengl.GLSurfaceView;
  19. import android.os.Bundle;
  20. import android.util.Log;
  21. import android.view.MotionEvent;
  22. import android.view.WindowManager;
  23. import android.view.Window;
  24. import javax.microedition.khronos.egl.EGL10;
  25. import javax.microedition.khronos.egl.EGLConfig;
  26. import javax.microedition.khronos.egl.EGLContext;
  27. import javax.microedition.khronos.egl.EGLDisplay;
  28. import javax.microedition.khronos.opengles.GL10;
  29. /* FIXME: this needs to have a different name for each project */
  30. public class LolActivity extends Activity
  31. {
  32. @Override protected void onCreate(Bundle savedInstanceState)
  33. {
  34. super.onCreate(savedInstanceState);
  35. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  36. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  37. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  38. getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
  39. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  40. requestWindowFeature(Window.FEATURE_NO_TITLE);
  41. nativeInit();
  42. mView = new LolView(getApplication());
  43. setContentView(mView);
  44. }
  45. @Override protected void onPause()
  46. {
  47. super.onPause();
  48. mView.onPause();
  49. }
  50. @Override protected void onResume()
  51. {
  52. super.onResume();
  53. mView.onResume();
  54. }
  55. private LolView mView;
  56. static
  57. {
  58. System.loadLibrary("stlport_shared");
  59. System.loadLibrary("@PROGRAM@");
  60. }
  61. public Bitmap openImage(String name)
  62. {
  63. try
  64. {
  65. return BitmapFactory.decodeStream(getAssets().open(name));
  66. }
  67. catch (Exception e) { }
  68. return null;
  69. }
  70. public int getWidth(Bitmap bmp) { return bmp.getWidth(); }
  71. public int getHeight(Bitmap bmp) { return bmp.getHeight(); }
  72. public void getPixels(Bitmap bmp, int[] pixels)
  73. {
  74. int w = bmp.getWidth();
  75. int h = bmp.getHeight();
  76. bmp.getPixels(pixels, 0, w, 0, 0, w, h);
  77. }
  78. public void closeImage(Bitmap bmp)
  79. {
  80. bmp.recycle();
  81. }
  82. private native void nativeInit();
  83. }
  84. class LolView extends GLSurfaceView
  85. {
  86. public LolView(Context context)
  87. {
  88. super(context);
  89. setEGLContextFactory(new ContextFactory());
  90. //setEGLConfigChooser(new ConfigChooser(4, 4, 4, 0, 8, 0));
  91. setEGLConfigChooser(new ConfigChooser(4, 4, 4, 4, 8, 0));
  92. setRenderer(new LolRenderer());
  93. }
  94. private static class ContextFactory implements GLSurfaceView.EGLContextFactory
  95. {
  96. private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
  97. public EGLContext createContext(EGL10 egl, EGLDisplay dpy, EGLConfig cfg)
  98. {
  99. Log.w("LOL", "creating OpenGL ES 2.0 context");
  100. int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
  101. EGLContext ctx = egl.eglCreateContext(dpy, cfg, EGL10.EGL_NO_CONTEXT, attrib_list);
  102. return ctx;
  103. }
  104. public void destroyContext(EGL10 egl, EGLDisplay dpy, EGLContext ctx)
  105. {
  106. egl.eglDestroyContext(dpy, ctx);
  107. }
  108. }
  109. private static class ConfigChooser implements GLSurfaceView.EGLConfigChooser
  110. {
  111. public ConfigChooser(int r, int g, int b, int a, int depth, int stencil)
  112. {
  113. mRed = r;
  114. mGreen = g;
  115. mBlue = b;
  116. mAlpha = a;
  117. mDepth = depth;
  118. mStencil = stencil;
  119. }
  120. private static int EGL_OPENGL_ES2_BIT = 4;
  121. private static int[] s_configAttribs2 =
  122. {
  123. // EGL10.EGL_BUFFER_SIZE, 16,
  124. EGL10.EGL_DEPTH_SIZE, 8,
  125. EGL10.EGL_RED_SIZE, 4,
  126. EGL10.EGL_GREEN_SIZE, 4,
  127. EGL10.EGL_BLUE_SIZE, 4,
  128. // EGL10.EGL_ALPHA_SIZE, 4,
  129. EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  130. EGL10.EGL_NONE
  131. };
  132. public EGLConfig chooseConfig(EGL10 egl, EGLDisplay dpy)
  133. {
  134. int[] num_config = new int[1];
  135. egl.eglChooseConfig(dpy, s_configAttribs2, null, 0, num_config);
  136. int n = num_config[0];
  137. if (n <= 0)
  138. throw new IllegalArgumentException("No GLES configs");
  139. EGLConfig[] configs = new EGLConfig[n];
  140. egl.eglChooseConfig(dpy, s_configAttribs2, configs, n, num_config);
  141. return choose(egl, dpy, configs);
  142. }
  143. public EGLConfig choose(EGL10 egl, EGLDisplay dpy, EGLConfig[] configs)
  144. {
  145. for(EGLConfig cfg : configs)
  146. {
  147. /* Do not complain if we get more bits than we asked. */
  148. if (get(egl, dpy, cfg, EGL10.EGL_STENCIL_SIZE, 0) >= mStencil
  149. && get(egl, dpy, cfg, EGL10.EGL_DEPTH_SIZE, 0) >= mDepth
  150. && get(egl, dpy, cfg, EGL10.EGL_RED_SIZE, 0) >= mRed
  151. && get(egl, dpy, cfg, EGL10.EGL_GREEN_SIZE, 0) >= mGreen
  152. && get(egl, dpy, cfg, EGL10.EGL_BLUE_SIZE, 0) >= mBlue
  153. && get(egl, dpy, cfg, EGL10.EGL_ALPHA_SIZE, 0) >= mAlpha)
  154. return cfg;
  155. }
  156. return null;
  157. }
  158. private int get(EGL10 egl, EGLDisplay dpy, EGLConfig cfg,
  159. int attr, int defval)
  160. {
  161. int[] value = new int[1];
  162. if (egl.eglGetConfigAttrib(dpy, cfg, attr, value))
  163. return value[0];
  164. return defval;
  165. }
  166. protected int mRed, mGreen, mBlue, mAlpha, mDepth, mStencil;
  167. }
  168. public boolean onTouchEvent(final MotionEvent ev)
  169. {
  170. final int action = ev.getAction();
  171. switch (action)
  172. {
  173. case MotionEvent.ACTION_DOWN:
  174. nativeMove((int)ev.getX(), (int)ev.getY());
  175. nativeDown();
  176. break;
  177. case MotionEvent.ACTION_UP:
  178. nativeMove((int)ev.getX(), (int)ev.getY());
  179. nativeUp();
  180. break;
  181. case MotionEvent.ACTION_MOVE:
  182. nativeMove((int)ev.getX(), (int)ev.getY());
  183. break;
  184. }
  185. return true;
  186. }
  187. private static native void nativePause();
  188. private static native void nativeDown();
  189. private static native void nativeUp();
  190. private static native void nativeMove(int x, int y);
  191. }
  192. class LolRenderer implements GLSurfaceView.Renderer
  193. {
  194. public void onSurfaceCreated(GL10 gl, EGLConfig config)
  195. {
  196. nativeInit();
  197. }
  198. public void onSurfaceChanged(GL10 gl, int w, int h)
  199. {
  200. Log.w("LOL", String.format("resizing to %dx%d", w, h));
  201. nativeResize(w, h);
  202. }
  203. public void onDrawFrame(GL10 gl)
  204. {
  205. nativeRender();
  206. }
  207. private static native void nativeInit();
  208. private static native void nativeResize(int w, int h);
  209. private static native void nativeRender();
  210. private static native void nativeDone();
  211. }