Purge all uses of printf, fprintf, perror and most uses of sprintf

Also:
- Print "Target Spell" before the explanatory prompts, instead of after (when fancy targeting)
- Fix sell costs being drawn on top of the button
- String-insensitive comparing for dialogue keys
This commit is contained in:
2015-01-08 22:26:41 -05:00
parent 8350a22ecb
commit 3ef98712d5
34 changed files with 449 additions and 798 deletions

View File

@@ -467,7 +467,7 @@ void load_spec_graphics(fs::path scen_file) {
static const char*const noGraphics = "The game will still work without the custom graphics, but some things will not look right.";
short i;
fs::path path(scen_file);
printf("Loading scenario graphics... (%s)\n",path.string().c_str());
std::cout << "Loading scenario graphics... (" << path << ")\n";
// Tried path.replace_extension, but that only deleted the extension, so I have to do it manually
std::string filename = path.stem().string();
path = path.parent_path();
@@ -1049,3 +1049,6 @@ std::string maybe_quote_string(std::string which) {
return which;
}
std::ostream& std_fmterr(std::ostream& out) {
return out << strerror(errno);
}

View File

@@ -59,6 +59,9 @@ void readArray(std::istream& from, T(* array)[D], int width, int height) {
}
}
// Manipulator to write an error code to a C++ string, similar to how perror() does
std::ostream& std_fmterr(std::ostream& out);
// SFML doesn't support standard C++ streams, so I need to do this in order to load images from a stream.
class StdInputStream : public sf::InputStream {
std::istream& stream;

View File

@@ -18,6 +18,7 @@
#include <GL/GL.h>
#endif
#include <iostream>
#include <typeinfo>
#include <unordered_map>
#include <boost/filesystem.hpp>
@@ -25,6 +26,7 @@
#include "restypes.hpp"
#include "mathutil.h"
#include "fileio.h"
using boost::math::constants::pi;
@@ -49,7 +51,7 @@ void init_graph_tool(){
std::ifstream fin;
fin.open(fragPath.c_str());
if(!fin.good()) perror("Error loading fragment shader");
if(!fin.good()) std::cerr << std_fmterr << ": Error loading fragment shader" << std::endl;
fin.seekg(0, std::ios::end);
int size = fin.tellg();
fin.seekg(0);
@@ -59,7 +61,7 @@ void init_graph_tool(){
fin.close();
fin.open(vertPath.c_str());
if(!fin.good()) perror("Error loading vertex shader");
if(!fin.good()) std::cerr << std_fmterr << ": Error loading vertex shader" << std::endl;
fin.seekg(0, std::ios::end);
size = fin.tellg();
fin.seekg(0);
@@ -68,7 +70,7 @@ void init_graph_tool(){
fin.read(vbuf, size);
if(!maskShader.loadFromMemory(vbuf, fbuf)) {
fprintf(stderr,"Error: Failed to load shaders from %s\nVertex:\n%s\nFragment:\n%s",shaderPath.string().c_str(),vbuf,fbuf);
std::cerr << "Error: Failed to load shaders from " << shaderPath << "\nVertex:\n" << vbuf << "\nFragment:\n" << fbuf << std::endl;
}
delete[] fbuf;
delete[] vbuf;
@@ -1036,7 +1038,7 @@ public:
return {w - r - r*cos(t - half_pi), r + r*sin(t - half_pi)};
}
// Unreachable
printf("Whoops, rounded rectangle had bad point!");
std::cerr << "Whoops, rounded rectangle had bad point!" << std::endl;
return {0,0};
}

View File

@@ -36,3 +36,12 @@ void erase_if(ContainerT& items, const PredicateT& predicate) {
else ++it;
}
};
// Case-insensitive string comparison seems to be semi-standard, but with different names.
#if defined(__APPLE__)
#define strnicmp strncasecmp
#elif defined(_MSC_VER)
#define strnicmp _strnicmp
#else
#error Missing strnicmp / strncasecmp
#endif

View File

@@ -16,6 +16,7 @@
#include <memory>
#include <boost/filesystem.hpp>
#include <functional>
#include <iostream>
/// A simple resource manager.
/// Handles loading, retaining, and releasing of resources as necessary.
@@ -59,7 +60,7 @@ namespace ResMgr {
std::stack<fs::path> tmpPaths = resPaths();
while(!tmpPaths.empty()) {
fs::path thisPath = tmpPaths.top()/path;
printf("Testing %s...\n",thisPath.string().c_str());
std::cout << "Testing " << thisPath << "...\n";
if(fs::exists(thisPath)) {
pathFound()[path] = tmpPaths.top();
return thisPath;
@@ -167,7 +168,7 @@ namespace ResMgr {
template<typename type> void pushPath(fs::path path) {
printf("Pushing path %s in %s...\n",path.string().c_str(),__FUNCTION__);
resPool<type>::resPaths().push(path);
if(resPool<type>::resPaths().empty()) printf("A problem occurred.\n");
if(resPool<type>::resPaths().empty()) std::cerr << "A problem occurred.\n";
}
/// Pop a path from the path resolution stack.

View File

@@ -16,6 +16,7 @@
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <map>
#include <boost/filesystem/path.hpp>
#include "cursors.h"
@@ -27,6 +28,9 @@ using FontRsrc = sf::Font;
using StringRsrc = std::vector<std::string>;
using SoundRsrc = sf::SoundBuffer;
// Redeclare this instead of including "fileio.h"
extern std::ostream& std_fmterr(std::ostream& out);
namespace ResMgr {
/// Load an image from a PNG file.
template<> inline ImageRsrc* resLoader<ImageRsrc>::operator() (std::string fname) {
@@ -61,9 +65,9 @@ namespace ResMgr {
} else {
auto entry = cursor_hs.find(fname);
if(entry == cursor_hs.end())
fprintf(stderr,"Cursor hotspot missing: %s\n",fname.c_str());
std::cerr << "Cursor hotspot missing: " << fname << std::endl;
else {
fprintf(stderr,"Cursor hotspot missing (using fallback value): %s\n",fname.c_str());
std::cerr << "Cursor hotspot missing (using fallback value): " << fname << std::endl;
location hs = entry->second;
x = hs.x; y = hs.y;
}
@@ -92,7 +96,7 @@ namespace ResMgr {
fs::path fpath = resPool<StringRsrc>::rel2abs(fname + ".txt");
std::ifstream fin(fpath.c_str());
if(fin.fail()) {
perror("Error opening file");
std::cerr << std_fmterr << ": Error opening file";
throw xResMgrErr("Failed to load string list: " + fpath.string());
}
std::string next;

View File

@@ -83,9 +83,7 @@ void play_sound(short which, short how_many_times) { // if < 0, play asynch
if(!play_sounds || how_many_times == 0) return;
if(abs(which) > NUM_SOUNDS) {
//char msg[50];
/*s*/printf(/*msg,*/"Error: Sound #%i does not exist.\n",abs(which));
//give_error(msg,"",0);
std::cerr << "Error: Sound #" << abs(which) << " does not exist." << std::endl;
return;
}

View File

@@ -84,7 +84,7 @@ void init_specials_parse() {
eSpecCat category = getNodeCategory(check);
if(category == eSpecCat::INVALID) continue;
if((*check).opcode().empty())
printf("Warning: Missing opcode definition for special node type with ID %d\n", i);
std::cout << "Warning: Missing opcode definition for special node type with ID " << i << std::endl;
else opcode.add((*check).opcode(), check);
}
}