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

Class ImageLoader_GDIPlus

olcPixelGameEngine.h:6468–6535  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6466 } gdistartup;
6467
6468 class ImageLoader_GDIPlus : public olc::ImageLoader
6469 {
6470 private:
6471 std::wstring ConvertS2W(std::string s)
6472 {
6473#ifdef __MINGW32__
6474 wchar_t* buffer = new wchar_t[s.length() + 1];
6475 mbstowcs(buffer, s.c_str(), s.length());
6476 buffer[s.length()] = L'\0';
6477#else
6478 int count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
6479 wchar_t* buffer = new wchar_t[count];
6480 MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buffer, count);
6481#endif
6482 std::wstring w(buffer);
6483 delete[] buffer;
6484 return w;
6485 }
6486
6487 public:
6488 ImageLoader_GDIPlus() : ImageLoader()
6489 {}
6490
6491 olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
6492 {
6493 // clear out existing sprite
6494 spr->pColData.clear();
6495
6496 // Open file
6497 UNUSED(pack);
6498 Gdiplus::Bitmap* bmp = nullptr;
6499 if (pack != nullptr)
6500 {
6501 // Load sprite from input stream
6502 ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
6503 bmp = Gdiplus::Bitmap::FromStream(SHCreateMemStream((BYTE*)rb.vMemory.data(), UINT(rb.vMemory.size())));
6504 }
6505 else
6506 {
6507 // Check file exists
6508 if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
6509
6510 // Load sprite from file
6511 bmp = Gdiplus::Bitmap::FromFile(ConvertS2W(sImageFile).c_str());
6512 }
6513
6514 if (bmp->GetLastStatus() != Gdiplus::Ok) return olc::rcode::FAIL;
6515 spr->width = bmp->GetWidth();
6516 spr->height = bmp->GetHeight();
6517
6518 spr->pColData.resize(spr->width * spr->height);
6519
6520 for (int y = 0; y < spr->height; y++)
6521 for (int x = 0; x < spr->width; x++)
6522 {
6523 Gdiplus::Color c;
6524 bmp->GetPixel(x, y, &c);
6525 spr->SetPixel(x, y, olc::Pixel(c.GetRed(), c.GetGreen(), c.GetBlue(), c.GetAlpha()));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected