Skip to content

Commit

Permalink
* brush也同时缓存
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 68c30cc commit e3c065c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 130 deletions.
52 changes: 30 additions & 22 deletions demo/egealpha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
61 changes: 27 additions & 34 deletions src/ege.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 */
);

// 其它函数
Expand Down
37 changes: 24 additions & 13 deletions src/ege_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class IMAGE
#ifdef EGE_GDIPLUS
std::shared_ptr<Gdiplus::Graphics> m_graphics;
std::shared_ptr<Gdiplus::Pen> m_pen;
std::shared_ptr<Gdiplus::Brush> m_brush;
#endif
bool m_aa;
void initimage(HDC refDC, int width, int height);
Expand All @@ -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;
Expand All @@ -300,40 +299,52 @@ 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;}
int getwidth() const {return m_width; }
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<Gdiplus::Graphics>& getGraphics() {
if (nullptr == m_graphics.get()) {
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 (nullptr == m_pen.get()) {
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;
}
#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<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);
}
}
#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);
Expand Down
50 changes: 19 additions & 31 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -2202,12 +2206,8 @@ 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();
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<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillPolygon(brush.get(), (Gdiplus::PointF*)polypoints, numpoints);
}
CONVERT_IMAGE_END;
}
Expand All @@ -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<Gdiplus::Graphics>& 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<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillRectangle(brush.get(), x, y, w, h);
}
CONVERT_IMAGE_END;
}
Expand All @@ -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<Gdiplus::Graphics>& 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<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillEllipse(brush.get(), x, y, w, h);
}
CONVERT_IMAGE_END;
}
Expand All @@ -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<Gdiplus::Graphics>& 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<Gdiplus::Brush>& brush=img->getBrush();
graphics->FillPie(brush.get(), x, y, w, h, stangle, sweepAngle);
}
CONVERT_IMAGE_END;
}
Expand Down
Loading

0 comments on commit e3c065c

Please sign in to comment.