|
|
@ -1,4 +1,5 @@
|
|
|
|
#include <mutex>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
|
|
|
|
#include "Internal.h"
|
|
|
|
#include "Internal.h"
|
|
|
@ -30,6 +31,10 @@ static std::unordered_map<TexposHandle, long> g_handle_to_texpos;
|
|
|
|
static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface;
|
|
|
|
static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface;
|
|
|
|
static std::mutex g_adding_mutex;
|
|
|
|
static std::mutex g_adding_mutex;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// it is SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32
|
|
|
|
|
|
|
|
// initialized inplace to avoid including SDL_pixels.h
|
|
|
|
|
|
|
|
static const uint32_t RGBA32 = 376840196;
|
|
|
|
|
|
|
|
|
|
|
|
// Converts an arbitrary Surface to something like the display format
|
|
|
|
// Converts an arbitrary Surface to something like the display format
|
|
|
|
// (32-bit RGBA), and converts magenta to transparency if convert_magenta is set
|
|
|
|
// (32-bit RGBA), and converts magenta to transparency if convert_magenta is set
|
|
|
|
// and the source surface didn't already have an alpha channel.
|
|
|
|
// and the source surface didn't already have an alpha channel.
|
|
|
@ -38,28 +43,14 @@ static std::mutex g_adding_mutex;
|
|
|
|
// It uses the same pixel format (RGBA, R at lowest address) regardless of
|
|
|
|
// It uses the same pixel format (RGBA, R at lowest address) regardless of
|
|
|
|
// hardware.
|
|
|
|
// hardware.
|
|
|
|
SDL_Surface* canonicalize_format(SDL_Surface* src) {
|
|
|
|
SDL_Surface* canonicalize_format(SDL_Surface* src) {
|
|
|
|
SDL_PixelFormat fmt;
|
|
|
|
// however we have null check after DFIMG_Load
|
|
|
|
fmt.palette = NULL;
|
|
|
|
// in loadTileset() (the only consumer of this method)
|
|
|
|
fmt.BitsPerPixel = 32;
|
|
|
|
// it's better put nullcheck here as well
|
|
|
|
fmt.BytesPerPixel = 4;
|
|
|
|
if (!src)
|
|
|
|
fmt.Rloss = fmt.Gloss = fmt.Bloss = fmt.Aloss = 0;
|
|
|
|
return src;
|
|
|
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
|
|
|
|
|
|
|
fmt.Rshift = 24;
|
|
|
|
auto fmt = DFSDL_AllocFormat(RGBA32);
|
|
|
|
fmt.Gshift = 16;
|
|
|
|
SDL_Surface* tgt = DFSDL_ConvertSurface(src, fmt, SDL_SWSURFACE);
|
|
|
|
fmt.Bshift = 8;
|
|
|
|
|
|
|
|
fmt.Ashift = 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
fmt.Rshift = 0;
|
|
|
|
|
|
|
|
fmt.Gshift = 8;
|
|
|
|
|
|
|
|
fmt.Bshift = 16;
|
|
|
|
|
|
|
|
fmt.Ashift = 24;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fmt.Rmask = 255 << fmt.Rshift;
|
|
|
|
|
|
|
|
fmt.Gmask = 255 << fmt.Gshift;
|
|
|
|
|
|
|
|
fmt.Bmask = 255 << fmt.Bshift;
|
|
|
|
|
|
|
|
fmt.Amask = 255 << fmt.Ashift;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SDL_Surface* tgt = DFSDL_ConvertSurface(src, &fmt, SDL_SWSURFACE);
|
|
|
|
|
|
|
|
DFSDL_FreeSurface(src);
|
|
|
|
DFSDL_FreeSurface(src);
|
|
|
|
for (int x = 0; x < tgt->w; ++x) {
|
|
|
|
for (int x = 0; x < tgt->w; ++x) {
|
|
|
|
for (int y = 0; y < tgt->h; ++y) {
|
|
|
|
for (int y = 0; y < tgt->h; ++y) {
|
|
|
@ -71,6 +62,7 @@ SDL_Surface* canonicalize_format(SDL_Surface* src) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tgt;
|
|
|
|
return tgt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -82,31 +74,34 @@ static long add_texture(SDL_Surface* surface) {
|
|
|
|
return texpos;
|
|
|
|
return texpos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TexposHandle Textures::loadTexture(SDL_Surface* surface) {
|
|
|
|
// delete surface from texture raws
|
|
|
|
if (!surface)
|
|
|
|
static void delete_texture(long texpos) {
|
|
|
|
return 0; // should be some error, i guess
|
|
|
|
std::lock_guard<std::mutex> lg_add_texture(g_adding_mutex);
|
|
|
|
|
|
|
|
if (texpos >= enabler->textures.raws.size())
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
enabler->textures.raws[texpos] = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto handle = reinterpret_cast<uintptr_t>(surface);
|
|
|
|
// create new surface with RGBA32 format and pixels as data
|
|
|
|
g_handle_to_surface.emplace(handle, surface);
|
|
|
|
SDL_Surface* create_texture(std::vector<uint32_t>& pixels, int texture_px_w, int texture_px_h) {
|
|
|
|
surface->refcount++; // prevent destruct on next FreeSurface by game
|
|
|
|
auto surface = DFSDL_CreateRGBSurfaceWithFormat(0, texture_px_w, texture_px_h, 32, RGBA32);
|
|
|
|
auto texpos = add_texture(surface);
|
|
|
|
for (auto i = 0; i < pixels.size() && i < texture_px_w * texture_px_h; i++) {
|
|
|
|
g_handle_to_texpos.emplace(handle, texpos);
|
|
|
|
uint32_t* p = (uint32_t*)surface->pixels + i;
|
|
|
|
return handle;
|
|
|
|
*p = pixels[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int tile_px_w,
|
|
|
|
// convert single surface into tiles according w/h
|
|
|
|
int tile_px_h) {
|
|
|
|
// register tiles in texture raws and return handles
|
|
|
|
|
|
|
|
std::vector<TexposHandle> slice_tileset(SDL_Surface* surface, int tile_px_w, int tile_px_h) {
|
|
|
|
std::vector<TexposHandle> handles{};
|
|
|
|
std::vector<TexposHandle> handles{};
|
|
|
|
|
|
|
|
if (!surface)
|
|
|
|
SDL_Surface* surface = DFIMG_Load(file.c_str());
|
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
|
|
|
|
ERR(textures).printerr("unable to load textures from '%s'\n", file.c_str());
|
|
|
|
|
|
|
|
return handles;
|
|
|
|
return handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
surface = canonicalize_format(surface);
|
|
|
|
|
|
|
|
int dimx = surface->w / tile_px_w;
|
|
|
|
int dimx = surface->w / tile_px_w;
|
|
|
|
int dimy = surface->h / tile_px_h;
|
|
|
|
int dimy = surface->h / tile_px_h;
|
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < dimy; y++) {
|
|
|
|
for (int y = 0; y < dimy; y++) {
|
|
|
|
for (int x = 0; x < dimx; x++) {
|
|
|
|
for (int x = 0; x < dimx; x++) {
|
|
|
|
SDL_Surface* tile = DFSDL_CreateRGBSurface(
|
|
|
|
SDL_Surface* tile = DFSDL_CreateRGBSurface(
|
|
|
@ -120,6 +115,32 @@ std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int til
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DFSDL_FreeSurface(surface);
|
|
|
|
DFSDL_FreeSurface(surface);
|
|
|
|
|
|
|
|
return handles;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TexposHandle Textures::loadTexture(SDL_Surface* surface) {
|
|
|
|
|
|
|
|
if (!surface)
|
|
|
|
|
|
|
|
return 0; // should be some error, i guess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto handle = reinterpret_cast<uintptr_t>(surface);
|
|
|
|
|
|
|
|
g_handle_to_surface.emplace(handle, surface);
|
|
|
|
|
|
|
|
surface->refcount++; // prevent destruct on next FreeSurface by game
|
|
|
|
|
|
|
|
auto texpos = add_texture(surface);
|
|
|
|
|
|
|
|
g_handle_to_texpos.emplace(handle, texpos);
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int tile_px_w,
|
|
|
|
|
|
|
|
int tile_px_h) {
|
|
|
|
|
|
|
|
SDL_Surface* surface = DFIMG_Load(file.c_str());
|
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
|
|
|
|
ERR(textures).printerr("unable to load textures from '%s'\n", file.c_str());
|
|
|
|
|
|
|
|
return std::vector<TexposHandle>{};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
surface = canonicalize_format(surface);
|
|
|
|
|
|
|
|
auto handles = slice_tileset(surface, tile_px_w, tile_px_h);
|
|
|
|
|
|
|
|
|
|
|
|
DEBUG(textures).print("loaded %zd textures from '%s'\n", handles.size(), file.c_str());
|
|
|
|
DEBUG(textures).print("loaded %zd textures from '%s'\n", handles.size(), file.c_str());
|
|
|
|
|
|
|
|
|
|
|
|
return handles;
|
|
|
|
return handles;
|
|
|
@ -142,6 +163,34 @@ long Textures::getTexposByHandle(TexposHandle handle) {
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TexposHandle Textures::createTile(std::vector<uint32_t>& pixels, int tile_px_w, int tile_px_h) {
|
|
|
|
|
|
|
|
auto texture = create_texture(pixels, tile_px_w, tile_px_h);
|
|
|
|
|
|
|
|
auto handle = Textures::loadTexture(texture);
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<TexposHandle> Textures::createTileset(std::vector<uint32_t>& pixels, int texture_px_w,
|
|
|
|
|
|
|
|
int texture_px_h, int tile_px_w, int tile_px_h) {
|
|
|
|
|
|
|
|
auto texture = create_texture(pixels, texture_px_w, texture_px_h);
|
|
|
|
|
|
|
|
auto handles = slice_tileset(texture, tile_px_w, tile_px_h);
|
|
|
|
|
|
|
|
return handles;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Textures::deleteHandle(TexposHandle handle) {
|
|
|
|
|
|
|
|
auto texpos = Textures::getTexposByHandle(handle);
|
|
|
|
|
|
|
|
if (texpos > 0)
|
|
|
|
|
|
|
|
delete_texture(texpos);
|
|
|
|
|
|
|
|
if (g_handle_to_texpos.contains(handle))
|
|
|
|
|
|
|
|
g_handle_to_texpos.erase(handle);
|
|
|
|
|
|
|
|
if (g_handle_to_surface.contains(handle)) {
|
|
|
|
|
|
|
|
auto surface = g_handle_to_surface[handle];
|
|
|
|
|
|
|
|
for (auto i = 0; i < surface->refcount; i++) {
|
|
|
|
|
|
|
|
DFSDL_FreeSurface(surface);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
g_handle_to_surface.erase(handle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void reset_texpos() {
|
|
|
|
static void reset_texpos() {
|
|
|
|
g_handle_to_texpos.clear();
|
|
|
|
g_handle_to_texpos.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|