PageRenderTime 73ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/SpaceWings/src/Phoenix/Root.cpp

https://github.com/lesnip3r/ICT207-Project
C++ | 182 lines | 135 code | 32 blank | 15 comment | 3 complexity | 2c69ec8f2b5abb05231d4f7894035848 MD5 | raw file
  1. /*
  2. * Root.cpp
  3. *
  4. * The root class object
  5. *
  6. * Created by Julien Lebot on 23/09/09.
  7. * Copyright 2009 Julien Lebot. All rights reserved.
  8. *
  9. */
  10. #include <Phoenix/Root.hpp>
  11. using namespace Phoenix;
  12. // The list of windows
  13. WindowPtr windows[256];
  14. // Instantiate the static members to avoid linking errors
  15. int Phoenix::Root::mCurrentIdleWindow = 0;
  16. bool Phoenix::Root::mIdleFuncEnabled = false;
  17. template<> Root* Singleton<Phoenix::Root>::mInstance = NULL;
  18. template<> bool Singleton<Phoenix::Root>::mDestroyed = false;
  19. #if defined(_THREAD_SUPPORT)
  20. template<> boost::recursive_mutex Singleton<Phoenix::Root>::singletonMutex;
  21. #endif
  22. bool Root::initialize(const std::string& logFile,
  23. const std::string& cfgFile)
  24. {
  25. // Dummy argv/argv pair
  26. int argc = 1;
  27. char *argv = "";
  28. glutInit(&argc, &argv);
  29. LogManager::instance().setDefaultLog(logFile);
  30. Log::Stream str = LogManager::instance().getDefaultLog()->prepareForStreaming();
  31. str << _PROJECT_NAME_ << " initializing ...";
  32. str.flush();
  33. str << "version " << _VERSION_MAJOR_ << "." << _VERSION_MINOR_ << "." << _VERSION_PATCH_ << " (" << _VERSION_NAME_ << ")";
  34. str.flush();
  35. str << "compiled with " << _COMPILER_STR_ << " version " << _COMPILER_VER_ << " for " << _PLATFORM_STR_ << " " << _ARCH_TYPE_STR_ << "bits (" << __TIMESTAMP__ << ")";
  36. str.flush();
  37. str << "Detected " << mCPUInfo.getNumLogicalCPUs() << " logical CPU core(s).";
  38. str.flush();
  39. str << mCPUInfo.getCPUString();
  40. str.flush();
  41. str << "Features: " << mCPUInfo.featuresAsString();
  42. str.flush();
  43. mInitDone = true;
  44. return true;
  45. }
  46. void Root::enterMainLoop()
  47. {
  48. glutMainLoop();
  49. }
  50. void Root::addWindow(WindowPtr window, bool autoConfigure)
  51. {
  52. // Make sure the window is initialized or face a core dump
  53. if (!window->isInitialised())
  54. window->initialize();
  55. // Store the window in our window list
  56. windows[window->getHandle()] = window;
  57. // These need to be called every time a window is added
  58. glutDisplayFunc(CallBackDisplayFunc);
  59. glutIdleFunc(CallBackIdleFunc);
  60. glutKeyboardUpFunc(CallBackKeyboardUpFunc);
  61. glutKeyboardFunc(CallBackKeyboardFunc);
  62. glutSpecialUpFunc(CallBackSpecialUpFunc);
  63. glutSpecialFunc(CallBackSpecialFunc);
  64. glutMouseFunc(CallBackMouseFunc);
  65. glutMotionFunc(CallBackMotionFunc);
  66. glutPassiveMotionFunc(CallBackPassiveMotionFunc);
  67. glutReshapeFunc(CallBackReshapeFunc);
  68. glutVisibilityFunc(CallBackVisibilityFunc);
  69. LogManager::instance().getDefaultLog()->prepareForStreaming() << "Added window \"" << window->getName() << "\" (" << window->getResolution() << ")";
  70. }
  71. void Root::shutdown()
  72. {
  73. LogManager::instance().getDefaultLog()->write("Shuting down...");
  74. }
  75. bool Root::isInitialised()
  76. {
  77. return mInitDone;
  78. }
  79. Root::Root()
  80. {
  81. }
  82. Root::~Root()
  83. {
  84. }
  85. void Root::CallBackDisplayFunc(void)
  86. {
  87. int windowID = glutGetWindow();
  88. windows[windowID]->onDisplay();
  89. }
  90. void Root::CallBackIdleFunc(void)
  91. {
  92. if(mIdleFuncEnabled && mCurrentIdleWindow)
  93. {
  94. glutSetWindow(mCurrentIdleWindow);
  95. windows[mCurrentIdleWindow]->onIdle();
  96. }
  97. }
  98. void Root::CallBackKeyboardFunc(unsigned char key, int x, int y)
  99. {
  100. int windowID = glutGetWindow();
  101. windows[windowID]->onKeyboard(key, x, y);
  102. }
  103. void Root::CallBackKeyboardUpFunc(unsigned char key, int x, int y)
  104. {
  105. int windowID = glutGetWindow();
  106. windows[windowID]->onKeyboardUp(key, x, y);
  107. }
  108. void Root::CallBackMotionFunc(int x, int y)
  109. {
  110. int windowID = glutGetWindow();
  111. windows[windowID]->onMotion(x, y);
  112. }
  113. void Root::CallBackMouseFunc(int button, int state, int x, int y)
  114. {
  115. int windowID = glutGetWindow();
  116. windows[windowID]->onMouseEvent(button, state, x, y);
  117. }
  118. void Root::CallBackPassiveMotionFunc(int x, int y)
  119. {
  120. int windowID = glutGetWindow();
  121. windows[windowID]->onPassiveMotion(x, y);
  122. }
  123. void Root::CallBackReshapeFunc(int w, int h)
  124. {
  125. int windowID = glutGetWindow();
  126. windows[windowID]->onReshape(w, h);
  127. }
  128. void Root::CallBackSpecialFunc(int key, int x, int y)
  129. {
  130. int windowID = glutGetWindow();
  131. windows[windowID]->onSpecialKey(key, x, y);
  132. }
  133. void Root::CallBackSpecialUpFunc(int key, int x, int y)
  134. {
  135. int windowID = glutGetWindow();
  136. windows[windowID]->onSpecialKeyUp(key, x, y);
  137. }
  138. void Root::CallBackVisibilityFunc(int visible)
  139. {
  140. int windowID = glutGetWindow();
  141. windows[windowID]->onVisibility(visible ? true : false);
  142. }
  143. void Root::setIdle(bool enabled)
  144. {
  145. mIdleFuncEnabled = enabled;
  146. }
  147. void Root::setIdle2ActiveWindow(void)
  148. {
  149. mCurrentIdleWindow = glutGetWindow();
  150. }