]> icculus.org git repositories - crow/jumpnbump.git/blob - dos/gfx.c
836ee5415c314b8f5d27fbe7cec13459e12bbb58
[crow/jumpnbump.git] / dos / gfx.c
1 #include "globals.h"
2
3
4 void open_screen(void)
5 {
6         __dpmi_regs regs;
7         char *ptr1;
8
9         regs.x.ax = 0x13;
10         __dpmi_int(0x10, &regs);
11
12         outportw(0x3c4, 0x0604);
13         outportw(0x3c4, 0x0100);
14         outportb(0x3c2, 0xe7);
15         outportw(0x3c4, 0x0300);
16
17         outportb(0x3d4, 0x11);
18         outportb(0x3d5, inportb(0x3d5) & 0x7f);
19
20         outportw(0x3d4, 0x7100);
21         outportw(0x3d4, 0x6301);
22         outportw(0x3d4, 0x6402);
23         outportw(0x3d4, 0x9203);
24         outportw(0x3d4, 0x6604);
25         outportw(0x3d4, 0x8205);
26         outportw(0x3d4, 0x2b06);
27         outportw(0x3d4, 0xb207);
28         outportw(0x3d4, 0x0008);
29         outportw(0x3d4, 0x6109);
30         outportw(0x3d4, 0x1310);
31         outportw(0x3d4, 0xac11);
32         outportw(0x3d4, 0xff12);
33         outportw(0x3d4, 0x3213);
34         outportw(0x3d4, 0x0014);
35         outportw(0x3d4, 0x0715);
36         outportw(0x3d4, 0x1a16);
37         outportw(0x3d4, 0xe317);
38
39         outportw(0x3d4, 0x3213);
40
41         ptr1 = (char *) (0xa0000 + __djgpp_conventional_base);
42         outportw(0x3c4, 0x0f02);
43         memset(ptr1, 0, 65535);
44
45 }
46
47
48 void wait_vrt(void)
49 {
50
51         while ((inportb(0x3da) & 8) == 0);
52         while ((inportb(0x3da) & 8) == 8);
53
54 }
55
56
57 #if 0
58 void get_block(char page, short x, short y, short width, short height, char *buffer)
59 {
60         short c1, c2, c3;
61         char *buffer_ptr, *vga_ptr;
62
63         for (c3 = 0; c3 < 4; c3++) {
64                 outportw(0x3ce, (((x + c3) & 3) << 8) + 0x04);
65                 for (c1 = 0; (c1 + c3) < width; c1 += 4) {
66                         buffer_ptr = &buffer[(c1 + c3) * height];
67                         vga_ptr = (char *) (0xa0000 + ((long) page << 15) + (long) y * 100 + ((x + c1 + c3) >> 2) + __djgpp_conventional_base);
68                         for (c2 = 0; c2 < height; c2++) {
69                                 *buffer_ptr = *vga_ptr;
70                                 buffer_ptr++;
71                                 vga_ptr += 100;
72                         }
73                 }
74         }
75
76 }
77 #endif
78
79 #if 0
80 void put_block(char page, short x, short y, short width, short height, char *buffer)
81 {
82         short c1, c2, c3;
83         char *vga_ptr, *buffer_ptr;
84
85         for (c3 = 0; c3 < 4; c3++) {
86                 outportw(0x3c4, ((1 << ((x + c3) & 3)) << 8) + 0x02);
87                 for (c1 = 0; (c1 + c3) < width; c1 += 4) {
88                         vga_ptr = (char *) (0xa0000 + ((long) page << 15) + (long) y * 100 + ((x + c1 + c3) >> 2) + __djgpp_conventional_base);
89                         buffer_ptr = &buffer[(c1 + c3) * height];
90                         for (c2 = 0; c2 < height; c2++) {
91                                 *vga_ptr = *buffer_ptr;
92                                 vga_ptr += 100;
93                                 buffer_ptr++;
94                         }
95                 }
96         }
97
98 }
99 #endif
100
101 void put_text(char page, int x, int y, char *text, char align)
102 {
103         int c1;
104         int t1;
105         int width;
106         int cur_x;
107         int image;
108
109         if (text == NULL || strlen(text) == 0)
110                 return;
111         if (font_gobs == NULL)
112                 return;
113
114         width = 0;
115         c1 = 0;
116         while (text[c1] != 0) {
117                 t1 = text[c1];
118                 c1++;
119                 if (t1 == ' ') {
120                         width += 5;
121                         continue;
122                 }
123                 if (t1 >= 33 && t1 <= 34)
124                         image = t1 - 33;
125                 else if (t1 >= 39 && t1 <= 41)
126                         image = t1 - 37;
127                 else if (t1 >= 44 && t1 <= 59)
128                         image = t1 - 39;
129                 else if (t1 >= 64 && t1 <= 90)
130                         image = t1 - 43;
131                 else if (t1 >= 97 && t1 <= 122)
132                         image = t1 - 49;
133                 else if (t1 == '~')
134                         image = 74;
135                 else if (t1 == '\84')
136                         image = 75;
137                 else if (t1 == '\86')
138                         image = 76;
139                 else if (t1 == '\8e')
140                         image = 77;
141                 else if (t1 == '\8f')
142                         image = 78;
143                 else if (t1 == '\94')
144                         image = 79;
145                 else if (t1 == '\99')
146                         image = 80;
147                 else
148                         continue;
149                 width += pob_width(image, font_gobs) + 1;
150         }
151
152         switch (align) {
153         case 0:
154                 cur_x = x;
155                 break;
156         case 1:
157                 cur_x = x - width;
158                 break;
159         case 2:
160                 cur_x = x - width / 2;
161                 break;
162         }
163         c1 = 0;
164         while (text[c1] != 0) {
165                 t1 = text[c1];
166                 c1++;
167                 if (t1 == ' ') {
168                         cur_x += 5;
169                         continue;
170                 }
171                 if (t1 >= 33 && t1 <= 34)
172                         image = t1 - 33;
173                 else if (t1 >= 39 && t1 <= 41)
174                         image = t1 - 37;
175                 else if (t1 >= 44 && t1 <= 59)
176                         image = t1 - 39;
177                 else if (t1 >= 64 && t1 <= 90)
178                         image = t1 - 43;
179                 else if (t1 >= 97 && t1 <= 122)
180                         image = t1 - 49;
181                 else if (t1 == '~')
182                         image = 74;
183                 else if (t1 == '\84')
184                         image = 75;
185                 else if (t1 == '\86')
186                         image = 76;
187                 else if (t1 == '\8e')
188                         image = 77;
189                 else if (t1 == '\8f')
190                         image = 78;
191                 else if (t1 == '\94')
192                         image = 79;
193                 else if (t1 == '\99')
194                         image = 80;
195                 else
196                         continue;
197                 put_pob(page, cur_x, y, image, font_gobs, 1, mask_pic);
198                 cur_x += pob_width(image, font_gobs) + 1;
199         }
200
201 }
202
203
204 void put_pob(char page, short x, short y, short image, char *pob_data, char mask, char *mask_pic)
205 {
206         long c1, c2, c3;
207         long pob_offset;
208         char *pob_ptr, *vga_ptr, *mask_ptr;
209         long width, height;
210         long draw_width, draw_height;
211         char colour;
212
213         if (image < 0 || image >= *(short *) (pob_data))
214                 return;
215
216         pob_offset = *(long *) (pob_data + image * 4 + 2);
217
218         width = draw_width = *(short *) (pob_data + pob_offset);
219         height = draw_height = *(short *) (pob_data + pob_offset + 2);
220         x -= *(short *) (pob_data + pob_offset + 4);
221         y -= *(short *) (pob_data + pob_offset + 6);
222
223         pob_offset += 8;
224
225         if ((x + width) <= 0 || x >= 400)
226                 return;
227         if ((y + height) <= 0 || y >= 256)
228                 return;
229         if (x < 0) {
230                 pob_offset -= x;
231                 draw_width += x;
232                 x = 0;
233         }
234         if ((x + width) > 400)
235                 draw_width -= x + width - 400;
236         if (y < 0) {
237                 pob_offset += -y * width;
238                 draw_height -= -y;
239                 y = 0;
240         }
241         if ((y + height) > 256)
242                 draw_height -= y + height - 256;
243
244         for (c3 = 0; c3 < 4; c3++) {
245                 outportw(0x3c4, ((1 << ((x + c3) & 3)) << 8) + 0x02);
246                 pob_ptr = &pob_data[pob_offset + c3];
247                 vga_ptr = (char *) (0xa0000 + (long) (page << 15) + (long) y * 100L + ((x + c3) >> 2) + __djgpp_conventional_base);
248                 mask_ptr = (char *) (mask_pic + (long) y * 400L + x + c3);
249                 for (c1 = 0; c1 < draw_height; c1++) {
250                         for (c2 = c3; c2 < draw_width; c2 += 4) {
251                                 colour = *mask_ptr;
252                                 if (mask == 0 || (mask == 1 && colour == 0)) {
253                                         colour = *pob_ptr;
254                                         if (colour != 0)
255                                                 *vga_ptr = colour;
256                                 }
257                                 pob_ptr += 4;
258                                 vga_ptr++;
259                                 mask_ptr += 4;
260                         }
261                         pob_ptr += width - c2 + c3;
262                         vga_ptr += (400 - c2 + c3) >> 2;
263                         mask_ptr += 400 - c2 + c3;
264                 }
265         }
266
267 }
268
269
270 char pob_col(short x1, short y1, short image1, char *pob_data1, short x2, short y2, short image2, char *pob_data2)
271 {
272         short c1, c2;
273         long pob_offset1, pob_offset2;
274         short width1, width2;
275         short height1, height2;
276         short check_width, check_height;
277         char *pob_ptr1, *pob_ptr2;
278
279         pob_offset1 = *(long *) (pob_data1 + image1 * 4 + 2);
280         width1 = *(short *) (pob_data1 + pob_offset1);
281         height1 = *(short *) (pob_data1 + pob_offset1 + 2);
282         x1 -= *(short *) (pob_data1 + pob_offset1 + 4);
283         y1 -= *(short *) (pob_data1 + pob_offset1 + 6);
284         pob_offset1 += 8;
285
286         pob_offset2 = *(long *) (pob_data2 + image2 * 4 + 2);
287         width2 = *(short *) (pob_data2 + pob_offset2);
288         height2 = *(short *) (pob_data2 + pob_offset2 + 2);
289         x2 -= *(short *) (pob_data2 + pob_offset2 + 4);
290         y2 -= *(short *) (pob_data2 + pob_offset2 + 6);
291         pob_offset2 += 8;
292
293         if (x1 < x2) {
294                 if ((x1 + width1) <= x2)
295                         return 0;
296                 else if ((x1 + width1) <= (x2 + width2)) {
297                         pob_offset1 += x2 - x1;
298                         check_width = x1 + width1 - x2;
299                 } else {
300                         pob_offset1 += x2 - x1;
301                         check_width = width2;
302                 }
303         } else {
304                 if ((x2 + width2) <= x1)
305                         return 0;
306                 else if ((x2 + width2) <= (x1 + width1)) {
307                         pob_offset2 += x1 - x2;
308                         check_width = x2 + width2 - x1;
309                 } else {
310                         pob_offset2 += x1 - x2;
311                         check_width = width1;
312                 }
313         }
314         if (y1 < y2) {
315                 if ((y1 + height1) <= y2)
316                         return 0;
317                 else if ((y1 + height1) <= (y2 + height2)) {
318                         pob_offset1 += (y2 - y1) * width1;
319                         check_height = y1 + height1 - y2;
320                 } else {
321                         pob_offset1 += (y2 - y1) * width1;
322                         check_height = height2;
323                 }
324         } else {
325                 if ((y2 + height2) <= y1)
326                         return 0;
327                 else if ((y2 + height2) <= (y1 + height1)) {
328                         pob_offset2 += (y1 - y2) * width2;
329                         check_height = y2 + height2 - y1;
330                 } else {
331                         pob_offset2 += (y1 - y2) * width2;
332                         check_height = height1;
333                 }
334         }
335
336         pob_ptr1 = (char *) (pob_data1 + pob_offset1);
337         pob_ptr2 = (char *) (pob_data2 + pob_offset2);
338         for (c1 = 0; c1 < check_height; c1++) {
339                 for (c2 = 0; c2 < check_width; c2++) {
340                         if (*pob_ptr1 != 0 && *pob_ptr2 != 0)
341                                 return 1;
342                         pob_ptr1++;
343                         pob_ptr2++;
344                 }
345                 pob_ptr1 += width1 - check_width;
346                 pob_ptr2 += width2 - check_width;
347         }
348
349         return 0;
350
351 }
352
353
354 short pob_width(short image, char *pob_data)
355 {
356         return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2));
357 }
358
359
360 short pob_height(short image, char *pob_data)
361 {
362         return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2) + 2);
363 }
364
365
366 short pob_hs_x(short image, char *pob_data)
367 {
368         return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2) + 4);
369 }
370
371
372 short pob_hs_y(short image, char *pob_data)
373 {
374         return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2) + 6);
375 }
376
377
378 char read_pcx(FILE * handle, char *buffer, long buf_len, char *pal)
379 {
380         short c1;
381         short a, b;
382         long ofs1;
383
384         if (buffer != 0) {
385                 fseek(handle, 128, SEEK_CUR);
386
387                 ofs1 = 0;
388
389                 while (ofs1 < buf_len) {
390                         a = fgetc(handle);
391                         if ((a & 0xc0) == 0xc0) {
392                                 b = fgetc(handle);
393                                 a &= 0x3f;
394                                 for (c1 = 0; c1 < a; c1++)
395                                         buffer[ofs1++] = b;
396                         } else
397                                 buffer[ofs1++] = a;
398                 }
399
400                 if (pal != 0) {
401                         fseek(handle, 1, SEEK_CUR);
402                         for (c1 = 0; c1 < 768; c1++)
403                                 pal[c1] = fgetc(handle) >> 2;
404                 }
405
406         }
407
408         fclose(handle);
409         return 0;
410 }