Browse Source

Add the fmt library as a dependency and add some examples

Attempt at fixing missing functions on some platforms
pull/1689/head
Jmgr 5 years ago committed by Anders Jenbo
parent
commit
f3a401b5f6
  1. 18
      3rdParty/libfmt/CMakeLists.txt
  2. 14
      CMakeLists.txt
  3. 11
      Source/DiabloUI/selgame.cpp
  4. 3
      Source/debug.cpp
  5. 1
      Source/init.cpp
  6. 6
      Source/stores.cpp
  7. 57
      Source/utils/format.hpp
  8. 91
      Source/utils/log.hpp

18
3rdParty/libfmt/CMakeLists.txt vendored

@ -0,0 +1,18 @@
if(NOT DEVILUTIONX_SYSTEM_LIBFMT)
include(FetchContent_MakeAvailableExcludeFromAll)
add_definitions(-D_POSIX_C_SOURCE=200809L)
if(DEVILUTIONX_STATIC_LIBFMT)
set(BUILD_SHARED_LIBS OFF)
else()
set(BUILD_SHARED_LIBS ON)
endif()
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 7bdf0628b1276379886c7f6dda2cef2b3b374f0b
)
FetchContent_MakeAvailableExcludeFromAll(fmt)
set(fmt_SOURCE_DIR ${fmt_SOURCE_DIR} PARENT_SCOPE)
endif()

14
CMakeLists.txt

@ -43,6 +43,10 @@ option(DEVILUTIONX_SYSTEM_LIBSODIUM "Use system-provided libsodium" ON)
cmake_dependent_option(DEVILUTIONX_STATIC_LIBSODIUM "Link static libsodium" OFF
"DEVILUTIONX_SYSTEM_LIBSODIUM AND NOT DIST" ON)
option(DEVILUTIONX_SYSTEM_LIBFMT "Use system-provided libfmt" OFF)
cmake_dependent_option(DEVILUTIONX_STATIC_LIBFMT "Link static libfmt" OFF
"DEVILUTIONX_STATIC_LIBFMT AND NOT DIST" ON)
if(NOT VERSION_NUM)
include(CMake/git.cmake)
get_git_tag(VERSION_NUM)
@ -169,6 +173,13 @@ if(NOT NONET)
endif()
endif()
if(DEVILUTIONX_SYSTEM_LIBFMT)
set(fmt_USE_STATIC_LIBS ${DEVILUTIONX_STATIC_LIBFMT})
find_package(fmt REQUIRED)
else()
add_subdirectory(3rdParty/libfmt)
endif()
if(USE_SDL1)
find_package(SDL REQUIRED)
find_package(SDL_ttf REQUIRED)
@ -456,6 +467,7 @@ target_include_directories(${BIN_TARGET} PRIVATE
3rdParty/asio/include
3rdParty/Radon/Radon/include
3rdParty/libsmacker
${fmt_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR})
if(NOT N3DS)
@ -473,6 +485,8 @@ if(NOT NONET)
target_link_libraries(${BIN_TARGET} PRIVATE sodium)
endif()
target_link_libraries(${BIN_TARGET} PRIVATE fmt)
genex_for_option(DEBUG)
target_compile_definitions(${BIN_TARGET} PUBLIC "$<${DEBUG_GENEX}:_DEBUG>")
target_compile_definitions(${BIN_TARGET} PRIVATE ASIO_STANDALONE)

11
Source/DiabloUI/selgame.cpp

