1 /*
2    Copyright (C) 1996-2017 Paul Sheer
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307, USA.
18  */
19 
20 #include <config.h>
21 
22 #ifndef DO_NOT_USE_VGALIB
23 #include <vga.h>
24 #endif
25 
26 #include <vgagl.h>
27 #include "triangle.h"
28 #include <stdio.h>
29 #include "mad.h"
30 
31 #define SHLB 8
32 #define SHC 0
33 #ifdef WRAP
34 #define S_MASK 0x01ff00
35 #endif
36 
37 
38 #ifdef byte
39 #undef byte
40 #endif
41 #define byte unsigned char
42 
43 #ifdef word
44 #undef word
45 #endif
46 #define word unsigned short
47 
48 #ifdef u_32bit_t
49 #undef u_32bit_t
50 #endif
51 
52 #ifdef INT_IS_16_BITS
53 #define u_32bit_t unsigned long
54 #else
55 #define u_32bit_t unsigned int
56 #endif
57 
58 
59 /* this assumes that BYTEWIDTH is not necessarily equal to bytes-per-pixel times WIDTH */
60 
61 #define assignvpoffset8(x, y, vp) vp = (y) * BYTEWIDTH + (x);
62 #define assignvpoffset16(x, y, vp) vp = (y) * BYTEWIDTH + ((x) << 1);
63 #define assignvpoffset24(x, y, vp) vp = (y) * BYTEWIDTH + (x) * 3;
64 #define assignvpoffset32(x, y, vp) vp = (y) * BYTEWIDTH + ((x) << 2);
65 
66 #define declarevp8 byte *vpbyte = (byte *) VBUF
67 #define declarevp16 word *vpword = (word *) VBUF
68 #define declarevp24 byte *vpbyte = (byte *) VBUF
69 #define declarevp32 u_32bit_t *vpquad = (u_32bit_t *) VBUF
70 
71 #define assignvp8(x, y, vp) vpbyte = (byte *) VBUF + (y) * BYTEWIDTH + (x);
72 #define assignvp16(x, y, vp) vpword = (word *) ((byte *) VBUF + (y) * BYTEWIDTH) + (x);
73 #define assignvp24(x, y, vp) vpbyte = (byte *) VBUF + (y) * BYTEWIDTH + (x) * 3;
74 #define assignvp32(x, y, vp) vpquad = (u_32bit_t *) ((byte *) VBUF + (y) * BYTEWIDTH) + (x);
75 
76 /* here we would like to have a single void pointer and cast it to byte, word or
77 u_32bit_t, but ansi does not allow casts on LHS   :(   */
78 
79 #define decvp8 *(--(vpbyte)) = lookup(color)
80 #define incvp8 *((vpbyte)++) = lookup(color)
81 #define decvp16 *(--(vpword)) = lookup(color)
82 #define incvp16 *((vpword)++) = lookup(color)
83 #define decvp24 *(--(vpbyte)) = lookup(color) >> 16; \
84 		*(--(vpbyte)) = lookup(color) >> 8; \
85 		*(--(vpbyte)) = lookup(color);
86 #define incvp24 *((vpbyte)++) = lookup(color); \
87 		*((vpbyte)++) = lookup(color) >> 8; \
88 		*((vpbyte)++) = lookup(color) >> 16;
89 #define decvp32 *(--(vpquad)) = lookup(color)
90 #define incvp32 *((vpquad)++) = lookup(color)
91 
92 #define decvpoffset8 \
93 		if (!offst--) \
94 		    vga_setpage (--pg); \
95 		*(vpbyte + offst) = lookup(color);
96 #define incvpoffset8 \
97 		*(vpbyte + offst) = lookup(color); \
98 		if (!(++offst)) \
99 		    vga_setpage (++pg);
100 #define decvpoffset16 \
101 		if (!offst) \
102 		    vga_setpage (--pg); \
103 		offst -= 2; \
104 		*(vpword + offst) = lookup(color);
105 #define incvpoffset16 \
106 		*(vpword + offst) = lookup(color); \
107 		offst += 2; \
108 		if (!offst) \
109 		    vga_setpage (++pg);
110 #define decvpoffset24 \
111 		if (!offst--) \
112 		    vga_setpage (--pg); \
113 		*(vpbyte + offst) = lookup(color) >> 16; \
114 		if (!offst--) \
115 		    vga_setpage (--pg); \
116 		*(vpbyte + offst) = lookup(color) >> 8; \
117 		if (!offst--) \
118 		    vga_setpage (--pg); \
119 		*(vpbyte + offst) = lookup(color);
120 #define incvpoffset24 \
121 		*(vpbyte + offst) = lookup(color); \
122 		if (!(++offst)) \
123 		    vga_setpage (++pg); \
124 		*(vpbyte + offst) = lookup(color) >> 8; \
125 		if (!(++offst)) \
126 		    vga_setpage (++pg); \
127 		*(vpbyte + offst) = lookup(color) >> 16; \
128 		if (!(++offst)) \
129 		    vga_setpage (++pg);
130 #define decvpoffset32 \
131 		if (!offst) \
132 		    vga_setpage (--pg); \
133 		offst -= 4; \
134 		*(vpquad + offst) = lookup(color);
135 #define incvpoffset32 \
136 		*(vpquad + offst) = lookup(color); \
137 		offst += 4; \
138 		if (!offst) \
139 		    vga_setpage (++pg);
140 
141 
142 static int px1, px2, py;
143 static long c;
144 
145 #ifdef INTERP
146 static long c_x;
147 #endif
148 
149 #ifdef WRAP
150 static long xd, xd_x, yd, yd_x;
151 static unsigned char *dat;
152 #endif
153 
154 static long dx0, dy0;
155 
156 #if defined(WRAP) && defined(INTERP)
157 
158 /* this must only occur once */
159 
160 long color_lookup[TRIANGLE_COLOR_LOOKUP_TABLE_SIZE];
161 static long *_color_lookup;
162 
gl_trisetcolorlookup(int i,long color)163 void gl_trisetcolorlookup (int i, long color)
164 {
165     if(i < TRIANGLE_COLOR_LOOKUP_TABLE_SIZE)
166 	color_lookup[i] = color;
167 }
168 
169 void (*tri_drawpoint) (int, int, int);
170 static void (*_tri_drawpoint) (int, int, int);
171 
gl_trisetdrawpoint(void (* draw_point)(int,int,int))172 void gl_trisetdrawpoint(void (*draw_point) (int, int, int))
173 {
174     tri_drawpoint = draw_point;
175 }
176 
177 int override_8bit_non_lookup = 0;
178 
179 #else
180 
181 extern long color_lookup[TRIANGLE_COLOR_LOOKUP_TABLE_SIZE];
182 static long *_color_lookup;
183 extern void (*tri_drawpoint) (int, int, int);
184 static void (*_tri_drawpoint) (int, int, int);
185 extern int override_8bit_non_lookup;
186 
187 #endif /* this static is just because static is faster than ordinary array (so I hear) in DLL's */
188 
189 
190 #define TRI_BPP 8
191 #include "trisetpixel.c"
192 
193 #undef TRI_BPP
194 /* `9' is a pseudonam for 8lookup */
195 #define TRI_BPP 9
196 #include "trisetpixel.c"
197 
198 #undef TRI_BPP
199 #define TRI_BPP 16
200 #include "trisetpixel.c"
201 
202 #undef TRI_BPP
203 #define TRI_BPP 24
204 #include "trisetpixel.c"
205 
206 #undef TRI_BPP
207 #define TRI_BPP 32
208 #include "trisetpixel.c"
209 
210 #undef TRI_BPP
211 
212 
213 /*
214    #define CONTEXT_VIRTUAL              0x0
215    #define CONTEXT_PAGED                0x1
216    #define CONTEXT_LINEAR               0x2
217    #define CONTEXT_MODEX                0x3
218    #define CONTEXT_PLANAR16             0x4
219  */
220 
221 static void (*linefuncs[80]) (void) =
222 {
223 	colhline_pos_direct8,
224 	colhline_neg_direct8,
225 	colhline_pos_paged8,
226 	colhline_neg_paged8,		/*2 */
227 	colhline_pos_direct8,
228 	colhline_neg_direct8,
229 	colhline_pos_setpixel8,
230 	colhline_neg_setpixel8,		/*4 */
231 	colhline_pos_setpixel8,
232 	colhline_neg_setpixel8,
233 	colhline_pos_setpixel8,
234 	colhline_neg_setpixel8,		/*6 */
235 	colhline_pos_setpixel8,
236 	colhline_neg_setpixel8,
237 	colhline_pos_setpixel8,
238 	colhline_neg_setpixel8,		/*8 */
239 
240 	colhline_pos_direct16,
241 	colhline_neg_direct16,
242 	colhline_pos_paged16,
243 	colhline_neg_paged16,		/*2 */
244 	colhline_pos_direct16,
245 	colhline_neg_direct16,
246 	colhline_pos_setpixel16,
247 	colhline_neg_setpixel16,	/*4 */
248 	colhline_pos_setpixel16,
249 	colhline_neg_setpixel16,
250 	colhline_pos_setpixel16,
251 	colhline_neg_setpixel16,	/*6 */
252 	colhline_pos_setpixel16,
253 	colhline_neg_setpixel16,
254 	colhline_pos_setpixel16,
255 	colhline_neg_setpixel16,	/*8 */
256 
257 	colhline_pos_direct24,
258 	colhline_neg_direct24,
259 	colhline_pos_paged24,
260 	colhline_neg_paged24,		/*2 */
261 	colhline_pos_direct24,
262 	colhline_neg_direct24,
263 	colhline_pos_setpixel24,
264 	colhline_neg_setpixel24,	/*4 */
265 	colhline_pos_setpixel24,
266 	colhline_neg_setpixel24,
267 	colhline_pos_setpixel24,
268 	colhline_neg_setpixel24,	/*6 */
269 	colhline_pos_setpixel24,
270 	colhline_neg_setpixel24,
271 	colhline_pos_setpixel24,
272 	colhline_neg_setpixel24,	/*8 */
273 
274 	colhline_pos_direct32,
275 	colhline_neg_direct32,
276 	colhline_pos_paged32,
277 	colhline_neg_paged32,		/*2 */
278 	colhline_pos_direct32,
279 	colhline_neg_direct32,
280 	colhline_pos_setpixel32,
281 	colhline_neg_setpixel32,	/*4 */
282 	colhline_pos_setpixel32,
283 	colhline_neg_setpixel32,
284 	colhline_pos_setpixel32,
285 	colhline_neg_setpixel32,	/*6 */
286 	colhline_pos_setpixel32,
287 	colhline_neg_setpixel32,
288 	colhline_pos_setpixel32,
289 	colhline_neg_setpixel32,	/*8 */
290 
291 	colhline_pos_direct8lookup,
292 	colhline_neg_direct8lookup,
293 	colhline_pos_paged8lookup,
294 	colhline_neg_paged8lookup,		/*2 */
295 	colhline_pos_direct8lookup,
296 	colhline_neg_direct8lookup,
297 	colhline_pos_setpixel8lookup,
298 	colhline_neg_setpixel8lookup,		/*4 */
299 	colhline_pos_setpixel8lookup,
300 	colhline_neg_setpixel8lookup,
301 	colhline_pos_setpixel8lookup,
302 	colhline_neg_setpixel8lookup,		/*6 */
303 	colhline_pos_setpixel8lookup,
304 	colhline_neg_setpixel8lookup,
305 	colhline_pos_setpixel8lookup,
306 	colhline_neg_setpixel8lookup,		/*8 */
307 };
308 
309 
310 #include "tri.c"
311