Advertisement
SilverTES

SFML + PHYSFS

Sep 23rd, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <physfs.h>
  2.  
  3. class FileStream : public sf::InputStream
  4. {
  5.     public:
  6.         FileStream() : m_file(NULL){}
  7.  
  8.         virtual ~FileStream()
  9.         {
  10.             PHYSFS_close(m_file);
  11.         }
  12.  
  13.         bool open(const char* filename)
  14.         {
  15.             if(m_file)
  16.             {
  17.                 PHYSFS_close(m_file);
  18.             }
  19.             m_file = PHYSFS_openRead(filename);
  20.             return m_file != NULL;
  21.         }
  22.  
  23.         virtual sf::Int64 read(void* data, sf::Int64 size)
  24.         {
  25.             if(m_file)
  26.             {
  27.                 return PHYSFS_read(m_file, data, 1, static_cast<PHYSFS_uint32>(size));
  28.             }
  29.             return -1;
  30.         }
  31.  
  32.         virtual sf::Int64 seek(sf::Int64 position)
  33.         {
  34.             if(m_file)
  35.             {
  36.                 PHYSFS_seek(m_file, position);
  37.                 return tell();
  38.             }
  39.             return -1;
  40.         }
  41.  
  42.         virtual sf::Int64 tell()
  43.         {
  44.             if (m_file)
  45.             {
  46.                 return PHYSFS_tell(m_file);
  47.             }
  48.             return -1;
  49.         }
  50.  
  51.         virtual sf::Int64 getSize()
  52.         {
  53.             if(m_file)
  54.             {
  55.                 return PHYSFS_fileLength(m_file);
  56.             }
  57.             return -1;
  58.         }
  59.  
  60.     private:
  61.         PHYSFS_File* m_file;
  62. };
  63.  
  64. // ----------------------------------------------------------------------------------------------
  65. // Example :
  66. // ----------------------------------------------------------------------------------------------
  67. PHYSFS_init(argv[0]);
  68. PHYSFS_addToSearchPath("images.zip",1);
  69. sf::Texture my_texture;
  70. FileStream my_stream;
  71.  
  72. if(!my_stream.open("images/img.png"))
  73. {
  74.     // Erreur
  75. }
  76. if(!my_texture.loadFromStream(my_stream))
  77. {
  78.     // Erreur
  79. }
  80. sf::Sprite my_sprite;
  81. my_sprite.setTexture(my_texture);
  82.  
  83. // Un peu plus bas... à la fin du programme
  84.  
  85. PHYSFS_deinit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement