字体冰封

转载请标明地址:http://www.cnblogs.com/wangmengmeng/

效果图:

 

源代码:

  1 #include <graphics.h>
  2 #include <conio.h>
  3 #include <time.h>
  4 
  5 
  6 // 定义全局变量
  7 POINT *g_pDst;        // 点集(目标)
  8 POINT *g_pSrc;        // 点集(源)
  9 int g_nWidth;        // 文字的宽度
 10 int g_nHeight;        // 文字的高度
 11 int g_nCount;        // 点集包含的点的数量
 12 
 13 
 14 // 获取目标点集
 15 void GetDstPoints()
 16 {
 17     // 设置临时绘图对象
 18     IMAGE img;
 19     SetWorkingImage(&img);
 20 
 21     // 定义目标字符串
 22     TCHAR s[] = _T("Hello world");
 23 
 24     // 计算目标字符串的宽高,并调整临时绘图对象的尺寸
 25     setcolor(WHITE);
 26     setfont(100, 0, _T("Arial"));
 27     g_nWidth = textwidth(s);
 28     g_nHeight = textheight(s);
 29     Resize(&img, g_nWidth, g_nHeight);
 30 
 31     // 输出目标字符串至 img 对象
 32     outtextxy(0, 0, s);
 33 
 34     // 计算构成目标字符串的点的数量
 35     int x, y;
 36     g_nCount = 0;
 37     for(x = 0; x < g_nWidth; x++)
 38         for(y = 0; y < g_nHeight; y++)
 39             if (getpixel(x, y) == WHITE)
 40                 g_nCount++;
 41 
 42     // 计算目标数据
 43     g_pDst = new POINT[g_nCount];
 44     int i = 0;
 45     for(x = 0; x < g_nWidth; x++)
 46         for(y = 0; y < g_nHeight; y++)
 47             if (getpixel(x, y) == WHITE)
 48             {
 49                 g_pDst[i].x = x + (640 - g_nWidth) / 2;
 50                 g_pDst[i].y = y + (480 - g_nHeight) / 2;
 51                 i++;
 52             }
 53 
 54     // 恢复对屏幕的绘图操作
 55     SetWorkingImage(NULL);
 56 }
 57 
 58 
 59 // 获取源点集
 60 void GetSrcPoints()
 61 {
 62     // 设置随机种子
 63     srand((unsigned int)time(NULL));
 64 
 65     // 设置随机的源数据
 66     g_pSrc = new POINT[g_nCount];
 67     for(int i = 0; i < g_nCount; i++)
 68     {
 69         g_pSrc[i].x = rand() % 640;
 70         g_pSrc[i].y = rand() % 480;
 71     }
 72 }
 73 
 74 
 75 // 全屏模糊处理(忽略屏幕第一行和最后一行)
 76 void Blur(DWORD* pMem)
 77 {
 78     for(int i = 640; i < 640 * 479; i++)
 79     {
 80         pMem[i] = RGB(
 81             (GetRValue(pMem[i]) + GetRValue(pMem[i - 640]) + GetRValue(pMem[i - 1]) + GetRValue(pMem[i + 1]) + GetRValue(pMem[i + 640])) / 5,
 82             (GetGValue(pMem[i]) + GetGValue(pMem[i - 640]) + GetGValue(pMem[i - 1]) + GetGValue(pMem[i + 1]) + GetGValue(pMem[i + 640])) / 5,
 83             (GetBValue(pMem[i]) + GetBValue(pMem[i - 640]) + GetBValue(pMem[i - 1]) + GetBValue(pMem[i + 1]) + GetBValue(pMem[i + 640])) / 5);
 84     }
 85 }
 86 
 87 
 88 // 主函数
 89 void main()
 90 {
 91     // 初始化
 92     initgraph(640, 480);                // 创建绘图窗口看
 93     DWORD* pBuf = GetImageBuffer();        // 获取显存指针
 94     GetDstPoints();                        // 获取目标点集
 95     GetSrcPoints();                        // 获取源点集
 96     setbkcolor(WHITE);//白色背景效果
 97     cleardevice();
 98 
 99     // 运算
100     int x, y;
101     for (int i = 2; i <= 256; i += 2)
102     {
103         COLORREF c = RGB(i-1, i-1, i-1);
104         Blur(pBuf);                        // 全屏模糊处理
105 
106         for (int d = 0; d < g_nCount; d++)
107         {
108             x = g_pSrc[d].x + (g_pDst[d].x - g_pSrc[d].x) * i / 256;
109             y = g_pSrc[d].y + (g_pDst[d].y - g_pSrc[d].y) * i / 256;
110             pBuf[y * 640 + x] = c;        // 直接操作显存画点
111         }
112 
113         FlushBatchDraw();                // 使显存操作生效
114         Sleep(10);                        // 延时
115     }
116 
117     // 清理内存
118     delete g_pDst;
119     delete g_pSrc;
120 
121     // 按任意键退出
122     getch();
123     closegraph();
124 }

 

原文地址:https://www.cnblogs.com/wangmengmeng/p/4715545.html