Piratux

Untitled

Jan 31st, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // O------------------------------------------------------------------------------O
  2. // | END RENDERER: OpenGL 3.3 (3.0 es) (sh-sh-sh-shaders....) |
  3. // O------------------------------------------------------------------------------O
  4.  
  5. // O------------------------------------------------------------------------------O
  6. // | START IMAGE LOADER: GDI+, Windows Only, always exists, a little slow |
  7. // O------------------------------------------------------------------------------O
  8. #if defined(OLC_IMAGE_GDI)
  9.  
  10. #define min(a, b) ((a < b) ? a : b)
  11. #define max(a, b) ((a > b) ? a : b)
  12. #include <gdiplus.h>
  13. #if defined(__MINGW32__) // Thanks Gusgo & Dandistine, but c'mon mingw!! wtf?!
  14. #include <gdiplus/gdiplusinit.h>
  15. #else
  16. #include <gdiplusinit.h>
  17. #endif
  18. #include <shlwapi.h>
  19. #undef min
  20. #undef max
  21.  
  22. #if !defined(__MINGW32__)
  23. #pragma comment(lib, "gdiplus.lib")
  24. #pragma comment(lib, "Shlwapi.lib")
  25. #endif
  26.  
  27. namespace olc
  28. {
  29. // Thanks @MaGetzUb for this, which allows sprites to be defined
  30. // at construction, by initialising the GDI subsystem
  31. static class GDIPlusStartup
  32. {
  33. public:
  34. GDIPlusStartup()
  35. {
  36. Gdiplus::GdiplusStartupInput startupInput;
  37. GdiplusStartup(&token, &startupInput, NULL);
  38. }
  39.  
  40. ULONG_PTR token;
  41.  
  42. ~GDIPlusStartup()
  43. {
  44. // Well, MarcusTU thought this was important :D
  45. Gdiplus::GdiplusShutdown(token);
  46. }
  47. } gdistartup;
  48.  
  49. class ImageLoader_GDIPlus : public olc::ImageLoader
  50. {
  51. private:
  52. std::wstring ConvertS2W(std::string s)
  53. {
  54. #ifdef __MINGW32__
  55. wchar_t* buffer = new wchar_t[s.length() + 1];
  56. mbstowcs(buffer, s.c_str(), s.length());
  57. buffer[s.length()] = L'\0';
  58. #else
  59. int count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
  60. wchar_t* buffer = new wchar_t[count];
  61. MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buffer, count);
  62. #endif
  63. std::wstring w(buffer);
  64. delete[] buffer;
  65. return w;
  66. }
  67.  
  68. public:
  69. ImageLoader_GDIPlus() : ImageLoader()
  70. {}
  71.  
  72. olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
  73. {
  74. // clear out existing sprite
  75. if (spr->pColData != nullptr) delete[] spr->pColData;
  76.  
  77. // Open file
  78. UNUSED(pack);
  79. Gdiplus::Bitmap* bmp = nullptr;
  80. if (pack != nullptr)
  81. {
  82. // Load sprite from input stream
  83. ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
  84. bmp = Gdiplus::Bitmap::FromStream(SHCreateMemStream((BYTE*)rb.vMemory.data(), UINT(rb.vMemory.size())));
  85. }
  86. else
  87. {
  88. // Check file exists
  89. if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
  90.  
  91. // Load sprite from file
  92. bmp = Gdiplus::Bitmap::FromFile(ConvertS2W(sImageFile).c_str());
  93. }
  94.  
  95. if (bmp->GetLastStatus() != Gdiplus::Ok) return olc::rcode::FAIL;
  96. spr->width = bmp->GetWidth();
Add Comment
Please, Sign In to add comment