FPS游戏:封装外部D3D透视模块

LyDrawingClass 库是一个运用D3Dx9绘图引擎封装实现的外部透视图形绘制模块,其功能参考了多个易语言版ImGUI绘制模块,并将其以C语言进行了重写,在重写过程中也封装了一些自己实现的较为通用的绘图方式,运用此库将使C/C++开发透视功能效率更高无需自己封装一遍,此处只做应用笔记源码暂不发布。

绘制矩形有四种,实心矩形,空心矩形,半角矩形,以及带有血量条的矩形。

#include "LyDrawingClass.h"

int w = 0;
int h = 0;

// 函数功能实现
void Function()
{

}

// 菜单绘制部分
void Draw()
{
// 实心矩形
DrawingClass::Start()->DrawFilledRectangle(10, 20, 90, 150, D3DCOLOR_RGBA(254, 0, 0, 184));

// 实心矩形 半透明
DrawingClass::Start()->DrawFilledRectangleB(160 , 20, 90, 150, D3DCOLOR_RGBA(254, 0, 0, 184));

// 空心矩形
DrawingClass::Start()->DrawBorderRectangle(300, 20, 90, 150, 2, D3DCOLOR_RGBA(15, 44, 0, 25));

// 绘制四角方框
DrawingClass::Start()->DrawFourCornersBox(450, 20, 90, 150, 2, D3DCOLOR_RGBA(254, 0, 0, 25));

// 绘制角框
DrawingClass::Start()->DrawCornerFrame(10, 200, 90, 150, 2, D3DCOLOR_RGBA(254, 0, 0, 25));

// 封装血条
DrawingClass::Start()->DrawPerspectiveRect(93, 160, 200, 90, 160);
DrawingClass::Start()->DrawPerspectiveRect(65, 300, 200, 60, 120);
DrawingClass::Start()->DrawPerspectiveRect(10, 450, 200, 90, 160);
}

void Ptr()
{
Function();
Draw();
}

int main(int argc, char *argv[])
{
// 运行线程
const char* class_name = "LyShark ClassName";

// 通过进程名得到进程PID
DWORD pid = DrawingClass::Start()->GetProcessIdByName(L"Win32Project.exe");
printf("进程PID = %d \n", pid);

// 获取进程句柄
HANDLE hwnd = DrawingClass::Start()->GetProcessHandle(pid);
printf("进程句柄 = %d \n", hwnd);

w = DrawingClass::Start()->GetWindowWidth(class_name, class_name);
h = DrawingClass::Start()->GetWindowHeight(class_name, class_name);
DrawingClass::Start()->createWindow(class_name, class_name, "黑体", Ptr);

return 0;
}

效果图如下:

绘制血条有三种,横向血条,竖向血条,中心血条。

#include "LyDrawingClass.h"

int w = 0;
int h = 0;

// 函数功能实现
void Function()
{

}

// 菜单绘制部分
void Draw()
{
// 普通血块(空块)
DrawingClass::Start()->DrawBloodFrame(10, 20, 100, 15, 2, D3DCOLOR_RGBA(254, 0, 0, 184));

// 竖向血条(基础)
DrawingClass::Start()->DrawBlood_Vertical(100, 100, 200, 100, 55, D3DCOLOR_RGBA(254, 0, 0, 184));

// 竖向血条(封装) style = 0 从下往上减血 style = 1 从上往下减血
DrawingClass::Start()->DrawBlood_Horizontal(67, 200, 300, 22, 100, D3DCOLOR_RGBA(254, 0, 0, 184), 1);

// 中心血条
DrawingClass::Start()->DrawCenterBlood(300, 55, 100, 25, 98, 0);

// 横向血条
DrawingClass::Start()->DrawBlood_Transverse(83, 360, 200, 100, 22, D3DCOLOR_RGBA(254, 0, 0, 184), D3DCOLOR_RGBA(44, 0, 0, 10));

DrawingClass::Start()->DrawBlood_Transverse(99, 400, 200, 22, 100, D3DCOLOR_RGBA(254, 0, 0, 184), D3DCOLOR_RGBA(44, 0, 0, 10));
}

void Ptr()
{
Function();
Draw();
}

int main(int argc, char *argv[])
{
// 运行线程
const char* class_name = "LyShark ClassName";

// 通过进程名得到进程PID
DWORD pid = DrawingClass::Start()->GetProcessIdByName(L"Win32Project.exe");
printf("进程PID = %d \n", pid);

// 获取进程句柄
HANDLE hwnd = DrawingClass::Start()->GetProcessHandle(pid);
printf("进程句柄 = %d \n", hwnd);

w = DrawingClass::Start()->GetWindowWidth(class_name, class_name);
h = DrawingClass::Start()->GetWindowHeight(class_name, class_name);
DrawingClass::Start()->createWindow(class_name, class_name, "黑体", Ptr);

return 0;
}

绘制效果如下:

绘制圆形与各种准星图形,以及透视雷达。

#include "LyDrawingClass.h"

int w = 0;
int h = 0;

// 函数功能实现
void Function()
{

}

// 菜单绘制部分
void Draw()
{
// 画圆形
DrawingClass::Start()->DrawCircle(100, 200, 55, 3, D3DCOLOR_RGBA(255, 0, 0, 255));

// 画圆点
DrawingClass::Start()->DrawPoint(55, 67, 2, D3DCOLOR_RGBA(255, 0, 0, 255));

// 画十字准星
DrawingClass::Start()->DrawCollimation(255, 44, D3DCOLOR_RGBA(255, 0, 0, 255), 1);

DrawingClass::Start()->DrawCollimation(280, 44, D3DCOLOR_RGBA(255, 0, 0, 255), 2);

DrawingClass::Start()->DrawCollimation(350, 44, D3DCOLOR_RGBA(255, 0, 0, 255), 3);

// 超级准星
DrawingClass::Start()->DrawSuperCollimation(255, 100, D3DCOLOR_RGBA(255, 0, 0, 255), 1);

DrawingClass::Start()->DrawSuperCollimation(300, 100, D3DCOLOR_RGBA(255, 0, 0, 255), 2);

DrawingClass::Start()->DrawSuperCollimation(350, 100, D3DCOLOR_RGBA(255, 0, 0, 255), 3);

// 绘制雷达
DrawingClass::Start()->DrawRadar(400, 200);
}

void Ptr()
{
Function();
Draw();
}

int main(int argc, char *argv[])
{
// 运行线程
const char* class_name = "LyShark ClassName";

// 通过进程名得到进程PID
DWORD pid = DrawingClass::Start()->GetProcessIdByName(L"Win32Project.exe");
printf("进程PID = %d \n", pid);

// 获取进程句柄
HANDLE hwnd = DrawingClass::Start()->GetProcessHandle(pid);
printf("进程句柄 = %d \n", hwnd);

w = DrawingClass::Start()->GetWindowWidth(class_name, class_name);
h = DrawingClass::Start()->GetWindowHeight(class_name, class_name);
DrawingClass::Start()->createWindow(class_name, class_name, "黑体", Ptr);

return 0;
}

具体效果如下:

针对文本的绘制有三种,普通文本,描边文本,以及重合绘制。

#include "LyDrawingClass.h"

int w = 0;
int h = 0;

// 函数功能实现
void Function()
{

}

// 菜单绘制部分
void Draw()
{
char szBuf[256] = { 0 };

strcpy(szBuf, "hello lyshark www.lyshark.com");

// 普通文本
DrawingClass::Start()->DrawString(szBuf, 25, 10, 20, D3DCOLOR_RGBA(255, 0, 0, 255));

// 描边文本
DrawingClass::Start()->DrawColourString(szBuf, 25, 20, 40, D3DCOLOR_RGBA(255, 0, 0, 255), D3DCOLOR_RGBA(5, 0, 0, 2));

// 多重文本
DrawingClass::Start()->DrawStringAndString(szBuf, szBuf, 30, 25, 50, 100, D3DCOLOR_RGBA(255, 0, 0, 255), D3DCOLOR_RGBA(5, 0, 0, 2));

DrawingClass::Start()->DrawColourString2("hello lyshark", 30, 20, 20, D3DCOLOR_RGBA(0, 0, 255, 221));

DrawingClass::Start()->DrawColourString3("welcome lyshark.com", 30, 40, 40, D3DCOLOR_RGBA(255, 0, 255, 255));
}

void Ptr()
{
Function();
Draw();
}

int main(int argc, char *argv[])
{
// 运行线程
const char* class_name = "LyShark ClassName";

// 通过进程名得到进程PID
DWORD pid = DrawingClass::Start()->GetProcessIdByName(L"Win32Project.exe");
printf("进程PID = %d \n", pid);

// 获取进程句柄
HANDLE hwnd = DrawingClass::Start()->GetProcessHandle(pid);
printf("进程句柄 = %d \n", hwnd);

w = DrawingClass::Start()->GetWindowWidth(class_name, class_name);
h = DrawingClass::Start()->GetWindowHeight(class_name, class_name);
DrawingClass::Start()->createWindow(class_name, class_name, "黑体", Ptr);

return 0;
}

效果如下:

这里扩展一个知识点,如何得到游戏中的FPS数据,得到这个数据我们可以使用timeGetTime() * 0.001这个内置函数来得到,具体代码如下:

#include <Windows.h>
#include "LyDrawingClass.h"

#pragma comment(lib,"winmm.lib")

int 窗口宽度 = 0;
int 窗口高度 = 0;

typedef struct
{
DWORD 当前帧数;
FLOAT 当前时间;
FLOAT 持续时间;
FLOAT 返回FPS;
}D3DFPS;

D3DFPS fps = { 0 };

FLOAT get_fps()
{
fps.当前帧数 = fps.当前帧数 + 1;
fps.当前时间 = timeGetTime() * 0.001;
if (fps.当前时间 - fps.持续时间 > 1)
{
fps.返回FPS = fps.当前帧数 / (fps.当前时间 - fps.持续时间);
fps.持续时间 = fps.当前时间;
fps.当前帧数 = 0;
}

return fps.返回FPS;
}

void Draw()
{
char sz[256] = { 0 };

sprintf(sz, "当前游戏 FPS: %f", get_fps());
DrawingClass::Start()->DrawColourString2(sz, 30, 20, 20, D3DCOLOR_RGBA(0, 0, 255, 221));

char nam[256] = { 0 };
strcpy(nam, "PowerBy: lyshark");
DrawingClass::Start()->DrawColourString2(nam, 20, 70, 70, D3DCOLOR_RGBA(0, 0, 255, 221));
}

int main(int argc, char *argv[])
{
// 运行线程
const char* class_name = "LyShark ClassName";

窗口宽度 = DrawingClass::Start()->GetWindowWidth(class_name, class_name);
窗口高度 = DrawingClass::Start()->GetWindowHeight(class_name, class_name);
DrawingClass::Start()->createWindow(class_name, class_name, "黑体", Draw);
return 0;
}

效果图如下: