Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for (int y = 0; y < spr->height; y++) // Thanks maksym33
- free(row_pointers[y]);
- free(row_pointers);
- png_destroy_read_struct(&png, &info, nullptr);
- };
- png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png) goto fail_load;
- info = png_create_info_struct(png);
- if (!info) goto fail_load;
- if (setjmp(png_jmpbuf(png))) goto fail_load;
- if (pack == nullptr)
- {
- FILE* f = fopen(sImageFile.c_str(), "rb");
- if (!f) return olc::rcode::NO_FILE;
- png_init_io(png, f);
- loadPNG();
- fclose(f);
- }
- else
- {
- ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
- std::istream is(&rb);
- png_set_read_fn(png, (png_voidp)&is, pngReadStream);
- loadPNG();
- }
- return olc::rcode::OK;
- fail_load:
- spr->width = 0;
- spr->height = 0;
- spr->pColData = nullptr;
- return olc::rcode::FAIL;
- }
- olc::rcode SaveImageResource(olc::Sprite* spr, const std::string& sImageFile) override
- {
- return olc::rcode::OK;
- }
- };
- }
- #endif
- // O------------------------------------------------------------------------------O
- // | END IMAGE LOADER: |
- // O------------------------------------------------------------------------------O
- // O------------------------------------------------------------------------------O
- // | START IMAGE LOADER: stb_image.h, all systems, very fast |
- // O------------------------------------------------------------------------------O
- // Thanks to Sean Barrett - https://github.com/nothings/stb/blob/master/stb_image.h
- // MIT License - Copyright(c) 2017 Sean Barrett
- // Note you need to download the above file into your project folder, and
- // #define OLC_IMAGE_STB
- // #define OLC_PGE_APPLICATION
- // #include "olcPixelGameEngine.h"
- #if defined(OLC_IMAGE_STB)
- #define STB_IMAGE_IMPLEMENTATION
- #include "stb_image.h"
- namespace olc
- {
- class ImageLoader_STB : public olc::ImageLoader
- {
- public:
- ImageLoader_STB() : ImageLoader()
- {}
- olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
- {
- UNUSED(pack);
- // clear out existing sprite
- if (spr->pColData != nullptr) delete[] spr->pColData;
- // Open file
- stbi_uc* bytes = nullptr;
- int w = 0, h = 0, cmp = 0;
- if (pack != nullptr)
- {
- ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
- bytes = stbi_load_from_memory((unsigned char*)rb.vMemory.data(), rb.vMemory.size(), &w, &h, &cmp, 4);
- }
- else
- {
- // Check file exists
- if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
- bytes = stbi_load(sImageFile.c_str(), &w, &h, &cmp, 4);
- }
- if (!bytes) return olc::rcode::FAIL;
- spr->width = w; spr->height = h;
- spr->pColData = new Pixel[spr->width * spr->height];
- std::memcpy(spr->pColData, bytes, spr->width * spr->height * 4);
- delete[] bytes;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement