Prechádzať zdrojové kódy

:art: so first thing I noticed about CLion Nova is that it ignores the classic C++ style settings

Felix Bytow 1 rok pred
rodič
commit
c6bd10796d
3 zmenil súbory, kde vykonal 41 pridanie a 56 odobranie
  1. 20 31
      game/AssetManager.cxx
  2. 20 23
      game/SplashState.cxx
  3. 1 2
      game/SplashState.hxx

+ 20 - 31
game/AssetManager.cxx

@@ -9,7 +9,7 @@
 AssetManager* AssetManager::instance_ = nullptr;
 
 AssetManager::AssetManager(SDLRenderer& renderer)
-  : renderer_{renderer}
+    :renderer_{renderer}
 {
   namespace fs = std::filesystem;
 
@@ -17,18 +17,17 @@ AssetManager::AssetManager(SDLRenderer& renderer)
 
   auto const 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::path{};
-  if (asset_directory.empty())
-  {
+      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;
@@ -37,21 +36,17 @@ AssetManager::AssetManager(SDLRenderer& renderer)
 AssetManager::~AssetManager()
 {
   assert(instance_!=nullptr);
-  if (loading_thread_.joinable())
-  {
+  if (loading_thread_.joinable()) {
     loading_thread_.join();
   }
-  for (auto const& kv : surface_assets_)
-  {
+  for (auto const& kv: surface_assets_) {
     SDL_FreeSurface(kv.second);
   }
-  for (auto const& kv : texture_assets_)
-  {
+  for (auto const& kv: texture_assets_) {
     SDL_DestroyTexture(kv.second);
     SDL_Log("Unloaded texture %s successfully.", kv.first.c_str());
   }
-  for (auto const& kv : font_assets_)
-  {
+  for (auto const& kv: font_assets_) {
     TTF_CloseFont(kv.second);
     SDL_Log("Unloaded font %s successfully.", kv.first.c_str());
   }
@@ -60,26 +55,21 @@ AssetManager::~AssetManager()
 
 void AssetManager::load_assets(std::filesystem::path const& asset_directory)
 {
-  for (auto const& entry : std::filesystem::directory_iterator(asset_directory))
-  {
+  for (auto const& entry: std::filesystem::directory_iterator(asset_directory)) {
     auto const path = entry.path().string();
     auto const ext = entry.path().extension();
     auto const filename = entry.path().filename().string();
-    if (ext == ".png" || ext == ".jpg")
-    {
+    if (ext==".png" || ext==".jpg") {
       auto const surface = IMG_Load(path.c_str());
-      if (surface == nullptr)
-      {
+      if (surface==nullptr) {
         throw SDLError{std::format("Failed to load texture {}.", path)};
       }
       surface_assets_[filename] = surface;
       SDL_Log("Loaded texture %s successfully.", filename.c_str());
     }
-    else if (ext == ".ttf")
-    {
+    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_[filename] = font;
@@ -91,16 +81,15 @@ void AssetManager::load_assets(std::filesystem::path const& asset_directory)
 
 float AssetManager::get_progress() const
 {
-  return static_cast<float>(assets_loaded_) / static_cast<float>(total_assets_);
+  return static_cast<float>(assets_loaded_)/static_cast<float>(total_assets_);
 }
 
 SDL_Texture* AssetManager::get_texture_asset(std::string const& filepath)
 {
   auto const it = texture_assets_.find(filepath);
-  if (it == std::end(texture_assets_))
-  {
+  if (it==std::end(texture_assets_)) {
     auto const surf_it = surface_assets_.find(filepath);
-    if (surf_it == std::end(surface_assets_))
+    if (surf_it==std::end(surface_assets_))
       return nullptr;
 
     auto const texture = SDL_CreateTextureFromSurface(renderer_, surf_it->second);

+ 20 - 23
game/SplashState.cxx

@@ -8,7 +8,7 @@ void SplashState::update(GameStateManager& gsm, std::chrono::milliseconds delta_
 {
   time_in_state_ += delta_time;
   auto const key_state = SDL_GetKeyboardState(nullptr);
-  if (time_in_state_ > 13'000ms || key_state[SDL_SCANCODE_SPACE] || key_state[SDL_SCANCODE_RETURN])
+  if (time_in_state_>13'000ms || key_state[SDL_SCANCODE_SPACE] || key_state[SDL_SCANCODE_RETURN])
     gsm.replace_state(GameStates::MainMenu);
 }
 
@@ -17,21 +17,17 @@ void SplashState::render(SDLRenderer& renderer)
   SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
   SDL_RenderClear(renderer);
 
-  if (time_in_state_ >= 2'000ms && time_in_state_ < 11'000ms)
-  {
-    if (time_in_state_ < 4'000ms)
-    {
-      auto const progress = static_cast<float>(time_in_state_.count() - 2'000) / 2'000.0f;
+  if (time_in_state_>=2'000ms && time_in_state_<11'000ms) {
+    if (time_in_state_<4'000ms) {
+      auto const progress = static_cast<float>(time_in_state_.count()-2'000)/2'000.0f;
       auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_TRANSPARENT, SDL_ALPHA_OPAQUE, progress));
       SDL_SetTextureAlphaMod(logo_, alpha);
     }
-    else if (time_in_state_ >= 4'000ms && time_in_state_ < 9'000ms)
-    {
+    else if (time_in_state_>=4'000ms && time_in_state_<9'000ms) {
       SDL_SetTextureAlphaMod(logo_, SDL_ALPHA_OPAQUE);
     }
-    else if (time_in_state_ >= 9'000ms)
-    {
-      auto const progress = static_cast<float>(time_in_state_.count() - 9'000) / 2'000.0f;
+    else if (time_in_state_>=9'000ms) {
+      auto const progress = static_cast<float>(time_in_state_.count()-9'000)/2'000.0f;
       auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, progress));
       SDL_SetTextureAlphaMod(logo_, alpha);
     }
@@ -41,23 +37,24 @@ void SplashState::render(SDLRenderer& renderer)
     int logo_w, logo_h;
     SDL_QueryTexture(logo_, nullptr, nullptr, &logo_w, &logo_h);
 
-    float const logo_aspect = static_cast<float>(logo_w) / static_cast<float>(logo_h);
-    float const screen_aspect = static_cast<float>(screen_w - 20) / static_cast<float>(screen_h - 20);
+    float const logo_aspect = static_cast<float>(logo_w)/static_cast<float>(logo_h);
+    float const screen_aspect = static_cast<float>(screen_w-20)/static_cast<float>(screen_h-20);
 
     int put_w, put_h;
-    if (logo_aspect > screen_aspect) {
-      put_w = (screen_w - 20);
-      put_h = static_cast<int>(static_cast<float>(put_w) / logo_aspect);
-    } else {
-      put_h = screen_h - 20;
-      put_w = static_cast<int>(static_cast<float>(put_h) * logo_aspect);
+    if (logo_aspect>screen_aspect) {
+      put_w = (screen_w-20);
+      put_h = static_cast<int>(static_cast<float>(put_w)/logo_aspect);
+    }
+    else {
+      put_h = screen_h-20;
+      put_w = static_cast<int>(static_cast<float>(put_h)*logo_aspect);
     }
 
     SDL_Rect const logo_rect = {
-      .x = (screen_w - put_w) / 2,
-      .y = (screen_h - put_h) / 2,
-      .w = put_w,
-      .h = put_h
+        .x = (screen_w-put_w)/2,
+        .y = (screen_h-put_h)/2,
+        .w = put_w,
+        .h = put_h
     };
     SDL_RenderCopy(renderer, logo_, nullptr, &logo_rect);
   }

+ 1 - 2
game/SplashState.hxx

@@ -7,8 +7,7 @@
 
 #include <SDL.h>
 
-class SplashState final : public GameState
-{
+class SplashState final : public GameState {
 public:
   void on_enter() override;