1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2004 Forgotten and the VBA development team
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // PaletteViewControl.cpp : implementation file
24 #include "PaletteViewControl.h"
31 static char THIS_FILE
[] = __FILE__
;
34 bool PaletteViewControl::isRegistered
= false;
36 /////////////////////////////////////////////////////////////////////////////
39 PaletteViewControl::PaletteViewControl()
41 memset(&bmpInfo
.bmiHeader
, 0, sizeof(bmpInfo
.bmiHeader
));
43 bmpInfo
.bmiHeader
.biSize
= sizeof(bmpInfo
.bmiHeader
);
44 bmpInfo
.bmiHeader
.biWidth
= 256;
45 bmpInfo
.bmiHeader
.biHeight
= -256;
46 bmpInfo
.bmiHeader
.biPlanes
= 1;
47 bmpInfo
.bmiHeader
.biBitCount
= 24;
48 bmpInfo
.bmiHeader
.biCompression
= BI_RGB
;
49 data
= (u8
*)malloc(3 * 256 * 256);
58 ZeroMemory(palette
, 512);
64 PaletteViewControl::~PaletteViewControl()
71 BEGIN_MESSAGE_MAP(PaletteViewControl
, CWnd
)
72 //{{AFX_MSG_MAP(PaletteViewControl)
80 /////////////////////////////////////////////////////////////////////////////
81 // PaletteViewControl message handlers
83 void PaletteViewControl::init(int c
, int w
, int h
)
89 bmpInfo
.bmiHeader
.biWidth
= w
;
90 bmpInfo
.bmiHeader
.biHeight
= -h
;
94 bool PaletteViewControl::saveAdobe(const char *name
)
96 FILE *f
= fopen(name
, "wb");
101 for(int i
= 0; i
< colors
; i
++) {
103 int r
= (c
& 0x1f) << 3;
104 int g
= (c
& 0x3e0) >> 2;
105 int b
= (c
& 0x7c00) >> 7;
107 u8 data
[3] = { r
, g
, b
};
108 fwrite(data
, 1, 3, f
);
111 for(int i
= colors
; i
< 256; i
++) {
112 u8 data
[3] = { 0, 0, 0 };
113 fwrite(data
, 1, 3, f
);
122 bool PaletteViewControl::saveMSPAL(const char *name
)
124 FILE *f
= fopen(name
, "wb");
129 u8 data
[4] = { 'R', 'I', 'F', 'F' };
131 fwrite(data
, 1, 4, f
);
132 utilPutDword(data
, 256 * 4 + 16);
133 fwrite(data
, 1, 4, f
);
134 u8 data3
[4] = { 'P', 'A', 'L', ' ' };
135 fwrite(data3
, 1, 4, f
);
136 u8 data4
[4] = { 'd', 'a', 't', 'a' };
137 fwrite(data4
, 1, 4, f
);
138 utilPutDword(data
, 256*4+4);
139 fwrite(data
, 1, 4, f
);
140 utilPutWord(&data
[0], 0x0300);
141 utilPutWord(&data
[2], 256); // causes problems if not 16 or 256
142 fwrite(data
, 1, 4, f
);
144 for(int i
= 0; i
< colors
; i
++) {
146 int r
= (c
& 0x1f) << 3;
147 int g
= (c
& 0x3e0) >> 2;
148 int b
= (c
& 0x7c00) >> 7;
150 u8 data7
[4] = { r
, g
, b
, 0 };
151 fwrite(data7
, 1, 4, f
);
154 for(int i
= colors
; i
< 256; i
++) {
155 u8 data7
[4] = { 0, 0, 0, 0 };
156 fwrite(data7
, 1, 4, f
);
165 bool PaletteViewControl::saveJASCPAL(const char *name
)
167 FILE *f
= fopen(name
, "wb");
172 fprintf(f
, "JASC-PAL\r\n0100\r\n256\r\n");
174 for(int i
= 0; i
< colors
; i
++) {
176 int r
= (c
& 0x1f) << 3;
177 int g
= (c
& 0x3e0) >> 2;
178 int b
= (c
& 0x7c00) >> 7;
180 fprintf(f
, "%d %d %d\r\n", r
, g
, b
);
183 for(int i
= colors
; i
< 256; i
++)
184 fprintf(f
, "0 0 0\r\n");
191 void PaletteViewControl::setPaletteAddress(int address
)
193 paletteAddress
= address
;
197 void PaletteViewControl::setSelected(int s
)
200 InvalidateRect(NULL
, FALSE
);
204 void PaletteViewControl::render(u16 color
, int x
, int y
)
206 u8
*start
= data
+ y
*16*w
*3 + x
*16*3;
209 int r
= (color
& 0x1f) << 3;
210 int g
= (color
& 0x3e0) >> 2;
211 int b
= (color
& 0x7c00) >> 7;
213 for(int i
= 0; i
< 16; i
++) {
282 void PaletteViewControl::refresh()
287 for(int i
= 0; i
< colors
; i
++) {
288 render(palette
[i
], i
& (sw
-1), i
/sw
);
290 InvalidateRect(NULL
, FALSE
);
293 void PaletteViewControl::OnLButtonDown(UINT nFlags
, CPoint point
)
298 GetClientRect(&rect
);
299 int h
= rect
.bottom
- rect
.top
;
300 int w
= rect
.right
- rect
.left
;
301 int sw
= (this->w
/16);
302 int sh
= (this->h
/16);
306 setSelected(x
/mult
+ (y
/multY
)*sw
);
308 GetParent()->SendMessage(WM_PALINFO
,
309 palette
[x
/mult
+(y
/multY
)*sw
],
310 paletteAddress
+(x
/mult
+(y
/multY
)*sw
));
313 BOOL
PaletteViewControl::OnEraseBkgnd(CDC
* pDC
)
319 void PaletteViewControl::OnPaint()
321 CPaintDC
dc(this); // device context for painting
324 GetClientRect(&rect
);
325 int w
= rect
.right
- rect
.left
;
326 int h
= rect
.bottom
- rect
.top
;
329 memDC
.CreateCompatibleDC(&dc
);
330 CBitmap bitmap
, *pOldBitmap
;
331 bitmap
.CreateCompatibleBitmap(&dc
, w
, h
);
332 pOldBitmap
= memDC
.SelectObject(&bitmap
);
334 StretchDIBits(memDC
.GetSafeHdc(),
347 int sw
= this->w
/ 16;
348 int sh
= this->h
/ 16;
352 pen
.CreatePen(PS_SOLID
, 1, RGB(192,192,192));
353 CPen
*old
= memDC
.SelectObject(&pen
);
355 for(i
= 1; i
< sh
; i
++) {
356 memDC
.MoveTo(0, i
* multY
);
357 memDC
.LineTo(w
, i
* multY
);
359 for(i
= 1; i
< sw
; i
++) {
360 memDC
.MoveTo(i
* mult
, 0);
361 memDC
.LineTo(i
* mult
, h
);
363 memDC
.DrawEdge(&rect
, EDGE_SUNKEN
, BF_RECT
);
364 memDC
.SelectObject(old
);
368 pen
.CreatePen(PS_SOLID
, 2, RGB(255, 0, 0));
369 old
= memDC
.SelectObject(&pen
);
371 int startX
= (selected
& (sw
-1))*mult
+1;
372 int startY
= (selected
/ sw
)*multY
+1;
373 int endX
= startX
+ mult
-2;
374 int endY
= startY
+ multY
-2;
376 memDC
.MoveTo(startX
, startY
);
377 memDC
.LineTo(endX
, startY
);
378 memDC
.LineTo(endX
, endY
);
379 memDC
.LineTo(startX
, endY
);
380 memDC
.LineTo(startX
, startY
-1);
382 memDC
.SelectObject(old
);
389 memDC
.SelectObject(pOldBitmap
);
390 bitmap
.DeleteObject();
394 void PaletteViewControl::registerClass()
398 ZeroMemory(&wc
, sizeof(wc
));
399 wc
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
400 wc
.lpfnWndProc
= (WNDPROC
)::DefWindowProc
;
401 wc
.hInstance
= AfxGetInstanceHandle();
402 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
403 wc
.hbrBackground
= (HBRUSH
)GetStockObject(BLACK_BRUSH
);
404 wc
.lpszMenuName
= NULL
;
405 wc
.lpszClassName
= "VbaPaletteViewControl";
406 AfxRegisterClass(&wc
);