举报

使用AlphaBlend函数绘制图片失败,怎么解决

0
#include <graphics.h>
#include <iostream>

inline bool putimage_ex(IMAGE* img, int camera_x, int camera_y, int x, int y, int w, int h)
{
	BLENDFUNCTION blend_func = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };

	int img_x = x >= camera_x ? 0 : camera_x - x;
	int img_y = y >= camera_y ? 0 : camera_y - y;
	w = w <= img->getwidth() - img_x ? w : img->getwidth() - img_x;
	h = h <= img->getheight() - img_y ? h : img->getheight() - img_y;

	return AlphaBlend(GetImageHDC(GetWorkingImage()), camera_x >= x ? 0 : x - camera_x, camera_y >= y ? 0 : y - camera_y, w, h, 
		GetImageHDC(img), img_x, img_y, w, h, blend_func);
}

struct Position
{
	int x;
	int y;
};

int main(int argc, char** argv)
{
	initgraph(1280, 720, EX_SHOWCONSOLE);

	IMAGE img_init;

	loadimage(&img_init, L"resources/init.png");

	Position pos_camera = { 0, 0 };

	BeginBatchDraw();

	if (putimage_ex(&img_init, pos_camera.x, pos_camera.y, 0, 0, 1280, 720))
		std::cout << "true" << std::endl;
	else 
		std::cout << "false" << std::endl;

	while (true)
	{
		cleardevice();
		std::cout << putimage_ex(&img_init, pos_camera.x, pos_camera.y, 0, 0, 1280 / 2, 720 / 2) << std::endl;;
		FlushBatchDraw();

		Sleep(10);
	}

	return 0;
}

在创建窗口后,我使用绘制函数来绘制图像时,第一次绘制有时候会失败,在while循环内的绘制全部成功,请问这个问题怎么解决

       

ava
夜 • 思念

2025-2-19

举报
0

要 FlushBatchDraw() 才能显示,while 前没有 FlushBatchDraw 所以不会显示。

另外流程有问题,一绘制就被 cleardevice 清屏

ava
xiongfj ◑◑

2025-2-19

技术讨论社区