////////////////////////////////////////////////////////
// 程序名称：合成立方体 (Cube)
// 编译环境：VS2019		EasyX_2021-7-30
// 作　　者：Margoo <1683691371@qq.com>
// 最后修改：2021-8-30
//
#pragma once

#include <graphics.h>
#include <vector>

#pragma comment(lib, "MSIMG32.LIB")

// 透明贴图
void imageput(int x, int y, IMAGE* image_beput,
	int transparency, IMAGE* target)
{
	HDC target_dc = GetImageHDC(target);
	HDC image_dc = GetImageHDC(image_beput);

	int width = image_beput->getwidth();
	int height = image_beput->getheight();

	BLENDFUNCTION blend_function = { AC_SRC_OVER, 0, static_cast<BYTE>(transparency), AC_SRC_ALPHA };
	AlphaBlend(target_dc, x, y, width, height,
		image_dc, 0, 0, width, height,
		blend_function);
}
// 设置图片的alpha值
void render_image_alpha(IMAGE* image)
{
	BYTE a, r, g, b;

	DWORD* image_buffer = GetImageBuffer(image);

	for (int image_count = image->getwidth() * image->getheight() - 1; image_count >= 0;
		--image_count)
	{
		if (image_buffer[image_count] == 0)
		{
			a = 0;
		}
		else
		{
			a = 255;
		}

		r = GetRValue(image_buffer[image_count]) * a / 255;
		g = GetGValue(image_buffer[image_count]) * a / 255;
		b = GetBValue(image_buffer[image_count]) * a / 255;

		image_buffer[image_count] = a << 24 | RGB(r, g, b);
	}
}

// 任何用户可以互动的元素都是对象
struct image_object
{
	long object_id;

	IMAGE image;

	int x      = 0;
	int y      = 0;
	int width  = 0;
	int height = 0;

	int object_type = 0;

	int transparency = 255;

	void draw(IMAGE* target_image = nullptr)
	{
		imageput(x, y, &image, transparency, target_image);
	}
	void init() 
	{
		image = IMAGE(width, height);
		render_image_alpha(&image);
	}
};
