Direct2d错误的交叉线?

时间:2015-11-25 12:53:45

标签: c++ direct2d

在一个简短的示例中,我生成一个HwndRenderTarget,一个Brush,这两行中的PathGeometry都是从同一个Point开始绘制的。但是在渲染帧中,它们不是从同一点开始,而是明显相互交叉(它们延伸到起点之外)

Main.cpp的

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

Graphics* graphics;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_DESTROY:
        PostQuitMessage(0);
    return 0;

    //case WM_PAINT:

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmd, int nCMDShow)
{
    WNDCLASSEX windowclass;
    ZeroMemory(&windowclass, sizeof(WNDCLASSEX));
    windowclass.cbSize = sizeof(WNDCLASSEX);
    windowclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
    windowclass.hInstance = hInstance;
    windowclass.lpfnWndProc = WindowProc;
    windowclass.lpszClassName = "MainWindow";
    windowclass.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&windowclass);

    RECT rect = { 0,0,564,564 };
    AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, false, WS_EX_OVERLAPPEDWINDOW);

    HWND windowhandle = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,"MainWindow", "Intersection Test", WS_OVERLAPPEDWINDOW, 100, 100,
                                       rect.right - rect.left, rect.bottom- rect.top, NULL, NULL, hInstance, 0);

    if (!windowhandle) return -1;

    graphics = new Graphics();

    if (!graphics->Init(windowhandle))
    {
        delete graphics;
    return -1;
    }
    ShowWindow(windowhandle, nCMDShow);

    graphics->PrepareSpirale();

    MSG msg;
    msg
        .message = WM_NULL;
    while (msg.message != WM_QUIT) // 
    {
        if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            DispatchMessage(&msg);
        }
        else
        {
            graphics->BeginDraw();
            graphics->ClearScreen(1, 1, 1);
            graphics->DrawGraphics();
            graphics->EndDraw();
        }
    }
    delete graphics;
    return 0;
}

有Graphics.h

#pragma once
#include <Windows.h>
#include<d2d1.h>
#include <math.h>



class Graphics
{
    ID2D1Factory* factory;
    ID2D1HwndRenderTarget* renderTarget;
    ID2D1PathGeometry * BorderCCW;
    ID2D1GeometrySink * pSink;
    ID2D1SolidColorBrush *RedBrush;


public:
Graphics();
~Graphics();

bool Init(HWND windowHandle);

void BeginDraw() { renderTarget->BeginDraw(); }
void EndDraw() { renderTarget->EndDraw(); }
void PrepareSpirale();
void DrawGraphics();

void ClearScreen(float r, float g, float b );

};

Graphics.cpp

#include"Graphics.h"


Graphics::Graphics()
{
    factory = NULL;
    renderTarget = NULL;
    pSink = NULL;
    BorderCCW = NULL;
    RedBrush = NULL;
}

Graphics::~Graphics()
{
    if (factory) factory->Release();
    if (renderTarget) renderTarget->Release();
}

bool Graphics::Init(HWND windowHandle)
{
    HRESULT res = 0;
    // Create renderTarget
    if (SUCCEEDED(res))
    {
        res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
        RECT rect;
        GetClientRect(windowHandle, &rect);

        D2D1_SIZE_U size = D2D1::SizeU(
            rect.right - rect.left,
            rect.bottom - rect.top
            );
        if (SUCCEEDED(res))
        res = factory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(windowHandle, size),
            &renderTarget);
        FLOAT dpiX, dpiY;
        factory->GetDesktopDpi(&dpiX, &dpiY);

        // Create Other Resurces
        if (SUCCEEDED(res))
        {
            // Brushes
            res = renderTarget->CreateSolidColorBrush(
                D2D1::ColorF(1.0f, 0.13f, 0.13f, 1.0f),
                &RedBrush);
        }
    return true;
    }
    return false;
}

void Graphics::PrepareSpirale()
{
    HRESULT res = 0;

    if (SUCCEEDED(res))
    {
        res = factory->CreatePathGeometry(&BorderCCW);

        if (SUCCEEDED(res))
        {
            res = BorderCCW->Open(&pSink);

            D2D1_POINT_2F currentLocation = { 281.5f, 281.5f };

            pSink->BeginFigure(currentLocation, D2D1_FIGURE_BEGIN_HOLLOW);
            pSink->AddLine(D2D1::Point2F(132.5, 432.5));
            pSink->EndFigure(D2D1_FIGURE_END_CLOSED);

            pSink->BeginFigure(currentLocation, D2D1_FIGURE_BEGIN_HOLLOW);
            pSink->AddLine(D2D1::Point2F(132+ 282.5f, 432.5));
            pSink->EndFigure(D2D1_FIGURE_END_CLOSED);

        }
        //}
    res = pSink->Close();
    pSink->Release();
    pSink = NULL;
}
}

void Graphics::DrawGraphics()

{
    //renderTarget->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
    renderTarget->DrawGeometry(BorderCCW, RedBrush, 2.f);
}

void Graphics::ClearScreen(float r, float g, float b )
{
    renderTarget->Clear(D2D1::ColorF(r, g, b));
}

渲染线

Rendered Lines

由于这是我第一次进入C ++,任何风格的评论也是最受欢迎的,但他们的主要问题是找到这种奇怪现象的解释。

0 个答案:

没有答案