void vsFailedAssert( const vsString &conditionStr, const vsString &msg, const char *file, int line ) { if ( !bAsserted ) { // failed assertion.. trace out some information and then crash. bAsserted = true; vsString trimmedFile(file); size_t pos = trimmedFile.rfind('/'); if ( pos ) { trimmedFile = trimmedFile.substr(pos+1); } vsLog("Failed assertion: %s", msg.c_str()); vsLog("Failed condition: (%s)", conditionStr.c_str()); vsLog("at %s:%d", trimmedFile.c_str(), line); vsLog_End(); { #if defined(_DEBUG) DEBUG_BREAK; #else vsString mbString = vsFormatString("Failed assertion: %s\nFailed condition: (%s)\nat %s:%d", msg.c_str(), conditionStr.c_str(), trimmedFile.c_str(), line); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed assertion", mbString.c_str(), NULL); #endif CRASH; } } else { vsLog("Error: Asserted while handling assertion!"); } }
void vsFile::EnsureWriteDirectoryExists( const vsString &writeDirectoryName ) // static method { if ( !DirectoryExists(writeDirectoryName) ) { int mkdirResult = PHYSFS_mkdir( writeDirectoryName.c_str() ); vsAssert( mkdirResult != 0, vsFormatString("Failed to create directory '%s%s%s': %s", PHYSFS_getWriteDir(), PHYSFS_getDirSeparator(), writeDirectoryName.c_str(), PHYSFS_getLastError()) ); } }
void vsErrorLog(const vsString &str) { fprintf(stderr, "%s\n", str.c_str() ); if ( s_log ) { PHYSFS_write( s_log, str.c_str(), 1, str.size() ); #ifdef _WIN32 PHYSFS_write( s_log, "\r\n", 1, 2 ); #else PHYSFS_write( s_log, "\n", 1, 1 ); #endif } }
float vsBuiltInFont::GetStringWidth( const vsString &string, float size, float capSize ) { if ( capSize == 0.f ) capSize = size; float width = 0.f; size_t len = strlen(string.c_str()); for ( size_t i = 0; i < len; i++ ) { char thisChar = string[i]; char upperChar = toupper(thisChar); float thisSize = size; if ( thisChar == ' ' ) { // half width spaces, because that looks better. thisSize *= c_spaceKerning; } else if ( upperChar >= 'A' && upperChar <= 'Z' && thisChar == upperChar && capSize > 0.f ) { thisSize = capSize; } width += 2.0f * c_kerningFactor * thisSize; } return width; }
void vsLocalisationTable::Init(const vsString &language) { s_localisationTable = new vsHashTable<vsString>( 128 ); vsString filename = vsFormatString("%s.xlat", language.c_str()); if ( vsFile::Exists(filename) ) { vsFile table(filename); vsString str; vsRecord r; while( table.Record(&r) ) { if ( r.GetTokenCount() > 0 ) { vsString label = r.GetLabel().AsString(); vsString string = r.GetToken(0).AsString(); s_localisationTable->AddItemWithKey(string, label); } } } }
bool vsFile::DeleteDirectory( const vsString &filename ) { if ( DirectoryExists(filename) ) { vsArray<vsString> files; DirectoryContents(&files, filename); for ( int i = 0; i < files.ItemCount(); i++ ) { vsString ff = vsFormatString("%s/%s", filename.c_str(), files[i].c_str()); if ( vsFile::DirectoryExists( ff ) ) { // it's a directory; remove it! DeleteDirectory( ff ); } else { // it's a file, delete it. Delete( ff ); } } // I should now be empty, so delete me. return DeleteEmptyDirectory( filename ); } return false; }
bool vsFile::Delete( const vsString &filename ) // static method { if ( DirectoryExists(filename) ) // This file is a directory, don't delete it! return false; return PHYSFS_delete(filename.c_str()) != 0; }
vsFile::vsFile( const vsString &filename, vsFile::Mode mode ): m_mode(mode), m_length(0) { vsAssert( !DirectoryExists(filename), vsFormatString("Attempted to open directory '%s' as a plain file", filename.c_str()) ); if ( mode == MODE_Read ) m_file = PHYSFS_openRead( filename.c_str() ); else m_file = PHYSFS_openWrite( filename.c_str() ); if ( m_file ) { m_length = (size_t)PHYSFS_fileLength(m_file); } vsAssert( m_file != NULL, STR("Error opening file '%s': %s", filename.c_str(), PHYSFS_getLastError()) ); }
void vsBuiltInFont::CreateStringInDisplayList(vsDisplayList *list, const vsString &string, float size, float capSize, JustificationType j, float maxWidth) { if ( maxWidth > 0.f ) { WrapLine(string, size, capSize, j, maxWidth); float lineHeight = capSize; float lineMargin = capSize * c_lineMarginFactor; // float baseOffsetDown = lineHeight * (m_wrappedLineCount * 0.5f); float totalHeight = (lineHeight * s_wrappedLineCount) + (lineMargin * (s_wrappedLineCount-1)); float baseOffsetDown = totalHeight * 0.5f; float topLinePosition = baseOffsetDown - totalHeight + lineHeight; // float halfTotalHeight = totalHeight * 0.5f; list->Clear(); vsVector2D offset(0.f,topLinePosition); for ( int i = 0; i < s_wrappedLineCount; i++ ) { offset.y = topLinePosition + (i*(lineHeight+lineMargin)); s_tempFontList.Clear(); BuildDisplayListFromString( &s_tempFontList, s_wrappedLine[i].c_str(), size, capSize, j, offset ); list->Append(s_tempFontList); } vsVector2D tl,br; list->GetBoundingBox(tl,br); /* float height = br.y-tl.y; list->Clear(); for ( int i = 0; i < m_wrappedLineCount; i++ ) { offset.Set(0.f,lineHeight*i - 0.5f*height); s_tempFontList.Clear(); BuildDisplayListFromString( &s_tempFontList, m_wrappedLine[i].c_str(), size, capSize, j, offset ); list->Append(s_tempFontList); }*/ } else { float lineHeight = capSize; float totalHeight = lineHeight; float baseOffsetDown = totalHeight * 0.5f; float topLinePosition = baseOffsetDown - totalHeight + lineHeight; list->Clear(); vsVector2D offset(0.f,topLinePosition); list->Clear(); BuildDisplayListFromString( list, string.c_str(), size, capSize, j, offset ); } }
bool vsFile::DeleteEmptyDirectory( const vsString &filename ) { // If it's not a directory, don't delete it! // // Note that PHYSFS_delete will return an error if we // try to delete a non-empty directory. // if ( DirectoryExists(filename) ) return PHYSFS_delete(filename.c_str()) != 0; return false; }
vsNetClient::vsNetClient(const vsString &address, uint16_t port) { hostent *h = gethostbyname( address.c_str() ); if (h == NULL) { #if !defined(_WIN32) // todo: Network errors under WinSock! herror("gethostbyname"); #endif vsAssert( h != NULL, vsFormatString("Gethostbyname error: See console output for details" ) ); } m_privateIP = ((struct in_addr *)h->h_addr)->s_addr; m_privatePort = port; }
vsString vsLocalisationTable::GetTranslation( const vsString &key ) { vsString *str = s_localisationTable->FindItem(key); if ( str ) { return *str; } else { return vsFormatString("<<%s>>", key.c_str()); } }
int vsFile::DirectoryContents( vsArray<vsString>* result, const vsString &dirName ) // static method { result->Clear(); char **files = PHYSFS_enumerateFiles(dirName.c_str()); char **i; std::vector<char*> s; for (i = files; *i != NULL; i++) s.push_back(*i); std::sort(s.begin(), s.end(), sortFilesByModificationDate(dirName)); for (size_t i = 0; i < s.size(); i++) result->AddItem( s[i] ); PHYSFS_freeList(files); return result->ItemCount(); }
vsPreferences::vsPreferences(const vsString &filename_in) { vsString filename = vsFormatString("%s.prefs",filename_in.c_str()); m_filename = filename; m_preferenceList = new vsPreferenceObject; if ( fsFile::Exists(filename) ) // if the file exists, then read the current pref values out of it. { fsRecord record; fsFile prefsFile(filename); while( prefsFile.Record(&record) ) { vsAssert( record.GetArgCount() == 3, vsFormatString("%s preference has wrong number of arguments!", record.GetLabel().c_str() )); AddPreference( record.GetLabel(), record.GetArgAsInt(0), record.GetArgAsInt(1), record.GetArgAsInt(2)); } } }
bool vsFile::DirectoryExists( const vsString &filename ) // static method { return PHYSFS_exists(filename.c_str()) && PHYSFS_isDirectory(filename.c_str()); }
void vsLog(const vsString &str) { printf("%s\n", str.c_str() ); }