1
0
mirror of https://github.com/chibicitiberiu/rainmeter-studio.git synced 2024-02-24 04:33:31 +00:00

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

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

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

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

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

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

@ -21,24 +21,12 @@
namespace Gfx { namespace Gfx {
TextFormatGDIP::TextFormatGDIP() : 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( void TextFormatGDIP::SetProperties(
@ -47,22 +35,20 @@ void TextFormatGDIP::SetProperties(
{ {
auto fontCollectionGDIP = (FontCollectionGDIP*)fontCollection; 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) if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
{ {
delete m_FontFamily; m_FontFamily.reset();
m_FontFamily = nullptr;
// Not found in system collection so try the private collection. // Not found in system collection so try the private collection.
if (fontCollectionGDIP && fontCollectionGDIP->m_PrivateCollection) 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) if (m_FontFamily->GetLastStatus() != Gdiplus::Ok)
{ {
delete m_FontFamily; m_FontFamily.reset();
m_FontFamily = nullptr;
} }
} }
} }
@ -91,22 +77,20 @@ void TextFormatGDIP::SetProperties(
if (m_FontFamily) 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) if (m_Font->GetLastStatus() != Gdiplus::Ok)
{ {
delete m_Font; m_Font.reset();
m_Font = nullptr;
} }
} }
if (!m_Font) if (!m_Font)
{ {
// Use default font ("Arial" or GenericSansSerif). // 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) if (m_Font->GetLastStatus() != Gdiplus::Ok)
{ {
delete m_Font; m_Font.reset();
m_Font = nullptr;
} }
} }
} }

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