Przeglądaj źródła

:sparkles: the loading state now displays a progress bar

Felix Bytow 1 rok temu
rodzic
commit
101d0e3e50
2 zmienionych plików z 43 dodań i 13 usunięć
  1. 5 5
      game/AssetManager.cxx
  2. 38 8
      game/LoadingState.cxx

+ 5 - 5
game/AssetManager.cxx

@@ -17,15 +17,15 @@ AssetManager::AssetManager(SDLRenderer& renderer)
 
   auto current_path = fs::current_path();
   auto const asset_directory =
-      fs::exists(current_path / "assets") ? current_path / "assets" :
-      fs::exists(current_path.parent_path() / "assets") ? current_path.parent_path() / "assets" :
+      fs::exists(current_path/"assets") ? current_path/"assets" :
+      fs::exists(current_path.parent_path()/"assets") ? current_path.parent_path()/"assets" :
       fs::path{};
   if (asset_directory.empty()) {
     throw std::runtime_error("Assets directory not found.");
   }
 
   total_assets_ = std::distance(fs::directory_iterator(asset_directory),
-                                fs::directory_iterator());
+      fs::directory_iterator());
 
   loading_thread_ = std::thread{&AssetManager::load_assets, this, asset_directory};
   instance_ = this;
@@ -57,7 +57,7 @@ void AssetManager::load_assets(std::filesystem::path const& asset_directory)
       SDL_Surface* temp_surface = IMG_Load(path.c_str());
       auto const texture = SDL_CreateTextureFromSurface(renderer_, temp_surface);
       SDL_FreeSurface(temp_surface);
-      if (texture == nullptr) {
+      if (texture==nullptr) {
         throw SDLError{std::format("Failed to load texture {}.", path)};
       }
       texture_assets_[path] = texture;
@@ -65,7 +65,7 @@ void AssetManager::load_assets(std::filesystem::path const& asset_directory)
     }
     else if (ext==".ttf") {
       auto const font = TTF_OpenFont(path.c_str(), 16);
-      if (font == nullptr) {
+      if (font==nullptr) {
         throw SDLError{std::format("Failed to load font {}.", path)};
       }
       font_assets_[path] = font;

+ 38 - 8
game/LoadingState.cxx

@@ -1,20 +1,50 @@
 #include "LoadingState.hxx"
-
 #include "AssetManager.hxx"
 
-#include "../SDLRenderer.hxx"
-
 void LoadingState::update(GameStateManager& gsm, std::chrono::milliseconds const delta_time)
 {
-  (void)gsm;
-  (void)delta_time;
+  (void) gsm;
+  (void) delta_time;
 }
 
-void LoadingState::render(SDLRenderer& renderer) {
+void LoadingState::render(SDLRenderer& renderer)
+{
   SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
   SDL_RenderClear(renderer);
 
-  
+  static int const BAR_MARGIN = 30;
+  static int const BAR_HEIGHT = 50;
+
+  int w, h;
+  SDL_GetRendererOutputSize(renderer, &w, &h);
+  float progress = AssetManager::instance().get_progress();
+
+  SDL_Rect const outer_rect = {
+      .x=BAR_MARGIN,
+      .y=h-BAR_MARGIN-BAR_HEIGHT,
+      .w=w-(BAR_MARGIN*2),
+      .h=BAR_HEIGHT
+  };
+  SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
+  SDL_RenderFillRect(renderer, &outer_rect);
+
+  SDL_Rect const inner_rect = {
+      .x=BAR_MARGIN+2,
+      .y=h-BAR_MARGIN-BAR_HEIGHT+2,
+      .w=w-(BAR_MARGIN*2)-4,
+      .h=BAR_HEIGHT-4
+  };
+  SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
+  SDL_RenderFillRect(renderer, &inner_rect);
+
+  SDL_Rect const progress_rect = {
+      .x=BAR_MARGIN+4,
+      .y=h-BAR_MARGIN-BAR_HEIGHT+4,
+      .w=static_cast<int>(std::lerp(0, w-(BAR_MARGIN*2)-8, progress)),
+      .h=BAR_HEIGHT-8
+  };
+  SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
+  SDL_RenderFillRect(renderer, &progress_rect);
 
   SDL_RenderPresent(renderer);
-}
+}