#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tk.h"
#define static
#if defined(__cplusplus) || defined(c_plusplus)
#define class c_class
#endif
#if DBG
#define TKASSERT(x) \
if ( !(x) ) { \
PrintMessage("%s(%d) Assertion failed %s\n", \
__FILE__, __LINE__, #x); \
}
#else
#define TKASSERT(x)
#endif
static struct _WINDOWINFO {
int x, y;
int width, height;
GLenum type;
GLenum dmPolicy;
int ipfd;
BOOL bDefPos;
} windInfo = {
0, 0, 100, 100, TK_INDEX | TK_SINGLE, TK_MINIMUM_CRITERIA, 0, TRUE
};
static HWND tkhwnd = NULL;
static HDC tkhdc = NULL;
static HPALETTE tkhpalette = NULL;
GLboolean tkPopupEnable = TRUE;
#define BLACK PALETTERGB(0,0,0)
#define WHITE PALETTERGB(255,255,255)
#define NUM_STATIC_COLORS (COLOR_BTNHIGHLIGHT - COLOR_SCROLLBAR + 1)
static void (*ExposeFunc)(int, int) = NULL;
static void (*ReshapeFunc)(GLsizei, GLsizei) = NULL;
static void (*DisplayFunc)(void) = NULL;
static GLenum (*KeyDownFunc)(int, GLenum) = NULL;
static GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
static GLenum (*MouseUpFunc)(int, int, GLenum) = NULL;
static GLenum (*MouseMoveFunc)(int, int, GLenum) = NULL;
static void (*IdleFunc)(void) = NULL;
static char *lpszClassName = "tkLibWClass";
static WCHAR *lpszClassNameW = L"tkLibWClass";
long FAR PASCAL _export tkWndProc(HWND hWnd, UINT message, DWORD wParam, LONG lParam);
static unsigned char ComponentFromIndex(int i, int nbits, int shift );
static void PrintMessage( const char *Format, ... );
static HPALETTE CreateCIPalette( HDC Dc );
static HPALETTE CreateRGBPalette( HDC hdc );
static void DestroyThisWindow( HWND Window );
static void CleanUp( void );
static void DelayPaletteRealization( void );
static long RealizePaletteNow( HDC Dc, HPALETTE Palette);
static void ForceRedraw( HWND Window );
static void *AllocateMemory( size_t Size );
static void *AllocateZeroedMemory( size_t Size );
static void FreeMemory( void *Chunk );
#define DBGFUNC 0
#if DBGFUNC
static void DbgPrintf( const char *Format, ... );
static void pwi( void );
static void pwr(RECT *pr);
#endif
#define NCOLORS 17
float tkRGBMap[NCOLORS][3] = {
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{1,0,0},
{0,1,0},
{1,1,0},
{0,0,1},
{1,0,1},
{0,1,1},
{1,1,1}
};
void tkErrorPopups(GLboolean bEnable)
{
tkPopupEnable = bEnable;
}
void tkCloseWindow(void)
{
DestroyThisWindow(tkhwnd);
WMesaDestroyContext(NULL);
}
void tkExec(void)
{
MSG Message;
if (ReshapeFunc)
{
RECT ClientRect;
GetClientRect(tkhwnd, &ClientRect);
(*ReshapeFunc)(ClientRect.right, ClientRect.bottom);
}
while (GL_TRUE)
{
while (PeekMessage(&Message, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (GetMessage(&Message, NULL, 0, 0) )
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
else
{
return;
}
}
if (IdleFunc)
{
(*IdleFunc)();
}
}
}
void tkExposeFunc(void (*Func)(int, int))
{
ExposeFunc = Func;
}
void tkReshapeFunc(void (*Func)(GLsizei, GLsizei))
{
ReshapeFunc = Func;
}
void tkDisplayFunc(void (*Func)(void))
{
DisplayFunc = Func;
}
void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
{
KeyDownFunc = Func;
}
void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
{
MouseDownFunc = Func;
}
void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
{
MouseUpFunc = Func;
}
void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
{
MouseMoveFunc = Func;
}
void tkIdleFunc(void (*Func)(void))
{
IdleFunc = Func;
}
void tkInitPosition(int x, int y, int width, int height)
{
if (x == CW_USEDEFAULT)
{
x = 0;
y = 0;
windInfo.bDefPos = TRUE;
}
else
windInfo.bDefPos = FALSE;
windInfo.x = x + GetSystemMetrics(SM_CXFRAME);
windInfo.y = y + GetSystemMetrics(SM_CYCAPTION)
- GetSystemMetrics(SM_CYBORDER)
+ GetSystemMetrics(SM_CYFRAME);
windInfo.width = width;
windInfo.height = height;
}
void tkInitDisplayMode(GLenum type)
{
windInfo.type = type;
}
void tkInitDisplayModePolicy(GLenum type)
{
windInfo.dmPolicy = type;
}
GLenum tkInitDisplayModeID(GLint ipfd)
{
windInfo.ipfd = ipfd;
return GL_TRUE;
}
GLenum tkInitWindowAW(char *title, BOOL bUnicode)
{
WMesaContext Cur;
WNDCLASS wndclass;
RECT WinRect;
HANDLE hInstance;
ATOM aRegister;
GLenum Result = GL_FALSE,RGB_Flag=GL_TRUE,DB_Flag=GL_FALSE;
hInstance = GetModuleHandle(NULL);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)tkWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
if (bUnicode)
wndclass.lpszClassName = (LPCSTR)lpszClassNameW;
else
wndclass.lpszClassName = (LPCSTR)lpszClassName;
if (bUnicode)
{
aRegister = RegisterClassW((CONST WNDCLASSW *)&wndclass);
}
else
{
aRegister = RegisterClass(&wndclass);
}
if(0 == aRegister)
{
PrintMessage("Failed to register window class\n");
return(Result);
}
WinRect.left = windInfo.x;
WinRect.right = windInfo.x + windInfo.width;
WinRect.top = windInfo.y;
WinRect.bottom = windInfo.y + windInfo.height;
AdjustWindowRect(&WinRect, WS_OVERLAPPEDWINDOW, FALSE);
if (bUnicode)
{
tkhwnd = CreateWindowW(
(LPCWSTR)lpszClassNameW,
(LPCWSTR)title,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
(windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.left,
(windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.top,
WinRect.right - WinRect.left,
WinRect.bottom - WinRect.top,
NULL,
NULL,
hInstance,
NULL);
}
else
{
tkhwnd = CreateWindow(
lpszClassName,
title,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
(windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.left,
(windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.top,
WinRect.right - WinRect.left,
WinRect.bottom - WinRect.top,
NULL,
NULL,
hInstance,
NULL);
}
if ( NULL != tkhwnd )
{
if (windInfo.bDefPos)
{
GetWindowRect(tkhwnd, &WinRect);
windInfo.x = WinRect.left + GetSystemMetrics(SM_CXFRAME);
windInfo.y = WinRect.top + GetSystemMetrics(SM_CYCAPTION)
- GetSystemMetrics(SM_CYBORDER)
+ GetSystemMetrics(SM_CYFRAME);
}
tkhdc = GetDC(tkhwnd);
if ( NULL != tkhdc )
ShowWindow(tkhwnd, SW_SHOWDEFAULT);
else
PrintMessage("Could not get an HDC for window 0x%08lX\n", tkhwnd );
}
else
PrintMessage("create window failed\n");
if (windInfo.type & TK_INDEX)
{
RGB_Flag=GL_FALSE;
tkSetRGBMap(NCOLORS,(float *) tkRGBMap);
}
if (windInfo.type & TK_DOUBLE)
DB_Flag=GL_TRUE;
Cur=WMesaCreateContext(tkhwnd,tkhpalette,RGB_Flag,DB_Flag);
WMesaMakeCurrent(Cur);
return GL_TRUE;
}
GLenum tkInitWindow(char *title)
{
TKASSERT( NULL==tkhwnd );
TKASSERT( NULL==tkhdc );
TKASSERT( NULL==tkhrc );
TKASSERT( NULL==tkhpalette );
return tkInitWindowAW(title, FALSE);
}
void tkQuit(void)
{
DestroyThisWindow(tkhwnd);
ExitProcess(0);
}
void tkSetOneColor(int index, float r, float g, float b)
{
PALETTEENTRY PalEntry;
HPALETTE Palette;
if ( NULL != (Palette = CreateCIPalette( tkhdc )) )
{
PalEntry.peRed = (BYTE)(r*(float)255.0 + (float)0.5);
PalEntry.peGreen = (BYTE)(g*(float)255.0 + (float)0.5);
PalEntry.peBlue = (BYTE)(b*(float)255.0 + (float)0.5);
PalEntry.peFlags = 0;
SetPaletteEntries( Palette, index, 1, &PalEntry);
DelayPaletteRealization();
}
}
void tkSetFogRamp(int density, int startIndex)
{
HPALETTE CurrentPal;
PALETTEENTRY *pPalEntry;
UINT n, i, j, k, intensity, fogValues, colorValues;
if ( NULL != (CurrentPal = CreateCIPalette(tkhdc)) )
{
n = GetPaletteEntries( CurrentPal, 0, 0, NULL );
pPalEntry = AllocateMemory( n * sizeof(PALETTEENTRY) );
if ( NULL != pPalEntry)
{
fogValues = 1 << density;
colorValues = 1 << startIndex;
for (i = 0; i < colorValues; i++) {
for (j = 0; j < fogValues; j++) {
k = i * fogValues + j;
intensity = i * fogValues + j * colorValues;
if (intensity > 0xFF)
intensity = 0xFF;
pPalEntry[k].peRed =pPalEntry[k].peGreen = pPalEntry[k].peBlue = (BYTE) intensity;
pPalEntry[k].peFlags = 0;
}
}
SetPaletteEntries(CurrentPal, 0, n, pPalEntry);
FreeMemory( pPalEntry );
DelayPaletteRealization();
}
}
}
void tkSetGreyRamp(void)
{
HPALETTE CurrentPal;
PALETTEENTRY *Entries;
UINT Count, i;
float intensity;
if ( NULL != (CurrentPal = CreateCIPalette( tkhdc )) )
{
Count = GetPaletteEntries( CurrentPal, 0, 0, NULL );
Entries = AllocateMemory( Count * sizeof(PALETTEENTRY) );
if ( NULL != Entries )
{
for (i = 0; i < Count; i++)
{
intensity = (float)(((double)i / (double)(Count-1)) * (double)255.0 + (double)0.5);
Entries[i].peRed =
Entries[i].peGreen =
Entries[i].peBlue = (BYTE) intensity;
Entries[i].peFlags = 0;
}
SetPaletteEntries( CurrentPal, 0, Count, Entries );
FreeMemory( Entries );
DelayPaletteRealization();
}
}
}
void tkSetRGBMap( int Size, float *Values )
{
HPALETTE CurrentPal;
int i;
if ( NULL != (CurrentPal = CreateCIPalette( tkhdc )) )
{
for (i=0; i<Size; i++)
tkSetOneColor(i,Values[i*3],Values[i*3+1],Values[i*3+2]);
}
}
void tkSwapBuffers(void)
{
WMesaSwapBuffers();
}
GLint tkGetColorMapSize(void)
{
CreateCIPalette( tkhdc );
if ( NULL == tkhpalette )
return( 0 );
return( GetPaletteEntries( tkhpalette, 0, 0, NULL ) );
}
void tkGetMouseLoc(int *x, int *y)
{
POINT Point;
*x = 0;
*y = 0;
GetCursorPos(&Point);
*x = Point.x - windInfo.x;
*y = Point.y - windInfo.y;
}
HWND tkGetHWND(void)
{
return tkhwnd;
}
HDC tkGetHDC(void)
{
return tkhdc;
}
GLenum tkGetDisplayModePolicy(void)
{
return windInfo.dmPolicy;
}
GLint tkGetDisplayModeID(void)
{
return windInfo.ipfd;
}
GLenum tkGetDisplayMode(void)
{
return windInfo.type;
}
long FAR PASCAL _export
tkWndProc(HWND hWnd, UINT message, DWORD wParam, LONG lParam)
{
int key;
PAINTSTRUCT paint;
HDC hdc;
switch (message)
{
case WM_USER:
if ( RealizePaletteNow( tkhdc, tkhpalette) > 0 )
ForceRedraw( hWnd );
return(0);
case WM_SIZE:
windInfo.width = LOWORD(lParam);
windInfo.height = HIWORD(lParam);
if (ReshapeFunc)
{
(*ReshapeFunc)(windInfo.width, windInfo.height);
ForceRedraw( hWnd );
}
return (0);
case WM_MOVE:
windInfo.x = LOWORD(lParam);
windInfo.y = HIWORD(lParam);
return (0);
case WM_PAINT:
hdc = BeginPaint(tkhwnd, &paint);
if (DisplayFunc)
{
(*DisplayFunc)();
}
EndPaint(tkhwnd, &paint);
return (0);
case WM_PALETTECHANGED:
if ( hWnd != (HWND) wParam )
RealizePaletteNow(tkhdc,tkhpalette);
return (0);
case WM_QUERYNEWPALETTE:
if ( NULL != tkhpalette )
{
if ( RealizePaletteNow(tkhdc, tkhpalette) > 0 )
ForceRedraw( hWnd );
return (1);
}
return (0);
case WM_ACTIVATE:
if ( LOWORD(wParam) == WA_INACTIVE )
{
if ( NULL != tkhpalette )
{
if ( RealizePaletteNow( tkhdc, tkhpalette) > 0 )
ForceRedraw( hWnd );
}
}
break;
case WM_MOUSEMOVE:
if (MouseMoveFunc)
{
GLenum mask;
mask = 0;
if (wParam & MK_LBUTTON) {
mask |= TK_LEFTBUTTON;
}
if (wParam & MK_MBUTTON) {
mask |= TK_MIDDLEBUTTON;
}
if (wParam & MK_RBUTTON) {
mask |= TK_RIGHTBUTTON;
}
if ((*MouseMoveFunc)( LOWORD(lParam), HIWORD(lParam), mask ))
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_LBUTTONDOWN:
SetCapture(hWnd);
if (MouseDownFunc)
{
if ( (*MouseDownFunc)(LOWORD(lParam), HIWORD(lParam),
TK_LEFTBUTTON) )
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_LBUTTONUP:
ReleaseCapture();
if (MouseUpFunc)
{
if ((*MouseUpFunc)(LOWORD(lParam), HIWORD(lParam), TK_LEFTBUTTON))
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_MBUTTONDOWN:
SetCapture(hWnd);
if (MouseDownFunc)
{
if ((*MouseDownFunc)(LOWORD(lParam), HIWORD(lParam),
TK_MIDDLEBUTTON))
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_MBUTTONUP:
ReleaseCapture();
if (MouseUpFunc)
{
if ((*MouseUpFunc)(LOWORD(lParam), HIWORD(lParam),
TK_MIDDLEBUTTON))
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_RBUTTONDOWN:
SetCapture(hWnd);
if (MouseDownFunc)
{
if ((*MouseDownFunc)(LOWORD(lParam), HIWORD(lParam),
TK_RIGHTBUTTON))
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_RBUTTONUP:
ReleaseCapture();
if (MouseUpFunc)
{
if ((*MouseUpFunc)(LOWORD(lParam), HIWORD(lParam),
TK_RIGHTBUTTON))
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_KEYDOWN:
switch (wParam) {
case VK_SPACE: key = TK_SPACE; break;
case VK_RETURN: key = TK_RETURN; break;
case VK_ESCAPE: key = TK_ESCAPE; break;
case VK_LEFT: key = TK_LEFT; break;
case VK_UP: key = TK_UP; break;
case VK_RIGHT: key = TK_RIGHT; break;
case VK_DOWN: key = TK_DOWN; break;
default: key = GL_FALSE; break;
}
if (key && KeyDownFunc)
{
GLenum mask;
mask = 0;
if (GetKeyState(VK_CONTROL)) {
mask |= TK_CONTROL;
}
if (GetKeyState(VK_SHIFT)) {
mask |= TK_SHIFT;
}
if ( (*KeyDownFunc)(key, mask) )
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_CHAR:
if (('0' <= wParam && wParam <= '9') ||
('a' <= wParam && wParam <= 'z') ||
('A' <= wParam && wParam <= 'Z')) {
key = wParam;
} else {
key = GL_FALSE;
}
if (key && KeyDownFunc) {
GLenum mask;
mask = 0;
if (GetKeyState(VK_CONTROL)) {
mask |= TK_CONTROL;
}
if (GetKeyState(VK_SHIFT)) {
mask |= TK_SHIFT;
}
if ( (*KeyDownFunc)(key, mask) )
{
ForceRedraw( hWnd );
}
}
return (0);
case WM_CLOSE:
DestroyWindow(tkhwnd);
return(0);
case WM_DESTROY:
CleanUp();
PostQuitMessage(TRUE);
return 0;
}
return(DefWindowProc( hWnd, message, wParam, lParam));
}
static HPALETTE CreateCIPalette( HDC Dc )
{
LOGPALETTE *LogicalPalette;
HPALETTE StockPalette;
UINT PaletteSize, StockPaletteSize, EntriesToCopy;
if ( (Dc != NULL) && (NULL == tkhpalette) )
{
PaletteSize = 256;
LogicalPalette = AllocateZeroedMemory( sizeof(LOGPALETTE) +
(PaletteSize * sizeof(PALETTEENTRY)) );
if ( NULL != LogicalPalette )
{
LogicalPalette->palVersion = 0x300;
LogicalPalette->palNumEntries = PaletteSize;
StockPalette = GetStockObject(DEFAULT_PALETTE);
StockPaletteSize = GetPaletteEntries( StockPalette, 0, 0, NULL );
EntriesToCopy = StockPaletteSize < PaletteSize ?
StockPaletteSize : PaletteSize;
GetPaletteEntries( StockPalette, 0, EntriesToCopy,
LogicalPalette->palPalEntry );
tkhpalette = CreatePalette(LogicalPalette);
FreeMemory(LogicalPalette);
RealizePaletteNow( Dc, tkhpalette);
}
}
return( tkhpalette );
}
static void
PrintMessage( const char *Format, ... )
{
va_list ArgList;
char Buffer[256];
va_start(ArgList, Format);
vsprintf(Buffer, Format, ArgList);
va_end(ArgList);
MessageBox(GetFocus(), Buffer, "Error", MB_OK);
}
static void
DelayPaletteRealization( void )
{
MSG Message;
TKASSERT(NULL!=tkhwnd);
if (!PeekMessage(&Message, tkhwnd, WM_USER, WM_USER, PM_NOREMOVE) )
{
PostMessage( tkhwnd, WM_USER, 0, 0);
}
}
static long RealizePaletteNow( HDC Dc, HPALETTE Palette)
{
long Result = -1;
TKASSERT( NULL!=Dc );
TKASSERT( NULL!=Palette );
if ( NULL != SelectPalette( Dc, Palette, FALSE ) )
{
Result = RealizePalette( Dc );
WMesaPaletteChange(Palette);
}
return( Result );
}
static void
ForceRedraw( HWND Window )
{
MSG Message;
if (!PeekMessage(&Message, Window, WM_PAINT, WM_PAINT, PM_NOREMOVE) )
{
InvalidateRect( Window, NULL, FALSE );
}
}
static void
DestroyThisWindow( HWND Window )
{
if ( NULL != Window )
{
DestroyWindow( Window );
}
}
static void
CleanUp( void )
{
HPALETTE hStock;
if ( NULL != tkhpalette )
{
if ((hStock = GetStockObject( DEFAULT_PALETTE ))!=NULL)
SelectPalette( tkhdc, hStock, FALSE );
DeleteObject( tkhpalette );
}
if ( NULL != tkhdc )
ReleaseDC( tkhwnd, tkhdc );
tkhwnd = NULL;
tkhdc = NULL;
tkhpalette = NULL;
ExposeFunc = NULL;
ReshapeFunc = NULL;
IdleFunc = NULL;
DisplayFunc = NULL;
KeyDownFunc = NULL;
MouseDownFunc = NULL;
MouseUpFunc = NULL;
MouseMoveFunc = NULL;
}
static void *
AllocateMemory( size_t Size )
{
return( LocalAlloc( LMEM_FIXED, Size ) );
}
static void *
AllocateZeroedMemory( size_t Size )
{
return( LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT, Size ) );
}
static void
FreeMemory( void *Chunk )
{
TKASSERT( NULL!=Chunk );
LocalFree( Chunk );
}