SOL9 Sample: Direct2DGeometry

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * Direct2DGeometry.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/Direct2D1EllipseGeometry.h>

#include <sol/directx/Direct2D1RectangleGeometry.h>
#include <sol/directx/Direct2D1RoundedRectangleGeometry.h>
#include <sol/directx/Direct2D1LinearGradientBrush.h>
#include <sol/directx/Direct2D1GradientStopCollection.h>
#include <sol/directx/Direct2D1SolidColorBrush.h>

#include "resource.h"

namespace SOL {

class MainView :public DirectXMainView {

private:
  //Inner class starts.
  class SimpleView : public DirectXView {
  private:
    SmartPtr<Direct2D1HwndRenderTarget>         renderTarget;    
    SmartPtr<Direct2D1EllipseGeometry>          ellipse;
    SmartPtr<Direct2D1RectangleGeometry>        rectangle;
    SmartPtr<Direct2D1RoundedRectangleGeometry> roundedRectangle;
    SmartPtr<Direct2D1LinearGradientBrush>      linearGradientBrush;
    SmartPtr<Direct2D1SolidColorBrush>          blueBrush;
    SmartPtr<Direct2D1SolidColorBrush>          greenBrush;
    SmartPtr<Direct2D1SolidColorBrush>          redBrush;
    
  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 - 210)
            );

                
          renderTarget ->drawRectangle(D2D1::RectF( 400.0f, 200.0f, 200.0f, 100.0f), *blueBrush, 6.0f);
          renderTarget ->fillRectangle(D2D1::RectF( 100.0f, 80.0f, 50.0f, 10.0f), *redBrush);

          renderTarget ->drawEllipse(D2D1::Ellipse( D2D1::Point2F(180.0f, 0.0f), 80.0f, 60.0f), *greenBrush, 2.0f);
          renderTarget ->fillEllipse(D2D1::Ellipse( D2D1::Point2F(180.0f, 0.0f), 20.0f, 20.0f), *blueBrush);

          renderTarget ->drawRoundedRectangle(D2D1::RoundedRect( D2D1::RectF(500.0f, 0.0f, 300.0f, 80.0f), 30.0f, 30.0f), *redBrush, 4.0f);
          renderTarget ->fillRoundedRectangle(D2D1::RoundedRect( D2D1::RectF(100.0f, 60.0f, 200.0f, 100.0f), 10.0f, 10.0f), *greenBrush);
          
          renderTarget -> drawLine(D2D1::Point2F(10.0f, -50.0f), D2D1::Point2F(250.0f, -50.0f), *linearGradientBrush, 6.0f);
          renderTarget -> drawLine(D2D1::Point2F(10.0f, -50.0f), D2D1::Point2F(250.0f, 180.0f), *linearGradientBrush, 6.0f);
          renderTarget -> drawLine(D2D1::Point2F(250.0f,-50.0f), D2D1::Point2F(250.0f, 180.0f), *linearGradientBrush, 6.0f);

          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());
        ellipse        = new Direct2D1EllipseGeometry(*d2d1Factory, 
        D2D1::Ellipse( D2D1::Point2F(150, 50), 100.0f, 50.0f) );
        
        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);
        blueBrush =  new Direct2D1SolidColorBrush(*renderTarget, 
                 D2D1::ColorF(D2D1::ColorF::Blue));

        greenBrush =  new Direct2D1SolidColorBrush(*renderTarget, 
                 D2D1::ColorF(D2D1::ColorF::Green));

        redBrush =  new Direct2D1SolidColorBrush(*renderTarget, 
                 D2D1::ColorF(D2D1::ColorF::Red));
        
      } 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 != nullptr) {
      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.