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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "const.h"
  27. #include "input.h"
  28. #include "configmanager.h"
  29. #include "logger.h"
  30. #include "gamestate.h"
  31. Engine g_engine;
  32. Engine::Engine() :
  33. m_stopping(false),
  34. m_running(false),
  35. m_lastFrameTicks(0),
  36. m_currentState(NULL)
  37. {
  38. }
  39. Engine::~Engine()
  40. {
  41. }
  42. void Engine::init()
  43. {
  44. Platform::init();
  45. int width = g_config.getInteger("width");
  46. int height = g_config.getInteger("height");
  47. // create the window
  48. Platform::createWindow(width, height, 550, 450);
  49. Platform::setWindowTitle(APP_NAME);
  50. Platform::setVsync();
  51. // initialize graphics stuff
  52. g_graphics.init();
  53. // finally show the window
  54. onResize(width, height);
  55. Platform::showWindow();
  56. Platform::hideMouseCursor();
  57. }
  58. void Engine::terminate()
  59. {
  60. // save configs
  61. g_config.setValue("width", Platform::getWindowWidth());
  62. g_config.setValue("height", Platform::getWindowHeight());
  63. Platform::showMouseCursor();
  64. Platform::terminate();
  65. g_graphics.terminate();
  66. }
  67. void Engine::run()
  68. {
  69. unsigned long ticks;
  70. m_running = true;
  71. m_lastFrameTicks = Platform::getTicks();
  72. while(!m_stopping) {
  73. // fire platform events
  74. Platform::poll();
  75. // update
  76. ticks = Platform::getTicks();
  77. update(ticks - m_lastFrameTicks);
  78. m_lastFrameTicks = ticks;
  79. // render
  80. render();
  81. // swap buffers
  82. Platform::swapBuffers();
  83. }
  84. m_lastFrameTicks = 0;
  85. m_stopping = false;
  86. m_running = false;
  87. }
  88. void Engine::stop()
  89. {
  90. m_stopping = true;
  91. }
  92. void Engine::changeState(GameState* newState)
  93. {
  94. if(m_currentState)
  95. m_currentState->onLeave();
  96. m_currentState = newState;
  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. }