ADTF_DISPLAY_TOOLBOX  3.8.0 (ADTF 3.14.3)
Source Code for Demo 2D Image Drawer
Location
./src/examples/src/imagedrawer
This example shows:
  • how to create a custom drawer.
  • how to use the ITexture interface.
  • how to use the canvas inside the Draw method.
Header
#pragma once
class cImageDrawer :
public adtf::ucom::object<adtf::disptb::drawerlib::cDrawer>
{
public:
ADTF_CLASS_ID_NAME(
cImageDrawer,
"demo_image.2d_drawer.disptb.cid",
"Demo 2D Image Drawer"
);
cImageDrawer();
~cImageDrawer() = default;
tResult Init(tInitStage eStage) override;
tResult Shutdown(tInitStage eStage) override;
tResult Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas) override;
private:
tResult RecreateTexture(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas);
tVoid DestroyTexture();
private:
adtf::base::property_variable<adtf_util::cString> m_strColor;
adtf::base::property_variable<adtf_util::cFilename> m_strPath;
adtf::base::property_variable<tBool> m_bEnableAlpha;
tBool m_bRecreateTexture;
};
The interface for common texture handling.
Copyright © Audi Electronics Venture GmbH.
Implementation
#include "image_drawer.h"
ADTF_PLUGIN_VERSION("Demo 2D Image Drawer Plugin",
disptb,
DISPTB_VERSION_MAJOR,
DISPTB_VERSION_MINOR,
DISPTB_VERSION_PATCH,
cImageDrawer)
cImageDrawer::cImageDrawer():
m_strColor("#00000000"),
m_strPath(""),
m_bEnableAlpha(tFalse),
m_pTexture(nullptr),
m_bRecreateTexture(tFalse)
{
m_strColor.SetDescription("Color key for transparency.");
RegisterPropertyVariable("Color", m_strColor);
m_bEnableAlpha.SetDescription("Adjustment for the alpha channel.");
RegisterPropertyVariable("EnableAlpha", m_bEnableAlpha);
m_strPath.SetDescription("Image file to load.");
m_strPath.SetFilenameExtensionFilter("Bitmap(*.bmp)");
RegisterPropertyVariable("Path", m_strPath);
SetDescription("Use this Drawer to paint a given image onto the canvas.");
SetHelpLink("$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_2d_image_drawer_readme.html");
}
tResult cImageDrawer::Init(tInitStage eStage)
{
RETURN_IF_FAILED(cDrawer::Init(eStage));
adtf_util::cString color = m_strColor;
RETURN_IF_FAILED(adtf::disptb::graphicslib::cColor::ColorFromString(color, m_oColor));
m_bRecreateTexture = tTrue;
RETURN_NOERROR;
}
tResult cImageDrawer::Shutdown(tInitStage eStage)
{
RETURN_IF_FAILED(cDrawer::Shutdown(eStage));
RETURN_NOERROR;
}
tResult cImageDrawer::RecreateTexture(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas)
{
DestroyTexture();
adtf_util::cString strPath = m_strPath;
if (strPath.IsEmpty())
{
LOG_WARNING("No Image specified");
RETURN_ERROR(ERR_NOT_FOUND);
}
tResult oError;
if (IS_FAILED(oError = oImage.Load(strPath)))
{
adtf_util::cFilename strFilename(m_strPath);
if (!strFilename.GetExtension().IsEqualNoCase(".bmp"))
{
LOG_ERROR("Unsupported image format. See documentation for supported formats.");
}
else
{
LOG_ERROR("Error while loading Image");
}
return oError;
};
if (!m_bEnableAlpha || IS_OK(adtf::disptb::graphicslib::cImage::SetTransparency(&oImage, m_oColor)))
{
RETURN_IF_FAILED(pCanvas->CreateTexture(&oImage, m_pTexture));
}
RETURN_NOERROR;
}
tVoid cImageDrawer::DestroyTexture()
{
if (m_pTexture)
{
m_pTexture->Release();
m_pTexture = nullptr;
}
}
tResult cImageDrawer::Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas)
{
if (m_bRecreateTexture)
{
RecreateTexture(pCanvas);
m_bRecreateTexture = tFalse;
}
if (m_pTexture)
{
// blit the texture in any case (even if the image is still the same)
tFloat32 fWidth = (tFloat32)m_pTexture->GetWidth();
tFloat32 fHeight = (tFloat32)m_pTexture->GetHeight();
tFloat32 vPoints[8];
vPoints[0] = -fWidth / 2 + fWidth; vPoints[1] = -fHeight / 2;
vPoints[2] = -fWidth / 2 + fWidth; vPoints[3] = -fHeight / 2 + fHeight;
vPoints[4] = -fWidth / 2; vPoints[5] = -fHeight / 2 + fHeight;
vPoints[6] = -fWidth / 2; vPoints[7] = -fHeight / 2;
if (m_bEnableAlpha)
{
pCanvas->EnableAlpha();
}
// own method in cDrawer to blit a texture into the window
pCanvas->DrawTexture(m_pTexture, vPoints);
if (m_bEnableAlpha)
{
pCanvas->DisableAlpha();
}
}
RETURN_NOERROR;
}
static tResult ColorFromString(const tChar *strColor, cColor &oColor)
Converts a hexadecimal color value into a cColor object The input format is like the HTML color defin...
Image and bitmap handling.
Definition: image.h:39
static tResult SetTransparency(disptb::graphicslib::dengar::cImage *pImage, disptb::graphicslib::dengar::cColor oColor)
Helper method for setting transparency in bitmaps.
tResult Load(const tChar *strFile, tUInt32 nFlags=0)
Loads an image from a file.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.