Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
NetImageElement.cpp
1#include "NetImageElement.hpp"
2
3NetImageElement::NetImageElement(const char *url, std::function<Texture *(void)> getImageFallback, bool immediateLoad)
4{
5 std::string key = std::string(url);
6 // printf("Key: %s\n", key.c_str());
7 if (loadFromCache(key)) {
8 loaded = true;
9
10 // if we're using the cache, we can update the size now
11 // printf("The size of the image is %d x %d\n", texW, texH);
12 width = texW;
13 height = texH;
14 }
15 else {
16 // setup a temporary image fallback
17 if (getImageFallback)
18 imgFallback = getImageFallback();
19
20 // start downloading the correct image
21 imgDownload = new DownloadOperation();
22 imgDownload->url = std::string(url);
23 imgDownload->cb = std::bind(&NetImageElement::imgDownloadComplete, this, std::placeholders::_1);
24
25 // load immediately
26 if (immediateLoad)
27 fetch();
28 }
29}
30
31NetImageElement::~NetImageElement()
32{
33 if (imgFallback)
34 delete imgFallback;
35
36 if (imgDownload)
37 {
38 DownloadQueue::downloadQueue->downloadCancel(imgDownload);
39 delete imgDownload;
40 }
41}
42
44{
45 if (!downloadStarted && imgDownload) {
46 DownloadQueue::downloadQueue->downloadAdd(imgDownload);
47 downloadStarted = true;
48 }
49}
50
51void NetImageElement::imgDownloadComplete(DownloadOperation *download)
52{
53 bool success = false;
54
55 if (download->status == DownloadStatus::COMPLETE)
56 {
57 CST_Surface *surface = IMG_Load_RW(SDL_RWFromMem((void*)download->buffer.c_str(), download->buffer.size()), 1);
58 success = loadFromSurfaceSaveToCache(download->url, surface);
59 CST_FreeSurface(surface);
60 }
61
62 if (success)
63 {
64 this->needsRedraw = true;
65 loaded = true;
66
67 delete imgFallback;
68 imgFallback = nullptr;
69
70 if (updateSizeAfterLoad) {
71 width = texW;
72 height = texH;
73 }
74 }
75
76 delete imgDownload;
77 imgDownload = nullptr;
78}
79
80
82{
83 // if we're hidden, don't render
84 if (hidden) return;
85
86 if (mTexture)
87 {
89 }
90 else if (imgFallback)
91 {
92 imgFallback->x = x;
93 imgFallback->y = y;
94 imgFallback->width = width;
95 imgFallback->height = height;
96 imgFallback->render(parent);
97 }
98}
void downloadAdd(DownloadOperation *download)
add a new download operation
void downloadCancel(DownloadOperation *download)
cancel a download operation
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:105
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:84
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:118
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:102
void render(Element *parent)
Render the image.
NetImageElement(const char *url, std::function< Texture *(void)> getImageFallback=NULL, bool immediateLoad=true)
Creates a new image element, downloading the image from url If the url is not cached,...
void fetch()
Start downloading the image (called in the constructor unless immediateLoad is false)
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
Definition: Texture.cpp:91
int texW
The size of the texture.
Definition: Texture.hpp:76
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:76
void render(Element *parent)
Renders the texture.
Definition: Texture.cpp:108
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:73