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.
 
 
 

172 lines
4.4 KiB

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