view src/win32/PaletteViewControl.cpp @ 4:5f6f2134e8ce

apu appears to not be used
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:35:58 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 // PaletteViewControl.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "PaletteViewControl.h"
7 #include "../common/Util.h"
9 bool PaletteViewControl::isRegistered = false;
11 /////////////////////////////////////////////////////////////////////////////
12 // PaletteViewControl
14 PaletteViewControl::PaletteViewControl()
15 {
16 memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader));
18 bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader);
19 bmpInfo.bmiHeader.biWidth = 256;
20 bmpInfo.bmiHeader.biHeight = -256;
21 bmpInfo.bmiHeader.biPlanes = 1;
22 bmpInfo.bmiHeader.biBitCount = 24;
23 bmpInfo.bmiHeader.biCompression = BI_RGB;
24 data = (u8 *)malloc(3 * 256 * 256);
26 w = 256;
27 h = 256;
29 colors = 256;
31 paletteAddress = 0;
33 ZeroMemory(palette, 512);
35 selected = -1;
36 registerClass();
37 }
39 PaletteViewControl::~PaletteViewControl()
40 {
41 if(data)
42 free(data);
43 }
46 BEGIN_MESSAGE_MAP(PaletteViewControl, CWnd)
47 //{{AFX_MSG_MAP(PaletteViewControl)
48 ON_WM_LBUTTONDOWN()
49 ON_WM_ERASEBKGND()
50 ON_WM_PAINT()
51 //}}AFX_MSG_MAP
52 END_MESSAGE_MAP()
55 /////////////////////////////////////////////////////////////////////////////
56 // PaletteViewControl message handlers
58 void PaletteViewControl::init(int c, int w, int h)
59 {
60 this->w = w;
61 this->h = h;
62 this->colors = c;
64 bmpInfo.bmiHeader.biWidth = w;
65 bmpInfo.bmiHeader.biHeight = -h;
66 }
69 bool PaletteViewControl::saveAdobe(const char *name)
70 {
71 FILE *f = fopen(name, "wb");
73 if(!f)
74 return false;
76 for(int i = 0; i < colors; i++) {
77 u16 c = palette[i];
78 int r = (c & 0x1f) << 3;
79 int g = (c & 0x3e0) >> 2;
80 int b = (c & 0x7c00) >> 7;
82 u8 data[3] = { r, g, b };
83 fwrite(data, 1, 3, f);
84 }
85 if(colors < 256) {
86 for(int i = colors; i < 256; i++) {
87 u8 data[3] = { 0, 0, 0 };
88 fwrite(data, 1, 3, f);
89 }
90 }
91 fclose(f);
93 return true;
94 }
97 bool PaletteViewControl::saveMSPAL(const char *name)
98 {
99 FILE *f = fopen(name, "wb");
101 if(!f)
102 return false;
104 u8 data[4] = { 'R', 'I', 'F', 'F' };
106 fwrite(data, 1, 4, f);
107 utilPutDword(data, 256 * 4 + 16);
108 fwrite(data, 1, 4, f);
109 u8 data3[4] = { 'P', 'A', 'L', ' ' };
110 fwrite(data3, 1, 4, f);
111 u8 data4[4] = { 'd', 'a', 't', 'a' };
112 fwrite(data4, 1, 4, f);
113 utilPutDword(data, 256*4+4);
114 fwrite(data, 1, 4, f);
115 utilPutWord(&data[0], 0x0300);
116 utilPutWord(&data[2], 256); // causes problems if not 16 or 256
117 fwrite(data, 1, 4, f);
119 for(int i = 0; i < colors; i++) {
120 u16 c = palette[i];
121 int r = (c & 0x1f) << 3;
122 int g = (c & 0x3e0) >> 2;
123 int b = (c & 0x7c00) >> 7;
125 u8 data7[4] = { r, g, b, 0 };
126 fwrite(data7, 1, 4, f);
127 }
128 if(colors < 256) {
129 for(int i = colors; i < 256; i++) {
130 u8 data7[4] = { 0, 0, 0, 0 };
131 fwrite(data7, 1, 4, f);
132 }
133 }
134 fclose(f);
136 return true;
137 }
140 bool PaletteViewControl::saveJASCPAL(const char *name)
141 {
142 FILE *f = fopen(name, "wb");
144 if(!f)
145 return false;
147 fprintf(f, "JASC-PAL\r\n0100\r\n256\r\n");
149 for(int i = 0; i < colors; i++) {
150 u16 c = palette[i];
151 int r = (c & 0x1f) << 3;
152 int g = (c & 0x3e0) >> 2;
153 int b = (c & 0x7c00) >> 7;
155 fprintf(f, "%d %d %d\r\n", r, g, b);
156 }
157 if(colors < 256) {
158 for(int i = colors; i < 256; i++)
159 fprintf(f, "0 0 0\r\n");
160 }
161 fclose(f);
163 return true;
164 }
166 void PaletteViewControl::setPaletteAddress(int address)
167 {
168 paletteAddress = address;
169 }
172 void PaletteViewControl::setSelected(int s)
173 {
174 selected = s;
175 InvalidateRect(NULL, FALSE);
176 }
179 void PaletteViewControl::render(u16 color, int x, int y)
180 {
181 u8 *start = data + y*16*w*3 + x*16*3;
182 int skip = w*3-16*3;
184 int r = (color & 0x1f) << 3;
185 int g = (color & 0x3e0) >> 2;
186 int b = (color & 0x7c00) >> 7;
188 for(int i = 0; i < 16; i++) {
189 *start++ = b;
190 *start++ = g;
191 *start++ = r;
193 *start++ = b;
194 *start++ = g;
195 *start++ = r;
197 *start++ = b;
198 *start++ = g;
199 *start++ = r;
201 *start++ = b;
202 *start++ = g;
203 *start++ = r;
205 *start++ = b;
206 *start++ = g;
207 *start++ = r;
209 *start++ = b;
210 *start++ = g;
211 *start++ = r;
213 *start++ = b;
214 *start++ = g;
215 *start++ = r;
217 *start++ = b;
218 *start++ = g;
219 *start++ = r;
221 *start++ = b;
222 *start++ = g;
223 *start++ = r;
225 *start++ = b;
226 *start++ = g;
227 *start++ = r;
229 *start++ = b;
230 *start++ = g;
231 *start++ = r;
233 *start++ = b;
234 *start++ = g;
235 *start++ = r;
237 *start++ = b;
238 *start++ = g;
239 *start++ = r;
241 *start++ = b;
242 *start++ = g;
243 *start++ = r;
245 *start++ = b;
246 *start++ = g;
247 *start++ = r;
249 *start++ = b;
250 *start++ = g;
251 *start++ = r;
253 start += skip;
254 }
255 }
257 void PaletteViewControl::refresh()
258 {
259 updatePalette();
260 int sw = w/16;
261 int sh = h/16;
262 for(int i = 0; i < colors; i++) {
263 render(palette[i], i & (sw-1), i/sw);
264 }
265 InvalidateRect(NULL, FALSE);
266 }
268 void PaletteViewControl::OnLButtonDown(UINT nFlags, CPoint point)
269 {
270 int x = point.x;
271 int y = point.y;
272 RECT rect;
273 GetClientRect(&rect);
274 int h = rect.bottom - rect.top;
275 int w = rect.right - rect.left;
276 int sw = (this->w/16);
277 int sh = (this->h/16);
278 int mult = w / sw;
279 int multY = h / sh;
281 setSelected(x/mult + (y/multY)*sw);
283 GetParent()->SendMessage(WM_PALINFO,
284 palette[x/mult+(y/multY)*sw],
285 paletteAddress+(x/mult+(y/multY)*sw));
286 }
288 BOOL PaletteViewControl::OnEraseBkgnd(CDC* pDC)
289 {
290 return TRUE;
291 }
294 void PaletteViewControl::OnPaint()
295 {
296 CPaintDC dc(this); // device context for painting
298 RECT rect;
299 GetClientRect(&rect);
300 int w = rect.right - rect.left;
301 int h = rect.bottom - rect.top;
303 CDC memDC;
304 memDC.CreateCompatibleDC(&dc);
305 CBitmap bitmap, *pOldBitmap;
306 bitmap.CreateCompatibleBitmap(&dc, w, h);
307 pOldBitmap = memDC.SelectObject(&bitmap);
309 StretchDIBits(memDC.GetSafeHdc(),
310 0,
311 0,
312 w,
313 h,
314 0,
315 0,
316 this->w,
317 this->h,
318 data,
319 &bmpInfo,
320 DIB_RGB_COLORS,
321 SRCCOPY);
322 int sw = this->w / 16;
323 int sh = this->h / 16;
324 int mult = w / sw;
325 int multY = h / sh;
326 CPen pen;
327 pen.CreatePen(PS_SOLID, 1, RGB(192,192,192));
328 CPen *old = memDC.SelectObject(&pen);
329 int i;
330 for(i = 1; i < sh; i++) {
331 memDC.MoveTo(0, i * multY);
332 memDC.LineTo(w, i * multY);
333 }
334 for(i = 1; i < sw; i++) {
335 memDC.MoveTo(i * mult, 0);
336 memDC.LineTo(i * mult, h);
337 }
338 memDC.DrawEdge(&rect, EDGE_SUNKEN, BF_RECT);
339 memDC.SelectObject(old);
340 pen.DeleteObject();
342 if(selected != -1) {
343 pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
344 old = memDC.SelectObject(&pen);
346 int startX = (selected & (sw-1))*mult+1;
347 int startY = (selected / sw)*multY+1;
348 int endX = startX + mult-2;
349 int endY = startY + multY-2;
351 memDC.MoveTo(startX, startY);
352 memDC.LineTo(endX, startY);
353 memDC.LineTo(endX, endY);
354 memDC.LineTo(startX, endY);
355 memDC.LineTo(startX, startY-1);
357 memDC.SelectObject(old);
358 pen.DeleteObject();
359 }
361 dc.BitBlt(0,0,w,h,
362 &memDC,0,0,SRCCOPY);
364 memDC.SelectObject(pOldBitmap);
365 bitmap.DeleteObject();
366 memDC.DeleteDC();
367 }
369 void PaletteViewControl::registerClass()
370 {
371 if(!isRegistered) {
372 WNDCLASS wc;
373 ZeroMemory(&wc, sizeof(wc));
374 wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
375 wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
376 wc.hInstance = AfxGetInstanceHandle();
377 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
378 wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
379 wc.lpszMenuName = NULL;
380 wc.lpszClassName = "VbaPaletteViewControl";
381 AfxRegisterClass(&wc);
382 isRegistered = true;
383 }
384 }