SOL9 Sample: Direct3DX10ImageSprite

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


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

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

#define COMMONCONTROLS_V6

#include <sol/direct3d10/DirectX3D10MainView.h>
#include <sol/direct3d10/DirectX3D10View.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 <sol/direct3d10/Direct3D10ShaderResourceView.h>


#include <sol/direct3d10/Direct3D10Resource.h>
#include <sol/direct3d10/Direct3DX10Sprite.h>

#include <sol/direct3d10/Direct3D10BlendState.h>

#include "resource.h"

namespace SOL {
  
class MainView :public DirectX3D10MainView {
private:
  ///////////////////////////////////////////
  // Inner class starts.
  class SimpleView : public DirectX3D10View {
  private:
    SmartPtr<Direct3D10RenderTargetView>   renderTargetView;
    SmartPtr<Direct3D10DepthStencilView>   depthStencilView;

    SmartPtr<Direct3D10ShaderResourceView> shaderResourceView[1];

    SmartPtr<Direct3D10BlendState>         blendState;
    SmartPtr<Direct3DX10Sprite>            sprite;

    StringT<TCHAR>         directory;
  
  public:
    void deleteViews()
    {
      Direct3D10Device*   d3d10Device = getD3D10Device();
      d3d10Device ->setOMRenderTargets(0, NULL, NULL);

      renderTargetView = NULL;
      depthStencilView = NULL;
    }

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

      try {
        Direct3D10Device*   d3d10Device  = getD3D10Device();
        DirectXGISwapChain* swapChain = getSwapChain();

        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* targetView[1];
        targetView[0] = *renderTargetView; 
        //6 Set renderTargetView and depthStencilView to id3d10Device
        d3d10Device ->setOMRenderTargets(1, targetView, *depthStencilView);

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

    virtual void initialize()
    {
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();
        //1 Create a sprite.
        sprite = new Direct3DX10Sprite(*d3d10Device, 1);

        //2 Create a blendState.
        D3D10_BLEND_DESC blendDesc;
        ZeroMemory(&blendDesc, sizeof(D3D10_BLEND_DESC));
        blendDesc.AlphaToCoverageEnable = FALSE;
        blendDesc.BlendEnable[0] = TRUE;
        blendDesc.SrcBlend         = D3D10_BLEND_SRC_ALPHA;
        blendDesc.DestBlend        = D3D10_BLEND_INV_SRC_ALPHA;
        blendDesc.BlendOp          = D3D10_BLEND_OP_ADD;
        blendDesc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;

        blendDesc.SrcBlendAlpha   = D3D10_BLEND_ONE;
        blendDesc.DestBlendAlpha  = D3D10_BLEND_ZERO;
        blendDesc.BlendOpAlpha    = D3D10_BLEND_OP_ADD;
        blendState = new Direct3D10BlendState(*d3d10Device, &blendDesc);

        //3 Create a shaderResourceView.
      
        TCHAR fullpath[MAX_PATH];
        _stprintf_s(fullpath, MAX_PATH, _T("%s\\..\\images\\%s"), (const TCHAR*)directory, _T("flower.png"));
        shaderResourceView[0] = new Direct3D10ShaderResourceView(*d3d10Device, fullpath, NULL);
        ID3D10ShaderResourceView *views[1];
        views[0] = *shaderResourceView[0];
        d3d10Device ->setPSShaderResources( 0,  1, views);

        //4 Create rendarTargetView, depthStencilView, and shaderResourceView. 
        createViews();

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

    virtual void display()
    {
      try {
        int width  = 0;
        int height = 0;
        getClientSize(width, height);

        Direct3D10Device*  d3d10Device = getD3D10Device();
        DirectXGISwapChain* swapChain = getSwapChain();
        if (renderTargetView && depthStencilView) {
          renderTargetView -> clear( D3DXCOLOR(0.0f, 0.3f, 0.3f, 0.0));
          depthStencilView -> clear();
        }
  
        D3DXMATRIX matrix;
        D3DXMatrixIdentity(&matrix);
        D3DXMatrixScaling(&matrix, 1.5f, 1.5f, 1.0f);
   
        D3DX10_SPRITE sprites[1];
        //sprite->setViewTransform(&matrix);

        d3d10Device->setOMBlendState(*blendState, D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f), 0xffffffff);

        for (int i = 0; i<1; i++) {
          memset(&sprites[i], 0, sizeof(D3DX10_SPRITE));
     
          sprites[i].matWorld = matrix;
          sprites[i].TexCoord.x = 0.0; 
          sprites[i].TexCoord.y = 0.0f;  
          sprites[i].TexSize.x  = 1.0f;
          sprites[i].TexSize.y  = 1.0f; 

          sprites[i].ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
          sprites[i].pTexture = *shaderResourceView[i];
          sprites[i].TextureIndex = i;
        }
      
        sprite -> begin(D3DX10_SPRITE_SORT_DEPTH_BACK_TO_FRONT);// D3DX10_SPRITE_SORT_TEXTURE  );
        sprite -> drawSpritesImmediate(sprites, 1, 0, 0);
        sprite -> flush();
        sprite -> end();

        swapChain -> present();

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

  public:
    // Constructor
    SimpleView(DirectX3D10MainView* parent, const TCHAR* name, Args& args)
    :DirectX3D10View(parent, name, args )
    {
      directory = (const TCHAR*)args.get(XmNapplicationDirectory);
    
      postResizeRequest(); 
    }

  public:
    ~SimpleView()
    {
    }

  private:
    void resize(int width, int height)
    {
      Direct3D10Device*   d3d10Device = getD3D10Device();
      DirectXGISwapChain* swapChain = getSwapChain();
      if (d3d10Device      == NULL &&
          swapChain        == NULL && 
          renderTargetView == NULL &&
          depthStencilView == NULL &&
          shaderResourceView) {
        return ;
      }
    
      try {
        // 1 Delete existing rendarTargetView and depthStencilView.
        deleteViews();
      
        // 2 ResizeBuffers swapChain(IDXGISwapChain)
        swapChain -> resizeBuffers(width, height); 
     
        // 3 Recreate rendarTargetView and depthStencilView.
        createViews();
          
        // 4 SetViewPort
        setViewPort(width, height);

      } catch (Exception& ex) {
        caught(ex);
      }
    } 
  };
  /////////////////////////////////////////////////
  // Inner class ends.
  
private:
  SmartPtr<SimpleView> view;
  
public:
  /**
   * Constructor
   */
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectX3D10MainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN|WS_CLIPSIBLINGS) )
  {
    const TCHAR* directory = (const TCHAR*)args.get(XmNapplicationDirectory);
    restorePlacement();
    Args ar;
    int width  = 0;
    int height = 0;
    getClientSize(width, height);
    ar.set(XmNwidth, width);
    ar.set(XmNheight, height);
    ar.set(XmNapplicationDirectory, directory);
    view = new SimpleView(this, _T(""), ar);
    
    postResizeRequest(); 
  }

public:
  ~MainView()
  {
  }

private:
  void resize(int width, int height)
  {
    if (view != nullptr) {
      view -> reshape(0, 0, width, height);
      view -> postResizeRequest(width, height);
    }
  }  
};

}


//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* directory = module.getDirectory();
  const TCHAR* appClass =  module.getAppName(); 
  
  try {
    Application applet(appClass, argc, argv);

    Args args;
    args.set(XmNapplicationDirectory, directory);
    args.set(XmNwidth, 640);
    args.set(XmNheight, 480);
    MainView mainv(applet, appClass, args);
    
    mainv.realize();

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




Last modified: 1 Feb 2017

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