example:refactor to remove naked malloc and RAII warning

This commit is contained in:
subhransu mohanty 2019-05-20 13:10:43 +09:00 committed by Subhransu
parent 23115b9092
commit 89a72b6e9c

View File

@ -4,15 +4,19 @@
#include<iostream> #include<iostream>
#include<vector> #include<vector>
#include<array> #include<array>
#include<cstdlib> #include<libgen.h>
class GifBuilder { class GifBuilder {
public: public:
GifBuilder(const std::string &fileName , const uint32_t width, explicit GifBuilder(const std::string &fileName , const uint32_t width,
const uint32_t height, const uint32_t delay = 2) const uint32_t height, const uint32_t delay = 2)
{ {
GifBegin(&handle, fileName.c_str(), width, height, delay); GifBegin(&handle, fileName.c_str(), width, height, delay);
} }
~GifBuilder()
{
GifEnd(&handle);
}
void addFrame(rlottie::Surface &s, uint32_t delay = 2) void addFrame(rlottie::Surface &s, uint32_t delay = 2)
{ {
argbTorgba(s); argbTorgba(s);
@ -56,10 +60,7 @@ public:
} }
} }
} }
void commit()
{
GifEnd(&handle);
}
private: private:
GifWriter handle; GifWriter handle;
}; };
@ -71,18 +72,15 @@ public:
auto player = rlottie::Animation::loadFromFile(fileName); auto player = rlottie::Animation::loadFromFile(fileName);
if (!player) return help(); if (!player) return help();
uint32_t* buffer = (uint32_t *) malloc(w * h * 4); auto buffer = std::make_unique<uint32_t[]>(w * h);
size_t frameCount = player->totalFrame(); size_t frameCount = player->totalFrame();
GifBuilder builder(baseName.data(), w, h); GifBuilder builder(baseName.data(), w, h);
for (size_t i = 0; i < frameCount ; i++) { for (size_t i = 0; i < frameCount ; i++) {
rlottie::Surface surface(buffer, w, h, w * 4); rlottie::Surface surface(buffer.get(), w, h, w * 4);
player->renderSync(i, surface); player->renderSync(i, surface);
builder.addFrame(surface); builder.addFrame(surface);
} }
builder.commit();
free(buffer);
return result(); return result();
} }