Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.4KB

  1. #include "RenderWindow.h"
  2. #include "Utilities/Log.h"
  3. using namespace std;
  4. namespace BlueCore
  5. {
  6. static RenderWindow* gRenderWindow = 0;
  7. //------------------------------------------------------------------------------
  8. RenderWindow::RenderWindow() :
  9. _Initialized(false)
  10. {
  11. glfwInit();
  12. gRenderWindow = this;
  13. clog << ">>> RenderWindow constructed..."<< endlog;
  14. }
  15. //------------------------------------------------------------------------------
  16. RenderWindow::~RenderWindow()
  17. {
  18. destroy();
  19. glfwTerminate();
  20. gRenderWindow = 0;
  21. clog << ">>> RenderWindow destructed..."<< endlog;
  22. }
  23. //------------------------------------------------------------------------------
  24. void RenderWindow::WindowRefreshCallback()
  25. {
  26. gRenderWindow->WindowRefreshSignal();
  27. }
  28. //------------------------------------------------------------------------------
  29. int RenderWindow::WindowCloseCallback()
  30. {
  31. clog << ">>> RenderWindow closed..."<< endlog;
  32. gRenderWindow->closeWindow();
  33. return GL_FALSE;
  34. }
  35. //------------------------------------------------------------------------------
  36. void RenderWindow::closeWindow()
  37. {
  38. _Closed = true;
  39. }
  40. //------------------------------------------------------------------------------
  41. void RenderWindow::WindowSizeCallback(int width, int height)
  42. {
  43. gRenderWindow->resizeWindow(width, height);
  44. }
  45. //------------------------------------------------------------------------------
  46. void RenderWindow::resizeWindow(int width, int height)
  47. {
  48. _Resized = true;
  49. if ( !_Initialized)
  50. return;
  51. clog << ">>> RenderWindow resized: "<< width << "x"<< height << endlog;
  52. _Width = width;
  53. _Height = height;
  54. WindowResizeSignal(width, height);
  55. }
  56. //------------------------------------------------------------------------------
  57. int RenderWindow::getWidth()
  58. {
  59. return _Width;
  60. }
  61. //------------------------------------------------------------------------------
  62. int RenderWindow::getHeight()
  63. {
  64. return _Height;
  65. }
  66. //------------------------------------------------------------------------------
  67. bool RenderWindow::create(int width, int height, int pixelbits, int depthbits,
  68. int stencilbits, bool fullscreen)
  69. {
  70. int channelbits = 8;
  71. if (pixelbits == 16)
  72. channelbits = 4;
  73. if (glfwOpenWindow(width, height, channelbits, channelbits, channelbits,
  74. channelbits, depthbits, stencilbits, fullscreen ? GLFW_FULLSCREEN
  75. : GLFW_WINDOW) == GL_FALSE)
  76. return false;
  77. glfwSetWindowSizeCallback(WindowSizeCallback);
  78. glfwSetWindowCloseCallback(WindowCloseCallback);
  79. glfwSetWindowRefreshCallback(WindowRefreshCallback);
  80. glfwSetMousePosCallback(MousePosCallback);
  81. glfwSetMouseButtonCallback(MouseButtonCallback);
  82. glfwSetMouseWheelCallback(MouseWheelCallback);
  83. glfwSetKeyCallback(KeyCallback);
  84. glfwSetCharCallback(CharCallback);
  85. clog << ">>> RenderWindow created"<< endlog;
  86. _Initialized = true;
  87. glfwGetWindowSize( &_Width, &_Height);
  88. resizeWindow(_Width, _Height);
  89. return true;
  90. }
  91. //------------------------------------------------------------------------------
  92. void RenderWindow::destroy()
  93. {
  94. if (_Initialized)
  95. {
  96. glfwCloseWindow();
  97. clog << ">>> RenderWindow destroyed"<< endlog;
  98. _Initialized = false;
  99. }
  100. }
  101. //------------------------------------------------------------------------------
  102. bool RenderWindow::isOpen()
  103. {
  104. if (_Initialized == false)
  105. return false;
  106. if (_Closed)
  107. return false;
  108. return (glfwGetWindowParam(GLFW_OPENED) == GL_TRUE);
  109. }
  110. //------------------------------------------------------------------------------
  111. void RenderWindow::MousePosCallback(int x, int y)
  112. {
  113. gRenderWindow->MouseMoveSignal(x, y);
  114. }
  115. //------------------------------------------------------------------------------
  116. void RenderWindow::MouseButtonCallback(int button, int action)
  117. {
  118. gRenderWindow->MouseButtonSignal(button, action);
  119. }
  120. //------------------------------------------------------------------------------
  121. void RenderWindow::MouseWheelCallback(int pos)
  122. {
  123. gRenderWindow->MouseWheelSignal(pos);
  124. }
  125. //------------------------------------------------------------------------------
  126. void RenderWindow::KeyCallback(int key, int action)
  127. {
  128. gRenderWindow->KeySignal(key, action);
  129. }
  130. //------------------------------------------------------------------------------
  131. void RenderWindow::CharCallback(int character, int action)
  132. {
  133. gRenderWindow->CharSignal(character);
  134. }
  135. } // namespace BlueCore