How to use pure C ++ (ndk) to develop Android applications? (1)

Source: Internet
Author: User

I will not describe how to install the android development environment and how to set ndk environment variables in front of the article, if any environment is not installed or set up, please refer to the content set up in the android environment first.

Good. If ndk is installed and Android is developed using pure c ++, the detailed steps and instructions are as follows:

1. Compile the entry function

Android_main is the entry function, which is the same as the main function in C ++. Create the CELLAndroidApp object and directly call the main function.

 
 
  1. void android_main(struct android_app* state)  
  2. {  
  3.     CELLAndroidApp    app(state);  
  4.  
  5.        app.main(0,0);  
  6. }  

Note: CELLAndroidApp is a graphic drawing class we designed. It will be described in detail later.

2. Implementation of the drawing class

2.1 class member description

 
 
  1. protected:  
  2.     EGLConfig        _config;  
  3.     EGLSurface       _surface;  
  4.     EGLContext       _context;  
  5.     EGLDisplay       _display;  
  6.     android_app*     _app;  
  7.     int              _width;  
  8.     int              _height; 

Some parameter descriptions:

_ Surface: used to draw a graph, which is equivalent to a bitmap in windows plotting.

_ Context: can be viewed as an opengl object.

_ Display: The device context used for drawing, similar to the dc in windows plotting

2.2 constructor description

 
 
  1. CELLAndroidApp (android_app * app): _ app (app)
  2. {
  3. _ Surface = 0;
  4. _ Context = 0;
  5. _ Display = 0;
  6. _ Width = 64;
  7. _ Height = 48;
  8. App-> userData = this; // user data
  9. App-> onAppCmd = handle_cmd; // window creation and destruction
  10. App-> onInputEvent = handle_input; // callback function
  11. }

It is worth noting that the app's userData here is used to pass in user data. Here, this is passed in directly, the handle_cmd callback function passed in by onAppCmd, And the handle_input callback function passed in by onInputEvent.

Main () Description of the class

 
 
  1. Virtual void main (int argc, char ** argv)
  2. {
  3. Int ident;
  4. Int events;
  5. Android_poll_source * source;
  6.  
  7. While (true)
  8. {
  9. While (ident = ALooper_pollAll (0, NULL, & events, (void **) & source)> = 0)
  10. {
  11. If (source! = NULL)
  12. Source-> process (_ app, source); // There is a touch event that calls the input function, which is equivalent to dispatchmessage
  13.  
  14. If (_ app-> destroyRequested! = 0)
  15. Return;
  16. }
  17. Render ();
  18. }
  19. }

Android_poll_source is equivalent to a message queue in windows used to store messages. This function simulates the message mechanism in windows.

The ALooper_pollAll () function is used to obtain messages. It is worth noting that the first parameter, if the first parameter is passed in 0, will not wait, and will be returned directly after the call, similar to the pickMessage () function in windows message mechanism. If-1 is passed in, it is similar to the SendMessage () function in windows message mechanism. Return Value: If the returned value is greater than or equal to 0, the data is obtained. If the returned value is-1, the data is failed and not obtained.

If the source is not empty, it indicates there is a touch event, and the process () function is called, which is equivalent to the dispatchMessage () function in windows.

Finally, call the render () function to draw a graph.

2.4 device initialization function initDevice ()

 
 
  1. virtual void     initDevice()  
  2.     {  
  3.         const EGLint attribs[] =  
  4.         {  
  5.             EGL_SURFACE_TYPE, EGL_WINDOW_BIT,  
  6.             EGL_BLUE_SIZE, 8,   
  7.             EGL_GREEN_SIZE, 8,  
  8.             EGL_RED_SIZE, 8,  
  9.             EGL_NONE  
  10.         };  
  11.         EGLint     format;  
  12.         EGLint    numConfigs;  
  13.  
  14.         _display    =    eglGetDisplay(EGL_DEFAULT_DISPLAY);  
  15.  
  16.         eglInitialize(_display, 0, 0);  
  17.  
  18.         eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);   
  19.  
  20.         eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);  
  21.  
  22.         ANativeWindow_setBuffersGeometry(_app->window, 0, 0, format);   
  23.  
  24.         _surface    =     eglCreateWindowSurface(_display, _config, _app->window, NULL);  
  25.  
  26. #if 0  
  27.         EGLint contextAtt[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };  
  28.  
  29.         _context     =     eglCreateContext(_display, _config, 0, contextAtt);  
  30. #else  
  31.         _context     =     eglCreateContext(_display, _config, 0, 0);   
  32. #endif  
  33.  
  34.         if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE)  
  35.         {  
  36.             LOGW("Unable to eglMakeCurrent");   
  37.             return;  
  38.         }  
  39.  
  40.         eglQuerySurface(_display, _surface, EGL_WIDTH, &_width);   
  41.         eglQuerySurface(_display, _surface, EGL_HEIGHT, &_height);  
  42.  
  43.         onCreate();  
  44.  
  45.         // Initialize GL state.  
  46.         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);   
  47.         glEnable(GL_CULL_FACE);  
  48.         glShadeModel(GL_SMOOTH);  
  49.         glDisable(GL_DEPTH_TEST);  
  50.         glViewport(0,0,_width,_height);  
  51.         glOrthof(0,_width,_height,0,-100,100);  
  52.     } 

The first thing to note is the attribs array, which mainly stores some attribute information of the drawing image. They appear in pairs. For example, EGL_SURFACE_TYPE indicates the drawing type, EGL_WINDOW_BIT indicates that it is drawn to the window.

EglGetDisplay () function: obtains a display device.

EglInitialize (): indicates the display device obtained during initialization.

EglChooseConfig (): configure the painting attributes

EglGetConfigAttrib (): sets the drawing format.

ANativeWindow_setBuffersGeometry (): Apply the format to the window

EglCreateWindowSurface (): Creates a drawing window.

EglCreateContext (): Creates the drawing context of opengl.

EglMakeCurrent (): bound to the drawing device context

EglQuerySurface (): Get the width and height of the image. Determine which one is obtained based on the last parameter.

GlHint (), glable (), glOrthof () and other functions are related to the projection of the drawing, including initialization, setting mode, and so on.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.