maain.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "TestApp.h"
  2. #include "ArenaScene.h"
  3. using namespace Crown;
  4. /*
  5. * Copyright (C) 2010 The Android Open Source Project
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. */
  20. //BEGIN_INCLUDE(all)
  21. #include <jni.h>
  22. #include <errno.h>
  23. #include <EGL/egl.h>
  24. #include <GLES/gl.h>
  25. #include <android/sensor.h>
  26. #include <android/log.h>
  27. #include <android_native_app_glue.h>
  28. #include "Crown.h"
  29. using namespace Crown;
  30. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
  31. #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
  32. static void foobar()
  33. {
  34. ArenaScene* arena = new ArenaScene(800, 480);
  35. GetDevice()->GetSceneManager()->SelectNextScene(arena);
  36. }
  37. /**
  38. * Process the next main command.
  39. */
  40. static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
  41. switch (cmd) {
  42. case APP_CMD_SAVE_STATE:
  43. // The system has asked us to save our current state. Do so.
  44. break;
  45. case APP_CMD_INIT_WINDOW:
  46. // The window is being shown, get it ready.
  47. GetDevice()->Init(480, 800, 32, true);
  48. foobar();
  49. break;
  50. case APP_CMD_TERM_WINDOW:
  51. // The window is being hidden or closed, clean it up.
  52. GetDevice()->StopRunning();
  53. GetDevice()->Shutdown();
  54. break;
  55. case APP_CMD_GAINED_FOCUS:
  56. // When our app gains focus, we start monitoring the accelerometer.
  57. GetDevice()->StartRunning();
  58. break;
  59. case APP_CMD_LOST_FOCUS:
  60. // When our app loses focus, we stop monitoring the accelerometer.
  61. // This is to avoid consuming battery while not being used.
  62. GetDevice()->StopRunning();
  63. // Also stop animating.
  64. break;
  65. }
  66. }
  67. /**
  68. * This is the main entry point of a native application that is using
  69. * android_native_app_glue. It runs in its own thread, with its own
  70. * event loop for receiving input events and doing other things.
  71. */
  72. void android_main(struct android_app* state) {
  73. // Make sure glue isn't stripped.
  74. app_dummy();
  75. LOGI("Entrato in android_main!!!");
  76. GetDevice()->_InitAndroidApp(state);
  77. state->userData = NULL;
  78. state->onAppCmd = NULL; // Manual cmd handling
  79. state->onInputEvent = NULL; // Manual input handling
  80. // loop waiting for stuff to do.
  81. while (1) {
  82. int ident;
  83. int events;
  84. struct android_poll_source* source;
  85. struct android_app* app = state;
  86. { //////////////////////////////////////
  87. if ((ident=ALooper_pollOnce(0, NULL, &events,
  88. (void**)&source)) >= 0) {
  89. if (source != NULL)
  90. {
  91. if (source->id == LOOPER_ID_MAIN)
  92. {
  93. //LOGI("Entrato in LOOPER_ID_MAIN");
  94. int8_t cmd = android_app_read_cmd(app);
  95. android_app_pre_exec_cmd(app, cmd);
  96. engine_handle_cmd(app, cmd);
  97. android_app_post_exec_cmd(app, cmd);
  98. //LOGI("Uscito da LOOPER_ID_MAIN");
  99. }
  100. }
  101. //LOGI("Uscito da source != NULL");
  102. }
  103. } ///////////////////////////////////
  104. // Check if we are exiting.
  105. if (state->destroyRequested != 0) {
  106. exit(0);
  107. }
  108. // Drawing is throttled to the screen update rate, so there
  109. // is no need to do timing here.
  110. if (GetDevice()->IsInit())
  111. {
  112. if (GetDevice()->IsRunning())
  113. {
  114. GetDevice()->GetInputManager()->GetTouch()->EventLoop();
  115. GetDevice()->GetRenderer()->_BeginFrame();
  116. GetDevice()->GetSceneManager()->UpdateScene(0.02);
  117. GetDevice()->GetSceneManager()->RenderScene();
  118. GetDevice()->GetRenderer()->_EndFrame();
  119. GetDevice()->GetMainWindow()->Update();
  120. }
  121. }
  122. }
  123. Log::D("Crown: bye bye.");
  124. }
  125. //END_INCLUDE(all)