• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

Direct2D Draw Bitmap

Messages
7
Country
ca-britishcolumbia
Hey devs, still brand new to gauge development so I apologize if this isn't the way we discuss code on FSDeveloper.

I've been experimenting with the Direct2D way of gauge development; I have done D2D in Win32 C++ in the past, so it wasn't so hard to pick up (I implemented a draw animation as a test project).

However, the part I'm stuck at is the bitmap loading. I wrote it all in the d2dg->pRT->BeginDraw() temporarily for sandbox purposes.

(I'm using Naruto-kun's blank D2D template)

Here's the code:
Code:
d2dg->pRT->BeginDraw();
             HRESULT hr;
             ID2D1Bitmap* bmp;

             // Create a WIC Factory
             IWICImagingFactory *wicFactory = NULL;
             hr = CoCreateInstance(
               CLSID_WICImagingFactory,
               NULL,
               CLSCTX_INPROC_SERVER,
               IID_IWICImagingFactory,
               (LPVOID*)&wicFactory);

             // Create a decoder
             IWICBitmapDecoder *wicDecoder = NULL;
             hr = wicFactory->CreateDecoderFromFilename(
               L"image1.png",
               NULL,
               GENERIC_READ,
               WICDecodeMetadataCacheOnLoad,
               &wicDecoder);

             // Read a frame from the image
             IWICBitmapFrameDecode* wicFrame = NULL;
             hr = wicDecoder->GetFrame(0, &wicFrame);

             // Create a converter
             IWICFormatConverter *wicConverter = NULL;
             hr = wicFactory->CreateFormatConverter(&wicConverter);

             // Setup the converter
             hr = wicConverter->Initialize(
               wicFrame,
               GUID_WICPixelFormat32bppBGRA,
               WICBitmapDitherTypeNone,
               NULL,
               0.0,
               WICBitmapPaletteTypeCustom);

             // Use the converter to create an D2D1Bitmap

             hr = d2dg->pRT->CreateBitmapFromWicBitmap(
               wicConverter,
               NULL,
               &bmp);

             if (wicFactory)
               wicFactory->Release();

             if (wicDecoder)
               wicDecoder->Release();

             if (wicConverter)
               wicConverter->Release();

             if (wicFrame)
               wicFrame->Release();

             // Draw the bitmap to the gauge screen
             d2dg->pRT->DrawBitmap(
               bmp,
               D2D1::RectF(0.0f, 0.0f, bmp->GetSize().width, bmp->GetSize().height),
               1.0f,
               D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
               D2D1::RectF(0.0f, 0.0f, bmp->GetSize().width, bmp->GetSize().height));
d2dg->pRT->EndDraw();

The code compiles, but whenever P3D attempts to load the gauge, it crashes. I have attached my Visual Studio debugger to P3D and ran it in Debug Mode with breakpoints, but there's nothing showing that could cause the sim to crash. Wondering if anyone has any suggestions on how they'd load a bitmap in D2D graphics?

Cheers,
hdcoder
 
Last edited:
Messages
7
Country
ca-britishcolumbia
I ran the debugger again and it seems this line is causing P3D to crash:

Code:
// Read a frame from the image
IWICBitmapFrameDecode* wicFrame = NULL;
hr = wicDecoder->GetFrame(0, &wicFrame);
 
Messages
7
Country
ca-britishcolumbia
Thanks for the suggestion, I left the checking out when I decided to do it in the switch statement directly. I just went back and re-worked my loading code so it is in a function returning HRESULT (the way I originally did it in win32 apps) and re-added the checking of CreateDecoderFromFileName. The code for the loading is below and now the breakpoint passes and P3D now runs without crashing while loading the image. The part that crashes P3D is now in the DrawBitmap call.

