Loading Textures from Qt resources

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Wol101
Posts: 9
Joined: Sat Apr 23, 2016 6:39 am

Loading Textures from Qt resources

Post by Wol101 »

For those of us using Qt for our user interfaces it is nice to take advantage of the built in resource management system. This snippet shows how to load up an image from a resource and convert it into an Irrlicht texture. It's not especially neat and involves more copying than I would ideally like but it seems to work OK and I couldn't think of anything better. It would be prefereable to hand the file data directly to Irrlich. i.e. read the PNG file into memory using QFile and then pass a pointer to the memory block to the normal Irrlicht texture loading routines but I don't think there is a public interface for that. The interfaces want a file path, and the leading ':' format to indicate that this is a resource not a real path isn't going to work (Qt actually compiles resources into the executable so they don't exist as standalone files - it's a neat system and potentially very fast).

Code: Select all

 
  QImage qImage;
  qImage.load(":/images/Floor Textures/floor_checker.png");
  if (qImage.format() != QImage::Format_ARGB32) qImage = qImage.convertToFormat(QImage::Format_ARGB32);
  irr::video::ITexture *floorTexture = window->videoDriver()->addTexture(irr::core::dimension2d<irr::u32>(qImage.width(), qImage.height()), 
    ":/images/Floor Textures/floor_checker.png", irr::video::ECF_A8R8G8B8);
  irr::u8 *texturePtr = static_cast<irr::u8 *>(floorTexture->lock(irr::video::ETLM_READ_WRITE, 0));
  memcpy(texturePtr, qImage.bits(), qImage.width() * qImage.height() * 4);
  floorTexture->unlock();
 
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Loading Textures from Qt resources

Post by hendu »

Irr can use executable resources with the IMemoryFile interface, how that would interact with Qt I don't know. For your texture lock, you should use WRITE instead of RW, because having READ there may cause a readback from the GPU.
Wol101
Posts: 9
Joined: Sat Apr 23, 2016 6:39 am

Re: Loading Textures from Qt resources

Post by Wol101 »

Thanks for the tip. This code is probably a bit neater than the previous suggestion (as long as I've got the memory management right - my assumption is that once getTexture has happened, I don't need to maintain the memory block):

Code: Select all

  QFile file(":/images/Floor Textures/floor_checker_2.png");
  bool ok = file.open(QIODevice::ReadOnly);
  Q_ASSERT(ok);
  QByteArray textureData = file.readAll();
  irr::io::IFileSystem *fs = window->irrlichtDevice()->getFileSystem();
  irr::io::IReadFile* readFile = fs->createMemoryReadFile(textureData.data(), textureData.size(), 
    ":/images/Floor Textures/floor_checker_2.png", false);
  irr::video::ITexture *floorTexture = window->videoDriver()->getTexture(readFile);
  readFile->drop();
 
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Loading Textures from Qt resources

Post by hendu »

Doesn't the "file.readAll" cause a needless memory copy?
Post Reply