128 lines
3.3 KiB
C++
128 lines
3.3 KiB
C++
/*
|
|
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
this list of conditions and the following disclaimer.
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
|
|
#include "SpriteCache.hpp"
|
|
|
|
#include <cstring>
|
|
|
|
#include <SDL_image.h>
|
|
|
|
|
|
namespace RPGEdit {
|
|
|
|
namespace View {
|
|
|
|
SpriteCache::sprite_value SpriteCache::load(const std::string &id) {
|
|
std::string filename = "../resources/sprite/" + id + ".png";
|
|
|
|
SDL_Surface *surface = IMG_Load(filename.c_str());
|
|
|
|
return sprite_value(surface, SDL_Surface_deleter());
|
|
}
|
|
|
|
SpriteCache::sprite_value SpriteCache::load(const std::string &id, unsigned rotation) {
|
|
if (!rotation)
|
|
return load(id);
|
|
|
|
sprite_value &base = get(id, 0);
|
|
SDL_Surface *surface;
|
|
|
|
SDL_LockSurface(base.get());
|
|
|
|
int w = base->w, h = base->h, d = base->format->BytesPerPixel, w2, h2;
|
|
|
|
if (rotation == 2) {
|
|
w2 = w;
|
|
h2 = h;
|
|
}
|
|
else {
|
|
w2 = h;
|
|
h2 = w;
|
|
}
|
|
|
|
surface = SDL_CreateRGBSurface(0, w2, h2, base->format->BitsPerPixel,
|
|
base->format->Rmask, base->format->Gmask, base->format->Bmask, base->format->Amask);
|
|
|
|
SDL_LockSurface(surface);
|
|
|
|
uint8_t *src = reinterpret_cast<uint8_t *>(base->pixels);
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(surface->pixels);
|
|
int pitch = base->pitch;
|
|
int pitch2 = surface->pitch;
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
for (int y = 0; y < h; y++) {
|
|
int x2, y2;
|
|
|
|
switch (rotation) {
|
|
case 1:
|
|
x2 = y;
|
|
y2 = w - x - 1;
|
|
break;
|
|
case 2:
|
|
x2 = w - x - 1;
|
|
y2 = h - y - 1;
|
|
break;
|
|
case 3:
|
|
x2 = h - y - 1;
|
|
y2 = x;
|
|
}
|
|
|
|
std::memcpy(dst + y2*pitch2 + d*x2, src + y*pitch + d*x, d);
|
|
}
|
|
}
|
|
|
|
SDL_UnlockSurface(surface);
|
|
SDL_UnlockSurface(base.get());
|
|
|
|
return sprite_value(surface, SDL_Surface_deleter());
|
|
}
|
|
|
|
SpriteCache::sprite_value & SpriteCache::get(const std::string &id, unsigned rotation) {
|
|
sprite_key key(id, rotation);
|
|
|
|
sprite_value &surface = sprites[key];
|
|
|
|
if (!surface)
|
|
surface = load(id, rotation);
|
|
|
|
if (!surface)
|
|
sprites.erase(sprites.find(key));
|
|
|
|
return surface;
|
|
}
|
|
|
|
SDL_Surface * SpriteCache::get(const std::string &type, const std::string &name, unsigned rotation) {
|
|
if (rotation >= 4)
|
|
return nullptr;
|
|
|
|
return get(type + "/" + name, rotation).get();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|