1 /* $Id: winBitmap.c,v 5.0 2001/04/07 20:00:59 dik Exp $
2  *
3  * XPilot, a multiplayer gravity war game.  Copyright (C) 1991-2001 by
4  *
5  *      Bj�rn Stabell        <bjoern@xpilot.org>
6  *      Ken Ronny Schouten   <ken@xpilot.org>
7  *      Bert Gijsbers        <bert@xpilot.org>
8  *      Dick Balaska         <dick@xpilot.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #include "../../common/NT/winX.h"
25 #include "../blockbitmaps.h"
26 #include "winbitmap.h"
27 
28 #include "../gfx2d.h"
29 #include "../../common/NT/winX_.h"
30 #include <math.h>
31 
32 #include "../../common/error.h"
33 #include "./../common/const.h"
34 #include "../paint.h"
35 #include "../protoclient.h"
36 #include "../../common/setup.h"
37 #include "../../common/bit.h"
38 
39 /* for blitting items onto the screen */
40 extern HDC			itemsDC;
41 
42 /* very unfortunate, but about the only way i could do this */
43 HDC hDCb;
44 HDC hDC;
45 
46 xp_picture_t radar_colors;
47 
48 extern int blockBitmaps;
Colors_init_block_bitmaps(void)49 int Colors_init_block_bitmaps(void)
50 {
51 //    blockBitmaps = false;
52     if (blockBitmaps) {
53 	Block_bitmaps_create();
54     }
55 
56     return (blockBitmaps == true) ? 0 : -1;
57 
58 }
Colors_free_block_bitmaps(void)59 void Colors_free_block_bitmaps(void)
60 {
61 }
62 
delete_bitmaps()63 void delete_bitmaps()
64 {
65     int i,j;
66     SelectObject(itemsDC, GetStockObject(BLACK_PEN));
67 
68     for (i = 0; i < NUM_BITMAPS; i++) {
69 	for (j = 0; j < xp_pixmaps[i].picture.images; j++) {
70 	    if (xp_pixmaps[i].bitmaps[j].bitmap) {
71 		DeleteObject((HBITMAP)xp_pixmaps[i].bitmaps[j].bitmap);
72 	    }
73 	}
74     }
75 }
Block_bitmap_create_begin(Drawable d,xp_pixmap_t * xp_pixmap,int image,int width,int height)76 void Block_bitmap_create_begin(Drawable d,xp_pixmap_t *xp_pixmap, int image,
77 			 int width, int height)
78 {
79     HBITMAP hbm;
80     hDC = GetDC(xid[d].hwnd.hWnd);
81     hDCb = CreateCompatibleDC(hDC);
82 
83     if (xp_pixmap->bitmaps[image].bitmap) {
84         SelectObject(itemsDC, GetStockObject(BLACK_PEN));
85 	DeleteObject((HBITMAP)xp_pixmap->bitmaps[image].bitmap);
86     }
87 
88     hbm = CreateCompatibleBitmap(hDC, width,
89 				 height);
90     SelectObject(hDCb, hbm);
91     if (bHasPal)
92     {
93         SelectPalette(hDCb, myPal, FALSE);
94         RealizePalette(hDCb);
95     }
96 
97     if (!hbm)
98         error("Can't create item bitmaps");
99     xp_pixmap->bitmaps[image].bitmap = (Pixmap)hbm;
100 }
101 
Block_bitmap_create_end(Drawable d)102 void Block_bitmap_create_end(Drawable d)
103 {
104     DeleteDC(hDCb);
105     ReleaseDC(xid[d].hwnd.hWnd, hDC);
106 }
107 
Block_bitmap_set_pixel(xp_pixmap_t * xp_pixmap,int image,int x,int y,RGB_COLOR color)108 void Block_bitmap_set_pixel(xp_pixmap_t *xp_pixmap, int image, int x, int y, RGB_COLOR color)
109 {
110     SetPixelV(hDCb, x, y, color);
111 }
112 
Block_bitmap_paint(Drawable d,int type,int x,int y,int width,int height,int number)113 void Block_bitmap_paint(Drawable d, int type, int x, int y, int width, int height,
114 		 int number)
115 {
116     HDC		hDC = xid[d].hwnd.hBmpDC;
117 
118     SelectObject(itemsDC, (HBITMAP)xp_pixmaps[type].bitmaps[number].bitmap);
119 
120     if (bHasPal)
121     {
122 	SelectPalette(itemsDC, myPal, FALSE);
123 	RealizePalette(itemsDC);
124     }
125     BitBlt(hDC, x, y, width, height, itemsDC, 0, 0, SRCPAINT);
126 }
127 
128 
129 /****************************IRRELEVANT STUFF HERE ***************************/
130 
131 
radar_light(int * radar,int x,int y)132 int radar_light(int *radar, int x, int y)
133 {
134 /*   int light_matrix[25] ={ 0, -1, -2, -2, -3,
135 			   1,  0, -1, -2, -2,
136 			   2,  1,  0, -1, -2,
137 			   2,  2,  1,  0, -1,
138 			   3,  2,  2,  1,  0
139 			};
140 */
141 
142    int light_matrix[25] ={ 0,  1,  2,  1, 0,
143 			   1,  2,  3,  2, 1,
144 			   2,  3,  0,  3, 2,
145 			   1,  2,  3,  2, 1,
146 			   0,  1,  2,  1, 0
147 			};
148 /*   int light_matrix[25] ={ 0,  1,  -1, -1,  0,
149 			   1,  2,  -1, -2, -1,
150 			   2,  3,  0, -2, -2,
151 			   1,  2,  3,  -1, -1,
152 			   0,  1,  2,  1, 0
153 			};
154 */
155     int xi, yi, light;
156     int cy, cx;
157 
158    light = 0;
159    for (xi = -2; xi < 3; xi ++) {
160 	cx = (xi + x + 256) %256;
161        for (yi = -2; yi < 3; yi ++) {
162 	    cy = (yi + y + RadarHeight) % RadarHeight;
163 	    if (radar[cy * 256 + cx])
164 		light += light_matrix[(xi + 2) + ((yi + 2) *5)];
165        }
166    }
167    return light /* + 20 */;
168 }
169 
170 
Winpaint_world_radar()171 void Winpaint_world_radar()
172 {
173     HDC		hDC = xid[s_radar].hwnd.hBmpDC;
174     int radar[256 * 300];
175     int cr, cg, cb;
176     int			i, xi, yi, xm, ym, xp, yp = 0, xmoff;
177     int			type, vis;
178     float		xs, ys;
179     int			npoint = 0, nsegment = 0, start, end;
180     const int		max = 256;
181     u_byte		visible[256], visibleColor[256];
182     int light;
183     RGB_COLOR	color;
184     radar_exposures = 2;
185 
186     memset(visible, 0, sizeof visible);
187     visible[SETUP_FILLED] = 1;
188     visible[SETUP_FILLED_NO_DRAW] = 1;
189     visible[SETUP_REC_LU] = 1;
190     visible[SETUP_REC_RU] = 1;
191     visible[SETUP_REC_LD] = 1;
192     visible[SETUP_REC_RD] = 1;
193     visible[SETUP_FUEL] = 1;
194     for (i = 0; i < 10; i++) {
195 	visible[SETUP_TARGET+i] = 1;
196     }
197     for (i = BLUE_BIT; i < sizeof visible; i++) {
198 	visible[i] = 1;
199     }
200     if (BIT(instruments, SHOW_DECOR)) {
201 	visible[SETUP_DECOR_FILLED] = 1;
202 	visible[SETUP_DECOR_LU] = 1;
203 	visible[SETUP_DECOR_RU] = 1;
204 	visible[SETUP_DECOR_LD] = 1;
205 	visible[SETUP_DECOR_RD] = 1;
206     }
207 
208     memset(visibleColor, 0, sizeof visibleColor);
209     visibleColor[SETUP_FILLED] =
210 	visibleColor[SETUP_FILLED_NO_DRAW] =
211 	visibleColor[SETUP_REC_LU] =
212 	visibleColor[SETUP_REC_RU] =
213 	visibleColor[SETUP_REC_LD] =
214 	visibleColor[SETUP_REC_RD] =
215 	visibleColor[SETUP_FUEL] = wallRadarColor;
216     for (i = 0; i < 10; i++) {
217 	visibleColor[SETUP_TARGET+i] = targetRadarColor;
218     }
219     for (i = BLUE_BIT; i < sizeof visible; i++) {
220 	visibleColor[i] = wallRadarColor;
221     }
222     if (BIT(instruments, SHOW_DECOR)) {
223 	visibleColor[SETUP_DECOR_FILLED] =
224 	    visibleColor[SETUP_DECOR_LU] =
225 	    visibleColor[SETUP_DECOR_RU] =
226 	    visibleColor[SETUP_DECOR_LD] =
227 	    visibleColor[SETUP_DECOR_RD] = decorRadarColor;
228     }
229 
230     xs = (float)(Setup->x - 1) / (256 - 1);
231     ys = (float)(Setup->y - 1) / (RadarHeight - 1);
232     for (xi = 0; xi < 256; xi++) {
233 	xm = (int)(xi * xs + 0.5);
234 	xmoff = xm * Setup->y;
235 	start = end = -1;
236 	xp = xi;
237 	for (yi = 0; yi < RadarHeight; yi++) {
238 	    ym = (int)(yi * ys + 0.5);
239 	    type = Setup->map_data[xmoff + ym];
240 	    vis = visible[type];
241 	    radar[((RadarHeight - 1) - yi) * 256 + xi] = vis;
242 	}
243     }
244     for (xi = 0; xi < 256; xi++) {
245 	for (yi = 0; yi < RadarHeight; yi++) {
246 	    light = radar_light(radar, xi, yi);
247 	    color = Picture_get_pixel(&radar_colors, 0,
248 			xi % RADAR_TEXTURE_SIZE,
249 			yi % RADAR_TEXTURE_SIZE);
250 	    cr = RED_VALUE(color);
251 	    cg = GREEN_VALUE(color);
252 	    cb = BLUE_VALUE(color);
253 	    if (radar[yi * 256 + xi]) {
254 		SetPixelV(hDC, xi, yi, RGB( (cr * light) /36,
255 		    (cg * light) / 36, (cb * light) / 36));
256 	    } else {
257 		SetPixelV(hDC, xi, yi, RGB( (cr * (20 + light)) /72,
258 		    (cg * (20 + light)) / 72, (cb * (20 +light)) / 72));
259 	    }
260 	}
261     }
262 
263     for (i = 0;; i++) {
264 	int dead_time, damage;
265 	if (Target_by_index(i, &xi, &yi, &dead_time, &damage) == -1) {
266 	    break;
267 	}
268 	if (dead_time) {
269 	    continue;
270 	}
271 	Paint_radar_block(xi, yi, targetRadarColor);
272     }
273 }
274 
Block_bitmap_paint_fuel_slice(Drawable d,int type,int x,int y,int width,int height,int image,int size)275 void Block_bitmap_paint_fuel_slice(Drawable d, int type, int x, int y, int width, int height, int image, int size)
276 {
277 
278     HDC		hDC = xid[d].hwnd.hBmpDC;
279 
280     SelectObject(itemsDC, (HBITMAP)xp_pixmaps[type].bitmaps[image].bitmap);
281 
282     if (bHasPal)
283     {
284 	SelectPalette(itemsDC, myPal, FALSE);
285 	RealizePalette(itemsDC);
286     }
287 
288     BitBlt(hDC, x, y, width, size, itemsDC, 0, 0, SRCPAINT);
289 
290 }
291 
292 
Block_bitmap_paint_meter(Drawable d,int type,int x,int y,int width,int height,int size)293 void Block_bitmap_paint_meter(Drawable d, int type, int x, int y, int width, int height, int size)
294 {
295     HDC		hDC = xid[d].hwnd.hBmpDC;
296 
297     SelectObject(itemsDC, (HBITMAP)xp_pixmaps[type].bitmaps[1].bitmap);
298 
299     if (bHasPal)
300     {
301 	SelectPalette(itemsDC, myPal, FALSE);
302 	RealizePalette(itemsDC);
303     }
304 
305     BitBlt(hDC, x, y, size, height, itemsDC, 0, 0, SRCPAINT);
306 
307     SelectObject(itemsDC, (HBITMAP)xp_pixmaps[type].bitmaps[0].bitmap);
308 
309     if (bHasPal)
310     {
311 	SelectPalette(itemsDC, myPal, FALSE);
312 	RealizePalette(itemsDC);
313     }
314 
315     BitBlt(hDC, x + size, y, width - size, height, itemsDC, size, 0, SRCPAINT);
316 
317 }
318 /*
319 void paintItemSymbol(unsigned char type, Drawable d, GC gc, int x, int y, int color)
320 {
321     PaintBitmap(d, BM_ALL_ITEMS, x, y, WINSCALE(16), WINSCALE(16), type);
322 //    PaintBitmap(d, BM_ALL_ITEMS, x, y, 16, 16, type);
323 }
324 */
325