StateMachine.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "StateMachine.h"
  2. #include "Timing.h"
  3. #include "platform.h"
  4. #include "Graphics.h"
  5. #include "render/Text.h"
  6. #include "version.h"
  7. #include "Input.h"
  8. using namespace WalrusRPG::Graphics;
  9. using namespace WalrusRPG::States;
  10. using namespace WalrusRPG::Timing;
  11. using WalrusRPG::Input::Key;
  12. using WalrusRPG::Input::KeyState;
  13. #define STATEMACHINE WalrusRPG::StateMachine
  14. namespace {
  15. void draw_button(unsigned x, unsigned y, KeyState state)
  16. {
  17. put_horizontal_line(x+1, x+5, y, Gray);
  18. put_horizontal_line(x+1, x+5, y+6, Gray);
  19. put_vertical_line(x, y+1, y+5, Gray);
  20. put_vertical_line(x+6, y+1, y+5, Gray);
  21. switch(state)
  22. {
  23. case KeyState::KS_RELEASED:
  24. put_rectangle({x+1, y+1, 5, 5}, White);
  25. break;
  26. case KeyState::KS_JUST_RELEASED:
  27. put_rectangle({x+1, y+1, 5, 5}, Magenta);
  28. break;
  29. case KeyState::KS_JUST_PRESSED:
  30. put_rectangle({x+1, y+1, 5, 5}, Cyan);
  31. break;
  32. case KeyState::KS_PRESSED:
  33. put_rectangle({x+1, y+1, 5, 5}, Black);
  34. break;
  35. }
  36. }
  37. void draw_buttons()
  38. {
  39. draw_button(0, 24, key_get_state(Key::K_L));
  40. draw_button(56, 24, key_get_state(Key::K_R));
  41. draw_button(8, 32, key_get_state(Key::K_UP));
  42. draw_button(0, 40, key_get_state(Key::K_LEFT));
  43. draw_button(16, 40, key_get_state(Key::K_RIGHT));
  44. draw_button(8, 48, key_get_state(Key::K_DOWN));
  45. draw_button(28, 48, key_get_state(Key::K_SELECT));
  46. draw_button(36, 48, key_get_state(Key::K_START));
  47. draw_button(48, 44, key_get_state(Key::K_B));
  48. draw_button(56, 36, key_get_state(Key::K_A));
  49. }
  50. } /* namespace */
  51. STATEMACHINE::StateMachine(State *state)
  52. {
  53. push(state);
  54. }
  55. STATEMACHINE::~StateMachine()
  56. {
  57. while (!stack.empty())
  58. pop();
  59. }
  60. void STATEMACHINE::push(State *state)
  61. {
  62. stack.push_back(state);
  63. }
  64. void STATEMACHINE::pop()
  65. {
  66. delete stack.back();
  67. stack.pop_back();
  68. }
  69. void STATEMACHINE::run()
  70. {
  71. const unsigned loop_time = TIMER_FREQ / 60;
  72. unsigned loop_next = loop_time;
  73. unsigned last_update = 0, update_stamp, update_time;
  74. unsigned last_frame = 0, frame_stamp, frame_time;
  75. while (!stack.empty())
  76. {
  77. update_stamp = Timing::gettime();
  78. update_time = update_stamp - last_update;
  79. Input::key_poll();
  80. stack.back()->update(100 * update_time / TIMER_FREQ);
  81. last_update = update_stamp;
  82. if (Timing::gettime() < loop_next)
  83. {
  84. frame_stamp = Timing::gettime();
  85. frame_time = frame_stamp - last_frame;
  86. Graphics::frame_begin();
  87. stack.back()->render(100 * frame_time / TIMER_FREQ);
  88. last_frame = frame_stamp;
  89. Text::print_format(0, 0, "WalrusRPG test build %s", git_version);
  90. if (frame_time != 0 && update_time != 0)
  91. {
  92. Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,
  93. TIMER_FREQ / update_time);
  94. }
  95. draw_buttons();
  96. Graphics::frame_end();
  97. }
  98. if (Input::key_pressed(Key::K_SELECT))
  99. {
  100. while (Input::key_down(Key::K_SELECT))
  101. Input::key_poll();
  102. this->pop();
  103. }
  104. #ifdef ACTIVE_WAIT
  105. while (Timing::gettime() < loop_next)
  106. ;
  107. #endif
  108. loop_next += loop_time;
  109. }
  110. }