SOL9 Sample: Direct2DPathGeometry

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * Direct2DPathGeometry.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2015/10/01

#define COMMONCONTROLS_V6

#include <sol/ApplicationView.h>
#include <sol/COMInitializer.h>
#include <sol/PaintDC.h>
#include <sol/StringT.h>

#include <sol/directx/DirectXMainView.h>
#include <sol/directx/DirectXView.h>
#include <sol/directx/Direct2D1PathGeometry.h>
#include <sol/directx/Direct2D1GeometrySink.h>

#include <sol/directx/Direct2D1LinearGradientBrush.h>
#include <sol/directx/Direct2D1GradientStopCollection.h>

#include "resource.h"

namespace SOL {

class MainView :public DirectXMainView {

private:
  //Inner class starts.
  class SimpleView : public DirectXView {
  private:
    SmartPtr<Direct2D1HwndRenderTarget>    renderTarget;
    SmartPtr<Direct2D1PathGeometry>        pathGeometry;
    SmartPtr<Direct2D1LinearGradientBrush> linearGradientBrush;
    
  private:
   void resize(int w, int h)
   {
     try {
       auto size = D2D1::SizeU(w, h);
       renderTarget ->resize(size);
     } catch (Exception& ex) {
       ex.display();
     }
   }
  
   void display()
   {
     try {
       if (!(renderTarget -> checkWindowState() & D2D1_WINDOW_STATE_OCCLUDED)){
         renderTarget -> beginDraw();

         renderTarget -> setTransform(D2D1::Matrix3x2F::Identity());
         renderTarget -> clear(D2D1::ColorF(D2D1::ColorF::White));

         auto size = renderTarget->getSize();
         auto rectangle = D2D1::RectF(0.0f, 0.0f, size.width, size.height);
         renderTarget -> setTransform(
            D2D1::Matrix3x2F::Translation(0, size.height - 200)
            );

         renderTarget -> fillGeometry(*pathGeometry, 
                *linearGradientBrush);

         renderTarget->endDraw();
        }
      } catch (Exception& ex) {
        ex.display();
      }
    }
    
  public:
    SimpleView(MainView* parent, const TCHAR* name, Args& args)
    :DirectXView(parent, name, args)
    {
      try {
        Direct2D1Factory* d2d1Factory = parent -> getD2D1Factory();
        renderTarget   = new Direct2D1HwndRenderTarget(*d2d1Factory, getWindow());

        DirectWriteFactory*  writeFactory = getWriteFactory();
        pathGeometry      = new Direct2D1PathGeometry(*d2d1Factory);
        
        Direct2D1GeometrySink* sink = new Direct2D1GeometrySink(*pathGeometry);
        
        sink -> setFillMode(D2D1_FILL_MODE_ALTERNATE);

        sink -> beginFigure(
            D2D1::Point2F(0, 0),
            D2D1_FIGURE_BEGIN_FILLED
            );

        sink -> addLine(D2D1::Point2F(400, 0));

        sink -> addBezier(
            D2D1::BezierSegment(
                D2D1::Point2F(150, 50),
                D2D1::Point2F(150, 150),
                D2D1::Point2F(400, 200))
        );

        sink -> addLine(D2D1::Point2F(0, 400));

        sink -> addBezier(
            D2D1::BezierSegment(
                D2D1::Point2F(400, 0), 
                D2D1::Point2F(300, 150),
                D2D1::Point2F(0, 0))
                );

        sink -> endFigure(D2D1_FIGURE_END_CLOSED);

        sink -> close();
        delete sink;

        static const D2D1_GRADIENT_STOP stops[] = {
          {   0.f,  { 0.f, 1.f, 1.f, 0.25f }  },
          {   1.f,  { 0.f, 0.f, 1.f, 1.f }  },
        };
        
        Direct2D1GradientStopCollection *gradientStops = new Direct2D1GradientStopCollection(
          *renderTarget, 
          stops, 
          CountOf(stops) );
          
        linearGradientBrush = new Direct2D1LinearGradientBrush(*renderTarget,
                     D2D1::LinearGradientBrushProperties(
                       D2D1::Point2F(100, 0),
                       D2D1::Point2F(200, 200)),
                       D2D1::BrushProperties(),
                       *gradientStops);
        
      } catch (Exception& ex) {
        ex.display();
      }
    }

    ~SimpleView()
    {
    }
  };
  // Inner class ends.

private:
  SmartPtr<SimpleView> view;
  
public:
  //  Constructor
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectXMainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) )
  {
    try {
      Args ar;
      ar.set(XmNstyle, WS_BORDER|WS_CHILD|WS_VISIBLE);
      view = new SimpleView(this, _T(""), ar);

      addEventHandler(WM_SIZE, this, 
        (Handler)&MainView::size, NULL);
    } catch (Exception& ex) {
      ex.display();
    }
    restorePlacement();
  }

public:
  ~MainView()
  {
  }

private:
  long size(Event& event)
  {  
    int w, h;
    event.getSize(w, h);
    if (view.notNull()) {
      view -> reshape(10, 10, w-20, h-20);
    }
    return 0;
  }

};

}


//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{  
  const TCHAR* appClass = appName(argv[0]);
  TCHAR directory[MAX_PATH];
  appDirectory(argv[0], directory, CountOf(directory));

  try {
    HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);

    COMInitializer initializer( COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    
    Application applet(appClass, argc, argv);

    Args args; 
    args.set(XmNapplicationDirectory, directory);
    MainView imageViewer(applet, appClass, args);
    imageViewer.realize();

    applet.run();
  } catch (Exception& ex) {
    ex.display();
  }
}


Last modified: 8 Dec 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.