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
Wrap
Text File
|
1995-11-14
|
19KB
|
441 lines
/**********************************************************************
*
* File : lightprp.c
*
* Abstract : Light properties dialog for the simple RenderWare
* application RWVIEW.EXE.
*
* This application had been written to be compatible with
* both the fixed and floating-point versions of the
* RenderWare library, i.e., it uses the macros CREAL,
* INT2REAL, RAdd, RDiv, RSub etc. If your application is
* intended for the floating-point version of the library
* only these macros are not necessary.
*
* Please note that this application is intended for
* demonstration purposes only. No support will be
* provided for this code and it comes with no warranty.
*
**********************************************************************
*
* This file is a product of Criterion Software Ltd.
*
* This file is provided as is with no warranties of any kind and is
* provided without any obligation on Criterion Software Ltd. or
* Canon Inc. to assist in its use or modification.
*
* Criterion Software Ltd. will not, under any
* circumstances, be liable for any lost revenue or other damages arising
* from the use of this file.
*
* Copyright (c) 1994, 1995 Criterion Software Ltd.
* All Rights Reserved.
*
* RenderWare is a trademark of Canon Inc.
*
**********************************************************************/
/**********************************************************************
*
* Header files.
*
**********************************************************************/
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rwlib.h>
#include <rwwin31.h>
#include "resource.h"
#include "common.h"
#include "lightprp.h"
#include "object.h"
#include "color.h"
/**********************************************************************
*
* Functions.
*
**********************************************************************/
/**********************************************************************/
/*
* The light properties dialog message procedure.
*/
BOOL CALLBACK
LightPropsDlgProc(HWND dialog, UINT message, WPARAM wParam, LPARAM lParam)
{
static RwLight *Light;
static RwInt32 RenderDepth;
static HPALETTE Palette = (HPALETTE)0;
static RwRGBColor Color;
DRAWITEMSTRUCT FAR *drawItem;
RwBool isPaletteBased;
RwReal bright;
RwReal coneAngle;
HWND scrollBar;
int pos;
int newPos;
int code;
int id;
char buffer[10];
HPALETTE oldPalette;
HPEN pen;
HPEN oldPen;
HBRUSH brush;
HBRUSH oldBrush;
FARPROC dialogProc;
switch (message)
{
case WM_INITDIALOG:
/*
* The parameter given to DialogBoxParam() is the light we are modifying.
*/
Light = (RwLight *)lParam;
/*
* If we are running on a palette based display we will
* select and realize RenderWare's own color palette when
* drawing the preview button to give a realistic picture
* of the available colors. So cache the RenderWare palette
* if we are using one.
*/
RwGetDeviceInfo(rwPALETTEBASED, &isPaletteBased, sizeof(isPaletteBased));
if (isPaletteBased)
RwGetDeviceInfo(rwPALETTE, &Palette, sizeof(Palette));
else
Palette = NULL;
/*
* In RenderWare V1.4 colored light sources are only available when
* performing 16-bit rendering, So we determine if we are performing
* 16-bit rendering and modify the dialog appropriately.
*/
RwGetDeviceInfo(rwRENDERDEPTH, &RenderDepth, sizeof(RenderDepth));
if (RenderDepth == 16L)
{
/*
* We have colored light sources. Hide the brightness scrollbar
* and show the color button.
*/
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTLABEL), SW_HIDE);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTEDIT), SW_HIDE);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SW_HIDE);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLOR), SW_SHOW);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLORPREVIEW), SW_SHOW);
/*
* Get the current light color.
*/
RwGetLightColor(Light, &Color);
}
else
{
/*
* We don't have colored light sources. Show the brightness
* slider, update it and hide the color button.
*/
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLOR), SW_HIDE);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_COLORPREVIEW), SW_HIDE);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTLABEL), SW_SHOW);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTEDIT), SW_SHOW);
ShowWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SW_SHOW);
bright = RwGetLightBrightness(Light);
SetScrollRange(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL, 0, 255, FALSE);
SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL, REALTOBYTE(bright), FALSE);
sprintf(buffer, "%d", (int)REALTOBYTE(bright));
SetDlgItemText(dialog, IDC_LIGHTPROPS_BRIGHTEDIT, buffer);
}
/*
* Display the light type and initialize the cone angle if necessary.
*/
switch (RwGetLightType(Light))
{
case rwDIRECTIONAL:
SetDlgItemText(dialog, IDC_LIGHTPROPS_TYPE, "Directional");
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONELABEL), FALSE);
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONEEDIT), FALSE);
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), FALSE);
coneAngle = CREAL(0.0);
break;
case rwPOINT:
SetDlgItemText(dialog, IDC_LIGHTPROPS_TYPE, "Point");
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONELABEL), FALSE);
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONEEDIT), FALSE);
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), FALSE);
coneAngle = RwGetLightConeAngle(Light);
break;
case rwCONICAL:
SetDlgItemText(dialog, IDC_LIGHTPROPS_TYPE, "Conical");
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONELABEL), TRUE);
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONEEDIT), TRUE);
EnableWindow(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), TRUE);
coneAngle = RwGetLightConeAngle(Light);
break;
}
SetScrollRange(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL,
0, 45, FALSE);
SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL,
(int)REAL2INT(coneAngle), TRUE);
sprintf(buffer, "%d", (int)REAL2INT(coneAngle));
SetDlgItemText(dialog, IDC_LIGHTPROPS_CONEEDIT, buffer);
return FALSE;
case WM_HSCROLL:
#if defined(WIN32)
code = (int)LOWORD(wParam);
newPos = (int)HIWORD(wParam);
scrollBar = (HWND)lParam;
id = (int)GetWindowLong(scrollBar, GWL_ID);
#else
code = (int)wParam;
newPos = (int)LOWORD(lParam);
scrollBar = (HWND)HIWORD(lParam);
id = (int)GetWindowWord(scrollBar, GWW_ID);
#endif
switch (id)
{
case IDC_LIGHTPROPS_BRIGHTSLIDER:
pos = GetScrollPos(scrollBar, SB_CTL);
switch (code)
{
case SB_BOTTOM: pos = 255; break;
case SB_LINEDOWN: pos += 1; break;
case SB_LINEUP: pos -= 1; break;
case SB_PAGEDOWN: pos += 16; break;
case SB_PAGEUP: pos -= 16; break;
case SB_THUMBTRACK: pos = newPos; break;
case SB_THUMBPOSITION: pos = newPos; break;
case SB_TOP: pos = 0; break;
}
/*
* Clamp the new value.
*/
if (pos < 0)
pos = 0;
else if (pos > 255)
pos = 255;
/*
* Update the scrollbar and edit control.
*/
SetScrollPos(scrollBar, SB_CTL, pos, TRUE);
sprintf(buffer, "%d", pos);
SetDlgItemText(dialog, IDC_LIGHTPROPS_BRIGHTEDIT, buffer);
break;
case IDC_LIGHTPROPS_CONESLIDER:
pos = GetScrollPos(scrollBar, SB_CTL);
switch (code)
{
case SB_BOTTOM: pos = 45; break;
case SB_LINEDOWN: pos += 1; break;
case SB_LINEUP: pos -= 1; break;
case SB_PAGEDOWN: pos += 5; break;
case SB_PAGEUP: pos -= 5; break;
case SB_THUMBTRACK: pos = newPos; break;
case SB_THUMBPOSITION: pos = newPos; break;
case SB_TOP: pos = 0; break;
}
/*
* Clamp the new value.
*/
if (pos < 0)
pos = 0;
else if (pos > 45)
pos = 45;
/*
* Update the scrollbar and edit control.
*/
SetScrollPos(scrollBar, SB_CTL, pos, TRUE);
sprintf(buffer, "%d", pos);
SetDlgItemText(dialog, IDC_LIGHTPROPS_CONEEDIT, buffer);
break;
}
return TRUE;
case WM_DRAWITEM:
#if defined(__WINDOWS_386__)
drawItem = (DRAWITEMSTRUCT FAR *)MK_FP32((void*)lParam);
#else
drawItem = (DRAWITEMSTRUCT FAR *)lParam;
#endif
switch (wParam)
{
case IDC_LIGHTPROPS_COLORPREVIEW:
/*
* If we have a palette based output device then
* select and realize the palette (we realize
* it as a background palette as we don't want to
* be too aggresive about getting the colors we ask
* for - it is only for feedback after all).
*/
if (Palette != (HPALETTE)0)
{
oldPalette = SelectPalette(drawItem->hDC, Palette, TRUE);
RealizePalette(drawItem->hDC);
}
/*
* Draw a rectangle framed by a black line and filled
* with the current color.
*/
pen = GetStockObject(BLACK_PEN);
oldPen = SelectObject(drawItem->hDC, pen);
brush = CreateSolidBrush(PALETTERGB(REALTOBYTE(Color.r),
REALTOBYTE(Color.g),
REALTOBYTE(Color.b)));
oldBrush = SelectObject(drawItem->hDC, brush);
Rectangle(drawItem->hDC, drawItem->rcItem.left,
drawItem->rcItem.top,
drawItem->rcItem.right,
drawItem->rcItem.bottom);
SelectObject(drawItem->hDC, oldBrush);
DeleteObject(brush);
SelectObject(drawItem->hDC, oldPen);
if (Palette != (HPALETTE)0)
{
SelectPalette(drawItem->hDC, oldPalette, TRUE);
RealizePalette(drawItem->hDC);
}
break;
}
return TRUE;
case WM_COMMAND:
switch (wParam)
{
case IDC_LIGHTPROPS_BRIGHTEDIT:
/*
* For the edit controls we only take any notice when the
* control loses the input focus.
*/
if (HIWORD(lParam) == EN_KILLFOCUS)
{
/*
* Get the edit control's text.
*/
GetDlgItemText(dialog, wParam, buffer, sizeof(buffer));
/*
* Ensure its a valid value.
*/
if ((sscanf(buffer, "%d", &pos) == 1) && (pos >= 0) && (pos <= 255))
{
/*
* Update the slider.
*/
SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL, pos, TRUE);
}
else
{
/*
* Restore the current value.
*/
pos = GetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL);
sprintf(buffer, "%d", pos);
SetDlgItemText(dialog, wParam, buffer);
}
}
break;
case IDC_LIGHTPROPS_CONEEDIT:
/*
* For the edit controls we only take any notice when the
* control loses the input focus.
*/
if (HIWORD(lParam) == EN_KILLFOCUS)
{
/*
* Get the edit control's text.
*/
GetDlgItemText(dialog, wParam, buffer, sizeof(buffer));
/*
* Ensure its a valid value.
*/
if ((sscanf(buffer, "%d", &pos) == 1) && (pos >= 0) && (pos <= 45))
{
/*
* Update the slider.
*/
SetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL, pos, TRUE);
}
else
{
/*
* Restore the current value.
*/
pos = GetScrollPos(GetDlgItem(dialog, IDC_LIGHTPROPS_CONESLIDER), SB_CTL);
sprintf(buffer, "%d", pos);
SetDlgItemText(dialog, wParam, buffer);
}
}
break;
case IDC_LIGHTPROPS_COLOR:
/*
* Use the little color picked dialog to pick a new light color.
*/
dialogProc = MakeProcInstance(ColorDlgProc, AppInstance);
if (DialogBoxParam(AppInstance, MAKEINTRESOURCE(IDD_COLORPICKER),
dialog, dialogProc, (LPARAM)&Color) == IDOK)
/*
* Force a redraw of the color preview button.
*/
InvalidateRect(GetDlgItem(dialog,
IDC_LIGHTPROPS_COLORPREVIEW), NULL, FALSE);
FreeProcInstance(dialogProc);
break;
case IDOK:
/*
* Update the color/brightness of the light.
*/
if (RenderDepth == 16L)
{
SetLightObjColor(Light, Color.r, Color.g, Color.b);
}
else
{
bright = BYTETOREAL(GetScrollPos(GetDlgItem(dialog,
IDC_LIGHTPROPS_BRIGHTSLIDER), SB_CTL));
SetLightObjBrightness(Light, bright);
}
/*
* Update the cone angle (if its a conical light source).
*/
if (RwGetLightType(Light) == rwCONICAL)
{
coneAngle = INT2REAL(GetScrollPos(GetDlgItem(dialog,
IDC_LIGHTPROPS_CONESLIDER), SB_CTL));
RwSetLightConeAngle(Light, coneAngle);
}
EndDialog(dialog, IDOK);
break;
case IDCANCEL:
EndDialog(dialog, IDCANCEL);
break;
}
return TRUE;
}
return FALSE;
}
/**********************************************************************/