* encapsulate image load/get functions + fix access in current code

* don't add new image resource if current one exists
This commit is contained in:
a 2024-08-17 18:26:48 +03:00
parent 80bc1e2490
commit 4d06c010a3
5 changed files with 49 additions and 23 deletions

View File

@ -208,6 +208,8 @@ private:
std::map<std::string, Leaderboard_config> leaderboards{};
std::map<std::string, Stat_config> stats{};
std::map<size_t, struct Image_Data> images{};
//supported languages
std::set<std::string> supported_languages_set{};
std::string supported_languages{};
@ -265,8 +267,6 @@ public:
// enable owning Steam Applications IDs (mostly builtin apps + dedicated servers)
bool enable_builtin_preowned_ids = false;
std::map<int, struct Image_Data> images{};
//subscribed lobby/group ids
std::set<uint64> subscribed_groups{};
std::vector<Group_Clans> subscribed_groups_clans{};
@ -383,6 +383,7 @@ public:
//images
int add_image(const std::string &data, uint32 width, uint32 height);
Image_Data* get_image(int handle);
// overlay auto accept stuff
void acceptAnyOverlayInvites(bool value);

View File

@ -859,9 +859,9 @@ std::vector<image_pixel_t> Local_Storage::load_image(std::string const& image_pa
std::vector<image_pixel_t> res{};
int width{}, height{};
image_pixel_t* img = (image_pixel_t*)stbi_load(image_path.c_str(), &width, &height, nullptr, 4);
if (img != nullptr)
{
res.resize(width*height);
PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img ? "loaded" : stbi_failure_reason()));
if (img) {
res.resize(width * height);
std::copy(img, img + width * height, res.begin());
stbi_image_free(img);
@ -875,19 +875,18 @@ std::string Local_Storage::load_image_resized(std::string const& image_path, std
{
std::string resized_image{};
const size_t resized_img_size = resolution * resolution * 4;
if (image_path.length() > 0) {
if (image_path.size()) {
int width = 0;
int height = 0;
unsigned char *img = stbi_load(image_path.c_str(), &width, &height, nullptr, 4);
PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img == nullptr ? stbi_failure_reason() : "loaded"));
if (img != nullptr) {
PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img ? "loaded" : stbi_failure_reason()));
if (img) {
std::vector<char> out_resized(resized_img_size);
stbir_resize_uint8(img, width, height, 0, (unsigned char*)&out_resized[0], resolution, resolution, 0, 4);
resized_image = std::string((char*)&out_resized[0], out_resized.size());
stbi_image_free(img);
}
} else if (image_data.length() > 0) {
} else if (image_data.size()) {
std::vector<char> out_resized(resized_img_size);
stbir_resize_uint8((unsigned char*)image_data.c_str(), 184, 184, 0, (unsigned char*)&out_resized[0], resolution, resolution, 0, 4);
resized_image = std::string((char*)&out_resized[0], out_resized.size());

View File

@ -353,13 +353,33 @@ std::map<std::string, Stat_config>::const_iterator Settings::setStatDefiniton(co
int Settings::add_image(const std::string &data, uint32 width, uint32 height)
{
int last = static_cast<int>(images.size()) + 1;
struct Image_Data dt;
auto previous_it = std::find_if(images.begin(), images.end(), [&](const std::pair<const size_t, Image_Data> &item) {
return item.second.data == data
&& item.second.height == height
&& item.second.width == width;
});
if (images.end() != previous_it) {
return static_cast<int>(previous_it->first);
}
struct Image_Data dt{};
dt.width = width;
dt.height = height;
dt.data = data;
images[last] = dt;
return last;
auto new_handle = images.size() + 1; // never return 0, it is a bad handle for most ISteamUserStats APIs
images[new_handle] = dt;
return static_cast<int>(new_handle);
}
Image_Data* Settings::get_image(int handle)
{
auto image_it = images.find(handle);
if (images.end() == image_it) {
return nullptr;
}
return &image_it->second;
}

View File

@ -1280,9 +1280,15 @@ void Steam_Friends::Callback(Common_Message *msg)
f->set_name(settings->get_local_name());
f->set_appid(settings->get_local_game_id().AppID());
f->set_lobby_id(settings->get_lobby().ConvertToUint64());
int avatar_number = GetLargeFriendAvatar(settings->get_local_steam_id());
if (settings->images[avatar_number].data.length() > 0) f->set_avatar(settings->images[avatar_number].data);
else f->set_avatar("");
auto avatar_info = settings->get_image(avatar_number);
if (avatar_info && avatar_info->data.size()) {
f->set_avatar(avatar_info->data);
} else {
f->set_avatar("");
}
msg_.set_allocated_friend_(f);
network->sendTo(&msg_, true);
}

View File

@ -81,11 +81,11 @@ bool Steam_Utils::GetImageSize( int iImage, uint32 *pnWidth, uint32 *pnHeight )
if (!iImage || !pnWidth || !pnHeight) return false;
auto image = settings->images.find(iImage);
if (settings->images.end() == image) return false;
auto image_info = settings->get_image(iImage);
if (!image_info) return false;
*pnWidth = image->second.width;
*pnHeight = image->second.height;
*pnWidth = image_info->width;
*pnHeight = image_info->height;
return true;
}
@ -99,10 +99,10 @@ bool Steam_Utils::GetImageRGBA( int iImage, uint8 *pubDest, int nDestBufferSize
if (!iImage || !pubDest || nDestBufferSize <= 0) return false;
auto image = settings->images.find(iImage);
if (settings->images.end() == image) return false;
auto image_info = settings->get_image(iImage);
if (!image_info) return false;
image->second.data.copy((char *)pubDest, nDestBufferSize);
image_info->data.copy((char *)pubDest, nDestBufferSize);
return true;
}