MCPcopy Create free account
hub / github.com/OneLoneCoder/olcPixelGameEngine / LoadImageResource

Method LoadImageResource

olcPixelGameEngine.h:5216–5242  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5214 {}
5215
5216 olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
5217 {
5218 UNUSED(pack);
5219 // clear out existing sprite
5220 spr->pColData.clear();
5221 // Open file
5222 stbi_uc* bytes = nullptr;
5223 int w = 0, h = 0, cmp = 0;
5224 if (pack != nullptr)
5225 {
5226 ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
5227 bytes = stbi_load_from_memory((unsigned char*)rb.vMemory.data(), int(rb.vMemory.size()), &w, &h, &cmp, 4);
5228 }
5229 else
5230 {
5231 // Check file exists
5232 if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
5233 bytes = stbi_load(sImageFile.c_str(), &w, &h, &cmp, 4);
5234 }
5235
5236 if (!bytes) return olc::rcode::FAIL;
5237 spr->width = w; spr->height = h;
5238 spr->pColData.resize(spr->width * spr->height);
5239 std::memcpy(spr->pColData.data(), bytes, spr->width * spr->height * 4);
5240 delete[] bytes;
5241 return olc::rcode::OK;
5242 }
5243
5244 olc::rcode SaveImageResource(olc::Sprite* spr, const std::string& sImageFile) override
5245 {

Callers 1

LoadFromFileMethod · 0.45

Calls 4

GetFileBufferMethod · 0.80
clearMethod · 0.45
sizeMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected