From b62beee3e4694fd955de3586219fa9b6fb6864d6 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 4 Aug 2021 21:58:31 -0700 Subject: [PATCH] Improved Language Support --- include/dawn/game/poker/pokergame.h | 4 + include/dawn/game/poker/pokerui.h | 8 +- include/dawn/locale/language.h | 1 + include/dawn/vn/vncharacter.h | 2 + src/game/game.h | 2 + src/game/poker/pokergame.c | 25 ++---- src/game/poker/pokergame.h | 3 +- src/game/poker/pokerui.c | 53 +++++++++++- src/game/poker/pokerui.h | 6 +- src/locale/language.c | 124 ++++++++++++++++++++++++---- src/locale/language.h | 32 ++++++- src/ui/label.c | 2 +- src/vn/vncharacter.c | 2 + 13 files changed, 221 insertions(+), 43 deletions(-) diff --git a/include/dawn/game/poker/pokergame.h b/include/dawn/game/poker/pokergame.h index 41582108..ee5acc28 100644 --- a/include/dawn/game/poker/pokergame.h +++ b/include/dawn/game/poker/pokergame.h @@ -11,6 +11,7 @@ #include "../../poker/poker.h" #include "../../vn/vnconversation.h" #include "../../vn/vnscene.h" +#include "pokerui.h" /** Name of the Poker Game */ #define POKER_GAME_NAME "Dawn Poker Game" @@ -24,4 +25,7 @@ typedef struct { /** Assets for the game. */ pokergameassets_t assets; + + /** UI For the Game */ + pokerui_t ui; } pokergame_t; \ No newline at end of file diff --git a/include/dawn/game/poker/pokerui.h b/include/dawn/game/poker/pokerui.h index ed2cf7d7..0bd63696 100644 --- a/include/dawn/game/poker/pokerui.h +++ b/include/dawn/game/poker/pokerui.h @@ -8,7 +8,13 @@ #pragma once #include "../../libs.h" #include "../../ui/label.h" +#include "../../poker/player.h" typedef struct { - label_t labelTest; + label_t labelChips; + label_t labelName; +} pokerplayerui_t; + +typedef struct { + pokerplayerui_t players[POKER_PLAYER_COUNT]; } pokerui_t; \ No newline at end of file diff --git a/include/dawn/locale/language.h b/include/dawn/locale/language.h index 17c8a3d1..f4f3c7f1 100644 --- a/include/dawn/locale/language.h +++ b/include/dawn/locale/language.h @@ -17,6 +17,7 @@ typedef struct { typedef struct { char *key; + char *text; } languagestring_t; typedef struct { diff --git a/include/dawn/vn/vncharacter.h b/include/dawn/vn/vncharacter.h index 11242687..ad44efe6 100644 --- a/include/dawn/vn/vncharacter.h +++ b/include/dawn/vn/vncharacter.h @@ -20,6 +20,8 @@ typedef struct { float scaleX, scaleY; float blinkStart; + char *name; + bool talking; tileset_t tilesetEyes; diff --git a/src/game/game.h b/src/game/game.h index 11cbbc00..d5765ece 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -7,6 +7,8 @@ #include #include "../engine/engine.h" +#include "../locale/language.h" + #if SETTING_GAME == SETTING_GAME_POKER #include "poker/pokergame.h" #elif SETTING_GAME == SETTING_GAME_DAWN diff --git a/src/game/poker/pokergame.c b/src/game/poker/pokergame.c index 0b8a79c6..0993973f 100644 --- a/src/game/poker/pokergame.c +++ b/src/game/poker/pokergame.c @@ -7,8 +7,6 @@ #include "pokergame.h" -label_t label; - bool pokerGameInit(game_t *game) { pokergame_t *pokerGame = &game->pokerGame; @@ -17,26 +15,13 @@ bool pokerGameInit(game_t *game) { // Load the Assets pokerGameAssetsInit(&pokerGame->assets); + + // Initialize the UI. + pokerUiInit(pokerGame); // Prep the VN Conversation Engine. vnSceneInit(&pokerGame->scene, &pokerGame->assets.font); pokerGameActionStartAdd(pokerGame); - - labelInit(&label); - label.y = 32; - label.fontSize = 8.0; - labelSetText(&label, &pokerGame->assets.font, "Hello World"); - - // pokerActionMatchAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); - // pokerActionMatchAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); - // pokerActionRoundAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); - // pokerActionBlindsAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); - // pokerActionDealAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); - // vnConversationTalk(&pokerGame->scene.conversation, "Betting Round", NULL); - // pokerActionFlopAdd(&pokerGame->scene.conversation.actionQueue, &pokerGame->poker); - - - // Begin the VN conversation queue. queueNext(&pokerGame->scene.conversation.actionQueue); return true; @@ -54,11 +39,13 @@ void pokerGameUpdate(game_t *game) { // Render the visual novel scene vnSceneRenderWorld(&pokerGame->scene, &game->engine, &pokerGame->assets.shader); + vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader); - labelRender(&label, &pokerGame->assets.shader); + pokerUiRender(pokerGame); } void pokerGameDispose(game_t *game) { + pokerUiDispose(&game->pokerGame); vnSceneDispose(&game->pokerGame.scene); pokerGameAssetsDispose(&game->pokerGame.assets); } \ No newline at end of file diff --git a/src/game/poker/pokergame.h b/src/game/poker/pokergame.h index 689861c6..f9219192 100644 --- a/src/game/poker/pokergame.h +++ b/src/game/poker/pokergame.h @@ -12,8 +12,7 @@ #include "../../vn/conversation/talk.h" #include "../../vn/vnscene.h" #include "actions/start.h" - -#include "../../ui/label.h" +#include "pokerui.h" /** * Initializes the game state for the poker game. diff --git a/src/game/poker/pokerui.c b/src/game/poker/pokerui.c index 4fd70e2d..92f37908 100644 --- a/src/game/poker/pokerui.c +++ b/src/game/poker/pokerui.c @@ -7,6 +7,55 @@ #include "pokerui.h" -void pokerUiInit(pokerui_t *ui) { - +void pokerUiInit(pokergame_t *pokerGame) { + pokerplayerui_t *playerUi; + pokerplayer_t *playerPoker; + vncharacter_t *character; + uint8_t i; + + for(i = 0; i < POKER_PLAYER_COUNT; i++) { + playerUi = pokerGame->ui.players + i; + playerPoker = pokerGame->poker.players + i; + character = pokerGame->scene.characters + i; + + // Chips label + labelInit(&playerUi->labelChips); + playerUi->labelChips.fontSize = 12.0; + + // Name Label + labelInit(&playerUi->labelName); + playerUi->labelName.fontSize = 12.0; + // labelSetText(&playerUi->labelChips, &pokerGame->assets.font, character->name); + } +} + +void pokerUiRender(pokergame_t *pokerGame) { + pokerplayerui_t *playerUi; + pokerplayer_t *playerPoker; + uint8_t i; + char buffer[256]; + + for(i = 0; i < POKER_PLAYER_COUNT; i++) { + playerUi = pokerGame->ui.players + i; + playerPoker = pokerGame->poker.players + i; + + // Player Chips + sprintf(buffer, "%u chips", playerPoker->chips); + playerUi->labelChips.y = (i+1) * 32; + labelSetText(&playerUi->labelChips, &pokerGame->assets.font, buffer); + labelRender(&playerUi->labelChips, &pokerGame->assets.shader); + + // Player Name + labelRender(&playerUi->labelName, &pokerGame->assets.shader); + } +} + +void pokerUiDispose(pokergame_t *pokerGame) { + pokerplayerui_t *player; + uint8_t i; + + for(i = 0; i < POKER_PLAYER_COUNT; i++) { + player = pokerGame->ui.players + i; + labelDispose(&player->labelChips); + } } \ No newline at end of file diff --git a/src/game/poker/pokerui.h b/src/game/poker/pokerui.h index a534ab77..f66c8778 100644 --- a/src/game/poker/pokerui.h +++ b/src/game/poker/pokerui.h @@ -9,4 +9,8 @@ #include #include "../../ui/label.h" -void pokerUiInit(pokerui_t *ui); \ No newline at end of file +void pokerUiInit(pokergame_t *pokerGame); + +void pokerUiRender(pokergame_t *pokerGame); + +void pokerUiDispose(pokergame_t *pokerGame); \ No newline at end of file diff --git a/src/locale/language.c b/src/locale/language.c index d869cb47..ea219a20 100644 --- a/src/locale/language.c +++ b/src/locale/language.c @@ -7,13 +7,29 @@ #include "language.h" +void languageInit(language_t *language) { + language->stringCount = 0; +} + +languagestring_t * languageAddString(language_t *lang, char *key, char *text) { + languagestring_t *string; + + string = lang->strings + lang->stringCount; + string->key = key; + string->text = text; + + lang->stringCount++; + + return string; +} + languagestring_t * languageGetStringByKey(language_t *language, char *key) { int32_t i; languagestring_t *string; for(i = 0; i < language->stringCount; i++) { string = language->strings + i; - if(string->key != key) continue; + if(strcmp(string->key, key) != 0) continue; return string; } @@ -21,25 +37,103 @@ languagestring_t * languageGetStringByKey(language_t *language, char *key) { } char * languageGetText(language_t *language, languagestring_t *string) { - // Try and find the value - - // Load the value into memory. - - // Return the value - return NULL; + // TODO: Buffer this from somewhere. + return string->text; } -char * languageGetTextWithVariables( - language_t *language, languagestring_t *string, languagevariable_t *variable +int32_t languageGetTextWithVariables( + language_t *language, languagestring_t *string, + languagevariable_t *variables, int32_t variableCount, + char *buffer ) { - char *text = languageGetText(language, string); - if(text == NULL) return NULL; + int32_t i, l, n; + char *text; + char c; + char keyBuffer[32]; + languagevariable_t *variable; - // Scan the string, determine the new length and positions of the variables + text = languageGetText(language, string); + if(text == NULL) return 0; - // Now create some memory for the new string + // Start two counters. I holds source index, L holds target index. + i = 0; + l = 0; - // Now buffer the old string into the new string and take the language + while(true) { + c = text[i]; + if(c == '\0') break;// Break on end of string. - return text; + // Look for {{, if not {{ then just treat as normal string. + if(c != '{' || text[i + 1] != '{') { + buffer[l] = c; + i++; + l++; + continue; + } + + //Ignore those two chars + i += 2; + + // Skip space(s) + while(true) { + c = text[i]; + if(c != ' ') break; + i++; + } + + // Get the key name + n = 0;// Will hold the index within keyBuffer to copy chars into. + while(true) { + c = text[i]; + + // Don't overflow + if(c == '\0') break; + + // Skip spaces + if(c == ' ') { + i++; + continue; + } + + // Handle end of key + if(c == '}') { + i++; + if(text[i] == '}') i++;// For }} then skip the second } + break; + } + + // Add to buffer. + keyBuffer[n] = c; + n++; + i++; + } + + // Seal the keyBuffer string + keyBuffer[n] = '\0'; + + // Now check each variable for a match. + for(n = 0; n < variableCount; n++) { + variable = variables + n; + if(strcmp(keyBuffer, variable->name) != 0) continue;// Does the key match? + + // Begin copying the variables' value into the string. + n = 0; + while(true) { + if(variable->value[n] == '\0') break;// Handle end of variable value + buffer[l] = variable->value[n]; + l++; + n++; + } + // We found the value, so break. + break; + } + + // Continue looking for next {{ variable }}... + } + + // Buffer has been fully cloned, seal the string. + buffer[l] = '\0'; + + // Return the count of chars we wrote to the buffer. -1 due to null term. + return l - 1; } \ No newline at end of file diff --git a/src/locale/language.h b/src/locale/language.h index 0deb3e8a..4d84c138 100644 --- a/src/locale/language.h +++ b/src/locale/language.h @@ -8,6 +8,21 @@ #pragma once #include +/** + * Initializes a language. + * @param language Language to initialize. + */ +void languageInit(language_t *language); + +/** + * Adds a string to a language. + * @param lang Language to add to. + * @param key Key to use for the string. + * @param text Value to use for the language string + * @return The language string added to the language. + */ +languagestring_t * languageAddString(language_t *lang, char *key, char *text); + /** * Returns the language string for a given language string key. * @@ -26,6 +41,19 @@ languagestring_t * languageGetStringByKey(language_t *language, char *key); */ char * languageGetText(language_t *language, languagestring_t *string); -char * languageGetTextWithVariables( - language_t *language, languagestring_t *string, languagevariable_t *variable +/** + * Retreives the value of a given language string, and replaces the variables in + * the string with the provided variable values. + * + * @param language Language to read from. + * @param string String to get and parse. + * @param variables Variables to use in the parsing. + * @param variableCount How many variables in the array. + * @param buffer The buffer to write the output data to. + * @return The count of characters that were written to the buffer. + */ +int32_t languageGetTextWithVariables( + language_t *language, languagestring_t *string, + languagevariable_t *variables, int32_t variableCount, + char *buffer ); \ No newline at end of file diff --git a/src/ui/label.c b/src/ui/label.c index 595d5974..b3de75e5 100644 --- a/src/ui/label.c +++ b/src/ui/label.c @@ -23,7 +23,7 @@ void labelSetText(label_t *label, font_t *font, char *text) { } label->font = font; - fontTextClamp(font, &label->info, text, -1, 16.0); + fontTextClamp(font, &label->info, text, -1, label->fontSize); fontTextInit(font, &label->primitive, &label->info); } diff --git a/src/vn/vncharacter.c b/src/vn/vncharacter.c index 0131fa15..2f61e69b 100644 --- a/src/vn/vncharacter.c +++ b/src/vn/vncharacter.c @@ -36,6 +36,8 @@ void vnCharacterInit(vncharacter_t *character, character->talking = false; character->blinkStart = randFloatRange(0, VN_CHARACTER_BLINK_TIME_RANGE_MAX); + character->name = ""; + // Setup Textures character->textureEyes = textureEyes; character->textureBody = textureBody;