Browse Source

Enable localized strings

master
C. J. Howard 4 years ago
parent
commit
43b4565946
5 changed files with 54 additions and 18 deletions
  1. +2
    -4
      src/application.cpp
  2. +24
    -11
      src/game/bootloader.cpp
  3. +8
    -0
      src/game/game-context.hpp
  4. +15
    -2
      src/resources/string-table.cpp
  5. +5
    -1
      src/resources/string-table.hpp

+ 2
- 4
src/application.cpp View File

@ -38,7 +38,6 @@
//#include "utility/timestamp.hpp" //#include "utility/timestamp.hpp"
//#include <thread> //#include <thread>
application::application(): application::application():
closed(false), closed(false),
exit_status(EXIT_SUCCESS), exit_status(EXIT_SUCCESS),
@ -361,10 +360,9 @@ void application::set_fullscreen(bool fullscreen)
if (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); SDL_SetWindowFullscreen(sdl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
resize_window(display_dimensions[0], display_dimensions[1]);
SDL_ShowWindow(sdl_window);
} }
else else
{ {

+ 24
- 11
src/game/bootloader.cpp View File

@ -96,6 +96,7 @@
static void parse_options(game_context* ctx, int argc, char** argv); static void parse_options(game_context* ctx, int argc, char** argv);
static void setup_resources(game_context* ctx); static void setup_resources(game_context* ctx);
static void load_config(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_window(game_context* ctx);
static void setup_rendering(game_context* ctx); static void setup_rendering(game_context* ctx);
static void setup_scenes(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); parse_options(ctx, argc, argv);
setup_resources(ctx); setup_resources(ctx);
load_config(ctx); load_config(ctx);
load_strings(ctx);
setup_window(ctx); setup_window(ctx);
setup_rendering(ctx); setup_rendering(ctx);
setup_scenes(ctx); setup_scenes(ctx);
@ -143,8 +145,11 @@ int bootloader(application* app, int argc, char** argv)
logger->pop_task(EXIT_SUCCESS); 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; return EXIT_SUCCESS;
} }
@ -326,6 +331,7 @@ void setup_resources(game_context* ctx)
ctx->resource_manager->include("/entities/"); ctx->resource_manager->include("/entities/");
ctx->resource_manager->include("/behaviors/"); ctx->resource_manager->include("/behaviors/");
ctx->resource_manager->include("/controls/"); ctx->resource_manager->include("/controls/");
ctx->resource_manager->include("/localization/");
ctx->resource_manager->include("/"); ctx->resource_manager->include("/");
} }
@ -345,6 +351,21 @@ void load_config(game_context* ctx)
logger->pop_task(EXIT_SUCCESS); 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<string_table>("strings.csv");
build_string_table_map(&ctx->string_table_map, *ctx->string_table);
ctx->language_code = ctx->config->get<std::string>("language");
ctx->strings = &ctx->string_table_map[ctx->language_code];
logger->pop_task(EXIT_SUCCESS);
}
void setup_window(game_context* ctx) void setup_window(game_context* ctx)
{ {
logger* logger = ctx->logger; logger* logger = ctx->logger;
@ -353,7 +374,6 @@ void setup_window(game_context* ctx)
application* app = ctx->app; application* app = ctx->app;
config_file* config = ctx->config; config_file* config = ctx->config;
logger->push_task("Setting fullscreen mode");
// Set fullscreen or windowed mode // Set fullscreen or windowed mode
bool fullscreen = true; bool fullscreen = true;
if (ctx->option_fullscreen.has_value()) if (ctx->option_fullscreen.has_value())
@ -362,13 +382,9 @@ void setup_window(game_context* ctx)
fullscreen = false; fullscreen = false;
else if (config->has("fullscreen")) else if (config->has("fullscreen"))
fullscreen = (config->get<int>("fullscreen") != 0); fullscreen = (config->get<int>("fullscreen") != 0);
app->set_fullscreen(fullscreen); app->set_fullscreen(fullscreen);
logger->pop_task(EXIT_SUCCESS);
// Set resolution // Set resolution
logger->push_task("Setting resolution");
const auto& display_dimensions = ctx->app->get_display_dimensions(); const auto& display_dimensions = ctx->app->get_display_dimensions();
int2 resolution = {display_dimensions[0], display_dimensions[1]}; int2 resolution = {display_dimensions[0], display_dimensions[1]};
if (fullscreen) if (fullscreen)
@ -381,9 +397,7 @@ void setup_window(game_context* ctx)
if (config->has("windowed_resolution")) if (config->has("windowed_resolution"))
resolution = config->get<int2>("windowed_resolution"); resolution = config->get<int2>("windowed_resolution");
} }
app->resize_window(resolution.x, resolution.y); app->resize_window(resolution.x, resolution.y);
logger->pop_task(EXIT_SUCCESS);
// Set v-sync // Set v-sync
bool vsync = true; bool vsync = true;
@ -394,8 +408,7 @@ void setup_window(game_context* ctx)
app->set_vsync(vsync); app->set_vsync(vsync);
// Set title // Set title
std::string title = "Antkeeper";
app->set_title(title);
app->set_title((*ctx->strings)["title"]);
logger->pop_task(EXIT_SUCCESS); logger->pop_task(EXIT_SUCCESS);
} }

+ 8
- 0
src/game/game-context.hpp View File

@ -21,6 +21,7 @@
#define ANTKEEPER_GAME_CONTEXT_HPP #define ANTKEEPER_GAME_CONTEXT_HPP
#include "utility/fundamental-types.hpp" #include "utility/fundamental-types.hpp"
#include "resources/string-table.hpp"
#include <optional> #include <optional>
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <fstream> #include <fstream>
@ -116,6 +117,13 @@ struct game_context
// Resources // Resources
resource_manager* resource_manager; 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<std::string, std::string>* strings;
// Framebuffers // Framebuffers
framebuffer* shadow_map_framebuffer; framebuffer* shadow_map_framebuffer;
texture_2d* shadow_map_depth_texture; texture_2d* shadow_map_depth_texture;

+ 15
- 2
src/resources/string-table.cpp View File

@ -19,7 +19,21 @@
#include "resources/string-table.hpp" #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; string_table_index index;
@ -30,4 +44,3 @@ string_table_index createIndex(const string_table& table)
return index; return index;
} }

+ 5
- 1
src/resources/string-table.hpp View File

@ -39,12 +39,16 @@ typedef std::vector string_table;
*/ */
typedef std::unordered_map<std::string, std::size_t> string_table_index; typedef std::unordered_map<std::string, std::size_t> string_table_index;
typedef std::unordered_map<std::string, std::unordered_map<std::string, std::string>> 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. * 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. * @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 #endif // ANTKEEPER_STRING_TABLE_HPP

Loading…
Cancel
Save