Gfx: Wrap pointers with std::unique_ptr

This commit is contained in:
Birunthan Mohanathas 2013-05-01 14:38:52 +03:00
parent d3f2e7ab83
commit 18d5ee383d
7 changed files with 44 additions and 87 deletions

View File

@ -51,8 +51,6 @@ Microsoft::WRL::ComPtr<IWICImagingFactory> CanvasD2D::c_WICFactory;
CanvasD2D::CanvasD2D() : Canvas(),
m_Bitmap(),
m_GdipGraphics(),
m_GdipBitmap(),
m_TextAntiAliasing(false)
{
Initialize();
@ -60,7 +58,6 @@ CanvasD2D::CanvasD2D() : Canvas(),
CanvasD2D::~CanvasD2D()
{
Dispose();
Finalize();
}
@ -118,27 +115,16 @@ void CanvasD2D::Finalize()
}
}
void CanvasD2D::Dispose()
{
m_Target.Reset();
delete m_GdipGraphics;
m_GdipGraphics = nullptr;
delete m_GdipBitmap;
m_GdipBitmap = nullptr;
}
void CanvasD2D::Resize(int w, int h)
{
__super::Resize(w, h);
Dispose();
m_Target.Reset();
m_Bitmap.Resize(w, h);
m_GdipBitmap = new Gdiplus::Bitmap(w, h, w * 4, PixelFormat32bppPARGB, m_Bitmap.GetData());
m_GdipGraphics = new Gdiplus::Graphics(m_GdipBitmap);
m_GdipBitmap.reset(new Gdiplus::Bitmap(w, h, w * 4, PixelFormat32bppPARGB, m_Bitmap.GetData()));
m_GdipGraphics.reset(new Gdiplus::Graphics(m_GdipBitmap.get()));
}
bool CanvasD2D::BeginDraw()

View File

@ -23,6 +23,7 @@
#include "FontCollectionD2D.h"
#include "TextFormatD2D.h"
#include "Util/WICBitmapDIB.h"
#include <memory>
#include <string>
#include <GdiPlus.h>
#include <d2d1.h>
@ -82,8 +83,6 @@ private:
static bool Initialize();
static void Finalize();
void Dispose();
bool BeginTargetDraw();
void EndTargetDraw();
@ -94,8 +93,8 @@ private:
Util::WICBitmapDIB m_Bitmap;
// GDI+ objects that share the pixel data of m_Bitmap.
Gdiplus::Graphics* m_GdipGraphics;
Gdiplus::Bitmap* m_GdipBitmap;
std::unique_ptr<Gdiplus::Graphics> m_GdipGraphics;
std::unique_ptr<Gdiplus::Bitmap> m_GdipBitmap;
bool m_TextAntiAliasing;

View File

