C/C++ language simple graphics library EasyX library and EGE graphics library

Some schools directly use VC to teach C language because VC's editing and debugging environment is excellent, and VC has a free version suitable for teaching. Unfortunately, you can only do some textual exercises in VC. It is very difficult to draw a straight line or a circle. For example, you need to register a window class, build a message loop, etc. Beginners will be severely affected. If you are a beginner in programming and want to draw, you have to use TC, which is very frustrating. Some schools use Turbo C as an environment to learn C language, but Turbo C is too old and copying and pasting is inconvenient.

EasyX

EasyX Graphics Library is a free drawing library for Visual C++. It supports VC6.0 ~ VC2022. It is simple and easy to use, has extremely low learning costs and has a wide range of applications. Many universities are already using EasyX in teaching.
Official website download
https://easyx.cn/
Insert image description here

Installation
1. Double-click the downloaded EasyX installation package. In the "Windows has protected your computer" prompt window, click "More Information", then click "Run Anyway". At this time, you can see the EasyX installation program.
2.EasyX installer will detect the version of Visual Studio installed in the current operating system. Click "Install" on the right side of the corresponding VS version.
The installation program is a 7z self-extracting package and will not leave any information in the system registry, so please feel free to use it.
When uninstalling, you also need to execute the installation program and click "Uninstall" on the right side of the corresponding VS version.
The ultra-light publishing process
EasyX uses static compilation and does not rely on any dll. It is no different from the traditional program publishing method. The introduction of EasyX will not add any publishing burden to the program.
Static linking of EasyX will increase the size of the compiled exe by about 70KB. For most applications, the increased volume is negligible.
If the runtime library of Visual C++ is changed to static link mode, the compiled exe can be run as a single file.
Function Description
EasyX functions are divided into the following categories:
Drawing device related functions
Color model
Color and style setting related functions
Graphic drawing related functions
Text output related functions
Image processing related functions
Message processing related functions
Other functions
graphics.h Persistent function
sample program Character
array

// 编译环境:Visual C++ 6.0~2022,EasyX_2023大暑版
// https://easyx.cn
//
#include <graphics.h>
#include <time.h>
#include <conio.h>

int main()
{
    
    
	// 设置随机种子
	srand((unsigned) time(NULL));

	// 初始化图形模式
	initgraph(640, 480);

	int  x, y;
	char c;

	settextstyle(16, 8, _T("Courier"));	// 设置字体

	// 设置颜色
	settextcolor(GREEN);
	setlinecolor(BLACK);

	for (int i = 0; i <= 479; i++)
	{
    
    
		// 在随机位置显示三个随机字母
		for (int j = 0; j < 3; j++)
		{
    
    
			x = (rand() % 80) * 8;
			y = (rand() % 20) * 24;
			c = (rand() % 26) + 65;
			outtextxy(x, y, c);
		}

		// 画线擦掉一个像素行
		line(0, i, 639, i);

		Sleep(10);					// 延时
		if (i >= 479)	i = -1;
		if (_kbhit())	break;		// 按任意键退出
	}

	// 关闭图形模式
	closegraph();
	return 0;
}

starry sky

// 编译环境:Visual C++ 6.0~2022,EasyX_2023大暑版
// https://easyx.cn
//
#include <graphics.h>
#include <time.h>
#include <conio.h>

#define MAXSTAR 200	// 星星总数

struct STAR
{
    
    
	double	x;
	int		y;
	double	step;
	int		color;
};

STAR star[MAXSTAR];

// 初始化星星
void InitStar(int i)
{
    
    
	star[i].x = 0;
	star[i].y = rand() % 480;
	star[i].step = (rand() % 5000) / 1000.0 + 1;
	star[i].color = (int)(star[i].step * 255 / 6.0 + 0.5);	// 速度越快,颜色越亮
	star[i].color = RGB(star[i].color, star[i].color, star[i].color);
}

// 移动星星
void MoveStar(int i)
{
    
    
	// 擦掉原来的星星
	putpixel((int)star[i].x, star[i].y, 0);

	// 计算新位置
	star[i].x += star[i].step;
	if (star[i].x > 640)	InitStar(i);

	// 画新星星
	putpixel((int)star[i].x, star[i].y, star[i].color);
}

// 主函数
int main()
{
    
    
	srand((unsigned)time(NULL));	// 随机种子
	initgraph(640, 480);			// 创建绘图窗口

	// 初始化所有星星
	for(int i = 0; i < MAXSTAR; i++)
	{
    
    
		InitStar(i);
		star[i].x = rand() % 640;
	}

	// 绘制星空,按任意键退出
	while(!_kbhit())
	{
    
    
		for(int i = 0; i < MAXSTAR; i++)
			MoveStar(i);
		Sleep(20);
	}

	closegraph();					// 关闭绘图窗口
	return 0;
}

Mouse operation


// 编译环境:Visual C++ 6.0~2022,EasyX_2023大暑版
// https://easyx.cn
//
#include <graphics.h>

