diff options
author | Dylam De La Torre <DyXel04@gmail.com> | 2021-11-23 05:21:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-23 04:21:56 +0000 |
commit | a51a54bc5979a2491f152abc47ad54e6b63f27c8 (patch) | |
tree | ce67092b2f6df366033a65a6111e4650866766b2 /imgmanager.cpp | |
parent | d88079000a79e6bcbe51c5a2868d57b303b5fcb6 (diff) | |
download | abaddon-portaudio-a51a54bc5979a2491f152abc47ad54e6b63f27c8.tar.gz abaddon-portaudio-a51a54bc5979a2491f152abc47ad54e6b63f27c8.zip |
Restructure source and resource files (#46)
importantly, res is now res/res and css is now res/css
Diffstat (limited to 'imgmanager.cpp')
-rw-r--r-- | imgmanager.cpp | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/imgmanager.cpp b/imgmanager.cpp deleted file mode 100644 index b97083c..0000000 --- a/imgmanager.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include "imgmanager.hpp" -#include "util.hpp" -#include "abaddon.hpp" - -ImageManager::ImageManager() { - m_cb_dispatcher.connect(sigc::mem_fun(*this, &ImageManager::RunCallbacks)); -} - -Cache &ImageManager::GetCache() { - return m_cache; -} - -void ImageManager::ClearCache() { - m_cache.ClearCache(); -} - -Glib::RefPtr<Gdk::Pixbuf> ImageManager::ReadFileToPixbuf(std::string path) { - const auto &data = ReadWholeFile(path); - if (data.size() == 0) return Glib::RefPtr<Gdk::Pixbuf>(nullptr); - auto loader = Gdk::PixbufLoader::create(); - loader->signal_size_prepared().connect([&loader](int w, int h) { - int cw, ch; - GetImageDimensions(w, h, cw, ch); // what could go wrong - loader->set_size(cw, ch); - }); - loader->write(static_cast<const guint8 *>(data.data()), data.size()); - loader->close(); - return loader->get_pixbuf(); -} - -Glib::RefPtr<Gdk::PixbufAnimation> ImageManager::ReadFileToPixbufAnimation(std::string path, int w, int h) { - const auto &data = ReadWholeFile(path); - if (data.size() == 0) return Glib::RefPtr<Gdk::PixbufAnimation>(nullptr); - auto loader = Gdk::PixbufLoader::create(); - loader->signal_size_prepared().connect([&loader, w, h](int, int) { - loader->set_size(w, h); - }); - loader->write(static_cast<const guint8 *>(data.data()), data.size()); - loader->close(); - return loader->get_animation(); -} - -void ImageManager::LoadFromURL(std::string url, callback_type cb) { - sigc::signal<void(Glib::RefPtr<Gdk::Pixbuf>)> signal; - signal.connect(cb); - m_cache.GetFileFromURL(url, [this, url, signal](std::string path) { - try { - auto buf = ReadFileToPixbuf(path); - if (!buf) - printf("%s (%s) is null\n", url.c_str(), path.c_str()); - else { - m_cb_mutex.lock(); - m_cb_queue.push([signal, buf]() { signal.emit(buf); }); - m_cb_dispatcher.emit(); - m_cb_mutex.unlock(); - } - } catch (const std::exception &e) { - fprintf(stderr, "err loading pixbuf from %s: %s\n", path.c_str(), e.what()); - } - }); -} - -void ImageManager::LoadAnimationFromURL(std::string url, int w, int h, callback_anim_type cb) { - sigc::signal<void(Glib::RefPtr<Gdk::PixbufAnimation>)> signal; - signal.connect(cb); - m_cache.GetFileFromURL(url, [this, url, signal, w, h](std::string path) { - try { - auto buf = ReadFileToPixbufAnimation(path, w, h); - if (!buf) - printf("%s (%s) is null\n", url.c_str(), path.c_str()); - else { - m_cb_mutex.lock(); - m_cb_queue.push([signal, buf]() { signal.emit(buf); }); - m_cb_dispatcher.emit(); - m_cb_mutex.unlock(); - } - } catch (const std::exception &e) { - fprintf(stderr, "err loading pixbuf animation from %s: %s\n", path.c_str(), e.what()); - } - }); -} - -void ImageManager::Prefetch(std::string url) { - m_cache.GetFileFromURL(url, [](const auto &) {}); -} - -void ImageManager::RunCallbacks() { - m_cb_mutex.lock(); - m_cb_queue.front()(); - m_cb_queue.pop(); - m_cb_mutex.unlock(); -} - -Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetFromURLIfCached(std::string url) { - std::string path = m_cache.GetPathIfCached(url); - if (path != "") - return ReadFileToPixbuf(path); - - return Glib::RefPtr<Gdk::Pixbuf>(nullptr); -} - -Glib::RefPtr<Gdk::PixbufAnimation> ImageManager::GetAnimationFromURLIfCached(std::string url, int w, int h) { - std::string path = m_cache.GetPathIfCached(url); - if (path != "") - return ReadFileToPixbufAnimation(path, w, h); - - return Glib::RefPtr<Gdk::PixbufAnimation>(nullptr); -} - -Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetPlaceholder(int size) { - std::string name = "/placeholder" + std::to_string(size); - if (m_pixs.find(name) != m_pixs.end()) - return m_pixs.at(name); - - try { - auto buf = Gdk::Pixbuf::create_from_file(Abaddon::Get().GetResPath() + "/decamarks.png", size, size); - m_pixs[name] = buf; - return buf; - } catch (std::exception &e) { - fprintf(stderr, "error loading placeholder\n"); - return Glib::RefPtr<Gdk::Pixbuf>(nullptr); - } -} |