NativeWindowAndroid.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Core/NativeWindowAndroid.h>
  6. namespace anki {
  7. Error NativeWindow::newInstance(const NativeWindowInitInfo& initInfo, NativeWindow*& nativeWindow)
  8. {
  9. HeapAllocator<U8> alloc(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData);
  10. NativeWindowAndroid* sdlwin = alloc.newInstance<NativeWindowAndroid>();
  11. sdlwin->m_alloc = alloc;
  12. const Error err = sdlwin->init(initInfo);
  13. if(err)
  14. {
  15. alloc.deleteInstance(sdlwin);
  16. nativeWindow = nullptr;
  17. return err;
  18. }
  19. else
  20. {
  21. nativeWindow = sdlwin;
  22. return Error::NONE;
  23. }
  24. }
  25. void NativeWindow::deleteInstance(NativeWindow* window)
  26. {
  27. if(window)
  28. {
  29. NativeWindowAndroid* self = static_cast<NativeWindowAndroid*>(window);
  30. HeapAllocator<U8> alloc = self->m_alloc;
  31. self->~NativeWindowAndroid();
  32. alloc.getMemoryPool().free(self);
  33. }
  34. }
  35. NativeWindowAndroid::~NativeWindowAndroid()
  36. {
  37. ANKI_CORE_LOGI("Destroying Android window");
  38. ANativeActivity_finish(g_androidApp->activity);
  39. // Loop until destroyRequested is set
  40. while(!g_androidApp->destroyRequested)
  41. {
  42. int ident;
  43. int events;
  44. android_poll_source* source;
  45. while((ident = ALooper_pollAll(0, nullptr, &events, reinterpret_cast<void**>(&source))) >= 0)
  46. {
  47. if(source != nullptr)
  48. {
  49. source->process(g_androidApp, source);
  50. }
  51. }
  52. }
  53. m_nativeWindow = nullptr;
  54. }
  55. Error NativeWindowAndroid::init(const NativeWindowInitInfo& init)
  56. {
  57. ANKI_CORE_LOGI("Initializing Android window");
  58. // Loop until the window is ready
  59. while(g_androidApp->window == nullptr)
  60. {
  61. int ident;
  62. int events;
  63. android_poll_source* source;
  64. const int timeoutMs = 5;
  65. while((ident = ALooper_pollAll(timeoutMs, nullptr, &events, reinterpret_cast<void**>(&source))) >= 0)
  66. {
  67. if(source != nullptr)
  68. {
  69. source->process(g_androidApp, source);
  70. }
  71. }
  72. }
  73. m_nativeWindow = g_androidApp->window;
  74. // Set some stuff
  75. m_width = ANativeWindow_getWidth(g_androidApp->window);
  76. m_height = ANativeWindow_getHeight(g_androidApp->window);
  77. return Error::NONE;
  78. }
  79. void NativeWindow::setWindowTitle(CString title)
  80. {
  81. // Nothing
  82. }
  83. } // end namespace anki