int main()
{
    
    
	// 初始化图形窗口
	initgraph(640, 480);

	ExMessage m;		// 定义消息变量

	while(true)
	{
    
    
		// 获取一条鼠标或按键消息
		m = getmessage(EX_MOUSE | EX_KEY);

		switch(m.message)
		{
    
    
			case WM_MOUSEMOVE:
				// 鼠标移动的时候画红色的小点
				putpixel(m.x, m.y, RED);
				break;

			case WM_LBUTTONDOWN:
				// 如果点左键的同时按下了 Ctrl 键
				if (m.ctrl)
					// 画一个大方块
					rectangle(m.x - 10, m.y - 10, m.x + 10, m.y + 10);
				else
					// 画一个小方块
					rectangle(m.x - 5, m.y - 5, m.x + 5, m.y + 5);
				break;

			case WM_KEYDOWN:
				if (m.vkcode == VK_ESCAPE)
					return 0;	// 按 ESC 键退出程序
		}
	}

	// 关闭图形窗口
	closegraph();
	return 0;
}

rainbow

// 编译环境:Visual C++ 6.0~2022,EasyX_2023大暑版
// https://easyx.cn
//
#include <graphics.h>
#include <conio.h>

int main()
{
    
    
	// 创建绘图窗口
	initgraph(640, 480);

	// 画渐变的天空(通过亮度逐渐增加)
	float H = 190;		// 色相
	float S = 1;		// 饱和度
	float L = 0.7f;		// 亮度
	for(int y = 0; y < 480; y++)
	{
    
    
		L += 0.0005f;
		setlinecolor( HSLtoRGB(H, S, L) );
		line(0, y, 639, y);
	}

	// 画彩虹(通过色相逐渐增加)
	H = 0;
	S = 1;
	L = 0.5f;
	setlinestyle(PS_SOLID, 2);		// 设置线宽为 2
	for(int r = 400; r > 344; r--)
	{
    
    
		H += 5;
		setlinecolor( HSLtoRGB(H, S, L) );
		circle(500, 480, r);
	}

	// 按任意键退出
	_getch();
	closegraph();
	return 0;
}

Easy Graphics Engine

EGE (Easy Graphics Engine) is a simple graphics library under Windows. It is a graphics library similar to BGI (graphics.h) for C/C++ language novices. Its goal is to replace TC's BGI library.

Its usage method is quite close to graphics.h in TC. For novices, it is simple, friendly, easy to use, free and open source, and the interface is intuitive. Even those who have never been exposed to graphics programming can quickly learn the basics. drawing.

At present, the EGE graphics library has perfectly supported IDEs such as VC6, VC2008, VC2010, VC2012, VC2013, VC2015, VC2017, VC2019, C-Free, DevCpp, Code::Blocks, wxDev, Eclipse for C/C++, etc., that is, it supports the use of MSVC and MinGW is an IDE for the compilation environment. If you need to use graphics.h under VC, then ege will be a good substitute.

Official website
https://xege.org/Installation ①Specify the compiler's header file search path (or place the header file in the include folder of the software installation directory) ②Specify the compiler's library file search path (or place the library file
Insert image description here
in lib folder in the software installation directory) ③Configure link parameters in the development environment and link related libraries (not required for Visual Studio)


Sample code
test program

#include <graphics.h>					//包含EGE的头文件

int main()
{
    
    
	initgraph(640, 480);				//初始化图形界面
	
	setcolor(EGERGB(0xFF, 0x0, 0x0));	//设置绘画颜色为红色
	
	setbkcolor(WHITE);					//设置背景颜色为白色
	
	circle(320, 240, 100);				//画圆
	
	getch();							//暂停,等待键盘按键

	closegraph();						//关闭图形界面
	
	return 0;
}

Use the mouse to draw a straight line

#define SHOW_CONSOLE  //显示控制台窗口

#include <graphics.h>
#include <cstdio>

int main()
{
    
    
    initgraph(640, 480);

    bool isDown = false;
    int startX, startY;
    int lastX, lastY;

    mouse_msg msg;

    int colorIndex = 0;

    for(; is_run(); )
    {
    
    
        msg = getmouse();

        switch (msg.msg)
        {
    
    
            case mouse_msg_down:
                startX = msg.x;
                startY = msg.y;
                lastX = msg.x;
                lastY = msg.y;
                printf("mouse down: %d, %d\n", msg.x, msg.y);
                isDown = true;
                break;
            case mouse_msg_up:
                //这里并没有什么卵用
                printf("mouse up: %d, %d\n", msg.x, msg.y);
                isDown = false;
                break;
            case mouse_msg_move:
                if(isDown)
                {
    
    
                    do
                    {
    
    
                        color_t color = hsv2rgb(colorIndex++ % 360, 1, 1); //使用hsv颜色轮换
                        setcolor(color);
                        line(startX, startY, msg.x, msg.y); //绘制当前点和按下的点
                        line(lastX, lastY, msg.x, msg.y); //绘制当前点和上一个点
                        lastX = msg.x;
                        lastY = msg.y;
                        printf("mouse moved to: %d, %d\n", msg.x, msg.y);
                        msg = getmouse();
                    }while(mousemsg() && msg.is_move());
                    
                    if(msg.is_up())
                        isDown = false;
                }
                break;
            case mouse_msg_wheel:
                printf("mousewheel: %d\n", msg.wheel);
                break;
            default:
                printf("呵呵\n");
                break;
        }
    }
    
    closegraph();
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_60352504/article/details/132630396