[XDK]
[reactos.git] / rostests / dibtests / palbitblt / pal.c
1 /* The idea of this test app is inspired from tutorial *
2 * found at http://www.theparticle.com/pgraph.html *
3 * *
4 * Developed by: Aleksey Bragin <aleksey@studiocerebral.com> *
5 * Version: 1.0 */
6
7 #include <windows.h>
8 #include <stdlib.h>
9
10 #define W_WIDTH 320
11 #define W_HEIGHT 240
12
13 // special version of BITMAPINFO and LOGPALETTE for max of 256 palette entries
14 typedef struct
15 {
16 BITMAPINFOHEADER bmiHeader;
17 RGBQUAD bmiColors[256];
18 } BITMAPINFO256;
19
20 typedef struct {
21 WORD palVersion;
22 WORD palNumEntries;
23 PALETTEENTRY palPalEntry[256];
24 } LOGPALETTE256;
25
26 // The only global variable --- contents of the DIBitmap
27 BYTE* dibits;
28
29 void GeneratePalette(RGBQUAD* p)
30 {
31 int i;
32 for(i=0;i<256;i++)
33 {
34 p[i].rgbRed = i;
35 p[i].rgbGreen = i;
36 p[i].rgbBlue = i;
37 p[i].rgbReserved = 0;
38 }
39 }
40
41 void DoBlt(HBITMAP hBM)
42 {
43 HDC hDC,Context;
44 HWND ActiveWindow;
45 RECT dest;
46 HBITMAP dflBmp;
47
48 if((ActiveWindow = GetActiveWindow()) == NULL)
49 return;
50
51 hDC = GetDC(ActiveWindow);
52 GetClientRect(ActiveWindow,&dest);
53
54 Context = CreateCompatibleDC(0);
55 dflBmp = SelectObject(Context, hBM);
56 BitBlt(hDC, 0, 0, dest.right, dest.bottom, Context, 0, 0, SRCCOPY);
57 SelectObject(Context, dflBmp);
58 DeleteDC(Context);
59 DeleteObject(dflBmp);
60 ReleaseDC(ActiveWindow, hDC);
61 }
62
63 void UpdatePalette(HBITMAP hBM){
64 int i,y;
65 static unsigned int c=0;
66
67 for(i=0;i<W_WIDTH;i++){
68 for(y=0;y<=W_HEIGHT-1;y++)
69 dibits[y*320+i] = c % 256;
70
71 if (c > 512)
72 c = 0;
73 else
74 c++; // It's operation of incrementing of c variable, not reference of a cool OO language :-)
75 }
76
77 DoBlt(hBM);
78 }
79
80 void InitBitmap(HBITMAP *hBM){
81 HPALETTE PalHan;
82 HWND ActiveWindow;
83 HDC hDC;
84 RGBQUAD palette[256];
85 int i;
86 BITMAPINFO256 bmInf;
87 LOGPALETTE256 palInf;
88
89 ActiveWindow = GetActiveWindow();
90 hDC = GetDC(ActiveWindow);
91
92 bmInf.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
93 bmInf.bmiHeader.biWidth = W_WIDTH;
94 bmInf.bmiHeader.biHeight = -abs(W_HEIGHT);
95 bmInf.bmiHeader.biPlanes = 1;
96 bmInf.bmiHeader.biBitCount = 8;
97 bmInf.bmiHeader.biCompression = BI_RGB;
98 bmInf.bmiHeader.biSizeImage = 0;
99 bmInf.bmiHeader.biXPelsPerMeter = 0;
100 bmInf.bmiHeader.biYPelsPerMeter = 0;
101 bmInf.bmiHeader.biClrUsed = 256;
102 bmInf.bmiHeader.biClrImportant = 256;
103
104 GeneratePalette(palette);
105
106 for(i=0;i<256;i++)
107 bmInf.bmiColors[i] = palette[i];
108
109 palInf.palVersion = 0x300;
110 palInf.palNumEntries = 256;
111 for(i=0;i<256;i++){
112 palInf.palPalEntry[i].peRed = palette[i].rgbRed;
113 palInf.palPalEntry[i].peGreen = palette[i].rgbGreen;
114 palInf.palPalEntry[i].peBlue = palette[i].rgbBlue;
115 palInf.palPalEntry[i].peFlags = PC_NOCOLLAPSE;
116 }
117
118 // Create palette
119 PalHan = CreatePalette((LOGPALETTE*)&palInf);
120
121 // Select it into hDC
122 SelectPalette(hDC,PalHan,FALSE);
123
124 // Realize palette in hDC
125 RealizePalette(hDC);
126
127 // Delete handle to palette
128 DeleteObject(PalHan);
129
130 // Create dib section
131 *hBM = CreateDIBSection(hDC,(BITMAPINFO*)&bmInf,
132 DIB_RGB_COLORS,(void**)&dibits,0,0);
133
134 // Release dc
135 ReleaseDC(ActiveWindow,hDC);
136 }
137
138 LRESULT CALLBACK WndProc(HWND hWnd,UINT msg, WPARAM wParam,LPARAM lParam)
139 {
140 switch(msg){
141 case WM_DESTROY:
142 PostQuitMessage(0);
143 return 0;
144 default:
145 return DefWindowProc(hWnd,msg,wParam,lParam);
146 }
147 }
148
149
150
151 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpszCmdParam,int nCmdShow)
152 {
153 WNDCLASS WndClass;
154 HWND hWnd;
155 MSG msg;
156 char szName[] = "Palette BitBlt test";
157 BOOL exit = FALSE;
158 HBITMAP hBM;
159
160 // Create and register window class (not modified!!!!!!!!!!!1)
161 WndClass.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
162 WndClass.lpfnWndProc = WndProc;
163 WndClass.cbClsExtra = 0;
164 WndClass.cbWndExtra = 0;
165 WndClass.hbrBackground = NULL;//GetStockObject(BLACK_BRUSH);
166 WndClass.hIcon = NULL;//LoadIcon(hInstance,NULL);
167 WndClass.hCursor = NULL;//LoadCursor(NULL,IDC_ARROW);
168 WndClass.hInstance = hInstance;
169 WndClass.lpszClassName = szName;
170 WndClass.lpszMenuName = 0;
171
172 RegisterClass(&WndClass);
173
174 // Create and show window (change styles !!!!!!!!)
175 hWnd = CreateWindow(szName, "ReactOS palette bitblt test",
176 WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU,
177 CW_USEDEFAULT,CW_USEDEFAULT,W_WIDTH,W_HEIGHT,
178 0,0,hInstance,0);
179 ShowWindow(hWnd,nCmdShow);
180
181 // Prepare bitmap to be bitblt
182 InitBitmap(&hBM);
183
184 // Main message loop
185 while (!exit)
186 {
187 UpdatePalette(hBM);
188 Sleep(200);
189
190 if(PeekMessage(&msg,0,0,0,PM_NOREMOVE) == TRUE)
191 {
192 if (!GetMessage(&msg,0,0,0))
193 exit = TRUE;
194
195 TranslateMessage(&msg);
196 DispatchMessage(&msg);
197 }
198 }
199
200 return msg.wParam;
201 }