diff --git a/demo/egealpha.cpp b/demo/egealpha.cpp index c68aec69..6de49202 100644 --- a/demo/egealpha.cpp +++ b/demo/egealpha.cpp @@ -3,32 +3,40 @@ int main() { - initgraph( 640, 480 ); + initgraph( 640, 480 ); setbkcolor(WHITE); setcolor(BLUE); setfillcolor(BLUE); bar(100,100,600,400); - + PIMAGE img=newimage(640,480); //set backgrounds alpha to 0 - setbkcolor(EGERGBA(0,0,0,0),img); - - //draw shapes with alpha to 255 - setfillcolor(EGEACOLOR(255,RED),img); - setcolor(EGEACOLOR(255,RED),img); - setlinestyle(CENTER_LINE,0,1,img); - setlinewidth(10,img); - ege_line(100,100,400,400,img); - setcolor(EGEACOLOR(255,GREEN),img); - setlinestyle(DOTTED_LINE,0,1,img); - setlinewidth(5,img); - ege_ellipse(200,100,200,200,img); - putimage_withalpha(NULL,img,0,0); - - //putimage(0,0,img); - delimage(img); - - getch(); - return 0; -} + setbkcolor(EGERGBA(0,0,0,0),img); + + //draw shapes with alpha to 255 + setfillcolor(EGEACOLOR(255,RED),img); + setcolor(EGEACOLOR(255,RED),img); + setlinestyle(CENTER_LINE,0,1,img); + setlinewidth(10,img); + ege_line(100,100,400,400,img); + //change color + setcolor(EGEACOLOR(255,GREEN),img); + setlinestyle(DOTTED_LINE,0,1,img); + setlinewidth(5,img); + ege_ellipse(200,100,200,200,img); + //set fill color + setfillcolor(EGEACOLOR(255,LIGHTMAGENTA),img); + ege_fillellipse(10,10,50,50,img); + + setfillcolor(EGEACOLOR(255,LIGHTBLUE),img); + ege_fillellipse(100,10,50,50,img); + + putimage_withalpha(NULL,img,0,0); + + //putimage(0,0,img); + delimage(img); + + getch(); + return 0; +} \ No newline at end of file diff --git a/src/ege.h b/src/ege.h index 799fba71..4a829114 100644 --- a/src/ege.h +++ b/src/ege.h @@ -462,13 +462,6 @@ typedef enum mouse_flag_e { mouse_flag_ctrl = 0x200, }mouse_flag_e; -typedef enum pattern_type_e { - pattern_none = 0, - pattern_lineargradient = 1, - pattern_pathgradient = 2, - pattern_texture = 3, -}pattern_type_e; - typedef DWORD color_t; struct viewporttype { @@ -1006,33 +999,33 @@ int EGEAPI putimage_rotatezoom( int alpha = -1, // in range[0, 256], alpha== -1 means no alpha int smooth = 0 ); - -int EGEAPI putimage_rotatetransparent( - PIMAGE imgdest,/* handle to dest, NULL means the SCREEN */ - PCIMAGE imgsrc,/* handle to source */ - int xCenterDest,/* x-coord of rotation center in dest */ - int yCenterDest,/* y-coord of rotation center in dest */ - int xCenterSrc, /* x-coord of rotation center in source */ - int yCenterSrc,/* y-coord of rotation center in source */ - color_t crTransparent, /* color to make transparent */ - float radian, /* rotation angle (clockwise, in radian) */ - float zoom=1.0 /* zoom factor */ -); - -int EGEAPI putimage_rotatetransparent( - PIMAGE imgdest,/* handle to dest, NULL means the SCREEN */ - PCIMAGE imgsrc,/* handle to source */ - int xCenterDest,/* x-coord of rotation center in dest */ - int yCenterDest,/* y-coord of rotation center in dest */ - int xOriginSrc, /* x-coord of source upper-left corner */ - int yOriginSrc, /* y-coord of source upper-left corner */ - int widthSrc, /* width of source rectangle */ - int heightSrc, /* height of source rectangle */ - int xCenterSrc, /* x-coord of rotation center in source */ - int yCenterSrc,/* y-coord of rotation center in source */ - color_t crTransparent, /* color to make transparent */ - float radian, /* rotation angle (clockwise, in radian) */ - float zoom=1.0 /* zoom factor */ + +int EGEAPI putimage_rotatetransparent( + PIMAGE imgdest,/* handle to dest, NULL means the SCREEN */ + PCIMAGE imgsrc,/* handle to source */ + int xCenterDest,/* x-coord of rotation center in dest */ + int yCenterDest,/* y-coord of rotation center in dest */ + int xCenterSrc, /* x-coord of rotation center in source */ + int yCenterSrc,/* y-coord of rotation center in source */ + color_t crTransparent, /* color to make transparent */ + float radian, /* rotation angle (clockwise, in radian) */ + float zoom=1.0 /* zoom factor */ +); + +int EGEAPI putimage_rotatetransparent( + PIMAGE imgdest,/* handle to dest, NULL means the SCREEN */ + PCIMAGE imgsrc,/* handle to source */ + int xCenterDest,/* x-coord of rotation center in dest */ + int yCenterDest,/* y-coord of rotation center in dest */ + int xOriginSrc, /* x-coord of source upper-left corner */ + int yOriginSrc, /* y-coord of source upper-left corner */ + int widthSrc, /* width of source rectangle */ + int heightSrc, /* height of source rectangle */ + int xCenterSrc, /* x-coord of rotation center in source */ + int yCenterSrc,/* y-coord of rotation center in source */ + color_t crTransparent, /* color to make transparent */ + float radian, /* rotation angle (clockwise, in radian) */ + float zoom=1.0 /* zoom factor */ ); // 其它函数 diff --git a/src/ege_head.h b/src/ege_head.h index e63a47d8..4cb30ec6 100644 --- a/src/ege_head.h +++ b/src/ege_head.h @@ -276,6 +276,7 @@ class IMAGE #ifdef EGE_GDIPLUS std::shared_ptr m_graphics; std::shared_ptr m_pen; + std::shared_ptr m_brush; #endif bool m_aa; void initimage(HDC refDC, int width, int height); @@ -289,8 +290,6 @@ class IMAGE linestyletype m_linestyle; float m_linewidth; color_t m_bk_color; - void* m_pattern_obj; - int m_pattern_type; void* m_texture; private: void inittest(const WCHAR* strCallFunction = NULL) const; @@ -300,8 +299,6 @@ class IMAGE IMAGE(const IMAGE &img); // 拷贝构造函数 IMAGE& operator = (const IMAGE &img); // 赋值运算符重载函数 ~IMAGE(); - void set_pattern(void* obj, int type); - void delete_pattern(); void gentexture(bool gen); public: HDC getdc() const {return m_hDC;} @@ -309,9 +306,9 @@ class IMAGE int getheight() const {return m_height;} color_t* getbuffer() const {return (color_t*)m_pBuffer;} #ifdef EGE_GDIPLUS - //TODO: thread safe + //TODO: thread safe? inline const std::shared_ptr& getGraphics() { - if (nullptr == m_graphics.get()) { + 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); @@ -319,21 +316,35 @@ class IMAGE return m_graphics; } inline const std::shared_ptr& getPen() { - if (nullptr == m_pen.get()) { + 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; } -#endif - inline void enable_anti_alias(bool enable){ - m_aa = enable; -#ifdef EGE_GDIPLUS - if (nullptr != m_graphics.get()) { - m_graphics->SetSmoothingMode(m_aa? Gdiplus::SmoothingModeAntiAlias : Gdiplus::SmoothingModeNone); + 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); } + } #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 +} int resize(int width, int height); void copyimage(PCIMAGE pSrcImg); diff --git a/src/egegapi.cpp b/src/egegapi.cpp index 4f1707eb..7cddb4c1 100644 --- a/src/egegapi.cpp +++ b/src/egegapi.cpp @@ -808,6 +808,9 @@ setfillcolor(color_t color, PIMAGE pimg) { if (hbr) { DeleteObject(SelectObject(img->m_hDC, hbr)); } +#ifdef EGE_GDIPLUS + img->set_pattern(NULL); +#endif CONVERT_IMAGE_END; } @@ -1563,6 +1566,9 @@ setfillstyle(int pattern, color_t color, PIMAGE pimg) { if (hbr) { DeleteObject(SelectObject(img->m_hDC, hbr)); } + #ifdef EGE_GDIPLUS + img->set_pattern(NULL); + #endif CONVERT_IMAGE_END; } @@ -2122,9 +2128,7 @@ ege_bezier(int numpoints, ege_point* polypoints, PIMAGE pimg) { void ege_setpattern_none(PIMAGE pimg) { PIMAGE img = CONVERT_IMAGE(pimg); - if (img) { - img->delete_pattern(); - } + img->set_pattern(NULL); CONVERT_IMAGE_END; } @@ -2138,7 +2142,7 @@ ege_setpattern_lineargradient(float x1, float y1, color_t c1, float x2, float y2 Gdiplus::Color(c1), Gdiplus::Color(c2) ); - img->set_pattern(pbrush, pattern_lineargradient); + img->set_pattern(pbrush); } CONVERT_IMAGE_END; } @@ -2157,7 +2161,7 @@ ege_setpattern_pathgradient(ege_point center, color_t centercolor, pbrush->SetCenterColor(Gdiplus::Color(centercolor)); pbrush->SetCenterPoint(Gdiplus::PointF(center.x, center.y)); pbrush->SetSurroundColors((Gdiplus::Color*)pointscolor, &colcount); - img->set_pattern(pbrush, pattern_pathgradient); + img->set_pattern(pbrush); } CONVERT_IMAGE_END; } @@ -2177,7 +2181,7 @@ ege_setpattern_ellipsegradient(ege_point center, color_t centercolor, pbrush->SetCenterColor(Gdiplus::Color(centercolor)); pbrush->SetCenterPoint(Gdiplus::PointF(center.x, center.y)); pbrush->SetSurroundColors((Gdiplus::Color*)&color, &count); - img->set_pattern(pbrush, pattern_pathgradient); + img->set_pattern(pbrush); } CONVERT_IMAGE_END; } @@ -2191,7 +2195,7 @@ ege_setpattern_texture(PIMAGE srcimg, float x, float y, float w, float h, PIMAGE (Gdiplus::Image*)srcimg->m_texture, Gdiplus::WrapModeTile, x, y, w, h); - img->set_pattern(pbrush, pattern_texture); + img->set_pattern(pbrush); } } CONVERT_IMAGE_END; @@ -2202,12 +2206,8 @@ ege_fillpoly(int numpoints, ege_point* polypoints, PIMAGE pimg) { PIMAGE img = CONVERT_IMAGE(pimg); if (img) { const std::shared_ptr& graphics=img->getGraphics(); - if (img->m_pattern_obj) { - graphics->FillPolygon((Gdiplus::Brush*)img->m_pattern_obj, (Gdiplus::PointF*)polypoints, numpoints); - } else { - Gdiplus::SolidBrush brush(img->m_fillcolor); - graphics->FillPolygon(&brush, (Gdiplus::PointF*)polypoints, numpoints); - } + const std::shared_ptr& brush=img->getBrush(); + graphics->FillPolygon(brush.get(), (Gdiplus::PointF*)polypoints, numpoints); } CONVERT_IMAGE_END; } @@ -2217,12 +2217,8 @@ 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(); - if (img->m_pattern_obj) { - graphics->FillRectangle((Gdiplus::Brush*)img->m_pattern_obj, x, y, w, h); - } else { - Gdiplus::SolidBrush brush(img->m_fillcolor); - graphics->FillRectangle(&brush, x, y, w, h); - } + const std::shared_ptr& brush=img->getBrush(); + graphics->FillRectangle(brush.get(), x, y, w, h); } CONVERT_IMAGE_END; } @@ -2232,12 +2228,8 @@ 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(); - if (img->m_pattern_obj) { - graphics->FillEllipse((Gdiplus::Brush*)img->m_pattern_obj, x, y, w, h); - } else { - Gdiplus::SolidBrush brush(img->m_fillcolor); - graphics->FillEllipse(&brush, x, y, w, h); - } + const std::shared_ptr& brush=img->getBrush(); + graphics->FillEllipse(brush.get(), x, y, w, h); } CONVERT_IMAGE_END; } @@ -2247,12 +2239,8 @@ ege_fillpie(float x, float y, float w, float h, float stangle, float sweepAngle, PIMAGE img = CONVERT_IMAGE(pimg); if (img) { const std::shared_ptr& graphics=img->getGraphics(); - if (img->m_pattern_obj) { - graphics->FillPie((Gdiplus::Brush*)img->m_pattern_obj, x, y, w, h, stangle, sweepAngle); - } else { - Gdiplus::SolidBrush brush(img->m_fillcolor); - graphics->FillPie(&brush, x, y, w, h, stangle, sweepAngle); - } + const std::shared_ptr& brush=img->getBrush(); + graphics->FillPie(brush.get(), x, y, w, h, stangle, sweepAngle); } CONVERT_IMAGE_END; } diff --git a/src/image.cpp b/src/image.cpp index 5ac02f50..634f2032 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -43,12 +43,11 @@ void IMAGE::reset() { memset(&m_linestyle, 0, sizeof(m_linestyle)); m_linewidth = 0.0f; m_bk_color = 0; - m_pattern_obj = NULL; - m_pattern_type = 0; m_texture = NULL; #ifdef EGE_GDIPLUS - m_graphics = nullptr; - m_pen = nullptr; + m_graphics.reset(); + m_pen.reset(); + m_brush.reset(); #endif } @@ -88,7 +87,6 @@ IMAGE::IMAGE(const IMAGE &img) { IMAGE::~IMAGE() { gentexture(false); - delete_pattern(); deleteimage(); } @@ -101,28 +99,6 @@ void IMAGE::inittest(const WCHAR* strCallFunction) const { } } -void -IMAGE::set_pattern(void* obj, int type) { - delete_pattern(); - m_pattern_type = type; - m_pattern_obj = obj; -} - -void -IMAGE::delete_pattern() { - if (m_pattern_obj == NULL) return; - - if (m_pattern_type == pattern_none) { - } else if (m_pattern_type == pattern_lineargradient) { - delete (Gdiplus::LinearGradientBrush*)m_pattern_obj; - } else if (m_pattern_type == pattern_pathgradient) { - delete (Gdiplus::PathGradientBrush*)m_pattern_obj; - } else if (m_pattern_type == pattern_texture) { - delete (Gdiplus::TextureBrush*)m_pattern_obj; - } - m_pattern_obj = NULL; -} - void IMAGE::gentexture(bool gen) { if (!gen) { @@ -142,8 +118,9 @@ IMAGE::gentexture(bool gen) { int IMAGE::deleteimage() { #ifdef EGE_GDIPLUS - m_graphics = nullptr; - m_pen = nullptr; + m_graphics.reset(); + m_pen.reset(); + m_brush.reset(); #endif HBITMAP hbmp = (HBITMAP)GetCurrentObject(m_hDC, OBJ_BITMAP); @@ -223,7 +200,7 @@ void IMAGE::setdefaultattribute() { setlinestyle(PS_SOLID, 0, 1, this); settextjustify(LEFT_TEXT, TOP_TEXT, this); setfont(16, 0, "SimSun", this); - ege_enable_aa(false, this); + enable_anti_alias(false); } int