LoadingState.cxx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "LoadingState.hxx"
  2. #include "AssetManager.hxx"
  3. #include "GameStateManager.hxx"
  4. void LoadingState::update(GameStateManager& gsm, std::chrono::milliseconds const delta_time)
  5. {
  6. (void) delta_time;
  7. if (AssetManager::instance().get_progress()==1.0f) {
  8. gsm.replace_state(GameStates::Splash);
  9. }
  10. }
  11. void LoadingState::render(SDLRenderer& renderer)
  12. {
  13. static int const BAR_MARGIN = 30;
  14. static int const BAR_HEIGHT = 50;
  15. int w, h;
  16. SDL_GetRendererOutputSize(renderer, &w, &h);
  17. float progress = AssetManager::instance().get_progress();
  18. SDL_Rect const outer_rect = {
  19. .x=BAR_MARGIN,
  20. .y=h-BAR_MARGIN-BAR_HEIGHT,
  21. .w=w-(BAR_MARGIN*2),
  22. .h=BAR_HEIGHT
  23. };
  24. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  25. SDL_RenderFillRect(renderer, &outer_rect);
  26. SDL_Rect const inner_rect = {
  27. .x=BAR_MARGIN+2,
  28. .y=h-BAR_MARGIN-BAR_HEIGHT+2,
  29. .w=w-(BAR_MARGIN*2)-4,
  30. .h=BAR_HEIGHT-4
  31. };
  32. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  33. SDL_RenderFillRect(renderer, &inner_rect);
  34. SDL_Rect const progress_rect = {
  35. .x=BAR_MARGIN+4,
  36. .y=h-BAR_MARGIN-BAR_HEIGHT+4,
  37. .w=static_cast<int>(std::lerp(0, w-(BAR_MARGIN*2)-8, progress)),
  38. .h=BAR_HEIGHT-8
  39. };
  40. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  41. SDL_RenderFillRect(renderer, &progress_rect);
  42. }
  43. void LoadingState::on_enter(GameStateManager& gsm)
  44. {
  45. SDL_ShowCursor(SDL_DISABLE);
  46. }