Advertisement
Piratux

Untitled

Jan 31st, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1.  
  2.  
  3. for (int y = 0; y < spr->height; y++) // Thanks maksym33
  4. free(row_pointers[y]);
  5. free(row_pointers);
  6. png_destroy_read_struct(&png, &info, nullptr);
  7. };
  8.  
  9. png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  10. if (!png) goto fail_load;
  11.  
  12. info = png_create_info_struct(png);
  13. if (!info) goto fail_load;
  14.  
  15. if (setjmp(png_jmpbuf(png))) goto fail_load;
  16.  
  17. if (pack == nullptr)
  18. {
  19. FILE* f = fopen(sImageFile.c_str(), "rb");
  20. if (!f) return olc::rcode::NO_FILE;
  21. png_init_io(png, f);
  22. loadPNG();
  23. fclose(f);
  24. }
  25. else
  26. {
  27. ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
  28. std::istream is(&rb);
  29. png_set_read_fn(png, (png_voidp)&is, pngReadStream);
  30. loadPNG();
  31. }
  32.  
  33. return olc::rcode::OK;
  34.  
  35. fail_load:
  36. spr->width = 0;
  37. spr->height = 0;
  38. spr->pColData = nullptr;
  39. return olc::rcode::FAIL;
  40. }
  41.  
  42. olc::rcode SaveImageResource(olc::Sprite* spr, const std::string& sImageFile) override
  43. {
  44. return olc::rcode::OK;
  45. }
  46. };
  47. }
  48. #endif
  49. // O------------------------------------------------------------------------------O
  50. // | END IMAGE LOADER: |
  51. // O------------------------------------------------------------------------------O
  52.  
  53.  
  54.  
  55.  
  56. // O------------------------------------------------------------------------------O
  57. // | START IMAGE LOADER: stb_image.h, all systems, very fast |
  58. // O------------------------------------------------------------------------------O
  59. // Thanks to Sean Barrett - https://github.com/nothings/stb/blob/master/stb_image.h
  60. // MIT License - Copyright(c) 2017 Sean Barrett
  61.  
  62. // Note you need to download the above file into your project folder, and
  63. // #define OLC_IMAGE_STB
  64. // #define OLC_PGE_APPLICATION
  65. // #include "olcPixelGameEngine.h"
  66.  
  67. #if defined(OLC_IMAGE_STB)
  68. #define STB_IMAGE_IMPLEMENTATION
  69. #include "stb_image.h"
  70. namespace olc
  71. {
  72. class ImageLoader_STB : public olc::ImageLoader
  73. {
  74. public:
  75. ImageLoader_STB() : ImageLoader()
  76. {}
  77.  
  78. olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
  79. {
  80. UNUSED(pack);
  81. // clear out existing sprite
  82. if (spr->pColData != nullptr) delete[] spr->pColData;
  83. // Open file
  84. stbi_uc* bytes = nullptr;
  85. int w = 0, h = 0, cmp = 0;
  86. if (pack != nullptr)
  87. {
  88. ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
  89. bytes = stbi_load_from_memory((unsigned char*)rb.vMemory.data(), rb.vMemory.size(), &w, &h, &cmp, 4);
  90. }
  91. else
  92. {
  93. // Check file exists
  94. if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
  95. bytes = stbi_load(sImageFile.c_str(), &w, &h, &cmp, 4);
  96. }
  97.  
  98. if (!bytes) return olc::rcode::FAIL;
  99. spr->width = w; spr->height = h;
  100. spr->pColData = new Pixel[spr->width * spr->height];
  101. std::memcpy(spr->pColData, bytes, spr->width * spr->height * 4);
  102. delete[] bytes;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement