SOL9 Sample: Direct3D10FullScreenWithColorChooser

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// 2015/11/26
// 2017/01/28 Updated to use ModuleFileName class and caught macro.

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

#define COMMONCONTROLS_V6

#include <sol/SystemMetrics.h>
#include <sol/ColorChooser.h>
#include <sol/PushButton.h>

#include <sol/direct3d10/DirectX3D10FullScreen.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 DirectX3D10FullScreen {
private:
  SmartPtr<Direct3D10RenderTargetView>  renderTargetView;
  SmartPtr<Direct3D10DepthStencilView>  depthStencilView;
  SmartPtr<Direct3DX10Font>             font;
  SmartPtr<ColorChooser>                chooser;
  SmartPtr<PushButton>                  quitButton;
  D3DXCOLOR                    textColor;

public:
  void deleteViews()
  {
    renderTargetView = NULL;
    depthStencilView = NULL;
  }

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

    try {
      Direct3D10Device*   d3dDevice  = 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   = *renderTargetView; 
      //6 Set renderTargetView and depthStencilView to id3d10Device
      d3d10Device ->setOMRenderTargets(1, &targetView, *depthStencilView);

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

  long picked(Event& event)
  {
    COLORREF rgb = (COLORREF)event.getLParam();
    BYTE r = GetRValue(rgb);
    BYTE g = GetGValue(rgb);
    BYTE b = GetBValue(rgb);
    
    float rf = (float)r/(float)0xff;
    float gf = (float)g/(float)0xff;
    float bf = (float)b/(float)0xff;
    
    textColor = D3DXCOLOR(rf, gf, bf, 1.0f);
    invalidateAll();
    update();
    
    return 0L;
  }
  virtual void initialize()
  {
    try {
      Direct3D10Device* d3d10Device = getD3D10Device();
      DirectXGISwapChain* swapChain = getSwapChain();
      //swapChain ->setFullscreenState(TRUE, NULL);
      //1 Create Direct3D10Font to draw a text string.
      Args args;
      args.set(XmNheight, 120);
      args.set(XmNwidth,   0);
      args.set(XmNweight, FW_NORMAL);
      args.set(XmNmipLevels, 1);
      args.set(XmNitalic, TRUE); //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("Gabriola"));
      font = new Direct3DX10Font(*d3d10Device, args);

      //2 Create rendarTargetView and depthStencilView.
      createViews();
    
      //3 Create the quitButton.
      Args ar;
      quitButton = new PushButton(this, _T("Quit"), ar);
      quitButton -> addCallback(XmNactivateCallback, this,
        (Callback)&MainView::doQuit, NULL);
    
     //4 Create the colorChooser.
      ar.reset();
      ar.set(XmNstyle, WS_VISIBLE);
      chooser = new ColorChooser(this, NULL, ar);
      addEventHandler(WM_COLORCHOOSER_PICKED, this,
        (Handler)&MainView::picked, NULL);
    
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  void  drawText(const TCHAR* text)
  {
    try {
      RECT r;
      getClientRect(&r);
      r.top    = 10; 
      r.left   = 10; 

      font -> drawText(NULL, text, -1, &r, DT_LEFT, textColor); 

    } 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("There is a tide in the affairs of men.Which, taken \nat the flood,")
          _T(" leads on to fortune; Omitted, all the \nvoyage of their life.")
          _T("Is bound in shallows and in miseries.\n\n")
          _T("Shakespeare ");

        drawText(text);
      }
      swapChain -> present();

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

  virtual void doQuit(Action& event)
  {
      swapChain ->setFullscreenState(FALSE, NULL);
      Action action;
      exit(action); 
  }
  
  virtual void keyDown(Event& event)
  {
    WPARAM wparam = event.getWParam();
    if (wparam == VK_ESCAPE) {
      DirectXGISwapChain* swapChain = getSwapChain();
      swapChain ->setFullscreenState(FALSE, NULL);
      Action action;
      exit(action); 
    }
  }
  virtual void resize(int width, int height)
  {
    Direct3D10Device*   device = getD3D10Device();
    DirectXGISwapChain* swapChain = getSwapChain();
    if (device           == NULL ||
        swapChain        == NULL ||
        renderTargetView == NULL ||
        depthStencilView == NULL ||
        quitButton       == NULL ||
        chooser          == NULL) {
        return ;
    }
    
    try {
      int cw = 0;
      int ch = 0;

      if (chooser) {
        cw = chooser->get(XmNwidth);
        ch = chooser->get(XmNheight);
        chooser -> reshape(width - cw-10, 10, cw, ch);
      
        if (quitButton) {
          quitButton -> reshape(width -150, 10 + ch + 30, 120, 30);  
        }
      }

      device ->setOMRenderTargets(0, NULL, NULL);
      // 1 Delete already existing rendarTargetView and depthStencilView.
      deleteViews();

      // 2 ResizeBuffers swapChain(IDXGISwapChain)
      swapChain -> resizeBuffers(width, height); 

      // 3 Call setNoCopyBits to display the colorChooser and quiButton in the fullScreen mode. 
      setNoCopyBits();
      
      // 4 Recreate rendarTargetView and depthStencilView.
      createViews();
      
      // 5 SetViewPort
      setViewPort(width, height);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }  
  
public:
  /**
   * Constructor
   */
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectX3D10FullScreen(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) )
  {
    textColor = D3DXCOLOR(1.0, 1.0, 1.0, 1.0);
    postResizeRequest(); 
  }

public:
  ~MainView()
  {
  }
};

}


//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* appClass =  module.getAppName(); 
  try {
    
    Application applet(appClass, argc, argv);
    SystemMetrics metrics;
    int width = metrics.fullScreenWidth();
    int height = metrics.fullScreenHeight();
    
    const int EXTRA_SIZE = 200;

    Args args;
    args.set(XmNx,  0);
    args.set(XmNy,  0);
    
    args.set(XmNwidth,  width  + EXTRA_SIZE);
    args.set(XmNheight, height + EXTRA_SIZE);
    MainView mainView(applet, appClass, args);
    
    mainView.realize();

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


Last modified: 1 Feb 2017

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