Przeglądaj źródła

:recycle: removed all usages of std::format, so Apple CLang can be used

Felix Bytow 1 rok temu
rodzic
commit
171452b996
5 zmienionych plików z 36 dodań i 21 usunięć
  1. 14 7
      SDL.cxx
  2. 2 3
      game/AssetManager.cxx
  3. 3 3
      game/GameOverState.cxx
  4. 1 2
      game/PlayingState.cxx
  5. 16 6
      game/ui/UiColor.cxx

+ 14 - 7
SDL.cxx

@@ -1,17 +1,24 @@
 #include "SDL.hxx"
 
 #include <cassert>
-#include <format>
+#include <sstream>
 
 #include <SDL_image.h>
 #include <SDL_ttf.h>
 
-SDLError::SDLError(std::string_view const message, std::source_location location)
-    :std::runtime_error{std::format(
-    "{}:{}:{} - {} ({})",
-    location.file_name(), location.line(), location.column(),
-    message,
-    SDL_GetError())}
+namespace {
+  std::string build_error_message(std::string_view const message, std::source_location const location)
+  {
+    std::ostringstream strm;
+    strm << location.file_name() << ":" << location.line() << ":" << location.column();
+    strm << " - ";
+    strm << message << " (" << SDL_GetError() << ")";
+    return strm.str();
+  }
+}
+
+SDLError::SDLError(std::string_view const message, std::source_location const location)
+    :std::runtime_error{::build_error_message(message, location)}
 {
 }
 

+ 2 - 3
game/AssetManager.cxx

@@ -2,7 +2,6 @@
 
 #include <cassert>
 #include <filesystem>
-#include <format>
 
 #include <SDL_image.h>
 
@@ -62,7 +61,7 @@ void AssetManager::load_assets(std::filesystem::path const& asset_directory)
     if (ext==".png" || ext==".jpg") {
       auto const surface = IMG_Load(path.c_str());
       if (surface==nullptr) {
-        throw SDLError{std::format("Failed to load texture {}.", path)};
+        throw SDLError{"Failed to load texture "+path+"."};
       }
       surface_assets_[filename] = surface;
       SDL_Log("Loaded texture %s successfully.", filename.c_str());
@@ -70,7 +69,7 @@ void AssetManager::load_assets(std::filesystem::path const& asset_directory)
     else if (ext==".ttf") {
       auto const font = TTF_OpenFont(path.c_str(), 42);
       if (font==nullptr) {
-        throw SDLError{std::format("Failed to load font {}.", path)};
+        throw SDLError{"Failed to load font "+path+"."};
       }
       font_assets_[filename] = font;
       SDL_Log("Loaded font %s successfully.", filename.c_str());

+ 3 - 3
game/GameOverState.cxx

@@ -71,9 +71,9 @@ void GameOverState::render(SDLRenderer& renderer)
   int base_y;
   auto const& hsm = HighScoreManager::instance();
   if (hsm.has_new_score()) {
-    auto const score_text = std::format(
-        "Congratulations, you made it to the top 10!\nYou reached {} points!\nPlease enter your name:",
-        hsm.get_new_score());
+    auto const score_text = "Congratulations, you made it to the top 10!\nYou reached "
+        +std::to_string(hsm.get_new_score())
+        +" points!\nPlease enter your name:";
     SDL_Surface* text_surface = TTF_RenderText_Solid_Wrapped(font_, score_text.c_str(),
         {255, 255, 255, SDL_ALPHA_OPAQUE}, 0);
     SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, text_surface);

+ 1 - 2
game/PlayingState.cxx

@@ -6,7 +6,6 @@
 #include <algorithm>
 #include <cfenv>
 #include <cmath>
-#include <format>
 #include <random>
 #include <unordered_set>
 
@@ -170,7 +169,7 @@ void PlayingState::render(SDLRenderer& renderer)
 
 void PlayingState::render_ui(SDLRenderer& renderer, SDL_Rect const& playing_field)
 {
-  auto const score_text = std::format("Score: {}", length_);
+  auto const score_text = "Score: {}"+std::to_string(length_);
   SDL_Surface* text_surface = TTF_RenderText_Solid(font_, score_text.c_str(), {255, 255, 255, SDL_ALPHA_OPAQUE});
   SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, text_surface);
   SDL_FreeSurface(text_surface);

+ 16 - 6
game/ui/UiColor.cxx

@@ -1,19 +1,29 @@
 #include "UiColor.hxx"
 
-#include <format>
+#include <sstream>
 
 std::string ui_image(std::string_view name, UiColor const color)
 {
+  std::ostringstream strm;
+
   switch (color) {
   case UiColor::Blue:
-    return std::format("blue_{}.png", name);
+    strm << "blue_";
+    break;
   case UiColor::Green:
-    return std::format("green_{}.png", name);
+    strm << "green_";
+    break;
   default:
-    return std::format("grey_{}.png", name);
+    strm << "grey_";
+    break;
   case UiColor::Red:
-    return std::format("red_{}.png", name);
+    strm << "red_";
+    break;
   case UiColor::Yellow:
-    return std::format("yellow_{}.png", name);
+    strm << "yellow_";
+    break;
   }
+
+  strm << name << ".png";
+  return strm.str();
 }