⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.cpp

📁 源代码演示了在Windows下角色扮演游戏的制作过程
💻 CPP
字号:
//
//	24Bits/Pixel 图像
//
//		Copyright (c) 2000-2002 Chihiro.SAKAMOTO (HyperWorks)
//
#include "StdAfx.h"
#include "Application.h"
#include "Image.h"
#include "File.h"
#include "Misc.h"
#include "dc.h"

//
// 构造函数
//
CImage::CImage(int width, int height)
{
	Create(width, height);
}

//
// DIB的制作
//
BOOL CImage::Create(int width, int height)
{
	return CDib::Create(width, height, 24);
}

//
// 由CGPATH指定之数据夹读取BMP档
//
BOOL CImage::LoadImage(const char *name, int ox, int oy)
{
	char	path[_MAX_PATH];
	sprintf(path, CGPATH "%s.bmp", name);
	CFile	file(path);
	if (!file)
		return FALSE;
	return LoadBMP(file, ox, oy);
}

//
// 绘制涂满矩形
//
void CImage::FillRect(const CRect &rect, COLORREF color)
{
	const unsigned char b = GetBValue(color);
	const unsigned char g = GetGValue(color);
	const unsigned char r = GetRValue(color);

	for (int y=rect.top; y<rect.bottom; y++) {
		byte_t *p = (byte_t *)GetBits(rect.left, y);
		for (int x=rect.left; x<rect.right; x++) {
			*p++ = b;
			*p++ = g;
			*p++ = r;
		}
	}
}

//
// 区域的复制
//
void CImage::Copy(const CImage *image, const CRect &rect)
{
	int		len = rect.Width() * 3;
	for (int y=rect.top; y<rect.bottom; y++) {
		memcpy(GetBits(rect.left, y), image->GetBits(rect.left, y), len);
	}
}

//
// 复制时考虑透明色
//
void CImage::MixImage(const CImage *image, const CRect &rect, COLORREF trans_color)
{
	const unsigned char trans_b = GetBValue(trans_color);
	const unsigned char trans_g = GetGValue(trans_color);
	const unsigned char trans_r = GetRValue(trans_color);

	for (int y=rect.top; y<rect.bottom; y++) {
		byte_t *p = (byte_t *)GetBits(rect.left, y);
		const byte_t *q = (byte_t *)image->GetBits(rect.left, y);
		for (int x=rect.left; x<rect.right; x++) {
			const byte_t b = *q++;
			const byte_t g = *q++;
			const byte_t r = *q++;

			if (b != trans_b || g != trans_g || r != trans_r) {
				p[0] = b;
				p[1] = g;
				p[2] = r;
			}
			p += 3;
		}
	}
}

//
// 矩形的描绘
//
// 宽度1的矩形 = 水平(垂直)线
//
void CImage::DrawRect(const CRect &rect, COLORREF color)
{
	int		width = rect.Width();
	int		height = rect.Height();
	FillRect(rect.left, rect.top, width, 1, color);
	FillRect(rect.left, rect.top, 1, height, color);
	FillRect(rect.right - 1, rect.top, 1, height, color);
	FillRect(rect.left, rect.bottom - 1, width, 1, color);
}

//
// 填入透明度50%的黑色
//
void CImage::FillHalfToneRect(const CRect &rect)
{
	for (int y=rect.top; y<rect.bottom; y++) {
		byte_t *p = (byte_t *)GetBits(rect.left, y);
		for (int x=rect.left; x<rect.right; x++) {
			*p++ /= 2;
			*p++ /= 2;
			*p++ /= 2;
		}
	}
}

//
// 框线的描绘
//
void CImage::DrawFrameRect(int x, int y, int w, int h, COLORREF color)
{
	DrawRect(x, y + 1, w, h - 2, color);
	DrawRect(x + 1, y, w - 2, h, color);
	FillHalfToneRect(x + 2, y + 2, w - 4, h - 4);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -