From ebe6c95a88c34988859fcd475839afdf7fcf5dd0 Mon Sep 17 00:00:00 2001 From: ZedThree Date: Sat, 26 Jul 2014 22:05:46 +0200 Subject: [PATCH] Add localised currency string formatting - fixes #222 --- projects/openrct2.vcxproj | 2 ++ projects/openrct2.vcxproj.filters | 6 ++++ src/config.h | 22 +++--------- src/currency.c | 34 ++++++++++++++++++ src/currency.h | 52 +++++++++++++++++++++++++++ src/string_ids.c | 58 ++++++++++++++++++++----------- 6 files changed, 137 insertions(+), 37 deletions(-) create mode 100644 src/currency.c create mode 100644 src/currency.h diff --git a/projects/openrct2.vcxproj b/projects/openrct2.vcxproj index 89c9d0d52b..13664c696b 100644 --- a/projects/openrct2.vcxproj +++ b/projects/openrct2.vcxproj @@ -20,6 +20,7 @@ + @@ -62,6 +63,7 @@ + diff --git a/projects/openrct2.vcxproj.filters b/projects/openrct2.vcxproj.filters index 00b53db492..25bb24864f 100644 --- a/projects/openrct2.vcxproj.filters +++ b/projects/openrct2.vcxproj.filters @@ -138,6 +138,9 @@ Header Files + + Header Files + @@ -335,6 +338,9 @@ Windows + + Source Files + diff --git a/src/config.h b/src/config.h index 6e43a04b4e..0d3ffa8ad8 100644 --- a/src/config.h +++ b/src/config.h @@ -21,6 +21,7 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ +#include "currency.h" #include "rct2.h" #include // for MAX_PATH @@ -87,20 +88,6 @@ enum { MEASUREMENT_FORMAT_METRIC }; -enum{ - CURRENCY_POUNDS, - CURRENCY_DOLLARS, - CURRENCY_FRANC, - CURRENCY_DEUTSCHMARK, - CURRENCY_YEN, - CURRENCY_PESETA, - CURRENCY_LIRA, - CURRENCY_GUILDERS, - CURRENCY_KRONA, - CURRENCY_EUROS - -}; - enum{ SOUND_QUALITY_LOW, SOUND_QUALITY_MEDIUM, @@ -156,9 +143,10 @@ static const struct { char *key; int value; } _currencyLookupTable[] = { { "DEK", CURRENCY_KRONA }, { "EUR", CURRENCY_EUROS }, - { "£", CURRENCY_POUNDS }, - { "$", CURRENCY_DOLLARS }, - { "€", CURRENCY_EUROS } + { "\xA3", CURRENCY_POUNDS }, + { "\x24", CURRENCY_DOLLARS }, + { "\xA5", CURRENCY_YEN }, + { "\xB5", CURRENCY_EUROS } }; //typedef struct hotkey_configuration{ diff --git a/src/currency.c b/src/currency.c new file mode 100644 index 0000000000..80d62eb5a3 --- /dev/null +++ b/src/currency.c @@ -0,0 +1,34 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John, Peter Hill + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ + +#include "currency.h" + +const rct_currency_spec g_currency_specs[CURRENCY_END] = { + {10 , "\xA3" , 1}, //British Pound + {10 , "\x24" , 1}, //US Dollar + {10 , "FRF" , 1}, //French Franc + {10 , "DM" , 1}, //Deutsche Mark + {1000 , "\xA5" , 1}, //Japanese Yen + {10 , "Pts" , 0}, //Spanish Peseta + {1000 , "L" , 1}, //Italian Lira + {10 , "Dfl." , 1}, //Dutch Guilder + {10 , "kr." , 0}, //Swedish Krona + {10 , "\xb5" , 1}, //Euro +}; diff --git a/src/currency.h b/src/currency.h new file mode 100644 index 0000000000..cce9e54b59 --- /dev/null +++ b/src/currency.h @@ -0,0 +1,52 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John, Peter Hill + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ + +#ifndef CURRENCY_H +#define CURRENCY_H + +// List of currencies +typedef enum { + CURRENCY_POUNDS, //British Pound + CURRENCY_DOLLARS, //US Dollar + CURRENCY_FRANC, //French Franc + CURRENCY_DEUTSCHMARK, //Deutsche Mark + CURRENCY_YEN, //Japanese Yen + CURRENCY_PESETA, //Spanish Peseta + CURRENCY_LIRA, //Italian Lira + CURRENCY_GUILDERS, //Dutch Gilder + CURRENCY_KRONA, //Swedish Krona + CURRENCY_EUROS, //Euro + + CURRENCY_END //Last item +} CURRENCY_TYPE; + +// Currency format specification - inspired by OpenTTD +typedef struct { + // Rate is relative to 0.1 GBP + int rate; + char symbol[8]; + // 0: symbol is a suffix, 1: symbol is a prefix + int prefix; +} rct_currency_spec; + +// List of currency formats +extern const rct_currency_spec g_currency_specs[CURRENCY_END]; + +#endif diff --git a/src/string_ids.c b/src/string_ids.c index b26be2e08c..32a1697ef1 100644 --- a/src/string_ids.c +++ b/src/string_ids.c @@ -21,6 +21,8 @@ #include #include #include "addresses.h" +#include "config.h" +#include "currency.h" #include "game.h" #include "date.h" #include "rct2.h" @@ -1218,48 +1220,64 @@ void format_comma_separated_fixed_2dp(char **dest, int value) void format_currency(char **dest, int value) { - int digit, groupIndex; - char *dst = *dest; - char *finish; - char tmp; + int rate = g_currency_specs[gGeneral_config.currency_format].rate; + value *= rate; // Negative sign if (value < 0) { - *dst++ = '-'; + *(*dest)++ = '-'; value = -value; } // Currency symbol - *dst++ = '£'; + char *symbol = &(g_currency_specs[gGeneral_config.currency_format].symbol); + // Prefix + if (g_currency_specs[gGeneral_config.currency_format].prefix) { + strcpy(*dest, symbol); + *dest += strlen(*dest); + } - *dest = dst; + // Divide by 100 to get rid of the pennies + format_comma_separated_integer(dest, value/100); - // value is in tens of pennies, so to get rid of - // the pence, just divide by ten - format_comma_separated_integer(dest, value/10); + // Currency symbol suffix + if (!g_currency_specs[gGeneral_config.currency_format].prefix) { + strcpy(*dest, symbol); + *dest += strlen(*dest); + } } void format_currency_2dp(char **dest, int value) { - int digit, groupIndex; - char *dst = *dest; - char *finish; - char tmp; + int rate = g_currency_specs[gGeneral_config.currency_format].rate; + value *= rate; // Negative sign if (value < 0) { - *dst++ = '-'; + *(*dest)++ = '-'; value = -value; } // Currency symbol - *dst++ = '£'; + char *symbol = &(g_currency_specs[gGeneral_config.currency_format].symbol); + // Prefix + if (g_currency_specs[gGeneral_config.currency_format].prefix) { + strcpy(*dest, symbol); + *dest += strlen(*dest); + } - *dest = dst; + // Drop the pennies for "large" currencies + if (rate > 10) { + format_comma_separated_integer(dest, value/100); + } else { + format_comma_separated_fixed_2dp(dest, value); + } - // value is in tens of pennies, so multiply by 10 - // to get in pounds and pence - format_comma_separated_fixed_2dp(dest, value*10); + // Currency symbol suffix + if (!g_currency_specs[gGeneral_config.currency_format].prefix) { + strcpy(*dest, symbol); + *dest += strlen(*dest); + } } void format_string_code(unsigned char format_code, char **dest, char **args)