Skip to content

Commit

Permalink
Setup UTF-8 directly for Windows, remove helper function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryouze committed Jan 4, 2025
1 parent d92b306 commit 2172494
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 41 deletions.
20 changes: 4 additions & 16 deletions src/core/windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
// Typically, you’d define this ID in a "resource.h" file and include it in both "icon.rc" and this file
// However, I want to avoid any platform-specific files whenever possible

#include <locale> // for setlocale, LC_ALL
#include <optional> // for std::optional, std::nullopt
#include <string> // for std::string
#include <windows.h> // for SetConsoleCP, SetConsoleOutputCP, GetLastError, HWND, HICON, LoadIcon, GetModuleHandle, MAKEINTRESOURCE, SendMessage, WM_SETICON, ICON_BIG, ICON_SMALL, LPARAM
#include <optional> // for std::optional, std::nullopt
#include <string> // for std::string

#include <windows.h> // for HWND, HICON, LoadIcon, GetModuleHandle, MAKEINTRESOURCE, GetLastError, SendMessage, WM_SETICON, ICON_BIG, ICON_SMALL, LPARAM

#include <SFML/Window.hpp>
#include <fmt/core.h>
Expand All @@ -22,18 +22,6 @@

namespace core::windows {

std::optional<std::string> setup_utf8_console()
{
if (!SetConsoleCP(CP_UTF8) || !SetConsoleOutputCP(CP_UTF8)) {
return fmt::format("Failed to set UTF-8 code page: {}", GetLastError());
}

if (!setlocale(LC_ALL, ".UTF8")) {
return "Failed to set UTF-8 locale";
}
return std::nullopt;
}

std::optional<std::string> setup_titlebar_icon(const sf::Window &window)
{
// Get the native window handle from the SFML window
Expand Down
7 changes: 0 additions & 7 deletions src/core/windows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@

namespace core::windows {

/**
* @brief Setup UTF-8 input/output on Windows. Do nothing on other platforms.
*
* @return Error message if the setup failed, "std::nullopt" otherwise.
*/
[[nodiscard]] std::optional<std::string> setup_utf8_console();

/**
* @brief Setup the titlebar icon on Windows using the embedded icon data (must be embedded by CMake).
*
Expand Down
20 changes: 8 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@
#include <exception> // for std::exception

#include <fmt/core.h>

#include "app.hpp"
#if defined(_WIN32)
#include "core/windows.hpp"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h> // for SetConsoleCP, SetConsoleOutputCP, CP_UTF8
#endif

#include "app.hpp"

/**
* @brief Entry-point of the application.
*
* @return EXIT_SUCCESS if the application ran successfully, EXIT_FAILURE otherwise.
*/
int main()
{
try {
#if defined(_WIN32)
// Boilerplate to make Windows behave more like *nix

// Setup UTF-8 input/output and locale
if (const auto e = core::windows::setup_utf8_console(); e.has_value()) {
fmt::print(stderr, "Warning: {}\n", *e);
}
#if defined(_WIN32) // Setup UTF-8 input/output
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif

try {
// Run the app
app::run();
}
Expand Down
11 changes: 5 additions & 6 deletions tests/test_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include "core/string.hpp"
#include "modules/vocabulary.hpp"
#if defined(_WIN32)
#include "core/windows.hpp"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h> // for SetConsoleCP, SetConsoleOutputCP, CP_UTF8
#endif

namespace test_assets {
Expand Down Expand Up @@ -53,11 +54,9 @@ namespace test_vocabulary {
int main(int argc,
char **argv)
{
#if defined(_WIN32)
// Setup UTF-8 input/output on Windows (does nothing on other platforms)
if (const auto e = core::windows::setup_utf8_console(); e.has_value()) {
fmt::print(stderr, "Warning: {}\n", *e);
}
#if defined(_WIN32) // Setup UTF-8 input/output
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif

// Define the formatted help message
Expand Down

0 comments on commit 2172494

Please sign in to comment.