SOL9 Sample: Direct3D10RenderTargetViewSaver

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// 2015/10/20
// 2017/01/28 Updated to use ModuleFileName class and caught macro.

#pragma warning(disable : 4005) 
#pragma warning(disable : 4838) 

#define COMMONCONTROLS_V6

#include <sol/COMInitializer.h>

#include <sol/FileDialog.h>

#include <sol/dxgi/DirectXGIFactory.h>
#include <sol/direct3d10/DirectX3D10ApplicationView.h>
#include <sol/dxgi/DirectXGISwapChain.h>
#include <sol/dxgi/DXGISwapChainDesc.h>

#include <sol/direct3d10/Direct3D10RenderTargetView.h>
#include <sol/direct3d10/Direct3D10DepthStencilView.h>

#include <sol/direct3d10/Direct3D10Texture2D.h>
#include <sol/direct3d10/Direct3DX10Font.h>
#include <sol/direct3d10/D3D10Texture2DDesc.h>
#include <sol/direct3d10/D3D10DepthStencilViewDesc.h>


#include "resource.h"

namespace SOL {
  
class MainView :public DirectX3D10ApplicationView {
private:
  SmartPtr<DirectXGISwapChain>          swapChain;
  SmartPtr<Direct3D10RenderTargetView>  renderTargetView;
  SmartPtr<Direct3D10DepthStencilView>  depthStencilView;
  SmartPtr<Direct3DX10Font>             font;

  FileDialog  fileDialog;
  StringT<TCHAR> savedFileName;

public:
   DirectXGISwapChain* getSwapChain()
   {
       return swapChain;
   }
public:
  void deleteViews()
  {
    renderTargetView = NULL;
    depthStencilView = NULL;
  }

