From 43b4565946916ea503aa4c182969b795c9eb2d13 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Mon, 17 Aug 2020 09:07:19 -0700 Subject: [PATCH] Enable localized strings --- src/application.cpp | 6 ++---- src/game/bootloader.cpp | 35 +++++++++++++++++++++++----------- src/game/game-context.hpp | 8 ++++++++ src/resources/string-table.cpp | 17 +++++++++++++++-- src/resources/string-table.hpp | 6 +++++- 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index fa6b5d3..7f5736f 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -38,7 +38,6 @@ //#include "utility/timestamp.hpp" //#include - application::application(): closed(false), exit_status(EXIT_SUCCESS), @@ -361,10 +360,9 @@ void application::set_fullscreen(bool fullscreen) if (fullscreen) { - SDL_SetWindowBordered(sdl_window, SDL_FALSE); - SDL_SetWindowResizable(sdl_window, SDL_FALSE); + SDL_HideWindow(sdl_window); SDL_SetWindowFullscreen(sdl_window, SDL_WINDOW_FULLSCREEN_DESKTOP); - resize_window(display_dimensions[0], display_dimensions[1]); + SDL_ShowWindow(sdl_window); } else { diff --git a/src/game/bootloader.cpp b/src/game/bootloader.cpp index 8cee67e..f2aec2e 100644 --- a/src/game/bootloader.cpp +++ b/src/game/bootloader.cpp @@ -96,6 +96,7 @@ static void parse_options(game_context* ctx, int argc, char** argv); static void setup_resources(game_context* ctx); static void load_config(game_context* ctx); +static void load_strings(game_context* ctx); static void setup_window(game_context* ctx); static void setup_rendering(game_context* ctx); static void setup_scenes(game_context* ctx); @@ -124,6 +125,7 @@ int bootloader(application* app, int argc, char** argv) parse_options(ctx, argc, argv); setup_resources(ctx); load_config(ctx); + load_strings(ctx); setup_window(ctx); setup_rendering(ctx); setup_scenes(ctx); @@ -143,8 +145,11 @@ int bootloader(application* app, int argc, char** argv) logger->pop_task(EXIT_SUCCESS); - // Change to splash state - app->change_state({std::bind(splash_state_enter, ctx), std::bind(splash_state_exit, ctx)}); + // Change state + if (ctx->option_quick_start.has_value()) + app->change_state({std::bind(play_state_enter, ctx), std::bind(play_state_exit, ctx)}); + else + app->change_state({std::bind(splash_state_enter, ctx), std::bind(splash_state_exit, ctx)}); return EXIT_SUCCESS; } @@ -326,6 +331,7 @@ void setup_resources(game_context* ctx) ctx->resource_manager->include("/entities/"); ctx->resource_manager->include("/behaviors/"); ctx->resource_manager->include("/controls/"); + ctx->resource_manager->include("/localization/"); ctx->resource_manager->include("/"); } @@ -345,6 +351,21 @@ void load_config(game_context* ctx) logger->pop_task(EXIT_SUCCESS); } +void load_strings(game_context* ctx) +{ + logger* logger = ctx->logger; + logger->push_task("Loading strings"); + + ctx->string_table = ctx->resource_manager->load("strings.csv"); + + build_string_table_map(&ctx->string_table_map, *ctx->string_table); + + ctx->language_code = ctx->config->get("language"); + ctx->strings = &ctx->string_table_map[ctx->language_code]; + + logger->pop_task(EXIT_SUCCESS); +} + void setup_window(game_context* ctx) { logger* logger = ctx->logger; @@ -353,7 +374,6 @@ void setup_window(game_context* ctx) application* app = ctx->app; config_file* config = ctx->config; - logger->push_task("Setting fullscreen mode"); // Set fullscreen or windowed mode bool fullscreen = true; if (ctx->option_fullscreen.has_value()) @@ -362,13 +382,9 @@ void setup_window(game_context* ctx) fullscreen = false; else if (config->has("fullscreen")) fullscreen = (config->get("fullscreen") != 0); - app->set_fullscreen(fullscreen); - logger->pop_task(EXIT_SUCCESS); - // Set resolution - logger->push_task("Setting resolution"); const auto& display_dimensions = ctx->app->get_display_dimensions(); int2 resolution = {display_dimensions[0], display_dimensions[1]}; if (fullscreen) @@ -381,9 +397,7 @@ void setup_window(game_context* ctx) if (config->has("windowed_resolution")) resolution = config->get("windowed_resolution"); } - app->resize_window(resolution.x, resolution.y); - logger->pop_task(EXIT_SUCCESS); // Set v-sync bool vsync = true; @@ -394,8 +408,7 @@ void setup_window(game_context* ctx) app->set_vsync(vsync); // Set title - std::string title = "Antkeeper"; - app->set_title(title); + app->set_title((*ctx->strings)["title"]); logger->pop_task(EXIT_SUCCESS); } diff --git a/src/game/game-context.hpp b/src/game/game-context.hpp index 7858a6a..2532b47 100644 --- a/src/game/game-context.hpp +++ b/src/game/game-context.hpp @@ -21,6 +21,7 @@ #define ANTKEEPER_GAME_CONTEXT_HPP #include "utility/fundamental-types.hpp" +#include "resources/string-table.hpp" #include #include #include @@ -116,6 +117,13 @@ struct game_context // Resources resource_manager* resource_manager; + // Localization + std::string language_code; + int language_index; + string_table* string_table; + string_table_map string_table_map; + std::unordered_map* strings; + // Framebuffers framebuffer* shadow_map_framebuffer; texture_2d* shadow_map_depth_texture; diff --git a/src/resources/string-table.cpp b/src/resources/string-table.cpp index a8f29fa..97a6c96 100644 --- a/src/resources/string-table.cpp +++ b/src/resources/string-table.cpp @@ -19,7 +19,21 @@ #include "resources/string-table.hpp" -string_table_index createIndex(const string_table& table) +void build_string_table_map(string_table_map* map, const string_table& table) +{ + map->clear(); + + for (std::size_t i = 0; i < table.size(); ++i) + { + for (std::size_t j = 2; j < table[i].size(); ++j) + { + const std::string& string = table[i][j]; + (*map)[table[0][j]][table[i][0]] = string.empty() ? "# MISSING STRING #" : string; + } + } +} + +string_table_index index_string_table(const string_table& table) { string_table_index index; @@ -30,4 +44,3 @@ string_table_index createIndex(const string_table& table) return index; } - diff --git a/src/resources/string-table.hpp b/src/resources/string-table.hpp index 5acf06c..747c912 100644 --- a/src/resources/string-table.hpp +++ b/src/resources/string-table.hpp @@ -39,12 +39,16 @@ typedef std::vector string_table; */ typedef std::unordered_map string_table_index; +typedef std::unordered_map> string_table_map; + +void build_string_table_map(string_table_map* map, const string_table& table); + /** * Creates an index for a string table using strings in the first column as keys. * * @param table Table for which an index will be created. */ -string_table_index createIndex(const string_table& table); +string_table_index index_string_table(const string_table& table); #endif // ANTKEEPER_STRING_TABLE_HPP