@ -21,10 +21,8 @@
namespace Gfx {
CanvasGDIP::CanvasGDIP() : Canvas(),
m_Graphics(),
m_Bitmap(),
m_DIBSectionBuffer(),
m_DIBSectionBufferPixels()
m_DIBSection(),
m_DIBSectionPixels()
{
}
@ -35,17 +33,11 @@ CanvasGDIP::~CanvasGDIP()
void CanvasGDIP::Dispose()
{
delete m_Graphics;
m_Graphics = nullptr;
delete m_Bitmap;
m_Bitmap = nullptr;
if (m_DIBSectionBuffer)
if (m_DIBSection)
{
DeleteObject(m_DIBSectionBuffer);
m_DIBSectionBuffer = nullptr;
m_DIBSectionBufferPixels = nullptr;
DeleteObject(m_DIBSection);
m_DIBSection = nullptr;
m_DIBSectionPixels = nullptr;
}
}
@ -66,23 +58,17 @@ void CanvasGDIP::Resize(int w, int h)
bh.bV4BlueMask = 0x000000FF;
bh.bV4AlphaMask = 0xFF000000;
m_DIBSectionBuffer = CreateDIBSection(
m_DIBSection = CreateDIBSection(
nullptr,
(BITMAPINFO*)&bh,
DIB_RGB_COLORS,
(void**)&m_DIBSectionBufferPixels,
(void**)&m_DIBSectionPixels,
nullptr,
0);
// Create GDI+ bitmap from the DIBSection pixels
m_Bitmap = new Gdiplus::Bitmap(
w,
h,
w * 4,
PixelFormat32bppPARGB,
(BYTE*)m_DIBSectionBufferPixels);
m_Graphics = new Gdiplus::Graphics(m_Bitmap);
m_Bitmap.reset(new Gdiplus::Bitmap(w, h, w * 4, PixelFormat32bppPARGB, (BYTE*)m_DIBSectionPixels));
m_Graphics.reset(new Gdiplus::Graphics(m_Bitmap.get()));
}
bool CanvasGDIP::BeginDraw()
@ -108,7 +94,7 @@ void CanvasGDIP::EndGdiplusContext()
HDC CanvasGDIP::GetDC()
{
HDC dcMemory = CreateCompatibleDC(nullptr);
SelectObject(dcMemory, m_DIBSectionBuffer);
SelectObject(dcMemory, m_DIBSection);
return dcMemory;
}
@ -119,9 +105,9 @@ void CanvasGDIP::ReleaseDC(HDC dc)
bool CanvasGDIP::IsTransparentPixel(int x, int y)
{
if (m_DIBSectionBufferPixels && x >= 0 && y >= 0 && x < m_W && y < m_H)
if (m_DIBSectionPixels && x >= 0 && y >= 0 && x < m_W && y < m_H)
{
DWORD pixel = m_DIBSectionBufferPixels[y * m_W + x]; // top-down DIB
DWORD pixel = m_DIBSectionPixels[y * m_W + x]; // top-down DIB
return ((pixel & 0xFF000000) != 0);
}
@ -163,7 +149,7 @@ void CanvasGDIP::Clear(const Gdiplus::Color& color)
{
if (color.GetValue() == 0x00000000)
{
memset(m_DIBSectionBufferPixels, 0, m_W * m_H * 4);
memset(m_DIBSectionPixels, 0, m_W * m_H * 4);
}
else
{
@ -174,13 +160,15 @@ void CanvasGDIP::Clear(const Gdiplus::Color& color)
void CanvasGDIP::DrawTextW(const WCHAR* str, UINT strLen, const TextFormat& format, Gdiplus::RectF& rect, const Gdiplus::SolidBrush& brush)
{
Gdiplus::StringFormat& stringFormat = ((TextFormatGDIP&)format).m_StringFormat;
m_Graphics->DrawString(str, (INT)strLen, ((TextFormatGDIP&)format).m_Font, rect, &stringFormat, &brush);
m_Graphics->DrawString(
str, (INT)strLen, ((TextFormatGDIP&)format).m_Font.get(), rect, &stringFormat, &brush);
}
bool CanvasGDIP::MeasureTextW(const WCHAR* str, UINT strLen, const TextFormat& format, Gdiplus::RectF& rect)
{
Gdiplus::StringFormat& stringFormat = ((TextFormatGDIP&)format).m_StringFormat;
const Gdiplus::Status status = m_Graphics->MeasureString(str, (INT)strLen, ((TextFormatGDIP&)format).m_Font, rect, &stringFormat, &rect);
const Gdiplus::Status status = m_Graphics->MeasureString(
str, (INT)strLen, ((TextFormatGDIP&)format).m_Font.get(), rect, &stringFormat, &rect);
return status == Gdiplus::Ok;
}
@ -196,7 +184,8 @@ bool CanvasGDIP::MeasureTextLinesW(const WCHAR* str, UINT strLen, const TextForm
stringFormat.SetFormatFlags(Gdiplus::StringFormatFlagsNoClip);
INT linesFilled = 0;
const Gdiplus::Status status = m_Graphics->MeasureString(str, (INT)strLen, ((TextFormatGDIP&)format).m_Font, rect, &stringFormat, &rect, nullptr, &linesFilled);
const Gdiplus::Status status = m_Graphics->MeasureString(
str, (INT)strLen, ((TextFormatGDIP&)format).m_Font.get(), rect, &stringFormat, &rect, nullptr, &linesFilled);
lines = linesFilled;
// Restore old options.

View File

@ -22,6 +22,7 @@
#include "Canvas.h"
#include "FontCollectionGDIP.h"
#include "TextFormatGDIP.h"
#include <memory>
#include <string>
#include <GdiPlus.h>
@ -72,10 +73,10 @@ private:
void Dispose();
Gdiplus::Graphics* m_Graphics;
Gdiplus::Bitmap* m_Bitmap;
HBITMAP m_DIBSectionBuffer;
LPDWORD m_DIBSectionBufferPixels;
std::unique_ptr<Gdiplus::Graphics> m_Graphics;
std::unique_ptr<Gdiplus::Bitmap> m_Bitmap;
HBITMAP m_DIBSection;
LPDWORD m_DIBSectionPixels;
//static ULONG_PTR c_GdiToken;
};

View File

@ -28,7 +28,6 @@ TextFormatD2D::TextFormatD2D()
TextFormatD2D::~TextFormatD2D()
{
Dispose();
}
void TextFormatD2D::Dispose()

View File

@ -21,24 +21,12 @@
namespace Gfx {
TextFormatGDIP::TextFormatGDIP() :
m_Font(),
m_FontFamily()
TextFormatGDIP::TextFormatGDIP()
{
}
TextFormatGDIP::~TextFormatGDIP()
{
Dispose();
}
void TextFormatGDIP::Dispose()
{
delete m_FontFamily;
m_FontFamily = nullptr;
delete m_Font;
m_Font = nullptr;
}
void TextFormatGDIP::SetProperties(
@ -47,22 +35,20 @@ void TextFormatGDIP::SetProperties(
{
auto fontCollectionGDIP = (FontCollectionGDIP*)fontCollection;
Dispose();
m_Font.reset();
m_FontFamily = new Gdiplus::FontFamily(fontFamily);
m_FontFamily.reset(new Gdiplus::FontFamily(fontFamily));
if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
{
delete m_FontFamily;
m_FontFamily = nullptr;
m_FontFamily.reset();
// Not found in system collection so try the private collection.
if (fontCollectionGDIP && fontCollectionGDIP->m_PrivateCollection)
{
m_FontFamily = new Gdiplus::FontFamily(fontFamily, fontCollectionGDIP->m_PrivateCollection);
m_FontFamily.reset(new Gdiplus::FontFamily(fontFamily, fontCollectionGDIP->m_PrivateCollection));
if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
{
delete m_FontFamily;
m_FontFamily = nullptr;
m_FontFamily.reset();
}
}
}
@ -91,22 +77,20 @@ void TextFormatGDIP::SetProperties(
if (m_FontFamily)
{
m_Font = new Gdiplus::Font(m_FontFamily, fontSize, style);
m_Font.reset(new Gdiplus::Font(m_FontFamily.get(), fontSize, style));
if (m_Font->GetLastStatus() != Gdiplus::Ok)
{
delete m_Font;
m_Font = nullptr;
m_Font.reset();
}
}
if (!m_Font)
{
// Use default font ("Arial" or GenericSansSerif).
m_Font = new Gdiplus::Font(L"Arial", fontSize, style);
m_Font.reset(new Gdiplus::Font(L"Arial", fontSize, style));
if (m_Font->GetLastStatus() != Gdiplus::Ok)
{
delete m_Font;
m_Font = nullptr;
m_Font.reset();
}
}
}

View File

@ -20,6 +20,7 @@
#define RM_GFX_TEXTFORMATGDIP_H_
#include "TextFormat.h"
#include <memory>
#include <GdiPlus.h>
namespace Gfx {
@ -47,10 +48,8 @@ private:
TextFormatGDIP(const TextFormatGDIP& other) {}
void Dispose();
Gdiplus::Font* m_Font;
Gdiplus::FontFamily* m_FontFamily;
std::unique_ptr<Gdiplus::Font> m_Font;
std::unique_ptr<Gdiplus::FontFamily> m_FontFamily;
Gdiplus::StringFormat m_StringFormat;
};