  virtual void createViews()
  {
    int width = 0;
    int height = 0;
    validateClientSize(width, height);
    
    try {

      Direct3D10Device*   d3d10Device  = getD3D10Device();
      DirectXGISwapChain* swapChain = getSwapChain();
      //if (d3dDevice && swapChain) {
      d3d10Device ->setOMRenderTargets(0, NULL, NULL);

      //1 Create an instance of Direct3D10RenderTargetView
      Direct3D10Texture2D renderTargetViewTexture(*swapChain); ; 

      renderTargetView = new Direct3D10RenderTargetView(*d3d10Device, renderTargetViewTexture, NULL);

      //2 Create a temporary depthDesc(D3D10Texture2DDesc).
      D3D10Texture2DDesc depthDesc;
      depthDesc.width(width);
      depthDesc.height(height);
      depthDesc.mipLevels(1);
      depthDesc.arraySize(1);
      depthDesc.format(DXGI_FORMAT_D32_FLOAT);
      depthDesc.sampleDescCount(1);
      depthDesc.sampleDescQuality(0);
      depthDesc.usage(D3D10_USAGE_DEFAULT);
      depthDesc.bindFlags(D3D10_BIND_DEPTH_STENCIL);

      //3 Create a temporary depthStencilTexture(Direct3D10Texture2D) from texture2DDesc.
      Direct3D10Texture2D depthStencilTexute(*d3d10Device, depthDesc); 

      //4 Create a temporary depthStencilViewDesc(D3D10DepthStencilViewDesc) 
      D3D10DepthStencilViewDesc depthStencilViewDesc(DXGI_FORMAT_D32_FLOAT, D3D10_DSV_DIMENSION_TEXTURE2D);

      //5 Create an instance of Direct3DDepthStencilView from depthStencilTexture and depthStencilViewDesc
      depthStencilView = new Direct3D10DepthStencilView(*d3d10Device, depthStencilTexute, depthStencilViewDesc);


      ID3D10RenderTargetView* targets[1];
      targets[0]= *renderTargetView; 
      //6 Set renderTargetView and depthStencilView to id3d10Device
      d3d10Device ->setOMRenderTargets(1, targets, *depthStencilView);
      //}
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  virtual void createSwapChain()
  {
    int width = 0;
    int height = 0;
    getClientSize(width, height);
    try {
      Args ar;
      ar.set(XmNbufferCount, 1);
      ar.set(XmNwidth,   width);
      ar.set(XmNheight,  height);
      ar.set(XmNformat, DXGI_FORMAT_R8G8B8A8_UNORM);
      ar.set(XmNrefreshRateNumerator, 60);
      ar.set(XmNrefreshRateDenominator, 1);
      ar.set(XmNsampleDescCount, 1);
      ar.set(XmNsampleDescQuality, 0);
      ar.set(XmNwindowed, TRUE);
      HWND hwnd = getWindow();
      DXGISwapChainDesc swapChainDesc(hwnd, ar);
     
      swapChain    = new DirectXGISwapChain(*dxgiFactory, *d3d10Device, swapChainDesc);

    } catch (Exception& ex) {
       caught(ex);
    }
    
  }
  
  //virtual void initialize()
  virtual void initialize()
  {
    try {
      createSwapChain();
      
      Direct3D10Device* d3d10Device = getD3D10Device();

      //1 Create Direct3D10Font to draw a text string.
      Args args;
      args.set(XmNheight, 70);
      args.set(XmNwidth,   0);
      args.set(XmNweight, FW_NORMAL);
      args.set(XmNmipLevels, 1);
      args.set(XmNitalic, FALSE);
      args.set(XmNcharSet, DEFAULT_CHARSET);
      args.set(XmNoutputPrecision, OUT_DEFAULT_PRECIS);
      args.set(XmNquality, DEFAULT_QUALITY);
      args.set(XmNpitchAndFamily, DEFAULT_PITCH | FF_DONTCARE);
      args.set(XmNfaceName, _T("Meiryo"));
      font = new Direct3DX10Font(*d3d10Device, args);

      //2 Create rendarTargetView and depthStencilView.
      createViews();

    } catch (Exception& ex) {
      caught(ex);
    }

  }

  void  drawText(const TCHAR* text)
  {
    try {
      RECT r;
      getClientRect(&r);
      int width  = r.right - r.left;
      int height = r.bottom - r.top;

      RECT rc;
      font->drawText(NULL, text, -1, &rc, DT_CALCRECT |DT_LEFT, 
          D3DXCOLOR(1.0, 1.0, 1.0, 1.0)); 

      int rcw = rc.right - rc.left;
      int rch = rc.bottom - rc.top;

      r.top    = (height- rch)/2;
      r.bottom = r.bottom -(height -rch)/2;
      r.left   = (width - rcw)/2;
      r.right  = r.right - (width - rcw)/2;

      font -> drawText(NULL, text, -1, &r, DT_LEFT, D3DXCOLOR(1.0, 1.0, 0.0, 1.0)); 

    } catch (Exception& ex) {
      caught(ex);
    }
  }

  virtual void display()
  {
    try {
      Direct3D10Device*  d310Device = getD3D10Device();
      DirectXGISwapChain* swapChain = getSwapChain();
      if (renderTargetView) { // && depthStencilView) {
        renderTargetView -> clear(D3DXCOLOR(0.0f, 0.3f, 0.3f, 0.0f));
        depthStencilView -> clear();

        const TCHAR* text = _T("Hello world. こんにちは世界\nGoodbye world. さようなら世界");
        drawText(text);
      }
      swapChain -> present();

    } catch (Exception& ex) {
      caught(ex);
    }
  }

public:
  /**
   * Constructor
   */
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectX3D10ApplicationView(applet, name, args )
  {
    savedFileName = _T("");
    Args ar;
    ar.set(XmNfilter, 
      _T("Image File(*.bmp,*.jpg,*.png,*.tif,*.ico,*.gif)\0*.bmp;*.jpg;*.png;*.tif;*.ico;*.gif\0JPEG File*.jpg)\0*.jpg\0PNG File(*.png)\0*.png\0Tiff File(*.tif)\0*.tif\0Icon File(*.ico)\0*.ico;\0GIF File(*.gif)\0*.gif;\0"));

    fileDialog.create(this, _T(""), ar);
    postResizeRequest(); 

  }

public:
  ~MainView()
  {
  }

private:

  void resize(int width, int height)
  {
    Direct3D10Device*   device = getD3D10Device();
    DirectXGISwapChain* swapChain = getSwapChain();
   
    if (device == NULL && 
      swapChain == NULL && 
      renderTargetView == NULL &&
      depthStencilView == NULL) {
        return ;
    }
    
    try {
      device ->setOMRenderTargets(0, NULL, NULL);
      // 1 Delete existing rendarTargetView and depthStencilView.
      deleteViews();

      // 2 ResizeBuffers swapChain(IDXGISwapChain)
      if (swapChain) 
      swapChain -> resizeBuffers(width, height); 
      // 3 Recreate rendarTargetView and depthStencilView.
      createViews();
      
      // 4 SetViewPort
      setViewPort(width, height);

    } catch (Exception& ex) {
      caught(ex);
    }
  }  

private:
  void saveTexture(const TCHAR* filename)
  {
    try {
      Direct3D10Resource resource(*renderTargetView);
      resource.saveToFile(filename);

    } catch (Exception& ex) {
      caught(ex);
    }
  }
private:
  void save(Action& action)
  {
    if (savedFileName.getLength() ==0) {
      saveAs(action);
    }
  }

private:
  void saveAs(Action& action)
  {
    Args ar;   
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.save()){

      TCHAR* filename = fileDialog.getFileName();
      savedFileName = filename;

      //TCHAR* ftitle = fileDialog.getFileTitle();
      saveFileFolder(filename);
      saveTexture(filename);
      TCHAR caption[MAX_PATH];
      _stprintf_s(caption, CountOf(caption), _T("%s - %s"), filename, getAppName());
      setText(caption);
    } 
  }
};

}


//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* appClass =  module.getAppName(); 
  try {
    HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);

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

    Args args;
    args.set(XmNwidth,  700);
    args.set(XmNheight, 480);
    MainView imageViewer(applet, appClass, args);
    
    imageViewer.realize();

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


Last modified: 1 Feb 2017

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