Simple GUI Library
image.cpp
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file image.cpp
4 * @date 2021-11-07
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#include "sml/graphics_wrapper/renderer.h"
10#include "media/image.h"
11
12namespace Sgl
13{
14 Image::Image(const char* filename, ImageFormat format)
15 : m_Format(format), m_Texture(new Sml::Texture())
16 {
17 m_Texture->loadFromImage(filename);
18 assert(m_Texture->getWidth() > 0);
19 assert(m_Texture->getHeight() > 0);
20 }
21
22 Image::Image(const Sml::Texture* texture)
23 : m_Format(ImageFormat::INVALID), m_Texture(texture->copy()) {}
24
25 int32_t Image::getWidth() const { return m_Texture->getWidth(); }
26 int32_t Image::getHeight() const { return m_Texture->getHeight(); }
27 ImageFormat Image::getFormat() const { return m_Format; }
28 const Sml::Texture* Image::getTexture() const { return m_Texture; }
29
30 void renderImage(const Image* image, const Sml::Rectangle<int32_t>& targetRegion)
31 {
32 assert(image);
33 assert(image->getTexture());
34
35 image->getTexture()->copyTo(Sml::Renderer::getInstance().getTarget(), &targetRegion, nullptr);
36 }
37
38 void renderImage(const Image* image, const Sml::Rectangle<int32_t>& targetRegion,
39 int32_t scaledWidth, int32_t scaledHeight)
40 {
41 assert(image);
42 assert(image->getTexture());
43
44 if (scaledWidth <= 0 || scaledHeight <= 0) { return; }
45
46 Sml::Rectangle<int32_t> scaledRegion{targetRegion.pos, scaledWidth, scaledHeight};
47 image->getTexture()->copyTo(Sml::Renderer::getInstance().getTarget(), &scaledRegion, nullptr);
48 }
49}