]> icculus.org git repositories - btb/d2x.git/blob - 2d/2dsline.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / 2d / 2dsline.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 /*
15  *
16  * Graphical routines for drawing solid scanlines.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <string.h>
25
26 #include "u_mem.h"
27 #include "gr.h"
28 #include "dxxerror.h"
29
30 #ifdef __MSDOS__
31 #include "modex.h"
32 #include "vesa.h"
33 #endif
34 #ifdef OGL
35 #include "ogl_init.h"
36 #endif
37
38
39 int Gr_scanline_darkening_level = GR_FADE_LEVELS;
40
41 #if !defined(NO_ASM) && defined(__WATCOMC__)
42
43 void gr_linear_darken( ubyte * dest, int darkening_level, int count, ubyte * fade_table );
44 #pragma aux gr_linear_darken parm [edi] [eax] [ecx] [edx] modify exact [eax ebx ecx edx edi] = \
45 "               xor ebx, ebx                "   \
46 "               mov bh, al                  "   \
47 "gld_loop:      mov bl, [edi]               "   \
48 "               mov al, [ebx+edx]           "   \
49 "               mov [edi], al               "   \
50 "               inc edi                     "   \
51 "               dec ecx                     "   \
52 "               jnz gld_loop                "
53
54 #elif !defined(NO_ASM) && defined(__GNUC__)
55
56 static inline void gr_linear_darken( ubyte * dest, int darkening_level, int count, ubyte * fade_table ) {
57    int dummy[4];
58    __asm__ __volatile__ (
59 "               xorl %%ebx, %%ebx;"
60 "               movb %%al, %%bh;"
61 "0:             movb (%%edi), %%bl;"
62 "               movb (%%ebx, %%edx), %%al;"
63 "               movb %%al, (%%edi);"
64 "               incl %%edi;"
65 "               decl %%ecx;"
66 "               jnz 0b"
67    : "=D" (dummy[0]), "=a" (dummy[1]), "=c" (dummy[2]), "=d" (dummy[3])
68    : "0" (dest), "1" (darkening_level), "2" (count), "3" (fade_table)
69    : "%ebx");
70 }
71
72 #elif !defined(NO_ASM) && defined(_MSC_VER)
73
74 __inline void gr_linear_darken( ubyte * dest, int darkening_level, int count, ubyte * fade_table )
75 {
76         __asm {
77     mov edi,[dest]
78     mov eax,[darkening_level]
79     mov ecx,[count]
80     mov edx,[fade_table]
81     xor ebx, ebx
82     mov bh, al
83 gld_loop:
84     mov bl,[edi]
85     mov al,[ebx+edx]
86     mov [edi],al
87     inc edi
88     dec ecx
89     jnz gld_loop
90         }
91 }
92
93 #else // Unknown compiler, or no assembler. So we use C.
94
95 void gr_linear_darken(ubyte * dest, int darkening_level, int count, ubyte * fade_table) {
96         register int i;
97
98         for (i=0;i<count;i++)
99         {
100                 *dest = fade_table[(*dest)+(darkening_level*256)];
101                 dest++;
102         }
103 }
104
105 #endif
106
107 #ifdef NO_ASM // No Assembler. So we use C.
108 #if 0
109 void gr_linear_stosd( ubyte * dest, ubyte color, unsigned short count )
110 {
111         int i, x;
112
113         if (count > 3) {
114                 while ((int)(dest) & 0x3) { *dest++ = color; count--; };
115                 if (count >= 4) {
116                         x = (color << 24) | (color << 16) | (color << 8) | color;
117                         while (count > 4) { *(int *)dest = x; dest += 4; count -= 4; };
118                 }
119                 while (count > 0) { *dest++ = color; count--; };
120         } else {
121                 for (i=0; i<count; i++ )
122                         *dest++ = color;
123         }
124 }
125 #else
126 void gr_linear_stosd( ubyte * dest, unsigned char color, unsigned int nbytes) {
127         memset(dest,color,nbytes);
128 }
129 #endif
130 #endif
131
132 void gr_uscanline( int x1, int x2, int y )
133 {
134         if (Gr_scanline_darkening_level >= GR_FADE_LEVELS ) {
135                 switch(TYPE)
136                 {
137 #ifdef OGL
138                 case BM_OGL:
139                         ogl_ulinec(x1, y, x2, y, COLOR);
140                         break;
141 #endif
142                 case BM_LINEAR:
143                         gr_linear_stosd( DATA + ROWSIZE*y + x1, (unsigned char)COLOR, x2-x1+1);
144                         break;
145 #ifdef __MSDOS__
146                 case BM_MODEX:
147                         gr_modex_uscanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
148                         break;
149                 case BM_SVGA:
150                         gr_vesa_scanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
151                         break;
152 #endif
153                 }
154         } else {
155                 switch(TYPE)
156                 {
157                 case BM_LINEAR:
158 #ifdef OGL
159                 case BM_OGL:
160 #endif
161                         gr_linear_darken( DATA + ROWSIZE*y + x1, Gr_scanline_darkening_level, x2-x1+1, gr_fade_table);
162                         break;
163 #ifdef __MSDOS__
164                 case BM_MODEX:
165                         gr_modex_uscanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
166                         break;
167                 case BM_SVGA:
168 #if 1
169                         {
170                                 ubyte * vram = (ubyte *)0xA0000;
171                                 int VideoLocation,page,offset1, offset2;
172
173                                 VideoLocation = (ROWSIZE * y) + x1;
174                                 page    = VideoLocation >> 16;
175                                 offset1  = VideoLocation & 0xFFFF;
176                                 offset2   = offset1 + (x2-x1+1);
177
178                                 gr_vesa_setpage( page );
179                                 if ( offset2 <= 0xFFFF ) {
180                                         gr_linear_darken( &vram[offset1], Gr_scanline_darkening_level, x2-x1+1, gr_fade_table);
181                                 } else {
182                                         gr_linear_darken( &vram[offset1], Gr_scanline_darkening_level, 0xFFFF-offset1+1, gr_fade_table);
183                                         page++;
184                                         gr_vesa_setpage(page);
185                                         gr_linear_darken( vram, Gr_scanline_darkening_level, offset2 - 0xFFFF, gr_fade_table);
186                                 }
187                         }
188 #else
189                         gr_vesa_scanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
190 #endif
191                         break;
192 #endif
193                 }
194         }
195 }
196
197 void gr_scanline( int x1, int x2, int y )
198 {
199         if ((y<0)||(y>MAXY)) return;
200
201         if (x2 < x1 ) x2 ^= x1 ^= x2;
202
203         if (x1 > MAXX) return;
204         if (x2 < MINX) return;
205
206         if (x1 < MINX) x1 = MINX;
207         if (x2 > MAXX) x2 = MAXX;
208
209         if (Gr_scanline_darkening_level >= GR_FADE_LEVELS ) {
210                 switch(TYPE)
211                 {
212 #ifdef OGL
213                 case BM_OGL:
214                         ogl_ulinec(x1, y, x2, y, COLOR);
215                         break;
216 #endif
217                 case BM_LINEAR:
218                         gr_linear_stosd( DATA + ROWSIZE*y + x1, (unsigned char)COLOR, x2-x1+1);
219                         break;
220 #ifdef __MSDOS__
221                 case BM_MODEX:
222                         gr_modex_uscanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
223                         break;
224                 case BM_SVGA:
225                         gr_vesa_scanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
226                         break;
227 #endif
228                 }
229         } else {
230                 switch(TYPE)
231                 {
232                 case BM_LINEAR:
233 #ifdef OGL
234                 case BM_OGL:
235 #endif
236                         gr_linear_darken( DATA + ROWSIZE*y + x1, Gr_scanline_darkening_level, x2-x1+1, gr_fade_table);
237                         break;
238 #ifdef __MSDOS__
239                 case BM_MODEX:
240                         gr_modex_uscanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
241                         break;
242                 case BM_SVGA:
243 #if 1
244                         {
245                                 ubyte * vram = (ubyte *)0xA0000;
246                                 int VideoLocation,page,offset1, offset2;
247
248                                 VideoLocation = (ROWSIZE * y) + x1;
249                                 page    = VideoLocation >> 16;
250                                 offset1  = VideoLocation & 0xFFFF;
251                                 offset2   = offset1 + (x2-x1+1);
252
253                                 gr_vesa_setpage( page );
254                                 if ( offset2 <= 0xFFFF )        {
255                                         gr_linear_darken( &vram[offset1], Gr_scanline_darkening_level, x2-x1+1, gr_fade_table);
256                                 } else {
257                                         gr_linear_darken( &vram[offset1], Gr_scanline_darkening_level, 0xFFFF-offset1+1, gr_fade_table);
258                                         page++;
259                                         gr_vesa_setpage(page);
260                                         gr_linear_darken( vram, Gr_scanline_darkening_level, offset2 - 0xFFFF, gr_fade_table);
261                                 }
262                         }
263 #else
264                         gr_vesa_scanline( x1+XOFFSET, x2+XOFFSET, y+YOFFSET, COLOR );
265 #endif
266                         break;
267 #endif
268                 }
269         }
270 }