Code:
HRESULT LoadBitmapFromFile(
   ID2D1DCRenderTarget *pRenderTarget,
   IWICImagingFactory *pIWICFactory,
   PCWSTR uri,
   UINT destinationWidth,
   UINT destinationHeight,
   ID2D1Bitmap **ppBitmap
   )
{
   IWICBitmapDecoder *pDecoder = NULL;
   IWICBitmapFrameDecode *pSource = NULL;
   IWICStream *pStream = NULL;
   IWICFormatConverter *pConverter = NULL;
   IWICBitmapScaler *pScaler = NULL;

   HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
     uri,
     NULL,
     GENERIC_READ,
     WICDecodeMetadataCacheOnLoad,
     &pDecoder
     );

   if (SUCCEEDED(hr))
   {
     // Create the initial frame.
     hr = pDecoder->GetFrame(0, &pSource);
   }
   if (SUCCEEDED(hr))
   {

     // Convert the image format to 32bppPBGRA
     // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
     hr = pIWICFactory->CreateFormatConverter(&pConverter);

   }


   if (SUCCEEDED(hr))
   {
     // If a new width or height was specified, create an
     // IWICBitmapScaler and use it to resize the image.
     if (destinationWidth != 0 || destinationHeight != 0)
     {
       UINT originalWidth, originalHeight;
       hr = pSource->GetSize(&originalWidth, &originalHeight);
       if (SUCCEEDED(hr))
       {
         if (destinationWidth == 0)
         {
           FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
           destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
         }
         else if (destinationHeight == 0)
         {
           FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
           destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
         }

         hr = pIWICFactory->CreateBitmapScaler(&pScaler);
         if (SUCCEEDED(hr))
         {
           hr = pScaler->Initialize(
             pSource,
             destinationWidth,
             destinationHeight,
             WICBitmapInterpolationModeCubic
             );
         }
         if (SUCCEEDED(hr))
         {
           hr = pConverter->Initialize(
             pScaler,
             GUID_WICPixelFormat32bppPBGRA,
             WICBitmapDitherTypeNone,
             NULL,
             0.f,
             WICBitmapPaletteTypeMedianCut
             );
         }
       }
     }
     else // Don't scale the image.
     {
       hr = pConverter->Initialize(
         pSource,
         GUID_WICPixelFormat32bppPBGRA,
         WICBitmapDitherTypeNone,
         NULL,
         0.f,
         WICBitmapPaletteTypeMedianCut
         );
     }
   }
   if (SUCCEEDED(hr))
   {

     // Create a Direct2D bitmap from the WIC bitmap.
     hr = pRenderTarget->CreateBitmapFromWicBitmap(
       pConverter,
       NULL,
       ppBitmap
       );
   }

   if (pDecoder) pDecoder->Release();
   if (pSource) pSource->Release();
   if (pStream) pStream->Release();
   if (pScaler) pScaler->Release();

   return hr;
}

Now, the crashing appears to be in this chunk of drawing code that I called inside the switch statement immediately after d2dg->pRT->BeginDraw();
Code:
d2dg->pRT->DrawBitmap(
               pBitmap,
               D2D1::RectF(
               upperLeftCorner.x,
               upperLeftCorner.y,
               upperLeftCorner.x + size.width,
               upperLeftCorner.y + size.height)
               );

I'm speculating that perhaps it has something to do with the render target, d2dg->pRT, although I changed it from ID2D1RenderTarget to ID2D1DCRenderTarget to match render target defined in the gauge header.
 

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
Hold your mouse over the different objects while debugging and see what their addresses are to see if you are calling some null or invalid pointer.
 
Messages
7
Country
ca-britishcolumbia
Hold your mouse over the different objects while debugging and see what their addresses are to see if you are calling some null or invalid pointer.

Ah I see it, thanks! Turns out the bitmap I want to draw is still NULL. I fixed it by removing the **ppBitmap parameter from LoadBitmapFromFile() and just working with the pointer &pBitmap.

Works perfectly and it draws! Thanks guys.

One last question - I know when D2D draws, it constantly draws the same image on top of each other as it updates per frame. When I'm done with the image, is there a way to remove it? I was going to call delete pBitmap but that removes the bitmap completely. I just want it gone from the render target (gauge screen) and memory deallocated so I can render another image when conditions are met.
 

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
ID2DRenderTarget::Clear. And you don't do delete with COM objects. You do Release.
 
Top