NativeWindowAndroid.cpp 2.2 KB

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