Skip to content

Commit

Permalink
Update image loading to support scaling
Browse files Browse the repository at this point in the history
Added width and height parameters to the drawImage function to enable image scaling. Adjusted Surface class to pass these parameters appropriately. Enhanced image loading logic to handle different image types with scaling.
  • Loading branch information
Charles-Mahoudeau committed Aug 23, 2024
1 parent 144158e commit 7da6c89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
17 changes: 10 additions & 7 deletions lib/graphics/src/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,22 @@ namespace graphics
color);
}

void Surface::drawImage(const SImage &image, const int16_t x, const int16_t y)
void Surface::drawImage(const SImage &image, const int16_t x, const int16_t y, const uint16_t w, const uint16_t h)
{
float scaleX = static_cast<float>(w) / static_cast<float>(image.getWidth());
float scaleY = static_cast<float>(h) / static_cast<float>(image.getHeight());

#ifdef ESP_PLATFORM
switch (image.getType()) // image size with right format
{
case BMP:
m_sprite.drawBmpFile(image.getPath().str().c_str(), x, y);
m_sprite.drawBmpFile(image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY);
break;
case PNG:
m_sprite.drawPngFile(image.getPath().str().c_str(), x, y);
m_sprite.drawPngFile(image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY);
break;
case JPG:
m_sprite.drawJpgFile(image.getPath().str().c_str(), x, y);
m_sprite.drawJpgFile(image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY);
break;
};

Expand All @@ -271,13 +274,13 @@ namespace graphics
switch (image.getType())
{
case BMP:
m_sprite.drawBmpFile(&file, image.getPath().str().c_str(), x, y);
m_sprite.drawBmpFile(&file, image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY);
break;
case PNG:
m_sprite.drawPngFile(&file, image.getPath().str().c_str(), x, y);
m_sprite.drawPngFile(&file, image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY);
break;
case JPG:
m_sprite.drawJpgFile(&file, image.getPath().str().c_str(), x, y);
m_sprite.drawJpgFile(&file, image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY);
break;
};
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/graphics/src/Surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace graphics

void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, color_t color);

void drawImage(const SImage &image, int16_t x, int16_t y);
void drawImage(const SImage &image, int16_t x, int16_t y, uint16_t w = 0, uint16_t h = 0);

void setFont(EFont font);
void setFontSize(float fontSize);
Expand Down
34 changes: 17 additions & 17 deletions lib/gui/src/elements/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ namespace gui::ImagesList

std::vector<ImageLoaded> images;

std::shared_ptr<graphics::Surface> loadImage(storage::Path path, uint16_t width, uint16_t height, color_t backgroundColor = 0xFFFF)
{
for (const auto& image : images)
{
if (image.path.str() == path.str() && image.width == width && image.height == height)
{
//std::cout << "image already loaded" << std::endl;
std::shared_ptr<graphics::Surface> loadImage(storage::Path path, uint16_t width, uint16_t height, const color_t backgroundColor = 0xFFFF) {
// ReSharper disable once CppUseStructuredBinding
for (const auto& image : images) {
if (image.path.str() == path.str() && image.width == width && image.height == height) {
return image.surface;
}
}

//std::cout << "image not loaded" << std::endl;
const auto i = graphics::SImage(path);

graphics::SImage i = graphics::SImage(path);
// libsystem::log("Image: " + std::to_string(i.getType()) + ", " + std::to_string(i.getWidth()) + ", " + std::to_string(i.getHeight()) + ", " + i.getPath().str());

libsystem::log("Image: " + std::to_string(i.getType()) + ", " + std::to_string(i.getWidth()) + ", " + std::to_string(i.getHeight()) + ", " + i.getPath().str());
ImageLoaded img = {
path,
i.getWidth(),
i.getHeight(),
std::make_shared<graphics::Surface>(width, height)
};

ImageLoaded img = { path, i.getWidth(), i.getHeight(), std::make_shared<graphics::Surface>(i.getWidth(), i.getHeight()) };

uint16_t m_width = i.getWidth();
uint16_t m_height = i.getHeight();

if(i.getType() != graphics::ImageType::BMP)
// Clear the background if it's a transparent image ?
// I guess so ?
if(i.getType() != graphics::ImageType::BMP) {
img.surface->clear(backgroundColor);
}

img.surface->drawImage(i, 0, 0);
img.surface->drawImage(i, 0, 0, width, height);

images.push_back(img);

Expand Down

0 comments on commit 7da6c89

Please sign in to comment.