bgfx/examples/common/entry/entry_android.cpp

244 lines
5.9 KiB
C++
Raw Normal View History

2013-04-16 02:10:32 -04:00
/*
2014-02-11 01:07:04 -05:00
* Copyright 2011-2014 Branimir Karadzic. All rights reserved.
2013-04-16 02:10:32 -04:00
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
2013-08-15 00:08:46 -04:00
#include "entry_p.h"
2013-04-16 02:10:32 -04:00
2013-08-15 00:08:46 -04:00
#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID
2013-04-16 02:10:32 -04:00
2013-08-08 02:11:20 -04:00
#include <bgfxplatform.h>
2013-04-19 00:16:09 -04:00
2013-04-16 02:10:32 -04:00
#include <stdio.h>
2013-04-19 00:16:09 -04:00
#include <bx/thread.h>
#include <android/input.h>
#include <android/log.h>
#include <android/looper.h>
#include <android/window.h>
#include <android_native_app_glue.h>
2013-04-19 00:16:09 -04:00
extern "C"
{
#include <android_native_app_glue.c>
} // extern "C"
2013-04-16 02:10:32 -04:00
namespace entry
{
2013-04-19 00:16:09 -04:00
struct MainThreadEntry
{
int m_argc;
char** m_argv;
static int32_t threadFunc(void* _userData);
};
struct Context
{
Context()
: m_window(NULL)
2013-04-19 00:16:09 -04:00
{
}
void run(android_app* _app)
{
m_app = _app;
m_app->userData = (void*)this;
m_app->onAppCmd = onAppCmdCB;
m_app->onInputEvent = onInputEventCB;
const char* argv[1] = { "android.so" };
m_mte.m_argc = 1;
m_mte.m_argv = const_cast<char**>(argv);
2013-04-19 00:16:09 -04:00
while (0 == m_app->destroyRequested)
{
int32_t num;
android_poll_source* source;
/*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
if (NULL != source)
{
source->process(m_app, source);
}
}
m_thread.shutdown();
2013-04-19 00:16:09 -04:00
}
void onAppCmd(int32_t _cmd)
{
switch (_cmd)
{
case APP_CMD_INPUT_CHANGED:
// Command from main thread: the AInputQueue has changed. Upon processing
// this command, android_app->inputQueue will be updated to the new queue
// (or NULL).
break;
case APP_CMD_INIT_WINDOW:
// Command from main thread: a new ANativeWindow is ready for use. Upon
// receiving this command, android_app->window will contain the new window
// surface.
if (m_window == NULL)
{
m_window = m_app->window;
bgfx::androidSetWindow(m_app->window);
m_thread.init(MainThreadEntry::threadFunc, &m_mte);
}
2013-04-19 00:16:09 -04:00
break;
case APP_CMD_TERM_WINDOW:
// Command from main thread: the existing ANativeWindow needs to be
// terminated. Upon receiving this command, android_app->window still
// contains the existing window; after calling android_app_exec_cmd
// it will be set to NULL.
break;
case APP_CMD_WINDOW_RESIZED:
// Command from main thread: the current ANativeWindow has been resized.
// Please redraw with its new size.
break;
case APP_CMD_WINDOW_REDRAW_NEEDED:
// Command from main thread: the system needs that the current ANativeWindow
// be redrawn. You should redraw the window before handing this to
// android_app_exec_cmd() in order to avoid transient drawing glitches.
break;
case APP_CMD_CONTENT_RECT_CHANGED:
// Command from main thread: the content area of the window has changed,
// such as from the soft input window being shown or hidden. You can
// find the new content rect in android_app::contentRect.
break;
case APP_CMD_GAINED_FOCUS:
// Command from main thread: the app's activity window has gained
// input focus.
break;
case APP_CMD_LOST_FOCUS:
// Command from main thread: the app's activity window has lost
// input focus.
break;
case APP_CMD_CONFIG_CHANGED:
// Command from main thread: the current device configuration has changed.
break;
case APP_CMD_LOW_MEMORY:
// Command from main thread: the system is running low on memory.
// Try to reduce your memory use.
break;
case APP_CMD_START:
// Command from main thread: the app's activity has been started.
break;
case APP_CMD_RESUME:
// Command from main thread: the app's activity has been resumed.
break;
case APP_CMD_SAVE_STATE:
// Command from main thread: the app should generate a new saved state
// for itself, to restore from later if needed. If you have saved state,
// allocate it with malloc and place it in android_app.savedState with
// the size in android_app.savedStateSize. The will be freed for you
// later.
break;
case APP_CMD_PAUSE:
// Command from main thread: the app's activity has been paused.
break;
case APP_CMD_STOP:
// Command from main thread: the app's activity has been stopped.
break;
case APP_CMD_DESTROY:
// Command from main thread: the app's activity is being destroyed,
// and waiting for the app thread to clean up and exit before proceeding.
m_eventQueue.postExitEvent();
break;
}
}
int32_t onInputEvent(AInputEvent* _event)
{
2013-12-02 23:09:03 -05:00
BX_UNUSED(_event);
2013-04-19 00:16:09 -04:00
return 0;
}
static void onAppCmdCB(struct android_app* _app, int32_t _cmd)
{
Context* self = (Context*)_app->userData;
self->onAppCmd(_cmd);
}
static int32_t onInputEventCB(struct android_app* _app, AInputEvent* _event)
{
Context* self = (Context*)_app->userData;
return self->onInputEvent(_event);
}
MainThreadEntry m_mte;
bx::Thread m_thread;
2013-04-19 00:16:09 -04:00
EventQueue m_eventQueue;
ANativeWindow* m_window;
2013-04-19 00:16:09 -04:00
android_app* m_app;
};
static Context s_ctx;
2013-04-16 02:10:32 -04:00
const Event* poll()
{
2013-04-19 00:16:09 -04:00
return s_ctx.m_eventQueue.poll();
2013-04-16 02:10:32 -04:00
}
void release(const Event* _event)
{
2013-04-19 00:16:09 -04:00
s_ctx.m_eventQueue.release(_event);
2013-04-16 02:10:32 -04:00
}
void setWindowSize(uint32_t _width, uint32_t _height)
{
2013-12-02 23:09:03 -05:00
BX_UNUSED(_width, _height);
2013-04-16 02:10:32 -04:00
}
void setWindowTitle(const char* _title)
2014-08-05 15:57:52 -04:00
{
BX_UNUSED(_title);
}
2013-04-16 02:10:32 -04:00
void toggleWindowFrame()
{
}
void setMouseLock(bool _lock)
{
2013-12-02 23:09:03 -05:00
BX_UNUSED(_lock);
2013-04-16 02:10:32 -04:00
}
2013-04-19 00:16:09 -04:00
int32_t MainThreadEntry::threadFunc(void* _userData)
{
int32_t result = chdir("/sdcard/bgfx/examples/runtime");
BX_CHECK(0 == result, "Failed to chdir to dir. android.permission.WRITE_EXTERNAL_STORAGE?", errno);
2013-04-19 00:16:09 -04:00
MainThreadEntry* self = (MainThreadEntry*)_userData;
result = main(self->m_argc, self->m_argv);
2013-04-19 00:16:09 -04:00
// PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
return result;
}
2013-04-16 02:10:32 -04:00
} // namespace entry
2013-04-19 00:16:09 -04:00
extern "C" void android_main(android_app* _app)
2013-04-16 02:10:32 -04:00
{
2013-04-19 00:16:09 -04:00
using namespace entry;
s_ctx.run(_app);
2013-04-16 02:10:32 -04:00
}
2013-08-15 00:08:46 -04:00
#endif // ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID