]> icculus.org git repositories - btb/d2x.git/blob - 2d/ibitblt.c
updated documentation
[btb/d2x.git] / 2d / ibitblt.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13 /*
14  *  Routines to to inverse bitblitting -- well not really.
15  *  We don't inverse bitblt like in the PC, but this code
16  *  does set up a structure that blits around the cockpit
17  *
18 */
19
20 #include <conf.h>
21
22 #include <string.h>
23 #include "pstypes.h"
24 #include "gr.h"
25 #include "grdef.h"
26 #include "ibitblt.h"
27 #include "error.h"
28 //added 05/17/99 Matt Mueller
29 #include "u_mem.h"
30 //end addition -MM
31
32 #define FIND_START              1
33 #define FIND_STOP               2
34
35 #define MAX_WIDTH                       1280    
36 #define MAX_SCANLINES           1024    
37 #define MAX_HOLES                               10
38
39 static short start_points[MAX_SCANLINES][MAX_HOLES];
40 static short hole_length[MAX_SCANLINES][MAX_HOLES];
41 static double *scanline = NULL;
42
43 // adb: gr_linear_movsd assumes c >= 4
44 #define gr_linear_movsd(s,d,c) memcpy(d,s,c)
45
46 void gr_ibitblt(grs_bitmap *src_bmp, grs_bitmap *dest_bmp, ubyte pixel_double)
47 {
48         int x, y, sw, sh, srowsize, drowsize, dstart, sy, dy;
49         ubyte *src, *dest;
50         short *current_hole, *current_hole_length;
51
52 // variable setup
53
54         sw = src_bmp->bm_w;
55         sh = src_bmp->bm_h;
56         srowsize = src_bmp->bm_rowsize;
57         drowsize = dest_bmp->bm_rowsize;
58         src = src_bmp->bm_data;
59         dest = dest_bmp->bm_data;
60
61         sy = 0;
62         while (start_points[sy][0] == -1) {
63                 sy++;
64                 dest += drowsize;
65         }
66         
67         if (pixel_double) {
68                 ubyte *scan = (ubyte *)scanline;                // set up for byte processing of scanline
69                 
70                 dy = sy;
71                 for (y = sy; y < sy + sh; y++) {
72                         gr_linear_rep_movsd_2x(src, scan, sw);
73                         current_hole = start_points[dy];
74                         current_hole_length = hole_length[dy];
75                         for (x = 0; x < MAX_HOLES; x++) {
76                                 if (*current_hole == -1)
77                                         break;
78                                 dstart = *current_hole;
79                                 gr_linear_movsd(&(scan[dstart]), &(dest[dstart]), *current_hole_length);
80                                 current_hole++;
81                                 current_hole_length++;
82                         }
83                         dy++;
84                         dest += drowsize;
85                         current_hole = start_points[dy];
86                         current_hole_length = hole_length[dy];
87                         for (x = 0;x < MAX_HOLES; x++) {
88                                 if (*current_hole == -1)
89                                         break;
90                                 dstart = *current_hole;
91                                 gr_linear_movsd(&(scan[dstart]), &(dest[dstart]), *current_hole_length);
92                                 current_hole++;
93                                 current_hole_length++;
94                         }
95                         dy++;
96                         dest += drowsize;
97                         src += srowsize;
98                 }
99         } else {
100                 Assert(sw <= MAX_WIDTH);
101                 Assert(sh <= MAX_SCANLINES);
102                 for (y = sy; y < sy + sh; y++) {
103                         for (x = 0; x < MAX_HOLES; x++) {
104                                 if (start_points[y][x] == -1)
105                                         break;
106                                 dstart = start_points[y][x];
107                                 gr_linear_movsd(&(src[dstart]), &(dest[dstart]), hole_length[y][x]);
108                         }
109                         dest += drowsize;
110                         src += srowsize;
111                 }
112         }
113 }
114
115 void gr_ibitblt_create_mask(grs_bitmap *mask_bmp, int sx, int sy, int sw, int sh, int srowsize)
116 {
117         int x, y;
118         ubyte mode;
119         int count = 0;
120         
121         Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
122
123         for (y = 0; y < MAX_SCANLINES; y++) {
124                 for (x = 0; x < MAX_HOLES; x++) {
125                         start_points[y][x] = -1;
126                         hole_length[y][x] = -1;
127                 }
128         }
129         
130         for (y = sy; y < sy+sh; y++) {
131                 count = 0;
132                 mode = FIND_START;
133                 for (x = sx; x < sx + sw; x++) {
134                         if ((mode == FIND_START) && (mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x] == TRANSPARENCY_COLOR)) {
135                                 start_points[y][count] = x;
136                                 mode = FIND_STOP;
137                         } else if ((mode == FIND_STOP) && (mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x] != TRANSPARENCY_COLOR)) {
138                                 hole_length[y][count] = x - start_points[y][count];
139                                 count++;
140                                 mode = FIND_START;
141                         }
142                 }
143                 if (mode == FIND_STOP) {
144                         hole_length[y][count] = x - start_points[y][count];
145                         count++;
146                 }
147                 Assert(count <= MAX_HOLES);
148         }
149 }
150
151 //added 7/11/99 by adb to prevent memleaks
152 static void free_scanline(void)
153 {
154         if (scanline) d_free(scanline);
155 }
156 //end additions - adb
157
158
159 void gr_ibitblt_find_hole_size(grs_bitmap *mask_bmp, int *minx, int *miny, int *maxx, int *maxy)
160 {
161         ubyte c;
162         int x, y, count = 0;
163         
164         Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
165         Assert( mask_bmp->bm_flags&BM_FLAG_TRANSPARENT );
166         
167         *minx = mask_bmp->bm_w - 1;
168         *maxx = 0;
169         *miny = mask_bmp->bm_h - 1;
170         *maxy = 0;
171
172         //changed 7/11/99 by adb to prevent memleaks
173         if (scanline == NULL) {
174                 scanline = (double *)d_malloc(sizeof(double) * (MAX_WIDTH / sizeof(double)));
175                 atexit(free_scanline);
176         }
177         //end changes - adb
178                 
179         for (y = 0; y < mask_bmp->bm_h; y++) {
180                 for (x = 0; x < mask_bmp->bm_w; x++) {
181                         c = mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x];
182                         if (c == TRANSPARENCY_COLOR) {                          // don't look for transparancy color here.
183                                 count++;
184                                 if (x < *minx) *minx = x;
185                                 if (y < *miny) *miny = y;
186                                 if (x > *maxx) *maxx = x;
187                                 if (y > *maxy) *maxy = y;
188                         }
189                 }
190         }
191         Assert (count);
192 }