Advertisement
spacechase0

Level Loading

Jan 8th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. bool Level::LoadFromFile( const std::string& filename )
  2. {
  3.     std::fstream file( filename.c_str(), std::fstream::in );
  4.     if ( !file )
  5.     {
  6.         return false;
  7.     }
  8.    
  9.     std::string sizeStr;
  10.     file >> sizeStr;
  11.     auto tokens = util::Tokenize( sizeStr, "x" );
  12.     if ( !file or tokens.size() != 2 )
  13.     {
  14.         return false;
  15.     }
  16.     size.x = util::FromString< sf::Int32 >( tokens[ 0 ] );
  17.     size.y = util::FromString< sf::Int32 >( tokens[ 1 ] );
  18.    
  19.     file.get();
  20.     std::getline( file, name );
  21.     if ( !file )
  22.     {
  23.         return false;
  24.     }
  25.    
  26.     for ( int iy = 0; iy < size.y; ++iy )
  27.     {
  28.         std::string line;
  29.         std::getline( file, line );
  30.         if ( !file or line.length() < size.x )
  31.         {
  32.             return false;
  33.         }
  34.        
  35.         for ( int ix = 0; ix < size.x; ++ix )
  36.         {
  37.             AddBlock( line[ ix ], ix, iy );
  38.         }
  39.     }
  40.    
  41.     return false;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement