如何用 純C++(ndk)開發(fā)安卓應(yīng)用 ?
如何安裝安卓的開發(fā)環(huán)境以及怎么設(shè)置ndk的環(huán)境變量等在前邊的文章已經(jīng)有了詳細(xì)的講解,在這里我就不再說明,如果有不會(huì)安裝和設(shè)置環(huán)境的,請(qǐng)先參考安卓環(huán)境搭建的內(nèi)容。
好,假設(shè)以及安裝好了ndk,使用純c++開發(fā)安卓程序,下邊是詳細(xì)的步驟與說明:
1.編寫入口函數(shù)
android_main為入口函數(shù),和C++中的main函數(shù)是一樣的。這里創(chuàng)建CELLAndroidApp的對(duì)象,直接調(diào)用main函數(shù)。
- void android_main(struct android_app* state)
- {
- CELLAndroidApp app(state);
- app.main(0,0);
- }
說明:其中的 CELLAndroidApp是我們?cè)O(shè)計(jì)的一個(gè)圖形繪制類,稍后將對(duì)其做詳細(xì)說明
2.繪制類的實(shí)現(xiàn)說明
2.1類的成員說明
- protected:
- EGLConfig _config;
- EGLSurface _surface;
- EGLContext _context;
- EGLDisplay _display;
- android_app* _app;
- int _width;
- int _height;
部分參數(shù)說明:
_surface:用于繪制圖形,相當(dāng)于windows繪圖中的位圖
_context:可以看做是opengl對(duì)象
_display:用于繪圖的設(shè)備上下文,類似于windows繪圖中的dc
2.2 構(gòu)造函數(shù)說明
- CELLAndroidApp(android_app* app):_app(app)
- {
- _surface = 0;
- _context = 0;
- _display = 0;
- _width = 64;
- _height = 48;
- app->userData = this; //用戶數(shù)據(jù)
- app->onAppCmd = handle_cmd; //窗口的創(chuàng)建銷毀等
- app->onInputEvent = handle_input; //回調(diào)函數(shù)
- }
值得注意的是,這里的app中的userData,傳入用戶數(shù)據(jù),這里直接傳入this,onAppCmd傳入的handle_cmd回調(diào)函數(shù),onInputEvent傳入的事handle_input回調(diào)函數(shù)
2.3 類中函數(shù)main()說明
- virtual void main(int argc,char** argv)
- {
- int ident;
- int events;
- android_poll_source* source;
- while (true)
- {
- while ((ident = ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0)
- {
- if (source != NULL)
- source->process(_app, source); //有觸摸事件,調(diào)用input函數(shù),相當(dāng)于dispatchmessage
- if (_app->destroyRequested != 0)
- return;
- }
- render();
- }
- }
其中的android_poll_source相當(dāng)于windows中的消息隊(duì)列,用于存放消息,這個(gè)函數(shù)中模擬了windows中的消息機(jī)制。
ALooper_pollAll()函數(shù),用于獲取消息。值得注意的是第一個(gè)參數(shù),如果第一個(gè)參數(shù)傳入0,則不等待,調(diào)用后直接返回,類似于windows消息機(jī)制中的pickMessage()函數(shù),如果傳入-1,則類似于windows消息機(jī)制中的SendMessage()函數(shù)。 返回值:如果返回值大于大于等于0表示獲取到數(shù)據(jù),如果為-1則表示失敗,未獲取到數(shù)據(jù)。
其中發(fā)source如果不為空,則表示有觸摸事件,則調(diào)用process()函數(shù),相當(dāng)于windows中調(diào)用dispatchMessage()函數(shù)。
最后,調(diào)用render()函數(shù),繪制圖形。
2.4 初始化設(shè)備函數(shù)initDevice()
- virtual void initDevice()
- {
- const EGLint attribs[] =
- {
- EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
- EGL_BLUE_SIZE, 8,
- EGL_GREEN_SIZE, 8,
- EGL_RED_SIZE, 8,
- EGL_NONE
- };
- EGLint format;
- EGLint numConfigs;
- _display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
- eglInitialize(_display, 0, 0);
- eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);
- eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);
- ANativeWindow_setBuffersGeometry(_app->window, 0, 0, format);
- _surface = eglCreateWindowSurface(_display, _config, _app->window, NULL);
- #if 0
- EGLint contextAtt[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
- _context = eglCreateContext(_display, _config, 0, contextAtt);
- #else
- _context = eglCreateContext(_display, _config, 0, 0);
- #endif
- if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE)
- {
- LOGW("Unable to eglMakeCurrent");
- return;
- }
- eglQuerySurface(_display, _surface, EGL_WIDTH, &_width);
- eglQuerySurface(_display, _surface, EGL_HEIGHT, &_height);
- onCreate();
- // Initialize GL state.
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
- glEnable(GL_CULL_FACE);
- glShadeModel(GL_SMOOTH);
- glDisable(GL_DEPTH_TEST);
- glViewport(0,0,_width,_height);
- glOrthof(0,_width,_height,0,-100,100);
- }
首先需要說明的是attribs數(shù)組,改數(shù)組中主要存儲(chǔ)了繪制圖形的一些屬性信息,他們是成對(duì)出現(xiàn)的,如EGL_SURFACE_TYPE則表示繪制圖形類型, EGL_WINDOW_BIT則表示繪制到窗口上。
eglGetDisplay()函數(shù):表示獲取一個(gè)顯示設(shè)備
eglInitialize():表示初始化獲取到的顯示設(shè)備
eglChooseConfig():繪制屬性的配置
eglGetConfigAttrib():設(shè)置繪制格式
ANativeWindow_setBuffersGeometry():將格式應(yīng)用到窗口
eglCreateWindowSurface():創(chuàng)建繪圖窗口
eglCreateContext():創(chuàng)建opengl的繪圖上下文
eglMakeCurrent():綁定到繪圖設(shè)備上下文
eglQuerySurface():獲取圖片的寬度和高度,具體獲取哪一個(gè)根據(jù)最后一個(gè)參數(shù)確定
glHint()、glEnable()和glOrthof()等函數(shù)則是與繪圖的投影相關(guān)的內(nèi)容,包括初始化、設(shè)置模式等內(nèi)容。
#p#
2.5 繪制函數(shù)render()
- virtual void render()
- {
- if(_display == 0)
- {
- return;
- }
- glClearColor(0,0,0, 1);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnableClientState(GL_VERTEX_ARRAY);
- if(g_arVertex.size() >= 2)
- {
- glColor4f(1,1,1,1);
- glVertexPointer(3,GL_FLOAT,0,&g_arVertex[0]);
- glDrawArrays(GL_LINE_STRIP,0,g_arVertex.size());
- }
- eglSwapBuffers(_display,_surface); //雙緩存的交換緩沖區(qū)
- }
render()函數(shù)主要用于繪制點(diǎn),對(duì)主要的幾個(gè)函數(shù)做如下說明:
glClearColor():用于將屏幕清為黑色
glClear():清空顏色緩沖區(qū)
glEnableClientState():?jiǎn)?dòng)定點(diǎn)數(shù)組
glVertexPointer():制定定點(diǎn)緩沖區(qū)
glDrawArrays():繪制點(diǎn)數(shù)組
eglSwapBuffers():類似雙緩存的交換緩沖區(qū)
2.6 handle_cmd()函數(shù)
- static void handle_cmd(android_app* app, int32_t cmd)
- {
- CELLAndroidApp* pThis = (CELLAndroidApp*)app->userData;
- pThis->cmd(app,cmd);
- }
2.7 handle_input()函數(shù)
- static void handle_input(android_app* app, AInputEvent* event)
- {
- CELLAndroidApp* pThis = (CELLAndroidApp*)app->userData;
- pThis->input(app,event);
- }
2.8 input()函數(shù)
- virtual int input(struct android_app* app, AInputEvent* event)
- {
- int32_t evtType = AInputEvent_getType(event);
- switch(evtType)
- {
- case AINPUT_EVENT_TYPE_KEY:
- break;
- case AINPUT_EVENT_TYPE_MOTION:
- {
- int32_t sourceId = AInputEvent_getSource(event);
- if(AINPUT_SOURCE_TOUCHSCREEN == sourceId)
- {
- int32_t id = AMotionEvent_getAction(event);
- switch(id)
- {
- case AMOTION_EVENT_ACTION_MOVE:
- {
- size_t cnt = AMotionEvent_getPointerCount(event);
- for( int i = 0 ;i < cnt; ++ i )
- {
- float x = AMotionEvent_getX(event,i);
- float y = AMotionEvent_getY(event,i);
- float3 pt;
- pt.x = x;
- pt.y = y;
- pt.z = 0;
- g_arVertex.push_back(pt);
- }
- }
- break;
- case AMOTION_EVENT_ACTION_DOWN:
- {
- size_t cnt = AMotionEvent_getPointerCount(event);
- for( int i = 0 ;i < cnt; ++ i )
- {
- float x = AMotionEvent_getX(event,i);
- float y = AMotionEvent_getY(event,i);
- }
- }
- break;
- case AMOTION_EVENT_ACTION_UP:
- {
- size_t cnt = AMotionEvent_getPointerCount(event);
- for( int i = 0 ;i < cnt; ++ i )
- {
- float x = AMotionEvent_getX(event,i);
- float y = AMotionEvent_getY(event,i);
- }
- }
- break;
- }
- }
- else if(AINPUT_SOURCE_TRACKBALL == sourceId)
- {
- }
- }
- break;
- }
- return 0;
- }
該函數(shù)主要用于對(duì)輸入進(jìn)行判斷,以確定是吉鍵盤、鼠標(biāo)或遙感等,根據(jù)具體輸入做相應(yīng)的操縱,這里就不再做過多的說明
AMotionEvent_getPointerCount():如果是多點(diǎn)觸控,則將各個(gè)點(diǎn)保存到vector中。
2.9 cmd()函數(shù)
- virtual int cmd(struct android_app* app, int32_t cmd)
- {
- switch(cmd)
- {
- case APP_CMD_SAVE_STATE:
- break;
- case APP_CMD_INIT_WINDOW:
- initDevice();
- break;
- case APP_CMD_TERM_WINDOW:
- shutDownDevice();
- break;
- case APP_CMD_GAINED_FOCUS:
- break;
- case APP_CMD_LOST_FOCUS:
- break;
- }
- return 0;
- }
根據(jù)傳入的命令,對(duì)窗口做相應(yīng)的處理。
APP_CMD_INIT_WINDOW:表示初始化窗口
2.10 shutDownDevice()函數(shù)
- virtual void shutDownDevice()
- {
- onDestroy();
- if (_display != EGL_NO_DISPLAY)
- {
- eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
- if (_context != EGL_NO_CONTEXT)
- {
- eglDestroyContext(_display, _context);
- }
- if (_surface != EGL_NO_SURFACE)
- {
- eglDestroySurface(_display, _surface);
- }
- eglTerminate(_display);
- }
- _display = EGL_NO_DISPLAY;
- _context = EGL_NO_CONTEXT;
- _surface = EGL_NO_SURFACE;
- }
關(guān)閉設(shè)備,主要是將與設(shè)備相關(guān)的綁定清除,釋放綁定。
編寫完上邊的代碼后,編譯程序,將程序?qū)氲侥M器中,最終運(yùn)行的效果圖如下:
好了,該說的基本都說玩了,下邊附上源碼地址,趕緊去嘗試吧: