Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // O------------------------------------------------------------------------------O
- // | END RENDERER: OpenGL 3.3 (3.0 es) (sh-sh-sh-shaders....) |
- // O------------------------------------------------------------------------------O
- // O------------------------------------------------------------------------------O
- // | START IMAGE LOADER: GDI+, Windows Only, always exists, a little slow |
- // O------------------------------------------------------------------------------O
- #if defined(OLC_IMAGE_GDI)
- #define min(a, b) ((a < b) ? a : b)
- #define max(a, b) ((a > b) ? a : b)
- #include <gdiplus.h>
- #if defined(__MINGW32__) // Thanks Gusgo & Dandistine, but c'mon mingw!! wtf?!
- #include <gdiplus/gdiplusinit.h>
- #else
- #include <gdiplusinit.h>
- #endif
- #include <shlwapi.h>
- #undef min
- #undef max
- #if !defined(__MINGW32__)
- #pragma comment(lib, "gdiplus.lib")
- #pragma comment(lib, "Shlwapi.lib")
- #endif
- namespace olc
- {
- // Thanks @MaGetzUb for this, which allows sprites to be defined
- // at construction, by initialising the GDI subsystem
- static class GDIPlusStartup
- {
- public:
- GDIPlusStartup()
- {
- Gdiplus::GdiplusStartupInput startupInput;
- GdiplusStartup(&token, &startupInput, NULL);
- }
- ULONG_PTR token;
- ~GDIPlusStartup()
- {
- // Well, MarcusTU thought this was important :D
- Gdiplus::GdiplusShutdown(token);
- }
- } gdistartup;
- class ImageLoader_GDIPlus : public olc::ImageLoader
- {
- private:
- std::wstring ConvertS2W(std::string s)
- {
- #ifdef __MINGW32__
- wchar_t* buffer = new wchar_t[s.length() + 1];
- mbstowcs(buffer, s.c_str(), s.length());
- buffer[s.length()] = L'\0';
- #else
- int count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
- wchar_t* buffer = new wchar_t[count];
- MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buffer, count);
- #endif
- std::wstring w(buffer);
- delete[] buffer;
- return w;
- }
- public:
- ImageLoader_GDIPlus() : ImageLoader()
- {}
- olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
- {
- // clear out existing sprite
- if (spr->pColData != nullptr) delete[] spr->pColData;
- // Open file
- UNUSED(pack);
- Gdiplus::Bitmap* bmp = nullptr;
- if (pack != nullptr)
- {
- // Load sprite from input stream
- ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
- bmp = Gdiplus::Bitmap::FromStream(SHCreateMemStream((BYTE*)rb.vMemory.data(), UINT(rb.vMemory.size())));
- }
- else
- {
- // Check file exists
- if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
- // Load sprite from file
- bmp = Gdiplus::Bitmap::FromFile(ConvertS2W(sImageFile).c_str());
- }
- if (bmp->GetLastStatus() != Gdiplus::Ok) return olc::rcode::FAIL;
- spr->width = bmp->GetWidth();
Add Comment
Please, Sign In to add comment