home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / lightprp.c_ / lightprp.bin
Text File  |  1995-11-14  |  19KB  |  441 lines

  1. /**********************************************************************
  2.  *
  3.  * File :     lightprp.c
  4.  *
  5.  * Abstract : Light properties dialog for the simple RenderWare
  6.  *            application RWVIEW.EXE.
  7.  *
  8.  *            This application had been written to be compatible with
  9.  *            both the fixed and floating-point versions of the
  10.  *            RenderWare library, i.e., it uses the macros CREAL,
  11.  *            INT2REAL, RAdd, RDiv, RSub etc. If your application is
  12.  *            intended for the floating-point version of the library
  13.  *            only these macros are not necessary.
  14.  *
  15.  *            Please note that this application is intended for
  16.  *            demonstration purposes only. No support will be
  17.  *            provided for this code and it comes with no warranty.
  18.  *
  19.  **********************************************************************
  20.  *
  21.  * This file is a product of Criterion Software Ltd.
  22.  *
  23.  * This file is provided as is with no warranties of any kind and is
  24.  * provided without any obligation on Criterion Software Ltd. or
  25.  * Canon Inc. to assist in its use or modification.
  26.  *
  27.  * Criterion Software Ltd. will not, under any
  28.  * circumstances, be liable for any lost revenue or other damages arising
  29.  * from the use of this file.
  30.  *
  31.  * Copyright (c) 1994, 1995 Criterion Software Ltd.
  32.  * All Rights Reserved.
  33.  *
  34.  * RenderWare is a trademark of Canon Inc.
  35.  *
  36.  **********************************************************************/
  37.  
  38. /**********************************************************************
  39.  *
  40.  * Header files.
  41.  *
  42.  **********************************************************************/
  43.  
  44. #include <windows.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48.  
  49. #include <rwlib.h>
  50. #include <rwwin31.h>
  51.  
  52. #include "resource.h"
  53.  
  54. #include "common.h"
  55. #include "lightprp.h"
  56. #include "object.h"
  57. #include "color.h"
  58.  
  59. /**********************************************************************
  60.  *
  61.  * Functions.
  62.  *
  63.  **********************************************************************/
  64.  
  65. /**********************************************************************/
  66.  
  67. /*
  68.  * The light properties dialog message procedure.
  69.  */
  70. BOOL CALLBACK
  71. LightPropsDlgProc(HWND dialog, UINT message, WPARAM wParam, LPARAM lParam)
  72. {
  73.     static RwLight     *Light;
  74.     static RwInt32      RenderDepth;
  75.     static HPALETTE     Palette = (HPALETTE)0;
  76.     static RwRGBColor   Color;
  77.  
  78.     DRAWITEMSTRUCT FAR *drawItem;
  79.     RwBool              isPaletteBased;    
  80.     RwReal              bright;
  81.     RwReal              coneAngle;
  82.     HWND                scrollBar;
  83.     int                 pos;
  84.     int                 newPos;
  85.     int                 code;
  86.     int                 id;
  87.     char                buffer[10];
  88.     HPALETTE            oldPalette;
  89.     HPEN                pen;
  90.     HPEN                oldPen;
  91.     HBRUSH              brush;
  92.     HBRUSH              oldBrush;
  93.     FARPROC             dialogProc;
  94.     
  95.     switch (message)
  96.     {
  97.         case WM_INITDIALOG:
  98.             /*
  99.              * The parameter given to DialogBoxParam() is the light we are modifying.
  100.              */
  101.             Light = (RwLight *)lParam;
  102.             
  103.             /*
  104.              * If we are running on a palette based display we will
  105.              * select and realize RenderWare's own color palette when
  106.              * drawing the preview button to give a realistic picture
  107.              * of the available colors. So cache the RenderWare palette
  108.              * if we are using one.
  109.              */
  110.             RwGetDeviceInfo(rwPALETTEBASED, &isPaletteBased, sizeof(isPaletteBased));
  111.             if (isPaletteBased)
  112.                 RwGetDeviceInfo(rwPALETTE, &Palette, sizeof(Palette));
  113.             else
  114.                 Palette = NULL;
  115.  
  116.             /*
  117.              * In RenderWare V1.4 colored light sources are only available when
  118.              * performing 16-bit rendering, So we determine if we are performing
  119.              * 16-bit rendering and modify the dialog appropriately.
  120.              */
  121.             RwGetDeviceInfo(rwRENDERDEPTH, &RenderDepth, sizeof(RenderDepth));
  122.             if (RenderDepth == 16L)
  123.             {
  124.                 /*
  125.                  * We have colored light sources. Hide the brightness scrollbar
  126.                  * and show the color button.
  127.                  */
  128.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTLABEL),  SW_HIDE);
  129.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTEDIT),   SW_HIDE);
  130.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SW_HIDE);
  131.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLOR),        SW_SHOW);
  132.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLORPREVIEW), SW_SHOW);
  133.                 
  134.                 /*
  135.                  * Get the current light color.
  136.                  */
  137.                 RwGetLightColor(Light, &Color);
  138.             }
  139.             else
  140.             {
  141.                 /*
  142.                  * We don't have colored light sources. Show the brightness
  143.                  * slider, update it and hide the color button.
  144.                  */
  145.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLOR),        SW_HIDE);
  146.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLORPREVIEW), SW_HIDE);
  147.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTLABEL),  SW_SHOW);
  148.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTEDIT),   SW_SHOW);
  149.                 ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SW_SHOW);
  150.                 bright = RwGetLightBrightness(Light);
  151.                 SetScrollRange(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL, 0, 255, FALSE);
  152.                 SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL, REALTOBYTE(bright), FALSE);
  153.                 sprintf(buffer, "%d", (int)REALTOBYTE(bright));
  154.                 SetDlgItemText(dialog, IDC_LIGHTPROPS_BRIGHTEDIT, buffer);
  155.             }
  156.             
  157.             /*
  158.              * Display the light type and initialize the cone angle if necessary.
  159.              */
  160.             switch (RwGetLightType(Light))
  161.             {
  162.                 case rwDIRECTIONAL:
  163.                     SetDlgItemText(dialog, IDC_LIGHTPROPS_TYPE, "Directional");
  164.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONELABEL),  FALSE);
  165.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONEEDIT),   FALSE);
  166.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), FALSE);
  167.                     coneAngle = CREAL(0.0);
  168.                     break;
  169.                 case rwPOINT:
  170.                     SetDlgItemText(dialog, IDC_LIGHTPROPS_TYPE, "Point");
  171.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONELABEL),  FALSE);
  172.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONEEDIT),   FALSE);
  173.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), FALSE);
  174.                     coneAngle = RwGetLightConeAngle(Light);
  175.                     break;
  176.                 case rwCONICAL:
  177.                     SetDlgItemText(dialog, IDC_LIGHTPROPS_TYPE, "Conical");
  178.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONELABEL),  TRUE);
  179.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONEEDIT),   TRUE);
  180.                     EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), TRUE);
  181.                     coneAngle = RwGetLightConeAngle(Light);
  182.                     break;
  183.             }
  184.             SetScrollRange(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL,
  185.                            0, 45, FALSE);
  186.             SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL,
  187.                          (int)REAL2INT(coneAngle), TRUE);
  188.             sprintf(buffer, "%d", (int)REAL2INT(coneAngle));
  189.             SetDlgItemText(dialog, IDC_LIGHTPROPS_CONEEDIT, buffer);
  190.             return FALSE;
  191.             
  192.         case WM_HSCROLL:
  193. #if defined(WIN32)
  194.             code      = (int)LOWORD(wParam);
  195.             newPos    = (int)HIWORD(wParam);
  196.             scrollBar = (HWND)lParam;
  197.             id        = (int)GetWindowLong(scrollBar, GWL_ID);
  198. #else
  199.             code      = (int)wParam;
  200.             newPos    = (int)LOWORD(lParam);
  201.             scrollBar = (HWND)HIWORD(lParam);
  202.             id        = (int)GetWindowWord(scrollBar, GWW_ID);
  203. #endif            
  204.  
  205.             switch (id)
  206.             {
  207.                 case IDC_LIGHTPROPS_BRIGHTSLIDER:
  208.                     pos = GetScrollPos(scrollBar, SB_CTL);
  209.                     switch (code)
  210.                     {
  211.                         case SB_BOTTOM:        pos =  255;    break;
  212.                         case SB_LINEDOWN:      pos += 1;      break;
  213.                         case SB_LINEUP:        pos -= 1;      break;
  214.                         case SB_PAGEDOWN:      pos += 16;     break;
  215.                         case SB_PAGEUP:        pos -= 16;     break;
  216.                         case SB_THUMBTRACK:    pos =  newPos; break;
  217.                         case SB_THUMBPOSITION: pos =  newPos; break;
  218.                         case SB_TOP:           pos =  0;      break;
  219.                     }
  220.                     /*
  221.                      * Clamp the new value.
  222.                      */
  223.                     if (pos < 0)
  224.                         pos = 0;
  225.                     else if (pos > 255)
  226.                         pos = 255;
  227.                         
  228.                     /*
  229.                      * Update the scrollbar and edit control.
  230.                      */
  231.                     SetScrollPos(scrollBar, SB_CTL, pos, TRUE);
  232.                     sprintf(buffer, "%d", pos);
  233.                     SetDlgItemText(dialog, IDC_LIGHTPROPS_BRIGHTEDIT, buffer);
  234.                     break;
  235.  
  236.                 case IDC_LIGHTPROPS_CONESLIDER:
  237.                     pos = GetScrollPos(scrollBar, SB_CTL);
  238.                     switch (code)
  239.                     {
  240.                         case SB_BOTTOM:        pos =  45;     break;
  241.                         case SB_LINEDOWN:      pos += 1;      break;
  242.                         case SB_LINEUP:        pos -= 1;      break;
  243.                         case SB_PAGEDOWN:      pos += 5;      break;
  244.                         case SB_PAGEUP:        pos -= 5;      break;
  245.                         case SB_THUMBTRACK:    pos =  newPos; break;
  246.                         case SB_THUMBPOSITION: pos =  newPos; break;
  247.                         case SB_TOP:           pos =  0;      break;
  248.                     }
  249.                     /*
  250.                      * Clamp the new value.
  251.                      */
  252.                     if (pos < 0)
  253.                         pos = 0;
  254.                     else if (pos > 45)
  255.                         pos = 45;
  256.  
  257.                     /*
  258.                      * Update the scrollbar and edit control.
  259.                      */
  260.                     SetScrollPos(scrollBar, SB_CTL, pos, TRUE);
  261.                     sprintf(buffer, "%d", pos);
  262.                     SetDlgItemText(dialog, IDC_LIGHTPROPS_CONEEDIT, buffer);
  263.                     break;
  264.             }
  265.             return TRUE;
  266.         
  267.         case WM_DRAWITEM:
  268. #if defined(__WINDOWS_386__)
  269.             drawItem = (DRAWITEMSTRUCT FAR *)MK_FP32((void*)lParam);
  270. #else
  271.             drawItem = (DRAWITEMSTRUCT FAR *)lParam;
  272. #endif
  273.             
  274.             switch (wParam)
  275.             {
  276.                 case IDC_LIGHTPROPS_COLORPREVIEW:
  277.                     /*
  278.                      * If we have a palette based output device then
  279.                      * select and realize the palette (we realize
  280.                      * it as a background palette as we don't want to
  281.                      * be too aggresive about getting the colors we ask
  282.                      * for - it is only for feedback after all).
  283.                      */
  284.                     if (Palette != (HPALETTE)0)
  285.                     {
  286.                         oldPalette = SelectPalette(drawItem->hDC, Palette, TRUE);
  287.                         RealizePalette(drawItem->hDC);
  288.                     }
  289.                                          
  290.                     /*
  291.                      * Draw a rectangle framed by a black line and filled
  292.                      * with the current color.
  293.                      */
  294.                     pen      = GetStockObject(BLACK_PEN);
  295.                     oldPen   = SelectObject(drawItem->hDC, pen);
  296.                     brush    = CreateSolidBrush(PALETTERGB(REALTOBYTE(Color.r), 
  297.                                                            REALTOBYTE(Color.g), 
  298.                                                            REALTOBYTE(Color.b)));
  299.                     oldBrush = SelectObject(drawItem->hDC, brush);
  300.                     Rectangle(drawItem->hDC, drawItem->rcItem.left,
  301.                                              drawItem->rcItem.top,
  302.                                              drawItem->rcItem.right,
  303.                                              drawItem->rcItem.bottom);
  304.                     SelectObject(drawItem->hDC, oldBrush);
  305.                     DeleteObject(brush);
  306.                     SelectObject(drawItem->hDC, oldPen);
  307.  
  308.                     if (Palette != (HPALETTE)0)
  309.                     {
  310.                         SelectPalette(drawItem->hDC, oldPalette, TRUE);
  311.                         RealizePalette(drawItem->hDC);
  312.                     }
  313.  
  314.                     break;
  315.             }
  316.             return TRUE;
  317.  
  318.         case WM_COMMAND:
  319.             switch (wParam)
  320.             {
  321.                 case IDC_LIGHTPROPS_BRIGHTEDIT:
  322.                     /*
  323.                      * For the edit controls we only take any notice when the
  324.                      * control loses the input focus.
  325.                      */
  326.                     if (HIWORD(lParam) == EN_KILLFOCUS)
  327.                     {
  328.                         /*
  329.                          * Get the edit control's text.
  330.                          */
  331.                         GetDlgItemText(dialog, wParam, buffer, sizeof(buffer));
  332.                         
  333.                         /*
  334.                          * Ensure its a valid value.
  335.                          */
  336.                         if ((sscanf(buffer, "%d", &pos) == 1) && (pos >= 0) && (pos <= 255))
  337.                         {
  338.                             /*
  339.                              * Update the slider.
  340.                              */
  341.                             SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL, pos, TRUE);
  342.                         }
  343.                         else
  344.                         {
  345.                             /*
  346.                              * Restore the current value.
  347.                              */
  348.                             pos = GetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL);
  349.                             sprintf(buffer, "%d", pos);
  350.                             SetDlgItemText(dialog, wParam, buffer);
  351.                         }
  352.                     }
  353.                     break;
  354.  
  355.                 case IDC_LIGHTPROPS_CONEEDIT:
  356.                     /*
  357.                      * For the edit controls we only take any notice when the
  358.                      * control loses the input focus.
  359.                      */
  360.                     if (HIWORD(lParam) == EN_KILLFOCUS)
  361.                     {
  362.                         /*
  363.                          * Get the edit control's text.
  364.                          */
  365.                         GetDlgItemText(dialog, wParam, buffer, sizeof(buffer));
  366.                         
  367.                         /*
  368.                          * Ensure its a valid value.
  369.                          */
  370.                         if ((sscanf(buffer, "%d", &pos) == 1) && (pos >= 0) && (pos <= 45))
  371.                         {
  372.                             /*
  373.                              * Update the slider.
  374.                              */
  375.                             SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL, pos, TRUE);
  376.                         }
  377.                         else
  378.                         {
  379.                             /*
  380.                              * Restore the current value.
  381.                              */
  382.                             pos = GetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL);
  383.                             sprintf(buffer, "%d", pos);
  384.                             SetDlgItemText(dialog, wParam, buffer);
  385.                         }
  386.                     }
  387.                     break;
  388.                     
  389.                 case IDC_LIGHTPROPS_COLOR:
  390.                     /*
  391.                      * Use the little color picked dialog to pick a new light color.
  392.                      */
  393.                     dialogProc = MakeProcInstance(ColorDlgProc, AppInstance);
  394.                     if (DialogBoxParam(AppInstance, MAKEINTRESOURCE(IDD_COLORPICKER),
  395.                         dialog, dialogProc, (LPARAM)&Color) == IDOK)
  396.                         /*
  397.                          * Force a redraw of the color preview button.
  398.                          */
  399.                         InvalidateRect(GetDlgItem(dialog,
  400.                                                   IDC_LIGHTPROPS_COLORPREVIEW), NULL, FALSE);
  401.                     FreeProcInstance(dialogProc);
  402.                     break;
  403.  
  404.                 case IDOK:
  405.                     /*
  406.                      * Update the color/brightness of the light.
  407.                      */
  408.                     if (RenderDepth == 16L)
  409.                     {
  410.                         SetLightObjColor(Light, Color.r, Color.g, Color.b);
  411.                     }
  412.                     else
  413.                     {
  414.                         bright = BYTETOREAL(GetScrollPos(GetDlgItem(dialog,
  415.                                             IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL));
  416.                         SetLightObjBrightness(Light, bright);
  417.                     }
  418.                     
  419.                     /*
  420.                      * Update the cone angle (if its a conical light source).
  421.                      */
  422.                     if (RwGetLightType(Light) == rwCONICAL)
  423.                     {
  424.                         coneAngle = INT2REAL(GetScrollPos(GetDlgItem(dialog,
  425.                                              IDC_LIGHTPROPS_CONESLIDER), SB_CTL));
  426.                         RwSetLightConeAngle(Light, coneAngle);
  427.                     }
  428.                     EndDialog(dialog, IDOK);
  429.                     break; 
  430.                     
  431.                 case IDCANCEL:
  432.                     EndDialog(dialog, IDCANCEL);
  433.                     break;
  434.             }
  435.             return TRUE;
  436.     }
  437.     return FALSE;
  438. }
  439.  
  440. /**********************************************************************/
  441.