Changeset 2583


Ignore:
Timestamp:
Mar 12, 2013, 1:13:51 PM (8 years ago)
Author:
sam
Message:

android: refactor the application code to use fewer free functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/platform/android/androidapp.cpp

    r2579 r2583  
    7171{
    7272public:
     73    int CreateDisplay();
     74    void DestroyDisplay();
     75    void DrawFrame();
     76
     77    static void StaticHandleCommand(android_app* native_app, int32_t cmd)
     78    {
     79        return ((AndroidAppData*)native_app->userData)->HandleCommand(cmd);
     80    }
     81
     82    static int32_t StaticHandleInput(android_app* native_app, AInputEvent* ev)
     83    {
     84        return ((AndroidAppData*)native_app->userData)->HandleInput(ev);
     85    }
     86
    7387    android_app* m_native_app;
    7488
    75     EGLDisplay display;
    76     EGLSurface surface;
    77     EGLContext context;
    78 
    7989    SavedState m_state;
     90
     91private:
     92    void HandleCommand(int32_t cmd);
     93    int32_t HandleInput(AInputEvent* event);
     94
     95    EGLDisplay m_display;
     96    EGLSurface m_surface;
     97    EGLContext m_context;
    8098};
    8199
     
    83101 * Initialize an EGL context for the current display.
    84102 */
    85 static int engine_init_display(struct AndroidAppData* engine)
    86 {
    87     /*
    88      * Here specify the attributes of the desired configuration.
    89      * Below, we select an EGLConfig with at least 8 bits per color
    90      * component compatible with on-screen windows
    91      */
     103int lol::AndroidAppData::CreateDisplay()
     104{
     105    /* FIXME: there is a lot of code common to eglapp.cpp here. */
    92106    const EGLint attribs[] =
    93107    {
     
    105119    EGLint numConfigs;
    106120    EGLConfig config;
    107     EGLSurface surface;
    108     EGLContext context;
    109 
    110     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    111 
    112     eglInitialize(display, 0, 0);
    113     eglChooseConfig(display, attribs, &config, 1, &numConfigs);
    114     eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
    115 
    116     ANativeWindow_setBuffersGeometry(engine->m_native_app->window,
     121
     122    m_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
     123
     124    eglInitialize(m_display, 0, 0);
     125    eglChooseConfig(m_display, attribs, &config, 1, &numConfigs);
     126    eglGetConfigAttrib(m_display, config, EGL_NATIVE_VISUAL_ID, &format);
     127
     128    ANativeWindow_setBuffersGeometry(m_native_app->window,
    117129                                     0, 0, format);
    118     surface = eglCreateWindowSurface(display, config,
    119                                      engine->m_native_app->window, nullptr);
     130    m_surface = eglCreateWindowSurface(m_display, config,
     131                                       m_native_app->window, nullptr);
    120132
    121133    EGLint ctxattr[] =
     
    124136        EGL_NONE
    125137    };
    126     context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctxattr);
    127 
    128     if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
     138    m_context = eglCreateContext(m_display, config, EGL_NO_CONTEXT, ctxattr);
     139
     140    if (eglMakeCurrent(m_display, m_surface, m_surface, m_context) == EGL_FALSE)
    129141    {
    130142        Log::Error("unable to eglMakeCurrent");
     
    132144    }
    133145
    134     eglQuerySurface(display, surface, EGL_WIDTH, &w);
    135     eglQuerySurface(display, surface, EGL_HEIGHT, &h);
    136 
    137     engine->display = display;
    138     engine->context = context;
    139     engine->surface = surface;
     146    eglQuerySurface(m_display, m_surface, EGL_WIDTH, &w);
     147    eglQuerySurface(m_display, m_surface, EGL_HEIGHT, &h);
    140148
    141149    /* Launch our ticker */
     
    147155}
    148156
    149 static void engine_draw_frame(AndroidAppData* engine)
    150 {
    151     if (!engine->display)
     157void lol::AndroidAppData::DrawFrame()
     158{
     159    if (!m_display)
    152160        return;
    153161
    154162    Ticker::TickDraw();
    155163
    156     eglSwapBuffers(engine->display, engine->surface);
     164    eglSwapBuffers(m_display, m_surface);
    157165}
    158166
     
    160168 * Tear down the EGL context currently associated with the display.
    161169 */
    162 static void engine_term_display(AndroidAppData* engine)
    163 {
    164     if (engine->display != EGL_NO_DISPLAY)
    165     {
    166         eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    167         if (engine->context != EGL_NO_CONTEXT)
     170void lol::AndroidAppData::DestroyDisplay()
     171{
     172    if (m_display != EGL_NO_DISPLAY)
     173    {
     174        eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
     175        if (m_context != EGL_NO_CONTEXT)
    168176        {
    169             eglDestroyContext(engine->display, engine->context);
     177            eglDestroyContext(m_display, m_context);
    170178        }
    171         if (engine->surface != EGL_NO_SURFACE)
     179        if (m_surface != EGL_NO_SURFACE)
    172180        {
    173             eglDestroySurface(engine->display, engine->surface);
     181            eglDestroySurface(m_display, m_surface);
    174182        }
    175         eglTerminate(engine->display);
    176     }
    177     engine->display = EGL_NO_DISPLAY;
    178     engine->context = EGL_NO_CONTEXT;
    179     engine->surface = EGL_NO_SURFACE;
     183        eglTerminate(m_display);
     184    }
     185    m_display = EGL_NO_DISPLAY;
     186    m_context = EGL_NO_CONTEXT;
     187    m_surface = EGL_NO_SURFACE;
    180188}
    181189
     
    183191 * Process the next input event.
    184192 */
    185 static int32_t engine_handle_input(android_app* native_app, AInputEvent* event)
    186 {
    187     AndroidAppData* engine = (AndroidAppData*)native_app->userData;
     193int32_t lol::AndroidAppData::HandleInput(AInputEvent* event)
     194{
    188195    switch (AInputEvent_getType(event))
    189196    {
     
    208215 * Process the next main command.
    209216 */
    210 static void engine_handle_cmd(android_app* native_app, int32_t cmd)
    211 {
    212     AndroidAppData* engine = (AndroidAppData*)native_app->userData;
     217void lol::AndroidAppData::HandleCommand(int32_t cmd)
     218{
    213219    switch (cmd)
    214220    {
    215221        case APP_CMD_SAVE_STATE:
    216222            /* The system has asked us to save our current state. Do so. */
    217             engine->m_native_app->savedState = malloc(sizeof(SavedState));
    218             *((SavedState*)engine->m_native_app->savedState) = engine->m_state;
    219             engine->m_native_app->savedStateSize = sizeof(SavedState);
     223            m_native_app->savedState = malloc(sizeof(SavedState));
     224            *((SavedState*)m_native_app->savedState) = m_state;
     225            m_native_app->savedStateSize = sizeof(SavedState);
    220226            break;
    221227        case APP_CMD_INIT_WINDOW:
    222228            /* The window is being shown, get it ready. */
    223             if (engine->m_native_app->window != nullptr)
     229            if (m_native_app->window != nullptr)
    224230            {
    225                 engine_init_display(engine);
    226                 engine_draw_frame(engine);
     231                CreateDisplay();
     232                DrawFrame();
    227233            }
    228234            break;
    229235        case APP_CMD_TERM_WINDOW:
    230236            /* The window is being hidden or closed, clean it up. */
    231             engine_term_display(engine);
     237            DestroyDisplay();
    232238            break;
    233239        case APP_CMD_GAINED_FOCUS:
     
    235241        case APP_CMD_LOST_FOCUS:
    236242            /* FIXME: stop animating */
    237             engine_draw_frame(engine);
     243            DrawFrame();
    238244            break;
    239245    }
     
    246252{
    247253    g_data = new AndroidAppData();
     254    g_data->m_native_app = native_app;
    248255
    249256    /* Make sure glue isn't stripped */
     
    251258
    252259    native_app->userData = g_data;
    253     native_app->onAppCmd = engine_handle_cmd;
    254     native_app->onInputEvent = engine_handle_input;
    255     g_data->m_native_app = native_app;
     260    native_app->onAppCmd = lol::AndroidAppData::StaticHandleCommand;
     261    native_app->onInputEvent = lol::AndroidAppData::StaticHandleInput;
    256262
    257263    if (native_app->savedState != nullptr)
     
    283289lol::AndroidApp::~AndroidApp()
    284290{
    285     engine_term_display(m_data);
     291    m_data->DestroyDisplay();
    286292    delete m_data;
    287293}
     
    308314    }
    309315
    310     engine_draw_frame(m_data);
     316    m_data->DrawFrame();
    311317}
    312318
Note: See TracChangeset for help on using the changeset viewer.