commit 15b9c93b
New printf functions to make some translation stuff faster and simpler
Changed files
| M | source/code/common.cpp before |
| M | source/code/common.h before |
| M | source/code/main.cpp before |
diff --git a/source/code/common.cpp b/source/code/common.cpp
index 99fa6ef..98023cc 100644
--- a/source/code/common.cpp
+++ b/source/code/common.cpp
@@ -26,6 +26,7 @@ http://blockattack.net
#include <cstring>
#include "os.hpp"
#include "sago/SagoMiscSdl2.hpp"
+#include <stdarg.h>
using namespace std;
using boost::format;
@@ -57,11 +58,6 @@ void dieOnNullptr(bool ptr, const char* msg) {
}
}
-/**
- * str2double parses a string and returns a double with the value of the string.
- * if the string is not a double then 0.0 is returned instead of throing an error
- * in that way this function will always return a useable value.
- */
double str2double(const string& str2parse) {
try {
converter.clear();
@@ -75,11 +71,26 @@ double str2double(const string& str2parse) {
}
}
-/**
- * str2int parses a string and returns an int with the value of the string.
- * if the string is not an int then 0 is returned instead of throing an error
- * in that way this function will always return a useable value.
- */
+std::string SPrintStringF(const char* fmt, ...) {
+ std::string ret;
+ char buffer[1024];
+ va_list args;
+ va_start(args, fmt);
+ vsnprintf(buffer, sizeof(buffer), fmt, args);
+ ret = buffer;
+ va_end(args);
+ return ret;
+}
+
+const char* SPrintCF(const char* fmt, ...) {
+ static char buffer[1024];
+ va_list args;
+ va_start(args, fmt);
+ vsnprintf(buffer, sizeof(buffer), fmt, args);
+ va_end(args);
+ return buffer;
+}
+
int str2int(const string& str2parse) {
try {
converter.clear();
diff --git a/source/code/common.h b/source/code/common.h
index c5a26fd..9b49e6b 100644
--- a/source/code/common.h
+++ b/source/code/common.h
@@ -71,6 +71,24 @@ void dieOnNullptr(bool, const char* msg);
*/
double str2double(const std::string &str2parse) __attribute__((const));
+/**
+ * Does the eqivilent to snprintf but returns a C++ string
+ * @param fmt The format string
+ * @param ... Additional paremeters for the place holders
+ * @return A string with the result
+ */
+std::string SPrintStringF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
+
+/**
+ * Prints to an internal C-buffer
+ * Because it uses an internal buffer the returned buffer is only valid until the next call
+ * The String is cut at 1024 chars (including the 0 terminator)
+ * This is the larges string that can safely be parsed to NFont
+ * @param fmt The format string
+ * @param ... Additional paremeters for the place holders
+ * @return Pointer to an internal buffer
+ */
+const char* SPrintCF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
class TimeHandler
{
diff --git a/source/code/main.cpp b/source/code/main.cpp
index ed1ece3..9c956f6 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -31,7 +31,7 @@ http://blockattack.net
#ifndef VERSION_NUMBER
-#define VERSION_NUMBER "version 2.0.0 BETA"
+#define VERSION_NUMBER "2.0.0 SNAPSHOT"
#endif
//If DEBUG is defined: AI info and FPS will be written to screen
@@ -247,6 +247,10 @@ void NFont_Write(SDL_Renderer* target, int x, int y, const string& text) {
nf_standard_blue_font.draw(target, x, y, "%s", text.c_str());
}
+void NFont_Write(SDL_Renderer* target, int x, int y, const char* text) {
+ nf_standard_blue_font.draw(target, x, y, "%s", text);
+}
+
SDL_Window* sdlWindow;
void ResetFullscreen() {
@@ -1041,26 +1045,26 @@ void DrawStats() {
NFont_Write(screen, 10,y, _("Run time: ") );
commonTime ct = TimeHandler::peekTime("totalTime",TimeHandler::ms2ct(SDL_GetTicks()));
y+=y_spacing;
- NFont_Write(screen, 10,y,((string)( _("Days: ")+itoa(ct.days))).c_str());
+ NFont_Write(screen, 10, y, SPrintCF( _("Days: %i"), ct.days) );
y+=y_spacing;
- NFont_Write(screen, 10,y,((string)( _("Hours: ")+itoa(ct.hours))).c_str());
+ NFont_Write(screen, 10, y, SPrintCF( _("Hours: %i"), ct.hours) );
y+=y_spacing;
- NFont_Write(screen, 10,y,((string)( _("Minutes: ")+itoa(ct.minutes))).c_str());
+ NFont_Write(screen, 10, y, SPrintCF( _("Minutes: %i"), ct.minutes) );
y+=y_spacing;
- NFont_Write(screen, 10,y,((string)( _("Seconds: ")+itoa(ct.seconds))).c_str());
+ NFont_Write(screen, 10, y, SPrintCF( _("Seconds: %i"), ct.seconds) );
y-=y_spacing*4; //Four rows back
const int x_offset3 = xsize/3+10; //Ofset for three rows
NFont_Write(screen, x_offset3,y, _("Play time: ") );
ct = TimeHandler::getTime("playTime");
y+=y_spacing;
- NFont_Write(screen, x_offset3,y,((string)( _("Days: ")+itoa(ct.days))).c_str());
+ NFont_Write(screen, x_offset3, y, SPrintCF( _("Days: %i"), ct.days) );
y+=y_spacing;
- NFont_Write(screen, x_offset3,y,((string)( _("Hours: ")+itoa(ct.hours))).c_str());
+ NFont_Write(screen, x_offset3, y, SPrintCF( _("Hours: %i"), ct.hours) );
y+=y_spacing;
- NFont_Write(screen, x_offset3,y,((string)( _("Minutes: ")+itoa(ct.minutes))).c_str());
+ NFont_Write(screen, x_offset3, y, SPrintCF( _("Minutes: %i"), ct.minutes) );
y+=y_spacing;
- NFont_Write(screen, x_offset3,y,((string)( _("Seconds: ")+itoa(ct.seconds))).c_str());
+ NFont_Write(screen, x_offset3, y, SPrintCF( _("Seconds: %i"), ct.seconds) );
const int x_offset = xsize/2+10;
y = 5+y_spacing*2;
@@ -1784,7 +1788,7 @@ static void StartTwoPlayerVs() {
}
//The main function, quite big... too big
-int main(int argc, char* argv[]) {
+int main(int argc, const char* argv[]) {
try {
OsCreateFolders();
highPriority = false; //if true the game will take most resources, but increase framerate.
@@ -1793,12 +1797,16 @@ int main(int argc, char* argv[]) {
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
- boost::program_options::options_description desc("Allowed options");
+ boost::program_options::options_description desc(
+ SPrintStringF(_("Block Attack - Rise of the blocks %s\n"
+ "%s\n\n"
+ "Allowed options"), VERSION_NUMBER, "www.blockattack.net")
+ );
desc.add_options()
- ("help,h", _("Displays this message"))
- ("nosound", _("Disables the sound. Can be used if sound gives you programs starting"))
- ("priority", _("Causes the game to not sleep between frames."))
- ("verbose-basic", _("Enables basic verbose messages"))
+ ("help,h", _("Displays this message"))
+ ("nosound", _("Disables the sound. Can be used if sound errors prevents you from starting"))
+ ("priority", _("Causes the game to not sleep between frames."))
+ ("verbose-basic", _("Enables basic verbose messages"))
;
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
@@ -1861,8 +1869,8 @@ int main(int argc, char* argv[]) {
if (verboseLevel) {
//Copyright notice:
- cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << endl << "http://blockattack.net" << endl << "Copyright 2004-2011 Poul Sander" << endl <<
- "A SDL based game (see www.libsdl.org)" << endl <<
+ cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << endl << "http://www.blockattack.net" << endl << "Copyright 2004-2016 Poul Sander" << endl <<
+ "A SDL2 based game (see www.libsdl.org)" << endl <<
"The game is availeble under the GPL, see COPYING for details." << endl;
cout << "-------------------------------------------" << endl;
}