diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-06-07 00:31:46 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-06-07 00:31:46 +0300 |
commit | 4c057739d0538d01481643848a6cb558e0ef4e51 (patch) | |
tree | fd09c3a5e5ddccd4d133ecdbe232f8490ba732a0 /src | |
parent | 5f434dba57bd80ab370dba5a9a425e86acbe800f (diff) | |
download | manaverse-4c057739d0538d01481643848a6cb558e0ef4e51.tar.gz manaverse-4c057739d0538d01481643848a6cb558e0ef4e51.tar.bz2 manaverse-4c057739d0538d01481643848a6cb558e0ef4e51.tar.xz manaverse-4c057739d0538d01481643848a6cb558e0ef4e51.zip |
More improve load image speed in opengl mode.
Diffstat (limited to 'src')
-rw-r--r-- | src/resources/dye.cpp | 23 | ||||
-rw-r--r-- | src/resources/dye.h | 2 | ||||
-rw-r--r-- | src/resources/image.cpp | 104 |
3 files changed, 98 insertions, 31 deletions
diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index 50972c6d6..78ad8137a 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -228,6 +228,29 @@ void DyePalette::replaceColor(Uint8 *color) const } } +void DyePalette::replaceOGLColor(Uint8 *color) const +{ + std::vector<Color>::const_iterator it = mColors.begin(); + std::vector<Color>::const_iterator it_end = mColors.end(); + while (it != it_end) + { + const Color &col = *it; + ++ it; + if (it == it_end) + return; + const Color &col2 = *it; + if (color[2] == col.value[0] && color[1] == col.value[1] + && color[0] == col.value[2]) + { + color[0] = col2.value[0]; + color[1] = col2.value[1]; + color[2] = col2.value[2]; + return; + } + ++ it; + } +} + Dye::Dye(const std::string &description) { for (int i = 0; i < dyePalateSize; ++i) diff --git a/src/resources/dye.h b/src/resources/dye.h index 03aec71cb..b238f65fb 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -63,6 +63,8 @@ class DyePalette void replaceColor(Uint8 *color) const; + void replaceOGLColor(Uint8 *color) const; + private: struct Color { unsigned char value[3]; }; diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 596a477c6..85643c8df 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -149,50 +149,92 @@ Resource *Image::load(SDL_RWops *rw, Dye const &dye) return nullptr; } + SDL_Surface *surf = nullptr; SDL_PixelFormat rgba; rgba.palette = nullptr; rgba.BitsPerPixel = 32; rgba.BytesPerPixel = 4; - rgba.Rmask = 0xFF000000; rgba.Rloss = 0; rgba.Rshift = 24; - rgba.Gmask = 0x00FF0000; rgba.Gloss = 0; rgba.Gshift = 16; - rgba.Bmask = 0x0000FF00; rgba.Bloss = 0; rgba.Bshift = 8; - rgba.Amask = 0x000000FF; rgba.Aloss = 0; rgba.Ashift = 0; rgba.colorkey = 0; rgba.alpha = 255; + if (mUseOpenGL) + { + surf = convertTo32Bit(tmpImage); + SDL_FreeSurface(tmpImage); - SDL_Surface *surf = SDL_ConvertSurface(tmpImage, &rgba, SDL_SWSURFACE); - SDL_FreeSurface(tmpImage); - - Uint32 *pixels = static_cast<Uint32 *>(surf->pixels); - DyePalette *pal = dye.getSPalete(); + Uint32 *pixels = static_cast<Uint32 *>(surf->pixels); + DyePalette *pal = dye.getSPalete(); - if (pal) - { - for (Uint32 *p_end = pixels + surf->w * surf->h; - pixels != p_end; ++pixels) + if (pal) { - Uint8 *p = (Uint8 *)pixels; - const int alpha = *p & 255; - if (!alpha) - continue; - pal->replaceColor(p + 1); + for (Uint32 *p_end = pixels + surf->w * surf->h; + pixels != p_end; ++pixels) + { + Uint8 *p = (Uint8 *)pixels; + const int alpha = *p & 255; + if (!alpha) + continue; + pal->replaceOGLColor(p); + } + } + else + { + for (Uint32 *p_end = pixels + surf->w * surf->h; + pixels != p_end; ++pixels) + { + const Uint32 p = *pixels; + const int alpha = (p >> 24) & 255; + if (!alpha) + continue; + int v[3]; + v[0] = (p) & 255; + v[1] = (p >> 8) & 255; + v[2] = (p >> 16 ) & 255; + dye.update(v); + *pixels = (v[0]) | (v[1] << 8) | (v[2] << 16) | (alpha << 24); + } } } else { - for (Uint32 *p_end = pixels + surf->w * surf->h; - pixels != p_end; ++pixels) + rgba.Rmask = 0xFF000000; rgba.Rloss = 0; rgba.Rshift = 24; + rgba.Gmask = 0x00FF0000; rgba.Gloss = 0; rgba.Gshift = 16; + rgba.Bmask = 0x0000FF00; rgba.Bloss = 0; rgba.Bshift = 8; + rgba.Amask = 0x000000FF; rgba.Aloss = 0; rgba.Ashift = 0; + + surf = SDL_ConvertSurface(tmpImage, &rgba, SDL_SWSURFACE); + SDL_FreeSurface(tmpImage); + + Uint32 *pixels = static_cast<Uint32 *>(surf->pixels); + DyePalette *pal = dye.getSPalete(); + + if (pal) + { + for (Uint32 *p_end = pixels + surf->w * surf->h; + pixels != p_end; ++pixels) + { + Uint8 *p = (Uint8 *)pixels; + const int alpha = *p & 255; + if (!alpha) + continue; + pal->replaceColor(p + 1); + } + } + else { - const Uint32 p = *pixels; - const int alpha = p & 255; - if (!alpha) - continue; - int v[3]; - v[0] = (p >> 24) & 255; - v[1] = (p >> 16) & 255; - v[2] = (p >> 8 ) & 255; - dye.update(v); - *pixels = (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | alpha; + for (Uint32 *p_end = pixels + surf->w * surf->h; + pixels != p_end; ++pixels) + { + const Uint32 p = *pixels; + const int alpha = p & 255; + if (!alpha) + continue; + int v[3]; + v[0] = (p >> 24) & 255; + v[1] = (p >> 16) & 255; + v[2] = (p >> 8 ) & 255; + dye.update(v); + *pixels = (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | alpha; + } } } @@ -908,7 +950,7 @@ void Image::dumpSurfaceFormat(SDL_Surface *image) { if (image->format) { - const SDL_PixelFormat *format = image->format; + const SDL_PixelFormat * const format = image->format; logger->log("Bytes per pixel: %d", format->BytesPerPixel); logger->log("Alpha: %d", format->alpha); logger->log("Loss: %02x, %02x, %02x, %02x", (int)format->Rloss, |