2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
25 #define PALETTE_WINDOW_CLASS "TPPalette"
28 int pal_width, pal_height;
29 int blocks_x, blocks_y;
32 unsigned selected_rgb;
36 float SnapAspect (float aspect)
49 void Pal_SetIndex (int index)
51 selected_index = index;
52 selected_rgb = palette[index*3] + (palette[index*3+1]<<8) + (palette[index*3+2]<<16);
53 InvalidateRect (palettewindow, NULL, false);
63 if (pal_width < 1 || pal_height < 1)
67 // determine the block arrangement
69 if (pal_width > pal_height)
71 aspect = SnapAspect (pal_width / pal_height);
73 blocks_y = 256/blocks_x;
77 aspect = SnapAspect (pal_height / pal_width);
79 blocks_x = 256/blocks_y;
85 glViewport (0,0,pal_width, pal_height);
86 glMatrixMode (GL_PROJECTION);
88 glOrtho (0,1,0,1,-100,100);
89 glMatrixMode (GL_MODELVIEW);
92 glClear (GL_COLOR_BUFFER_BIT);
93 glDisable (GL_DEPTH_TEST);
94 glDisable (GL_CULL_FACE);
95 glDisable (GL_TEXTURE_2D);
100 for (x=0 ; x<blocks_x ; x++)
102 for (y=0 ; y<blocks_y ; y++)
104 c = x*blocks_y+(blocks_y-1-y);
105 glColor3ubv (palette+c*3);
106 glRectf (x*xs, y*ys, (x+1)*xs, (y+1)*ys);
110 // highlight the selected texture
111 y = selected_index % blocks_y;
112 x = selected_index / blocks_y;
116 glRectf ( (x+0.4)*xs, (y+0.4)*ys, (x+0.6)*xs, (y+0.6)*ys);
119 void Pal_Click (int x, int y)
123 x = x*blocks_x/pal_width;
124 y = y*blocks_y/pal_height;
127 index = x*blocks_y + y;
128 Pal_SetIndex (index);
136 LONG WINAPI Palette_WndProc (
143 int fwKeys, xPos, yPos;
146 GetClientRect(hWnd, &rect);
147 pal_width = rect.right-rect.left;
148 pal_height = rect.bottom-rect.top;
154 bSetupPixelFormat(paldc);
160 BeginPaint(hWnd, &ps);
161 if (!wglMakeCurrent( paldc, baseRC ))
162 Error ("wglMakeCurrent failed");
170 if (wParam != MK_LBUTTON)
173 if (GetTopWindow(mainwindow) != hWnd)
174 BringWindowToTop(hWnd);
176 xPos = (short)LOWORD(lParam); // horizontal position of cursor
177 yPos = (short)HIWORD(lParam); // vertical position of cursor
178 yPos = (int)rect.bottom - 1 - yPos;
180 Pal_Click (xPos, yPos);
186 fwKeys = wParam; // key flags
187 xPos = (short)LOWORD(lParam); // horizontal position of cursor
188 yPos = (short)HIWORD(lParam); // vertical position of cursor
189 yPos = (int)rect.bottom - 1 - yPos;
194 InvalidateRect(skinwindow, NULL, false);
196 case WM_NCCALCSIZE:// don't let windows copy pixels
197 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
200 /* call destroy window to cleanup and go away */
201 DestroyWindow (hWnd);
205 /* pass all unhandled messages to DefWindowProc */
206 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
210 /* return 1 if handled message, 0 if not */
220 void WPal_Create (HINSTANCE hInstance)
224 /* Register the skin class */
225 memset (&wc, 0, sizeof(wc));
228 wc.lpfnWndProc = (WNDPROC)Palette_WndProc;
231 wc.hInstance = hInstance;
233 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
234 wc.hbrBackground = NULL;
236 wc.lpszClassName = PALETTE_WINDOW_CLASS;
238 if (!RegisterClass (&wc) )
239 Error ("RegisterClass failed");
241 palettewindow = CreateWindow (PALETTE_WINDOW_CLASS ,
244 (int)(screen_width*0.5),
246 (int)(screen_width*0.5),
247 (int)(screen_height*.2), // size
248 mainwindow, // parent window
253 Error ("Couldn't create palettewindow");
255 // RestoreWindowState(palettewindow, "palettewindow");
256 ShowWindow (palettewindow, SW_SHOWDEFAULT);