No Description
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.

engine.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "engine.h"
  24. #include "platform.h"
  25. #include "graphics.h"
  26. #include "input.h"
  27. #include "configmanager.h"
  28. #include "gamestate.h"
  29. Engine g_engine;
  30. Engine::Engine() :
  31. m_stopping(false),
  32. m_running(false),
  33. m_currentState(NULL)
  34. {
  35. }
  36. Engine::~Engine()
  37. {
  38. }
  39. void Engine::init()
  40. {
  41. Platform::init();
  42. int width = g_config.getInteger("width");
  43. int height = g_config.getInteger("height");
  44. // create the window
  45. Platform::createWindow(0, 0, width, height, 550, 450, false);
  46. Platform::setWindowTitle(APP_NAME);
  47. Platform::setVsync();
  48. // initialize graphics stuff
  49. g_graphics.init();
  50. // finally show the window
  51. onResize(width, height);
  52. Platform::showWindow();
  53. //Platform::hideMouseCursor();
  54. }
  55. void Engine::terminate()
  56. {
  57. changeState(NULL);
  58. // save configs
  59. g_config.setValue("width", Platform::getWindowWidth());
  60. g_config.setValue("height", Platform::getWindowHeight());
  61. Platform::showMouseCursor();
  62. Platform::terminate();
  63. g_graphics.terminate();
  64. }
  65. void Engine::run()
  66. {
  67. unsigned long ticks;
  68. static unsigned long lastFrameTicks;
  69. m_running = true;
  70. lastFrameTicks = Platform::getTicks();
  71. while(!m_stopping) {
  72. // fire platform events
  73. Platform::poll();
  74. // update
  75. ticks = Platform::getTicks();
  76. update(ticks - lastFrameTicks);
  77. lastFrameTicks = ticks;
  78. // render
  79. render();
  80. // swap buffers
  81. Platform::swapBuffers();
  82. }
  83. lastFrameTicks = 0;
  84. m_stopping = false;
  85. m_running = false;
  86. }
  87. void Engine::stop()
  88. {
  89. m_stopping = true;
  90. }
  91. void Engine::changeState(GameState* newState)
  92. {
  93. if(m_currentState)
  94. m_currentState->onLeave();
  95. m_currentState = newState;
  96. if(m_currentState)
  97. m_currentState->onEnter();
  98. }
  99. void Engine::render()
  100. {
  101. g_graphics.beginRender();
  102. if(m_currentState)
  103. m_currentState->render();
  104. g_graphics.endRender();
  105. }
  106. void Engine::update(int elapsedTicks)
  107. {
  108. if(m_currentState)
  109. m_currentState->update(elapsedTicks);
  110. }
  111. void Engine::onClose()
  112. {
  113. if(m_currentState)
  114. m_currentState->onClose();
  115. }
  116. void Engine::onResize(int width, int height)
  117. {
  118. g_graphics.resize(width, height);
  119. }
  120. void Engine::onInputEvent(InputEvent *event)
  121. {
  122. if(m_currentState)
  123. m_currentState->onInputEvent(event);
  124. }