Compilation and use of Agg under Windows

Keywords: Windows Linker

Compilation and use of Agg under Windows

flyfish

Agg introduction
AGG (anti grain geometry) is an open source and free graphics library.

Official website: www.antigrain.com

It is mainly compiled to be called Lib library, which is then provided to other programs for use
Environmental Science:
Win10 x64
Visual Studio 2013
Character set Unicode

Download the address of the compiled Lib Library (including the source code)

Function substitution
sprintf sprintf_s
strcpy strcpy_s
fopen fopen_s
And so on are some unsafe functions changed to safe functions

Create a new Win32 project. The application type is static library
Add the files in src to the project, as shown in the figure. The structure of Shuai selector is the same as that of directory

When you add a file, you do not add AGG platform support.cpp, because the code is written in a character set with multiple bytes. If the compilation environment uses multiple bytes, you can add the file

The configuration is as follows
1 configuration properties general character set: use Unicode character set
2 C / C + + - > General - 'additional include directory
./include;./font_freetype;./font_win32_tt;./gpc;
3 C/C + + - "precompiled header: no precompiled header is used

Build Lib

Use under MFC
Create a new dialog based project

Take Agg source code under F:\Lib as an example
The configuration is as follows
1 C/C + + - "" general - "" additional directory F:\lib\Agg\Agg\include;
2 linker - Additional Library Directory: F:\lib\Agg\Debug;
3 linker input additional dependency: agg.lib

Code
Mainly written in OnPaint of dialog box

void CAggTestDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // Device context for drawing

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in workspace rectangle
        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;

        // draw icon
        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();
    }
}

Effect

Posted by toodi4 on Fri, 01 May 2020 10:44:16 -0700