Agg在Windows下的编译与使用
flyfish
Agg简介
AGG(Anti-Grain Geometry)是一个开源免费的图形库。
官网地址: www.antigrain.com
主要是编译称为Lib库,然后提供给其他程序使用
环境:
Win10 x64
Visual Studio 2013
字符集 Unicode
下载已经编译好Lib的库的地址(包含源码)
函数替换
sprintf sprintf_s
strcpy strcpy_s
fopen fopen_s
等 就是一些不安全的函数更改为安全的函数
新建一个 Win32 项目,应用类型是 静态库
将src中文件添加到项目中,如图,帅选器也和目录结构一样
添加文件时,不添加agg_platform_support.cpp,因为该代码时在字符集是多字节下编写的,如果编译环境使用多字节,可以添加该文件
配置如下
1配置属性-》常规-》字符集:使用Unicode字符集
2 C/C++->常规-》附加包含目录
./include;./font_freetype;./font_win32_tt;./gpc;
3 C/C++ -》预编译头:不使用预编译头
编译生成Lib
MFC下的使用
新建一个基于对话框的项目
以Agg源码在F:\Lib下为例
配置如下
1 C/C++ -》常规-》附加包含目录F:\lib\Agg\Agg\include;
2 链接器-》附加库目录:F:\lib\Agg\Debug;
3 链接器-》输入-》附加依赖项:agg.lib
代码
主要是在对话框的OnPaint中编写
void CAggTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
RECT rt;
GetClientRect(&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
//============================================================
//Creating compatible DC and a bitmap to render the image
BITMAPINFO bmp_info;
bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmp_info.bmiHeader.biWidth = width;
bmp_info.bmiHeader.biHeight = height;
bmp_info.bmiHeader.biPlanes = 1;
bmp_info.bmiHeader.biBitCount = 32;
bmp_info.bmiHeader.biCompression = BI_RGB;
bmp_info.bmiHeader.biSizeImage = 0;
bmp_info.bmiHeader.biXPelsPerMeter = 0;
bmp_info.bmiHeader.biYPelsPerMeter = 0;
bmp_info.bmiHeader.biClrUsed = 0;
bmp_info.bmiHeader.biClrImportant = 0;
HDC hdc = ::GetDC(m_hWnd);
HDC mem_dc = ::CreateCompatibleDC(hdc);
void* buf = 0;
HBITMAP bmp = ::CreateDIBSection(
mem_dc,
&bmp_info,
DIB_RGB_COLORS,
&buf,
0,
0
);
// Selecting the object before doing anything allows you
// to use AGG together with native Windows GDI.
HBITMAP temp = (HBITMAP)::SelectObject(mem_dc, bmp);
//============================================================
// AGG lowest level code.
agg::rendering_buffer rbuf;
rbuf.attach((unsigned char*)buf, width, height, -width * 4); // Use negative stride in order
// to keep Y-axis consistent with
// WinGDI, i.e., going down.
// Pixel format and basic primitives renderer
agg::pixfmt_bgra32 pixf(rbuf);
agg::renderer_base<agg::pixfmt_bgra32> renb(pixf);
renb.clear(agg::rgba8(255, 255, 255, 255));
// Scanline renderer for solid filling.
agg::renderer_scanline_aa_solid<agg::renderer_base<agg::pixfmt_bgra32> > ren(renb);
// Rasterizer & scanline
agg::rasterizer_scanline_aa<> ras;
agg::scanline_p8 sl;
// Polygon (triangle)
ras.move_to_d(20.7, 34.15);
ras.line_to_d(398.23, 123.43);
ras.line_to_d(165.45, 401.87);
// Setting the attrribute (color) & Rendering
ren.color(agg::rgba8(80, 90, 60));
agg::render_scanlines(ras, sl, ren);
//============================================================
//------------------------------------------------------------
// Display the image. If the image is B-G-R-A (32-bits per pixel)
// one can use AlphaBlend instead of BitBlt. In case of AlphaBlend
// one also should clear the image with zero alpha, i.e. rgba8(0,0,0,0)
::BitBlt(
hdc,
rt.left,
rt.top,
width,
height,
mem_dc,
0,
0,
SRCCOPY
);
// Free resources
::SelectObject(mem_dc, temp);
::DeleteObject(bmp);
::DeleteObject(mem_dc);
CDialogEx::OnPaint();
}
}