Skip to content

Commit

Permalink
* 改用普通指针重新实现
Browse files Browse the repository at this point in the history
  • Loading branch information
royqh1979@gmail.com authored and royqh1979@gmail.com committed Oct 18, 2020
1 parent e3c065c commit bb8e08b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 99 deletions.
63 changes: 9 additions & 54 deletions src/ege_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@

#ifdef EGE_GDIPLUS
# include <gdiplus.h>
# include <memory>
#endif

#define QUEUE_LEN 1024
Expand Down Expand Up @@ -245,19 +244,7 @@ struct EGEMSG {
};

#ifdef EGE_GDIPLUS
inline Gdiplus::DashStyle linestyle_to_dashstyle(int linestyle) {
switch(linestyle){
case SOLID_LINE:
return Gdiplus::DashStyleSolid;
case PS_DASH:
return Gdiplus::DashStyleDash;
case PS_DOT:
return Gdiplus::DashStyleDot;
case PS_DASHDOT:
return Gdiplus::DashStyleDashDot;
}
return Gdiplus::DashStyleSolid;
}
Gdiplus::DashStyle linestyle_to_dashstyle(int linestyle);
#endif

// 定义图像对象
Expand All @@ -274,9 +261,9 @@ class IMAGE
color_t m_fillcolor;
private:
#ifdef EGE_GDIPLUS
std::shared_ptr<Gdiplus::Graphics> m_graphics;
std::shared_ptr<Gdiplus::Pen> m_pen;
std::shared_ptr<Gdiplus::Brush> m_brush;
Gdiplus::Graphics* m_graphics;
Gdiplus::Pen* m_pen;
Gdiplus::Brush* m_brush;
#endif
bool m_aa;
void initimage(HDC refDC, int width, int height);
Expand Down Expand Up @@ -307,44 +294,12 @@ class IMAGE
color_t* getbuffer() const {return (color_t*)m_pBuffer;}
#ifdef EGE_GDIPLUS
//TODO: thread safe?
inline const std::shared_ptr<Gdiplus::Graphics>& getGraphics() {
if (m_graphics.get() == nullptr) {
m_graphics=std::make_shared<Gdiplus::Graphics>(m_hDC);
m_graphics->SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
m_graphics->SetSmoothingMode(m_aa? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone);
}
return m_graphics;
}
inline const std::shared_ptr<Gdiplus::Pen>& getPen() {
if (m_pen.get() == nullptr) {
m_pen = std::make_shared<Gdiplus::Pen>(m_color,m_linewidth);
m_pen->SetDashStyle(linestyle_to_dashstyle(m_linestyle.linestyle));
}
return m_pen;
}
inline const std::shared_ptr<Gdiplus::Brush>& getBrush() {
if (m_brush.get() == nullptr) {
m_brush = std::make_shared<Gdiplus::SolidBrush>(m_fillcolor);
}
return m_brush;
}
inline void set_pattern(Gdiplus::Brush* brush) {
if (NULL == brush) {
m_brush.reset();
} else {
m_brush.reset(brush);
}
}
Gdiplus::Graphics* getGraphics();
Gdiplus::Pen* getPen();
Gdiplus::Brush* getBrush();
void set_pattern(Gdiplus::Brush* brush);
#endif

inline void enable_anti_alias(bool enable){
m_aa = enable;
#ifdef EGE_GDIPLUS
if (m_graphics.get() != nullptr) {
m_graphics->SetSmoothingMode(m_aa? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone);
}
#endif
}
void enable_anti_alias(bool enable);

