VIZ++ Class: OpenGLPixelFormat

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * OpenGLPixelFormat.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2015/07/22

#pragma once

#include <viz++/Object.h>
#include <viz++/ClientDC.h>
#include <viz++/opengl/OpenGLObject.h>

namespace VIZ {

class OpenGLPixelFormat :public OpenGLObject {
protected:
  PIXELFORMATDESCRIPTOR pfd;
  HDC hDC;

protected:
  static const DWORD DOUBLE_BUFFER = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
  static const DWORD SINGLE_BUFFER = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; 

public:
  OpenGLPixelFormat(ClientDC* clientDC, DWORD flags = DOUBLE_BUFFER, BYTE pixelType=PFD_TYPE_RGBA, 
     BYTE colorBits=24, BYTE depthBits=16, BYTE alphaBits = 8)
  :hDC(NULL)
  {
    if (clientDC == NULL) {
      throw IException("Invalid argument: ClientDC is NULL");
    }
    hDC = clientDC -> get();
    
    memset(&pfd, 0, sizeof(pfd));
    pfd.nSize      = sizeof(pfd);
    pfd.nVersion   = 1;
    pfd.dwFlags    = flags; //FLAGS; //PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = pixelType;  //PFD_TYPE_RGBA;
    pfd.cColorBits = colorBits;  //24;
    pfd.cDepthBits = depthBits;  //16;
    pfd.cAlphaBits = alphaBits;  //8;
    pfd.iLayerType = PFD_MAIN_PLANE;  
  }

  void pixelType(BYTE pixelType)
  {
    pfd.iPixelType = pixelType; //PFD_TYPE_RGBA; 
  }

  void depthBits(BYTE bits)
  {
    pfd.cDepthBits = bits; //16;
  }

  void alphaBits(BYTE bits)
  {
    pfd.cAlphaBits = bits; //8;
  }

  void setLayerType(BYTE layerType)
  {
     pfd.iLayerType = layerType;
  }

  BOOL setPixelFormat()
  {
     int pixelFormat = chooseFormat();
     return setPixelFormat(pixelFormat);
  }


  int chooseFormat()
  {
    int  pixelFormat = ChoosePixelFormat(hDC, &pfd);
    if (pixelFormat == 0) {
      throw IException("Failed to ChoosePixel: Error(%s)", errorString());
    
    }
    return pixelFormat;
  }

  BOOL setPixelFormat(int pixelFormat)
  {
    BOOL rc = SetPixelFormat(hDC, pixelFormat, &pfd);
    if (rc == FALSE) {
      throw IException("Failed to SetPixelFormat: Error(%s)", errorString());
    }
    return rc;
  }

  int getPixelFormat()
  {
    return GetPixelFormat(hDC);
  }

  int desribePixelFormat(int pixelFormat, PIXELFORMATDESCRIPTOR& pfd)
  {
    BOOL rc = DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
    if (rc == FALSE) {
      throw IException("Failed to DescribePixelFormat: Error(%s)", errorString());
    }
    return rc;
  }

};

}

Last modified: 10 Feb 2017

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