@ -11,6 +11,8 @@
#include "options.h"
#include "storm/storm.h"
#include <fmt/format.h>
namespace devilution {
char selgame_Label[32];
@ -410,10 +412,11 @@ static bool IsGameCompatible(GameData *data)
if (data->programid != GAME_ID) {
UiSelOkDialog(title, "The host is running a different game than you.", false);
} else {
char msg[64];
sprintf(msg, "Your version %s does not match the host %d.%d.%d.", PROJECT_VERSION, PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_VERSION_PATCH);
UiSelOkDialog(title, msg, false);
// An example of using a format with references. Note the references that allow a localized string to change the order of the inserted parameters.
// Note that named insertions would probably be better here, but this is just to provide an example.
auto msg = fmt::format("Your version {0} does not match the host {1}.{2}.{3}.", PROJECT_VERSION, PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_VERSION_PATCH);
UiSelOkDialog(title, msg.c_str(), false);
}
LoadBackgroundArt("ui_art\\selgame.pcx");

3
Source/debug.cpp

@ -7,6 +7,9 @@
#include "cursor.h"
#include "inv.h"
#include "spells.h"
#include "utils/format.hpp"
#include <fmt/format.h>
namespace devilution {

1
Source/init.cpp

@ -14,6 +14,7 @@
#include "storm/storm.h"
#include "utils/paths.h"
#include "utils/ui_fwd.h"
#include "utils/log.hpp"
#ifdef __vita__
// increase default allowed heap size on Vita

6
Source/stores.cpp

@ -6,6 +6,7 @@
#include "stores.h"
#include <algorithm>
#include <fmt/format.h>
#include "cursor.h"
#include "init.h"
@ -270,8 +271,9 @@ void S_StartSBuy()
stextsize = true;
stextscrl = true;
stextsval = 0;
sprintf(tempstr, "I have these items for sale: Your gold: %i", plr[myplr]._pGold);
AddSText(0, 1, true, tempstr, COL_GOLD, false);
// An example of a named format insertion.
auto str = fmt::format("I have these items for sale: Your gold: {gold}", fmt::arg("gold", plr[myplr]._pGold));
AddSText(0, 1, true, str.c_str(), COL_GOLD, false);
AddSLine(3);
AddSLine(21);
S_ScrollSBuy(stextsval);

57
Source/utils/format.hpp

@ -0,0 +1,57 @@
#pragma once
#include "../player.h"
#include <fmt/format.h>
namespace fmt {
template <>
struct formatter<devilution::PLR_MODE> : formatter<string_view> {
template <typename FormatContext>
auto format(devilution::PLR_MODE mode, FormatContext &ctx)
{
string_view name = "unknown";
switch (mode) {
case devilution::PM_STAND:
name = "stand";
break;
case devilution::PM_WALK:
name = "walk (N, NW, or NE)";
break;
case devilution::PM_WALK2:
name = "walk (S, SW, or SE)";
break;
case devilution::PM_WALK3:
name = "walk (W or E)";
break;
case devilution::PM_ATTACK:
name = "attack";
break;
case devilution::PM_RATTACK:
name = "ranged attack";
break;
case devilution::PM_BLOCK:
name = "block";
break;
case devilution::PM_GOTHIT:
name = "got hit";
break;
case devilution::PM_DEATH:
name = "death";
break;
case devilution::PM_SPELL:
name = "spell";
break;
case devilution::PM_NEWLVL:
name = "new level";
break;
case devilution::PM_QUIT:
name = "quit";
break;
}
return formatter<string_view>::format(name, ctx);
}
};
}

91
Source/utils/log.hpp

@ -0,0 +1,91 @@
#pragma once
#include "fmt/core.h"
#include "fmt/ranges.h"
#include <SDL.h>
#ifdef USE_SDL1
#include "sdl2_to_1_2_backports.h"
#endif
namespace devilution {
enum class LogCategory {
Application = SDL_LOG_CATEGORY_APPLICATION,
Error = SDL_LOG_CATEGORY_ERROR,
Assert = SDL_LOG_CATEGORY_ASSERT,
System = SDL_LOG_CATEGORY_SYSTEM,
Audio = SDL_LOG_CATEGORY_AUDIO,
Video = SDL_LOG_CATEGORY_VIDEO,
Render = SDL_LOG_CATEGORY_RENDER,
Input = SDL_LOG_CATEGORY_INPUT,
Test = SDL_LOG_CATEGORY_TEST,
};
enum class LogPriority {
Verbose = SDL_LOG_PRIORITY_VERBOSE,
Debug = SDL_LOG_PRIORITY_DEBUG,
Info = SDL_LOG_PRIORITY_INFO,
Warn = SDL_LOG_PRIORITY_WARN,
Error = SDL_LOG_PRIORITY_ERROR,
Critical = SDL_LOG_PRIORITY_CRITICAL,
};
template <typename... Args>
void Log(const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_Log("%s", str.c_str());
}
template <typename... Args>
void LogVerbose(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogVerbose(static_cast<int>(category), "%s", str.c_str());
}
template <typename... Args>
void LogDebug(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogDebug(static_cast<int>(category), "%s", str.c_str());
}
template <typename... Args>
void LogInfo(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogInfo(static_cast<int>(category), "%s", str.c_str());
}
template <typename... Args>
void LogWarn(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogWarn(static_cast<int>(category), "%s", str.c_str());
}
template <typename... Args>
void LogError(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogError(static_cast<int>(category), "%s", str.c_str());
}
template <typename... Args>
void LogCritical(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogCritical(static_cast<int>(category), "%s", str.c_str());
}
template <typename... Args>
void LogMessageV(LogCategory category, LogPriority priority, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
SDL_LogMessageV(static_cast<int>(category), static_cast<SDL_LogPriority>(priority), "%s", str.c_str());
}
} // namespace devilution
Loading…
Cancel
Save