Commit 805ec687 authored by mercury233's avatar mercury233 Committed by GitHub

use multi-thread in loading thumbnails (#2207)

parent 697a6f7a
......@@ -1157,8 +1157,13 @@ void Game::DrawDeckBd() {
DrawShadowText(numFont, deckBuilder.result_string, Resize(875, 137, 935, 157), Resize(1, 1, 1, 1), 0xffffffff, 0xff000000, false, true);
driver->draw2DRectangle(Resize(805, 160, 1020, 630), 0x400000ff, 0x400000ff, 0x40000000, 0x40000000);
driver->draw2DRectangleOutline(Resize(804, 159, 1020, 630));
for(size_t i = 0; i < 7 && i + scrFilter->getPos() < deckBuilder.results.size(); ++i) {
for(size_t i = 0; i < 9 && i + scrFilter->getPos() < deckBuilder.results.size(); ++i) {
code_pointer ptr = deckBuilder.results[i + scrFilter->getPos()];
if(i >= 7)
{
imageManager.GetTextureThumb(ptr->second.code);
break;
}
if(deckBuilder.hovered_pos == 4 && deckBuilder.hovered_seq == (int)i)
driver->draw2DRectangle(0x80000000, Resize(806, 164 + i * 66, 1019, 230 + i * 66));
DrawThumb(ptr, position2di(810, 165 + i * 66), deckBuilder.filterList);
......
......@@ -15,6 +15,8 @@ bool ImageManager::Initial() {
tUnknown = NULL;
tUnknownFit = NULL;
tUnknownThumb = NULL;
tLoading = NULL;
tThumbLoadingThreadRunning = false;
tAct = driver->getTexture("textures/act.png");
tAttack = driver->getTexture("textures/attack.png");
tChain = driver->getTexture("textures/chain.png");
......@@ -55,12 +57,18 @@ void ImageManager::ClearTexture() {
driver->removeTexture(tit->second);
}
for(auto tit = tThumb.begin(); tit != tThumb.end(); ++tit) {
if(tit->second)
if(tit->second && tit->second != tLoading)
driver->removeTexture(tit->second);
}
tMap[0].clear();
tMap[1].clear();
tThumb.clear();
tThumbLoadingMutex.lock();
tThumbLoading.clear();
while(!tThumbLoadingCodes.empty())
tThumbLoadingCodes.pop();
tThumbLoadingThreadRunning = false;
tThumbLoadingMutex.unlock();
tFields.clear();
}
void ImageManager::RemoveTexture(int code) {
......@@ -96,9 +104,11 @@ void ImageManager::ResizeTexture() {
driver->removeTexture(tUnknown);
driver->removeTexture(tUnknownFit);
driver->removeTexture(tUnknownThumb);
driver->removeTexture(tLoading);
tUnknown = GetTextureFromFile("textures/unknown.jpg", CARD_IMG_WIDTH, CARD_IMG_HEIGHT);
tUnknownFit = GetTextureFromFile("textures/unknown.jpg", imgWidthFit, imgHeightFit);
tUnknownThumb = GetTextureFromFile("textures/unknown.jpg", imgWidthThumb, imgHeightThumb);
tLoading = GetTextureFromFile("textures/cover.jpg", imgWidthThumb, imgHeightThumb);
driver->removeTexture(tBackGround);
tBackGround = GetTextureFromFile("textures/bg.jpg", bgWidth, bgHeight);
driver->removeTexture(tBackGround_menu);
......@@ -236,30 +246,90 @@ irr::video::ITexture* ImageManager::GetTexture(int code, bool fit) {
else
return mainGame->gameConf.use_image_scale ? (fit ? tUnknownFit : tUnknown) : GetTextureThumb(code);
}
irr::video::ITexture* ImageManager::GetTextureThumb(int code) {
if(code == 0)
return tUnknownThumb;
auto tit = tThumb.find(code);
int width = CARD_THUMB_WIDTH * mainGame->xScale;
int height = CARD_THUMB_HEIGHT * mainGame->yScale;
if(tit == tThumb.end()) {
int ImageManager::LoadThumbThread() {
while(true) {
imageManager.tThumbLoadingMutex.lock();
int code = imageManager.tThumbLoadingCodes.front();
imageManager.tThumbLoadingCodes.pop();
imageManager.tThumbLoadingMutex.unlock();
char file[256];
sprintf(file, "expansions/pics/thumbnail/%d.jpg", code);
irr::video::ITexture* img = GetTextureFromFile(file, width, height);
irr::video::IImage* img = imageManager.driver->createImageFromFile(file);
if(img == NULL) {
sprintf(file, "pics/thumbnail/%d.jpg", code);
img = GetTextureFromFile(file, width, height);
img = imageManager.driver->createImageFromFile(file);
}
if(img == NULL && mainGame->gameConf.use_image_scale) {
sprintf(file, "expansions/pics/%d.jpg", code);
img = GetTextureFromFile(file, width, height);
if(img == NULL) {
sprintf(file, "pics/%d.jpg", code);
img = GetTextureFromFile(file, width, height);
img = imageManager.driver->createImageFromFile(file);
}
if(img == NULL && mainGame->gameConf.use_image_scale) {
sprintf(file, "pics/%d.jpg", code);
img = imageManager.driver->createImageFromFile(file);
}
if(img != NULL) {
int width = CARD_THUMB_WIDTH * mainGame->xScale;
int height = CARD_THUMB_HEIGHT * mainGame->yScale;
if(img->getDimension() == irr::core::dimension2d<u32>(width, height)) {
img->grab();
imageManager.tThumbLoadingMutex.lock();
if(imageManager.tThumbLoadingThreadRunning)
imageManager.tThumbLoading[code] = img;
imageManager.tThumbLoadingMutex.unlock();
} else {
irr::video::IImage *destimg = imageManager.driver->createImage(img->getColorFormat(), irr::core::dimension2d<u32>(width, height));
imageScaleNNAA(img, destimg);
img->drop();
destimg->grab();
imageManager.tThumbLoadingMutex.lock();
if(imageManager.tThumbLoadingThreadRunning)
imageManager.tThumbLoading[code] = destimg;
imageManager.tThumbLoadingMutex.unlock();
}
} else {
imageManager.tThumbLoadingMutex.lock();
if(imageManager.tThumbLoadingThreadRunning)
imageManager.tThumbLoading[code] = NULL;
imageManager.tThumbLoadingMutex.unlock();
}
imageManager.tThumbLoadingMutex.lock();
imageManager.tThumbLoadingThreadRunning = !imageManager.tThumbLoadingCodes.empty();
if(!imageManager.tThumbLoadingThreadRunning)
break;
imageManager.tThumbLoadingMutex.unlock();
}
imageManager.tThumbLoadingMutex.unlock();
return 0;
}
irr::video::ITexture* ImageManager::GetTextureThumb(int code) {
if(code == 0)
return tUnknownThumb;
imageManager.tThumbLoadingMutex.lock();
auto lit = tThumbLoading.find(code);
if(lit != tThumbLoading.end()) {
if(lit->second != NULL) {
char file[256];
sprintf(file, "pics/thumbnail/%d.jpg", code);
irr::video::ITexture* texture = driver->addTexture(file, lit->second); // textures must be added in the main thread due to OpenGL
lit->second->drop();
tThumb[code] = texture;
} else {
tThumb[code] = NULL;
}
tThumbLoading.erase(lit);
}
imageManager.tThumbLoadingMutex.unlock();
auto tit = tThumb.find(code);
if(tit == tThumb.end()) {
tThumb[code] = tLoading;
imageManager.tThumbLoadingMutex.lock();
tThumbLoadingCodes.push(code);
if(!tThumbLoadingThreadRunning) {
tThumbLoadingThreadRunning = true;
std::thread(LoadThumbThread).detach();
}
tThumb[code] = img;
return (img == NULL) ? tUnknownThumb : img;
imageManager.tThumbLoadingMutex.unlock();
return tLoading;
}
if(tit->second)
return tit->second;
......
......@@ -4,6 +4,7 @@
#include "config.h"
#include "data_manager.h"
#include <unordered_map>
#include <queue>
namespace ygo {
......@@ -18,16 +19,22 @@ public:
irr::video::ITexture* GetTexture(int code, bool fit = false);
irr::video::ITexture* GetTextureThumb(int code);
irr::video::ITexture* GetTextureField(int code);
static int LoadThumbThread();
std::unordered_map<int, irr::video::ITexture*> tMap[2];
std::unordered_map<int, irr::video::ITexture*> tThumb;
std::unordered_map<int, irr::video::ITexture*> tFields;
std::unordered_map<int, irr::video::IImage*> tThumbLoading;
std::queue<int> tThumbLoadingCodes;
std::mutex tThumbLoadingMutex;
bool tThumbLoadingThreadRunning;
irr::IrrlichtDevice* device;
irr::video::IVideoDriver* driver;
irr::video::ITexture* tCover[4];
irr::video::ITexture* tUnknown;
irr::video::ITexture* tUnknownFit;
irr::video::ITexture* tUnknownThumb;
irr::video::ITexture* tLoading;
irr::video::ITexture* tAct;
irr::video::ITexture* tAttack;
irr::video::ITexture* tNegated;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment