EasyX 绘图在多线程的绘图下怎样解决开启双缓冲的冲突问题?

0

在复杂的绘图下,用星星函数绘制两个旋转的星星,在一个进程里开启两个不同的线程,用于并发绘制两个旋转星星,不开启缓冲区的情况下两个星星会闪,
在开启绘图模式下闪的更历害了,有啥方法能解决吗?
实例代码:

#include<graphics.h>
#include<math.h>
#define PI 3.1415926535
	int re1 = 0;

	int pts[40] = { 0 };
	int pts2[40] = { 0 };

	void pong(int x1, int y1, int line, int line1, int temp, double a)
	{
		int x11 = 0;
		int y11 = 0;

		for (int x = 0; x < temp; x++)
		{
			pts[x * 2] = int(line * -cos(PI * 4 / 5 * x + a) + x1);
			pts[x * 2 + 1] = int(line * sin(PI * 4 / 5 * x + a) + y1);
		}

		setlinestyle(PS_DASH, 2);
		double a1 = 0;

		for (int x = 0; x < temp; x++)
		{
			circle(pts[x * 2], pts[x * 2 + 1], 40);
			circle(pts[x * 2], pts[x * 2 + 1], 30);
			setfillcolor(RGB(250, 235, 212));
			x11 = pts[x * 2];
			y11 = pts[x * 2 + 1];
			for (int x = 0; x < temp; x++)
			{
				pts2[x * 2] = int(30 * -cos(PI * 4 / 5 * x + -a) + x11);
				pts2[x * 2 + 1] = int(30 * sin(PI * 4 / 5 * x + -a) + y11);
			}
			fillpolygon((POINT*)pts2, temp);

		}

		setfillcolor(RGB(250, 235, 212));
		fillpolygon((POINT*)pts, temp);


	}


// 多线程函数
DWORD WINAPI ThreadFun(LPVOID pM)
{
	BeginBatchDraw();
	for (int i = 0;; i++)
	{
	cleardevice();
		pong(100, 100, 50, 50, 5,i);
		Sleep(20);
	FlushBatchDraw();
	}
	EndBatchDraw();
	return 0;
}
// 多线程函数
DWORD WINAPI ThreadFun1(LPVOID pM)
{
	BeginBatchDraw();
	for (int i = 0;; i++)
	{
	cleardevice();
	pong(400, 400, 50, 50, 5, i);
	Sleep(20);
	FlushBatchDraw();
	}
	EndBatchDraw();
	return 0;
}

int serial_dtu_test()
{
	HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);			// 建立多线程加载多线程函数
	HANDLE handle1 = CreateThread(NULL, 0, ThreadFun1, NULL, 0, NULL);			// 建立多线程加载多线程函数

	if (handle == NULL)return 0; if (handle1 == NULL)return 0;					// 如果多线程等于0就返回

	WaitForSingleObject(handle, INFINITE); WaitForSingleObject(handle1, INFINITE);// 删除多线程
}
int main()
{
	// 合成初始化精简版。
	initgraph(600, 600);
	// 设置本程序窗口,变成时实桌面动态壁纸函数。
	serial_dtu_test();
	getmessage(EX_CHAR);
	// 按 ESC 键停止桌面壁纸程序,可以退出。
	closegraph();
	// 返回。
	return 0;
}
ava
Margoo

2023-3-30

0

不要使用多线程绘图,这是严重错误的设计方式。

ava
慢羊羊

2023-3-30

原谅我电脑差还是算法差,我写的程序好卡,除了多线程外我想不到能解决的方法了,程序卡哭了呜呜呜~ -  Margoo  2023-3-31
技术讨论社区