LoadingState.cxx 1.5 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. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  14. SDL_RenderClear(renderer);
  15. static int const BAR_MARGIN = 30;
  16. static int const BAR_HEIGHT = 50;
  17. int w, h;
  18. SDL_GetRendererOutputSize(renderer, &w, &h);
  19. float progress = AssetManager::instance().get_progress();
  20. SDL_Rect const outer_rect = {
  21. .x=BAR_MARGIN,
  22. .y=h-BAR_MARGIN-BAR_HEIGHT,
  23. .w=w-(BAR_MARGIN*2),
  24. .h=BAR_HEIGHT
  25. };
  26. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  27. SDL_RenderFillRect(renderer, &outer_rect);
  28. SDL_Rect const inner_rect = {
  29. .x=BAR_MARGIN+2,
  30. .y=h-BAR_MARGIN-BAR_HEIGHT+2,
  31. .w=w-(BAR_MARGIN*2)-4,
  32. .h=BAR_HEIGHT-4
  33. };
  34. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  35. SDL_RenderFillRect(renderer, &inner_rect);
  36. SDL_Rect const progress_rect = {
  37. .x=BAR_MARGIN+4,
  38. .y=h-BAR_MARGIN-BAR_HEIGHT+4,
  39. .w=static_cast<int>(std::lerp(0, w-(BAR_MARGIN*2)-8, progress)),
  40. .h=BAR_HEIGHT-8
  41. };
  42. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  43. SDL_RenderFillRect(renderer, &progress_rect);
  44. SDL_RenderPresent(renderer);
  45. }