diff --git a/src/ege_head.h b/src/ege_head.h index 4cb30ec6..33c865e3 100644 --- a/src/ege_head.h +++ b/src/ege_head.h @@ -115,7 +115,6 @@ #ifdef EGE_GDIPLUS # include -# include #endif #define QUEUE_LEN 1024 @@ -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 // 定义图像对象 @@ -274,9 +261,9 @@ class IMAGE color_t m_fillcolor; private: #ifdef EGE_GDIPLUS - std::shared_ptr m_graphics; - std::shared_ptr m_pen; - std::shared_ptr 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); @@ -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& getGraphics() { - if (m_graphics.get() == nullptr) { - m_graphics=std::make_shared(m_hDC); - m_graphics->SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf); - m_graphics->SetSmoothingMode(m_aa? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone); - } - return m_graphics; - } - inline const std::shared_ptr& getPen() { - if (m_pen.get() == nullptr) { - m_pen = std::make_shared(m_color,m_linewidth); - m_pen->SetDashStyle(linestyle_to_dashstyle(m_linestyle.linestyle)); - } - return m_pen; - } - inline const std::shared_ptr& getBrush() { - if (m_brush.get() == nullptr) { - m_brush = std::make_shared(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); diff --git a/src/egegapi.cpp b/src/egegapi.cpp index 7cddb4c1..91ed010f 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -776,7 +776,7 @@ static void update_pen(PIMAGE img) { // why update pen not in IMAGE??? #ifdef EGE_GDIPLUS - const std::shared_ptr& 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)); @@ -2020,6 +2020,19 @@ 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) { @@ -2027,9 +2040,9 @@ ege_line(float x1, float y1, float x2, float y2, PIMAGE pimg) { if (img) { if (img->m_linestyle.linestyle == PS_NULL) return; - const std::shared_ptr& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); - const std::shared_ptr& 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; } @@ -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& graphics=img->getGraphics(); + Gdiplus::Graphics* graphics=img->getGraphics(); /* Gdiplus::ImageAttributes ia; Gdiplus::ColorMatrix mx = { @@ -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& graphics=img->getGraphics(); + Gdiplus::Graphics* graphics=img->getGraphics(); HFONT hf = (HFONT)GetCurrentObject(img->m_hDC, OBJ_FONT); LOGFONT lf; diff --git a/src/image.cpp b/src/image.cpp index 634f2032..5c5387c9 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -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 } @@ -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); @@ -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");