#ifndef UTILS_H
#define UTILS_H
#if defined(WIN32)
#include <tchar.h>
#else
#ifndef _TCHAR
#define _TCHAR char
#define TCHAR _TCHAR
#define _T(x) (x)
#define _tmain main
#endif
#endif
#include <stdexcept>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#endif
namespace bex {
typedef std::basic_string<TCHAR> tstring;
typedef std::basic_ostream<TCHAR> tostream;
typedef std::basic_ofstream<TCHAR> tofstream;
inline tstring string2tstring(const std::string& src) {
#ifdef UNICODE
size_t chars = strlen(src.c_str()) + 1;
tstring result;
result.resize(chars);
if (MultiByteToWideChar(CP_ACP, 0, src.c_str(), (int)chars, (LPWSTR)result.c_str(), (int)chars) == 0) {
return L"";
}
return result;
#else
return src;
#endif
}
class Exception : public std::runtime_error {
public:
Exception(
ILBStatus s) : std::runtime_error(
"Beast exception"), status(s) {}
};
throw Exception(s);
}
}
int32 len;
std::basic_string<TCHAR> result(len - 1, '\0');
apiCall(
ILBCopy(h, &result[0], static_cast<int32>(len)));
return result;
}
std::basic_ostream<TCHAR>& logTarget,
bool returnWhenComplete = false,
bool destroyJob = true,
bool firstJob = true;
int32 oldProgress = 0;
while (isRunning) {
if (!completed) {
if (completed) {
logTarget << "Job is completed. It might still be running if the user has selected ILB_SR_KEEP_OPEN." << std::endl;
}
}
if (isRunning) {
if (newProgress) {
const int progressBarSize = 20;
int32 progress;
progress /= 100 / progressBarSize;
std::basic_string<TCHAR> jobNameString = convertStringHandle(taskName);
if (newActivity) {
if (!firstJob && oldProgress < progressBarSize) {
for (int i = 0; i < progressBarSize-oldProgress; i++) {
logTarget << "-";
}
logTarget << "]" << std::endl;
} else {
firstJob = false;
}
logTarget << jobNameString << std::endl;
logTarget << "[";
}
for (int i = 0; i < progress-oldProgress; i++) {
logTarget << "-";
}
if (progress == progressBarSize && oldProgress < progressBarSize) {
logTarget << "]" << std::endl;
}
logTarget.flush();
oldProgress = progress;
}
logTarget.flush();
}
}
switch(status) {
logTarget << "User canceled rendering" << std::endl;
break;
logTarget << "Problem with the Beast License!" << std::endl;
break;
logTarget << "Error parsing Beast command line!" << std::endl;
break;
logTarget << "Error parsing Beast config files!" << std::endl;
break;
logTarget << "Error: Beast crashed!" << std::endl;
break;
logTarget << "Other error running Beast." << std::endl;
break;
}
return false;
}
if (destroyJob) {
}
return true;
}
inline bool findCachedMesh(
ILBManagerHandle bmh,
const std::basic_string<TCHAR>& name,
ILBMeshHandle& target, std::basic_ostream<TCHAR>& logTarget) {
logTarget << "Found the mesh: " << name << ", in the cache!" << std::endl;
return true;
}
logTarget << "Didn't find the mesh: " << name << ", creating it!" << std::endl;
return false;
}
throw Exception(findRes);
}
logTarget << "Found the texture: " << name << ", in the cache!" << std::endl;
return true;
}
logTarget << "Didn't find the texture: " << name << ", creating it!" << std::endl;
return false;
}
throw Exception(findRes);
}
#ifdef WIN32
inline std::basic_string<TCHAR> getLicenseKey() {
std::basic_stringstream<TCHAR> result;
char *tempVal;
size_t length;
errno_t error = _dupenv_s(&tempVal, &length, "BEAST_LICENSE");
if (error == 0 && tempVal != NULL) {
result << tempVal;
free(tempVal);
}
return result.str();
}
#else
inline std::basic_string<TCHAR> getLicenseKey() {
std::basic_stringstream<TCHAR> result;
char* temp = getenv("BEAST_LICENSE");
if (temp != NULL) {
result << temp;
}
return result.str();
}
#endif
}
#endif // UTILS_H