int resize(int width, int height);
void copyimage(PCIMAGE pSrcImg);
Expand Down
91 changes: 52 additions & 39 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ static void update_pen(PIMAGE img) {

// why update pen not in IMAGE???
#ifdef EGE_GDIPLUS
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
Gdiplus::Pen* pen=img->getPen();
pen->SetColor(img->m_color);
pen->SetWidth(img->m_linewidth);
pen->SetDashStyle(linestyle_to_dashstyle(img->m_linestyle.linestyle));
Expand Down Expand Up @@ -2020,16 +2020,29 @@ clearviewport(PIMAGE pimg) {
}

#ifdef EGE_GDIPLUS
Gdiplus::DashStyle linestyle_to_dashstyle(int linestyle){
switch(linestyle){
case SOLID_LINE:
return Gdiplus::DashStyleSolid;
case PS_DASH:
return Gdiplus::DashStyleDash;
case PS_DOT:
return Gdiplus::DashStyleDot;
case PS_DASHDOT:
return Gdiplus::DashStyleDashDot;
}
return Gdiplus::DashStyleSolid;
}

void
ege_line(float x1, float y1, float x2, float y2, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawLine(pen.get(), x1, y1, x2, y2);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawLine(pen, x1, y1, x2, y2);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2040,9 +2053,9 @@ ege_drawpoly(int numpoints, ege_point* polypoints, PIMAGE pimg) {
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawLines(pen.get(), (Gdiplus::PointF*)polypoints, numpoints);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawLines(pen, (Gdiplus::PointF*)polypoints, numpoints);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2053,9 +2066,9 @@ ege_drawcurve(int numpoints, ege_point* polypoints, PIMAGE pimg) {
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawCurve(pen.get(), (Gdiplus::PointF*)polypoints, numpoints);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawCurve(pen, (Gdiplus::PointF*)polypoints, numpoints);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2066,9 +2079,9 @@ ege_rectangle(float x, float y, float w, float h, PIMAGE pimg) {
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawRectangle(pen.get(), x, y, w, h);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawRectangle(pen, x, y, w, h);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2079,9 +2092,9 @@ ege_ellipse(float x, float y, float w, float h, PIMAGE pimg) {
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawEllipse(pen.get(), x, y, w, h);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawEllipse(pen, x, y, w, h);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2092,9 +2105,9 @@ ege_pie(float x, float y, float w, float h, float stangle, float sweepAngle, PIM
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawPie(pen.get(), x, y, w, h, stangle, sweepAngle);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawPie(pen, x, y, w, h, stangle, sweepAngle);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2105,9 +2118,9 @@ ege_arc(float x, float y, float w, float h, float stangle, float sweepAngle, PIM
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawArc(pen.get(), x, y, w, h, stangle, sweepAngle);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawArc(pen, x, y, w, h, stangle, sweepAngle);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2118,9 +2131,9 @@ ege_bezier(int numpoints, ege_point* polypoints, PIMAGE pimg) {
if (img) {
if (img->m_linestyle.linestyle == PS_NULL)
return;
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Pen>& pen=img->getPen();
graphics->DrawBeziers(pen.get(), (Gdiplus::PointF*)polypoints, numpoints);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Pen* pen=img->getPen();
graphics->DrawBeziers(pen, (Gdiplus::PointF*)polypoints, numpoints);
}
CONVERT_IMAGE_END;
}
Expand Down Expand Up @@ -2205,9 +2218,9 @@ void
ege_fillpoly(int numpoints, ege_point* polypoints, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
if (img) {
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillPolygon(brush.get(), (Gdiplus::PointF*)polypoints, numpoints);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Brush* brush=img->getBrush();
graphics->FillPolygon(brush, (Gdiplus::PointF*)polypoints, numpoints);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2216,9 +2229,9 @@ void
ege_fillrect(float x, float y, float w, float h, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
if (img) {
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillRectangle(brush.get(), x, y, w, h);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Brush* brush=img->getBrush();
graphics->FillRectangle(brush, x, y, w, h);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2227,9 +2240,9 @@ void
ege_fillellipse(float x, float y, float w, float h, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
if (img) {
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillEllipse(brush.get(), x, y, w, h);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Brush* brush=img->getBrush();
graphics->FillEllipse(brush, x, y, w, h);
}
CONVERT_IMAGE_END;
}
Expand All @@ -2238,9 +2251,9 @@ void
ege_fillpie(float x, float y, float w, float h, float stangle, float sweepAngle, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
if (img) {
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
const std::shared_ptr<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillPie(brush.get(), x, y, w, h, stangle, sweepAngle);
Gdiplus::Graphics* graphics=img->getGraphics();
Gdiplus::Brush* brush=img->getBrush();
graphics->FillPie(brush, x, y, w, h, stangle, sweepAngle);
}
CONVERT_IMAGE_END;
}
Expand Down Expand Up @@ -2294,7 +2307,7 @@ ege_puttexture(PCIMAGE srcimg, ege_rect dest, ege_rect src, PIMAGE pimg) {
PIMAGE img = CONVERT_IMAGE(pimg);
if (img) {
if (srcimg->m_texture) {
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
Gdiplus::Graphics* graphics=img->getGraphics();
/*
Gdiplus::ImageAttributes ia;
Gdiplus::ColorMatrix mx = {
Expand Down Expand Up @@ -2326,7 +2339,7 @@ ege_puttexture(PCIMAGE srcimg, ege_rect dest, ege_rect src, PIMAGE pimg) {

// TODO: 错误处理
static void ege_drawtext_p(LPCWSTR textstring, float x, float y, PIMAGE img) {
const std::shared_ptr<Gdiplus::Graphics>& graphics=img->getGraphics();
Gdiplus::Graphics* graphics=img->getGraphics();

HFONT hf = (HFONT)GetCurrentObject(img->m_hDC, OBJ_FONT);
LOGFONT lf;
Expand Down
59 changes: 53 additions & 6 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ void IMAGE::reset() {
m_bk_color = 0;
m_texture = NULL;
#ifdef EGE_GDIPLUS
m_graphics.reset();
m_pen.reset();
m_brush.reset();
m_graphics=NULL;
m_pen=NULL;
m_brush=NULL;
#endif
}

Expand Down Expand Up @@ -118,9 +118,15 @@ IMAGE::gentexture(bool gen) {
int
IMAGE::deleteimage() {
#ifdef EGE_GDIPLUS
m_graphics.reset();
m_pen.reset();
m_brush.reset();
if (NULL!=m_graphics)
delete m_graphics;
m_graphics=NULL;
if (NULL!=m_pen)
delete m_pen;
m_pen=NULL;
if (NULL!=m_brush)
delete m_brush;
m_brush=NULL;
#endif

HBITMAP hbmp = (HBITMAP)GetCurrentObject(m_hDC, OBJ_BITMAP);
Expand Down Expand Up @@ -203,6 +209,47 @@ void IMAGE::setdefaultattribute() {
enable_anti_alias(false);
}

#ifdef EGE_GDIPLUS

Gdiplus::Graphics* IMAGE::getGraphics() {
if (NULL == m_graphics) {
m_graphics=new Gdiplus::Graphics(m_hDC);
m_graphics->SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
m_graphics->SetSmoothingMode(m_aa? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone);
}
return m_graphics;
}
Gdiplus::Pen* IMAGE::getPen() {
if (NULL == m_pen) {
m_pen = new Gdiplus::Pen(m_color,m_linewidth);
m_pen->SetDashStyle(linestyle_to_dashstyle(m_linestyle.linestyle));
}
return m_pen;
}
Gdiplus::Brush* IMAGE::getBrush() {
if (NULL == m_brush) {
m_brush = new Gdiplus::SolidBrush(m_fillcolor);
}
return m_brush;
}

void IMAGE::set_pattern(Gdiplus::Brush* brush) {
if (NULL != m_brush) {
delete m_brush;
}
m_brush = brush;
}
#endif

void IMAGE::enable_anti_alias(bool enable){
m_aa = enable;
#ifdef EGE_GDIPLUS
if (NULL != m_graphics) {
m_graphics->SetSmoothingMode(m_aa? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone);
}
#endif
}

int
IMAGE::resize(int width, int height) {
inittest(L"IMAGE::resize");
Expand Down

0 comments on commit bb8e08b

Please sign in to comment.