Download: EasyX official website
PS: Dev C ++ may not be detected, we recommend using Visual Studio,
VS official website to download: https://visualstudio.microsoft.com/zh-hans/free-developer-offers/
table of Contents
Location analysis of FlushBatchDraw
1. Eju Article parallel straight Line
2. The color of the window in a line drawing of the gradation
3. Draw lines alternately with red and blue
First, the basic
1. Drawing Environment
(1)initgraph
Initialize drawing environment
prototype:
HWND initgraph(
int width, //绘图环境的宽度
int height, //绘图环境的高度
int flag = NULL
);
parameter:
flag
Style drawing environment, the default is NULL. SHOWCONSOLE retain the original console window.
(2)closegraph
void closegraph();
//关闭绘图环境
2. Color
(1) Constant Color
RGB mode
setcolor(RGB(0,0,0));
Alphabetic
setcolor(BLACK);
Six hexadecimal
setcolor(0x000000);
Each two-digit hexadecimal respectively, blue, green, red.
Black 0x000000
0xFF0000 (dark) blue
0xFFAAAA (light) blue
0xFFFFFF white
(2)setcolor
Set the foreground color (font color and line color), is equivalent to the continuous execution easyx.h settextcolor setlinecolor and functions.
void setcolor(COLORREF color);
(3)setlinecolor
This function is used to set the current color of the line drawing.
void setlinecolor(COLORREF color);
(4)settextcolor
This function is used to set the current text color.
void settextcolor(COLORREF color);
(5)setbkcolor
Set current drawing background color.
void setbkcolor(COLORREF color);
Performing cleardevice () or clearcliprgn () function is cleared and the like, using the color screen or empty crop area.
(6) setfillcolor
void setfillcolor(COLORREF color);
Sets the current fill color.
3. Style
(1)setbkmode
Background setting mode and text output hatch
void setbkmode(int mode);
Parameters: mode
-OPAQUE opaque background filled with the current background color (default).
-TRANSPARENT background is transparent.
(2)setlinestyle
Set the current line drawing style.
Prototype 1:
void setlinestyle(
const LINESTYLE* pstyle
);
pstyle
A pointer to draw the line style LINESTYLE.
Prototype 2:
void setlinestyle(
int style,
int thickness = 1,
const DWORD *puserstyle = NULL,
DWORD userstylecount = 0
);
style
Drawing a line pattern, a linear pattern, end pattern, composed of three types of connection patterns. Wherein the combination may be a class or classes. The same type can only specify one style.
Line styles can be the following values: Value Meaning
PS_SOLID linear solid line.
PS_DASH line shape: ------------
PS_DOT line shape: ············
PS_DASHDOT linear as: - * - * - * - * - * - *
PS_DASHDOTDOT linear as: - · - · - · - ·
PS_NULL linear invisible.
PS_USERSTYLE user-defined line pattern, specified by the parameter puserstyle and userstylecount.
Macro PS_STYLE_MASK rectilinear mask pattern, line styles can be isolated by drawing lines from the macro style.
Cap style can be the following values: Value Meaning
PS_ENDCAP_ROUND endpoint is circular.
PS_ENDCAP_SQUARE endpoint square.
PS_ENDCAP_FLAT flat endpoint.
Macro PS_ENDCAP_MASK endpoint style mask, the endpoint can be isolated from the macro pattern by drawing a line style.
Styles can be connected to the following values: Value Meaning
PS_JOIN_BEVEL beveled connections.
PS_JOIN_MITER as miter joints.
PS_JOIN_ROUND connection circular arc.
PS_JOIN_MASK macro is a mask-style connector, the connection patterns can be separated from the macro pattern by drawing lines.
thickness
The width of the line, in pixels.
puserstyle
User-defined style array, only when the line is PS_USERSTYLE This parameter is valid.
The first element of the array specifies the length of a line drawing, the second element specifies the length of the blank, the third element objects specified length, and the fourth element specifies the length of the blank, and so on.
userstylecount
User-defined number of elements in an array of styles.
return value:
(no)
Description:
Macro mask indicates that all bits correspond to the style group occupied. For example, for a variety of styles had been mixed style variable, if desired to modify only the straight style dot chain line, it can be done:
style = (style & ~ PS_STYLE_MASK) | PS_DASHDOT; Example:
The following partial code sets of dot chain line style Videos:
setlinestyle (PS_DASHDOT); the following partial code sets a width of a broken line pattern objects 3 pixels, a flat endpoint:
setlinestyle (PS_DASH | PS_ENDCAP_FLAT, 3); the following partial objects style code sets a width of 10 pixels by the solid line, the connection is an inclined surface:
setlinestyle (PS_SOLID | PS_JOIN_BEVEL, 10); The following code sets the local style objects custom styles (Videos five pixels, skipping two pixels, three pixels Videos, ...... skipping one pixel), a flat end :
DWORD a[4] = {5, 2, 3, 1};
setlinestyle(PS_USERSTYLE | PS_ENDCAP_FLAT, 1, a, 4);
4. Graphics
(1)circle
Draw a circle, precisely, it is a circular border.
void circle(
int x, //圆的圆心 x 坐标。
int y, //圆的圆心 y 坐标。
int radius //圆的半径。
);
(2)fillcircle
Painting filled circles (with border). The current linear and the current fill style
void fillcircle(
int x,
int y,
int radius
);
(3)solidcircle
This function is used to draw filled circles (borderless). Draw the current fill style
void solidcircle(
int x,
int y,
int radius
);
(4)line
This function is used to draw a straight line.
void line(
int x1, //直线的起始点的 x 坐标。
int y1, //直线的起始点的 y 坐标。
int x2, //直线的终止点的 x 坐标。
int y2 //直线的终止点的 y 坐标。
);
(5)putpixel
This function is used to draw points.
void putpixel(
int x, //点的 x 坐标。
int y, //点的 y 坐标。
COLORREF color //点的颜色。
);
5. scraper
(1) BeginBatchDraw
This function is used to start batch plotting. After performing any drawing operation will temporarily output to the screen, until only perform FlushBatchDraw or EndBatchDraw output before the drawing.
Used in the outside loop, as described in Example 5
void BeginBatchDraw();
(2)FlushBatchDraw
This function is used to perform drawing tasks unfinished.
By drawing in the middle.
void FlushBatchDraw();
(3)EndBatchDraw
This function is used to draw the end of the batch, and perform drawing tasks unfinished.
void EndBatchDraw();
Position (4) Analysis of FlushBatchDraw
As in Example 5, intermediate two functions on the bottom drawing the drawing instead of two, because the flicker is not blue ball is drawn out, not black pellets are not drawn. So just make sure to draw a green ball, but the function is the most close to the top of the drawing will be able to draw. If the two lowermost plot, as described below, plus a Sleep function control speed, the result is nothing. Because green flash a few milliseconds gone.
BeginBatchDraw();
while (1)
{
setcolor(GREEN);
setfillcolor(BLUE);
fillcircle(ball_x, ball_y, radius);
Sleep(5);
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x, ball_y, radius);
FlushBatchDraw();
ball_x += ball_vx;
ball_y += ball_vy;
if(ball_x <= radius || ball_x >= Width - radius)
ball_vx = -ball_vx;
if (ball_y <= radius || ball_y >= Height - radius)
ball_vy = -ball_vy;
}
EndBatchDraw();
6. Image
(1)IMAGE
Direct assignment of objects to achieve IMAGE
class IMAGE(int width = 0, int height = 0);
Public members:
int getwidth();
IMAGE object returns the width, in pixels.
int getheight();
Returns the height IMAGE object, in pixels.
Example:
The following code creates a local img1, img2 two objects, after loading the picture into test.jpg img1, and by assignment to copy the contents of img1 img2:
IMAGE img1, img2;
loadimage(&img1, _T("test.jpg"));
img2 = img1;以下局部代码创建 img 对象,之后加载图片 test.jpg,并将图片的宽高赋值给变量 w、h:
IMAGE img;
loadimage(&img, _T("test.jpg"));
int w, h;
w = img.getwidth();
h = img.getheight();
(2)loadimage
This function is used to read an image from a file.
// 从图片文件获取图像(bmp/jpg/gif/emf/wmf/ico)
void loadimage(
IMAGE* pDstImg, // 保存图像的 IMAGE 对象指针
LPCTSTR pImgFile, // 图片文件名
int nWidth = 0, // 图片的拉伸宽度
int nHeight = 0, // 图片的拉伸高度
bool bResize = false // 是否调整 IMAGE 的大小以适应图片
);
// 从资源文件获取图像(bmp/jpg/gif/emf/wmf/ico)
void loadimage(
IMAGE* pDstImg, // 保存图像的 IMAGE 对象指针
LPCTSTR pResType, // 资源类型
LPCTSTR pResName, // 资源名称
int nWidth = 0, // 图片的拉伸宽度
int nHeight = 0, // 图片的拉伸高度
bool bResize = false // 是否调整 IMAGE 的大小以适应图片
);
parameter:
pDstImg
IMAGE object pointer to save the image. If NULL, the picture will be read to indicate the drawing window.
pImgFile
Picture file name. Supports bmp / jpg / gif / emf / wmf / ico type of picture. gif type image loading only the first frame, it does not support transparency.
nWidth
Stretching the width of the image. After the image is loaded, it will be stretched to the width. If 0 is the width of the original use.
nHeight
Pictures of stretch height. After loading the picture will be stretched to the height. If it is 0, which means original height.
Brisise
IMAGE whether to adjust the size to fit the picture.
pResType
Picture resource type.
dub
Picture resource name.
(3) putimage
Several override this function is specified for drawing an image on the current device.
// 绘制图像
void putimage(
int dstX, // 绘制位置的 x 坐标
int dstY, // 绘制位置的 y 坐标
IMAGE *pSrcImg, // 要绘制的 IMAGE 对象指针
DWORD dwRop = SRCCOPY // 三元光栅操作码(详见备注)
);
// 绘制图像(指定宽高和起始位置)
void putimage(
int dstX, // 绘制位置的 x 坐标
int dstY, // 绘制位置的 y 坐标
int dstWidth, // 绘制的宽度
int dstHeight, // 绘制的高度
IMAGE *pSrcImg, // 要绘制的 IMAGE 对象指针
int srcX, // 绘制内容在 IMAGE 对象中的左上角 x 坐标
int srcY, // 绘制内容在 IMAGE 对象中的左上角 y 坐标
DWORD dwRop = SRCCOPY // 三元光栅操作码(详见备注)
);
parameter:
(See Note in each of the overloaded function prototype)
Remarks:
Three yuan raster operation codes (Accession mode of operation), just one triplet 256 supports all raster operation code, several commonly used as follows:
Value Meaning
DSTINVERT pixel color drawn out = NOT screen colors
MERGECOPY plotted pixel color = color image AND the current fill color
MERGEPAINT plotted pixel color = color screen OR (NOT color images)
NOTSRCCOPY plotted pixel color image color = NOT
NOTSRCERASE pixel color drawn out = NOT (OR screen color image color) [mask] FIG.
PATCOPY pixel color drawn out current fill color =
PATINVERT plotted pixel color = color screen XOR the current fill color
PATPAINT plotted screen pixel color = color OR ((NOT color images) OR current fill color)
SRCAND plotted pixel color = color screen color image AND
SRCCOPY plotted pixel color = color image [Normal]
SRCERASE plotted pixel color = (NOT screen color) AND color image
SRCINVERT plotted pixel color = color screen XOR sprite image color []
SRCPAINT plotted pixel color = color of the screen image color OR
Note:
1. AND / OR / NOT / XOR Boolean operation.
2. "Screen Color" refers to the color of screen pixel point through which to draw.
3. "image color" refers to the color image by the IMAGE object.
4. "current fill color" means the color of the fill for the current setfillcolor provided.
7. clear screen
(1)cleardevice
This function is used to clear the screen. Specifically, the current background color is clear the screen, and moves the current point (0, 0).
void cleardevice();
(2)clearrectangle
This function is used to empty a rectangular area.
void clearrectangle(
int left, //矩形左部 x 坐标。
int top, //矩形上部 y 坐标。
int right, //矩形右部 x 坐标。
int bottom //矩形下部 y 坐标。
);
8. The output string
(1)outtextxy
This function is used the output string at the specified location.
void outtextxy(
int x, //字符串输出时头字母的 x 轴的坐标值
int y, //字符串输出时头字母的 y 轴的坐标值
LPCTSTR str
);
void outtextxy(
int x,
int y,
TCHAR c
);
How to Use LPCTSTR str and TCHAR c:
TCHAR s[100];
_stprintf(s, _T("你的分数:%d"), kill);
outtextxy((WINDOW_WIDTH / 2 + WIDTH / 2) + 50, WINDOW_HEIGHT/2, s);
Second, the example
1. Eju Article parallel straight Line
initgraph(640, 480);
for (int y = 0; y <= 480; y += 48)
{
line(0, y, 640, y);
}
_getch();
closegraph();
2. The color of the window in a line drawing of the gradation
initgraph(640, 480);
for (int y = 0; y <= 480; y++)
{
setcolor(RGB(0, 0, y/2));
line(0, y, 640, y);
}
_getch();
closegraph();
3. Draw lines alternately with red and blue
initgraph(640, 480);
for (int y = 0; y <= 480; y+=48)
{
if ((y / 48) % 2 == 1)
setcolor(RGB(255, 0, 0));
else
setcolor(RGB(0, 0, 255));
line(0, y, 640, y);
}
_getch();
closegraph();
4. animation
(1) only a small ball code:
initgraph(640, 480);
setfillcolor(BLUE);
for (int y = 0; y <= 480; y+=10)
{
fillcircle(100, y, 25);
Sleep(100);
cleardevice();
}
_getch();
closegraph();
(2) a complex drawing code:
initgraph(640, 480);
for (int y = 0; y <= 480; y+=10)
{
setcolor(GREEN);
setfillcolor(BLUE);
fillcircle(100, y, 25);
Sleep(100);
setcolor(BLACK); //不仅填充色要求是黑色,而且边框色要得改变,默认白色
setfillcolor(BLACK);
fillcircle(100, y, 25);
}
_getch();
closegraph();
The ball rebound
#include <graphics.h>
#include <conio.h>
#define Width 640
#define Height 480
int main()
{
// 初始化绘图窗口
initgraph(640, 480);
double ball_x=100, ball_y=100;
double ball_vx=1, ball_vy=1;
double radius=25;
BeginBatchDraw();
while (1)
{
setcolor(GREEN);
setfillcolor(BLUE);
fillcircle(ball_x, ball_y, radius);
Sleep(5);
FlushBatchDraw();
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x, ball_y, radius);
ball_x += ball_vx;
ball_y += ball_vy;
if (ball_x <= radius || ball_x >= Width - radius)
ball_vx = -ball_vx;
if (ball_y <= radius || ball_y >= Height - radius)
ball_vy = -ball_vy;
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}
6. Clock
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define Width 640
#define Height 480
#define PI 3.14159
int main()
{
// 初始化绘图窗口
initgraph(640, 480);
//秒针起始坐标
int center_x = Width / 2, center_y = Height / 2;
//秒针终点坐标
int secondEnd_x, secondEnd_y;
//分针终点坐标
int minuteEnd_x, minuteEnd_y;
//时针终点坐标
int hourEnd_x, hourEnd_y;
//秒针长度
int secondLength = Width / 4;
//分针长度
int minuteLength = Width / 5.5;
//时针长度
int hourLength = Width / 7;
//秒针对应转到角度
float secondAngle = 0;
//分针对应转到角度
float minuteAngle = 0;
//时针对应转到角度
float hourAngle = 0;
//定义变量存储系统时间
SYSTEMTIME ti;
BeginBatchDraw();
while (1)
{
setfillcolor(YELLOW);
setlinestyle(PS_DASHDOTDOT, 5);
setlinecolor(0x555555);
circle(center_x, center_y, secondLength + 30);
setcolor(0xAAAAAA);
setlinestyle(PS_DOT|PS_ENDCAP_SQUARE, 2);
circle(center_x, center_y, secondLength + 15);
solidcircle(center_x + 175, center_y, 5);
solidcircle(center_x - 175, center_y, 5);
solidcircle(center_x, center_y + 175, 5);
solidcircle(center_x, center_y - 175, 5);
GetLocalTime(&ti);
secondAngle = ti.wSecond * 2 * PI / 60;
minuteAngle = ti.wMinute * 2 * PI / 60;
hourAngle = ti.wHour * 2 * PI / 60;
secondEnd_x = center_x + secondLength*sin(secondAngle);
secondEnd_y = center_y - secondLength*cos(secondAngle);
minuteEnd_x = center_x + minuteLength*sin(minuteAngle);
minuteEnd_y = center_y - minuteLength*cos(minuteAngle);
hourEnd_x = center_x + hourLength*sin(hourAngle);
hourEnd_y = center_y - hourLength*cos(hourAngle);
//画秒针
setlinestyle(PS_SOLID,1);
setcolor(WHITE);
line(center_x, center_y, secondEnd_x, secondEnd_y);
//画分针
setlinestyle(PS_SOLID,2);
setcolor(GREEN);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
//画时针
setlinestyle(PS_SOLID,5);
setcolor(RED);
line(center_x, center_y, hourEnd_x, hourEnd_y);
FlushBatchDraw();
setlinestyle(PS_SOLID, 1);
setcolor(BLACK);
line(center_x, center_y, secondEnd_x, secondEnd_y);
setlinestyle(PS_SOLID, 2);
setcolor(BLACK);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
setlinestyle(PS_SOLID, 5);
setcolor(BLACK);
line(center_x, center_y, hourEnd_x, hourEnd_y);
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}
7. Thunder fighter
https://blog.csdn.net/sandalphon4869/article/details/80861915