home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5257 / source.7z / x_preview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2012-02-11  |  6.8 KB  |  215 lines

  1. #include "xentax.h"
  2.  
  3. typedef HGLRC HRC;
  4. static HWND window = 0;
  5. static HDC device = 0;
  6. static HRC render = 0;
  7.  
  8. static INT_PTR CALLBACK PreviewProc(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam);
  9. static INT_PTR EvInitDialog(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam);
  10. static INT_PTR EvCommand(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam);
  11. static INT_PTR EvDestroy(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam);
  12. static INT_PTR EvPaint(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam);
  13. static INT_PTR OnOK(HWND dialog, WPARAM wparam, LPARAM lparam);
  14. static INT_PTR OnCancel(HWND dialog, WPARAM wparam, LPARAM lparam);
  15.  
  16. static BOOL InitOpenGL(void);
  17. static BOOL FreeOpenGL(void);
  18. static BOOL IsRenderContextValid(void);
  19. static BOOL RenderFrame(void);
  20. static BOOL SwapBuffers(void);
  21.  
  22. BOOL PreviewDialog(void)
  23. {
  24.  if(DialogBox(GetModuleHandle(NULL), TEXT("IDD_PREVIEW"), NULL, PreviewProc) == TRUE) return TRUE;
  25.  return FALSE;
  26. }
  27.  
  28. BOOL OnExtensionError(LPCSTR error)
  29. {
  30.  // forgive ARB_imaging
  31.  if(strcmp("glColorTable", error) == 0) return TRUE;
  32.  if(strcmp("glColorSubTable", error) == 0) return TRUE;
  33.  if(strcmp("glColorTableParameteriv", error) == 0) return TRUE;
  34.  if(strcmp("glColorTableParameterfv", error) == 0) return TRUE;
  35.  if(strcmp("glCopyColorSubTable", error) == 0) return TRUE;
  36.  if(strcmp("glCopyColorTable", error) == 0) return TRUE;
  37.  if(strcmp("glGetColorTable", error) == 0) return TRUE;
  38.  if(strcmp("glGetColorTableParameterfv", error) == 0) return TRUE;
  39.  if(strcmp("glGetColorTableParameteriv", error) == 0) return TRUE;
  40.  if(strcmp("glHistogram", error) == 0) return TRUE;
  41.  if(strcmp("glResetHistogram", error) == 0) return TRUE;
  42.  if(strcmp("glGetHistogram", error) == 0) return TRUE;
  43.  if(strcmp("glGetHistogramParameterfv", error) == 0) return TRUE;
  44.  if(strcmp("glGetHistogramParameteriv", error) == 0) return TRUE;
  45.  if(strcmp("glMinmax", error) == 0) return TRUE;
  46.  if(strcmp("glResetMinmax", error) == 0) return TRUE;
  47.  if(strcmp("glGetMinmaxParameterfv", error) == 0) return TRUE;
  48.  if(strcmp("glGetMinmaxParameteriv", error) == 0) return TRUE;
  49.  if(strcmp("glConvolutionFilter1D", error) == 0) return TRUE;
  50.  if(strcmp("glConvolutionFilter2D", error) == 0) return TRUE;
  51.  if(strcmp("glConvolutionParameterf", error) == 0) return TRUE;
  52.  if(strcmp("glConvolutionParameterfv", error) == 0) return TRUE;
  53.  if(strcmp("glConvolutionParameteri", error) == 0) return TRUE;
  54.  if(strcmp("glConvolutionParameteriv", error) == 0) return TRUE;
  55.  if(strcmp("glCopyConvolutionFilter1D", error) == 0) return TRUE;
  56.  if(strcmp("glCopyConvolutionFilter2D", error) == 0) return TRUE;
  57.  if(strcmp("glGetConvolutionFilter", error) == 0) return TRUE;
  58.  if(strcmp("glGetConvolutionParameterfv", error) == 0) return TRUE;
  59.  if(strcmp("glGetConvolutionParameteriv", error) == 0) return TRUE;
  60.  if(strcmp("glSeparableFilter2D", error) == 0) return TRUE;
  61.  if(strcmp("glGetSeparableFilter", error) == 0) return TRUE;
  62.  if(strcmp("glGetMinmax", error) == 0) return TRUE;
  63.  return FALSE;
  64. }
  65.  
  66. INT_PTR CALLBACK PreviewProc(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam)
  67. {
  68.  switch(message) {
  69.    case(WM_INITDIALOG) : return EvInitDialog(dialog, message, wparam, lparam);
  70.    case(WM_COMMAND) : return EvCommand(dialog, message, wparam, lparam);
  71.    case(WM_DESTROY) : return EvDestroy(dialog, message, wparam, lparam);
  72.    case(WM_PAINT) : return EvPaint(dialog, message, wparam, lparam);
  73.   }
  74.  return FALSE;
  75. }
  76.  
  77. INT_PTR EvInitDialog(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam)
  78. {
  79.  window = dialog;
  80.  if(InitOpenGL() == FALSE) {
  81.     EndDialog(dialog, IDCANCEL);
  82.    }
  83.  return TRUE;
  84. }
  85.  
  86. INT_PTR EvCommand(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam)
  87. {
  88.  int cmd = LOWORD(wparam);
  89.  if(cmd == IDOK) return OnOK(dialog, wparam, lparam);
  90.  else if(cmd == IDCANCEL) return OnCancel(dialog, wparam, lparam);
  91.  return FALSE;
  92. }
  93.  
  94. INT_PTR EvDestroy(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam)
  95. {
  96.  FreeOpenGL();
  97.  window = NULL;
  98.  return TRUE;
  99. }
  100.  
  101. INT_PTR EvPaint(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam)
  102. {
  103.  RenderFrame();
  104.  SwapBuffers();
  105.  ValidateRect(dialog, NULL);
  106.  return TRUE;
  107. }
  108.  
  109. INT_PTR OnOK(HWND dialog, WPARAM wparam, LPARAM lparam)
  110. {
  111.  EndDialog(dialog, IDOK);
  112.  return TRUE;
  113. }
  114.  
  115. INT_PTR OnCancel(HWND dialog, WPARAM wparam, LPARAM lparam)
  116. {
  117.  EndDialog(dialog, IDCANCEL);
  118.  return TRUE;
  119. }
  120.  
  121. BOOL InitOpenGL(void)
  122. {
  123.  // validate window
  124.  if(!window || !IsWindow(window)) {
  125.     MessageBox(window, TEXT("Invalid client window."), TEXT("Error"), MB_ICONSTOP);
  126.     return FALSE;
  127.    }
  128.  
  129.  // create device context
  130.  device = GetDC(window);
  131.  if(!device) {
  132.     MessageBox(window, TEXT("Failed to create device context."), TEXT("Error"), MB_ICONSTOP);
  133.     return FALSE;
  134.    }
  135.  
  136.  // initialize device pixel format
  137.  PIXELFORMATDESCRIPTOR pfd;
  138.  ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
  139.  pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
  140.  pfd.nVersion   = 1;
  141.  pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  142.  pfd.iPixelType = PFD_TYPE_RGBA;
  143.  pfd.cColorBits = 32;
  144.  pfd.cDepthBits = 32;
  145.  pfd.iLayerType = PFD_MAIN_PLANE;
  146.  
  147.  // choose and set pixel format
  148.  int pixel_format = ChoosePixelFormat(device, &pfd);
  149.  if(pixel_format == 0) return FALSE;
  150.  if(!SetPixelFormat(device, pixel_format, &pfd)) return FALSE;
  151.  
  152.  // create render context
  153.  render = wglCreateContextEx(device);
  154.  if(!render) {
  155.     ReleaseDC(window, device);
  156.     device = 0;
  157.     MessageBox(window, TEXT("Failed to create render context."), TEXT("Error"), MB_ICONSTOP);
  158.     return FALSE;
  159.    }
  160.  
  161.  // make render context current
  162.  wglMakeCurrent(device, render);
  163.  
  164.  // load extensions
  165.  wglLoadExtensions(OnExtensionError);
  166.  return TRUE;
  167. }
  168.  
  169. BOOL FreeOpenGL(void)
  170. {
  171.  // validate window
  172.  if(!window || !IsWindow(window)) return FALSE;
  173.  
  174.  // delete render context
  175.  if(render) {
  176.     if(wglMakeCurrent(NULL, NULL) == FALSE) {
  177.        MessageBox(window, TEXT("Failed to release OpenGL rendering context."), TEXT("Error"), MB_ICONSTOP);
  178.        return FALSE;
  179.       }
  180.     if(wglDeleteContext(render) == FALSE) {
  181.        MessageBox(window, TEXT("Failed to delete OpenGL rendering context."), TEXT("Error"), MB_ICONSTOP);
  182.        return FALSE;
  183.       }
  184.     render = 0;
  185.    }
  186.  
  187.  // delete device context
  188.  if(device) {
  189.     if(ReleaseDC(window, device) == 0) {
  190.        MessageBox(window, TEXT("Failed to release device context."), TEXT("Error"), MB_ICONSTOP);
  191.        return FALSE;
  192.       }
  193.     device = 0;
  194.    }
  195.  
  196.  return TRUE;
  197. }
  198.  
  199. BOOL IsRenderContextValid(void)
  200. {
  201.  return (render == 0 ? FALSE : TRUE);
  202. }
  203.  
  204. BOOL RenderFrame(void)
  205. {
  206.  if(!IsRenderContextValid()) return FALSE;
  207.  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  208.  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  209.  return TRUE;
  210. }
  211.  
  212. BOOL SwapBuffers(void)
  213. {
  214.  return ::SwapBuffers(device);
  215. }