Android遊戲開發底層起步(一)

Android遊戲開發的特點有二:

一是基於Java語言,二是基於OpenGL

因此要精通Android開發的底層技術,不得不從此兩方面入手。


本系列文章,分析用的代碼來自於LGame-0.3.1版本,在此感謝其作者爲我們提供學習用源碼!


OpenGL能給我們的遊戲帶來優異的性能。但是OpenGL只是一個優秀的圖形編程標準,爲我們提供了發揮圖形硬件性能的藉口,但是它並沒有提供創建窗口的方法。我們一切的繪圖的操作都需要一個窗口(包括全屏模式),沒有窗口我們什麼都做不了。


幸運地是,出現了GLUT (OpenGL Utility Toolkit)(OpenGL工具包)。它被用來輕鬆應對窗口、按鈕以及用戶事件。我們可以使用C/C++去調用它,但是對於想要使用真正面嚮對象語言的JAVA程序員來說,或者是Android程序員來說。Java與OpenGL的綁定是必須的。


雖然Android提供了OpenGL的支持,但是在模擬器上調試OpenGL程序是非常痛苦的。所以我建議想深入瞭解Android遊戲開發的朋友,最好還是先從桌面端的JAVA+OpenGL入手。

桌面端的JAVA程序玩的很熟了,Android上的程序也就只是“照貓畫虎”了。特別是對於那些沒有Android真機的朋友。


有許多用Java去結合OpenGL的嘗試,但是第一個被大家認可並注意的是Java對於OpenGl的綁定(Java Bindings for OpenGL), 或者稱爲JOGL。理由是它得到Sun(Java的創建者)和SGI(OpenGL的創建者)的支持。


OpenGL 的發展歷程
  1992年7月 發佈了 OpenGL 1.0 版本,並與微軟共同推出 Windows NT 版本的 OpenGL
  1995年 OpenGL 1.1 版本面市,加入了新功能,並引入了紋理特性等等。
  一直到 2009年8月Khronos小組發佈了OpenGL 3.2,這是一年以來OpenGL進行的第三次重要升級。

Android 3D 引擎採用的是OpenGL ESOpenGL ES是一套爲手持和嵌入式系統設計的3D引擎API。

Android系統使用 OpenGL 的標準接口來支持3D圖形功能,android 3D 圖形系統也分爲 java 框架和本地代碼兩部分。本地代碼主要實現的 OpenGL接口的庫,在Java框架層,javax.microedition.khronos.openglesjava標準的 OpenGL包,android.opengl包提供了 OpenGL系統和Android GUI系統之間的聯繫。

Android 支持 OpenGL 列表
1、GL
2、GL 10
3、GL 10 EXT
4、GL 11
5、GL 11 EXT
6、GL 11 ExtensionPack

任何遊戲開發都是基於框架的,從現在開始我們就一步一步的學習,怎麼去寫一個圖形渲染框架去封裝OpenGL,最後再發展成爲一個成熟的遊戲框架。

我們必須提供一個接口,以使上層程序與具體的底層的OpenGL的具體版本實現之間解除耦合。

因此需要一個GLEx類。我們再實時判斷OpenGL的版本,從而去按需實例化GLEx類。

仔細研究Android的OpenGL包,發現其提供了:                需要搞懂Java中子接口的概念。

接口   javax.microedition.khronos.opengles.GL
接口   javax.microedition.khronos.opengles.GL10           執行了GL接口
接口        javax.microedition.khronos.opengles.GL10Ext                        執行了GL接口

接口   javax.microedition.khronos.opengles.GL11                             執行了GL10接口
接口   javax.microedition.khronos.opengles.GL11Ext                        執行了GL接口
接口       javax.microedition.khronos.opengles.GL11ExtensionPack   執行了GL接口


利用如下方法,我們可以獲得OpenGL的版本號:

        String renderer = g10.glGetString(GL10.GL_RENDERER).toLowerCase();
        GLEx.isPixelFlinger = renderer.indexOf("pixelflinger") != -1;
        Log.i("Android2DView", "GLES:" + renderer);

其中:

public static final int GL_RENDERER

Constant Value: 7937 (0x00001f01)

public abstract String glGetString (int name)

如果返回的字符串裏含有“pixelflinger”        則OpenGL是軟件實現。



OpenGl一般是在SurfaceView上繪圖。

public abstract void onSurfaceCreated (GL10 gl, EGLConfig config)

Since: API Level 3

Called when the surface is created or recreated.

Called when the rendering thread starts and whenever the EGL context is lost. The EGL context will typically be lost when the Android device awakes after going to sleep.

Since this method is called at the beginning of rendering, as well as every time the EGL context is lost, this method is a convenient place to put code to create resources that need to be created when the rendering starts, and that need to be recreated when the EGL context is lost. Textures are an example of a resource that you might want to create here.

Note that when the EGL context is lost, all OpenGL resources associated with that context will be automatically deleted. You do not need to call the corresponding "glDelete" methods such as glDeleteTextures to manually delete these lost resources.

Parameters
gl the GL interface. Use instanceof to test if the interface supports GL11 or higher interfaces.
config the EGLConfig of the created surface. Can be used to create matching pbuffers.









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章