]> 4ch.mooo.com Git - 16.git/blob - src/v2/source/ENGINE/MODE13H.C
2dcbf9ce5d31adbf0bff3961e2739fe53daf81f7
[16.git] / src / v2 / source / ENGINE / MODE13H.C
1 /*\r
2 Copyright (C) 1998 BJ Eirich (aka vecna)\r
3 This program is free software; you can redistribute it and/or\r
4 modify it under the terms of the GNU General Public License\r
5 as published by the Free Software Foundation; either version 2\r
6 of the License, or (at your option) any later version.\r
7 This program is distributed in the hope that it will be useful,\r
8 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
10 See the GNU General Public Lic\r
11 See the GNU General Public License for more details.\r
12 You should have received a copy of the GNU General Public License\r
13 along with this program; if not, write to the Free Software\r
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
15 */\r
16 \r
17 #include <dpmi.h>\r
18 #include <sys\nearptr.h>\r
19 #include "verge.h"\r
20 \r
21 // ================================= Data ====================================\r
22 \r
23 byte *screen, *screenx;             // virscr | realscr\r
24 \r
25 // ================================= Code ====================================\r
26 \r
27 void SetMode(int mode)\r
28 {\r
29   REGISTERS r;\r
30   SET_AX(r, mode);\r
31   INTERRUPT(0x10, r);\r
32 }\r
33 \r
34 int Mode13hShutdown(void)\r
35 {\r
36   SetMode(0x3);\r
37   free(screen);\r
38   return 0;\r
39 }\r
40 \r
41 void xShowPage(int startofs)\r
42 {\r
43   asm("movl _screen, %%esi              \n\t"\r
44       "addl %0, %%esi                   \n\t"\r
45       "movl _screenx, %%edi             \n\t"\r
46       "movl $200, %%eax                 \n\t"\r
47 "lineloop:                              \n\t"\r
48       "movl $80, %%ecx                  \n\t"\r
49       "rep                              \n\t"\r
50       "movsl                            \n\t"\r
51       "addl $32, %%esi                  \n\t"\r
52       "decl %%eax                       \n\t"\r
53       "jnz lineloop                     \n\t"\r
54       :\r
55       : "m" (startofs)\r
56       : "esi", "edi", "cc", "eax", "ecx");\r
57 }\r
58 \r
59 int Mode13hShowPage(void)\r
60 {\r
61   RenderGUI();\r
62   cpubyte=PFLIP;\r
63   xShowPage((16*352)+16);\r
64   cpubyte=ETC;\r
65   return 0;\r
66 }\r
67 \r
68 int Mode13hCopySprite(int x, int y, int width, int height, byte *src)\r
69 {\r
70   cpubyte=RENDER;\r
71 \r
72   asm("movl %3, %%edx                   \n\t"\r
73       "movl %4, %%esi                   \n\t"\r
74 "csl0:                                  \n\t"\r
75       "movl %1, %%eax                   \n\t"\r
76       "imul $352, %%eax                 \n\t"\r
77       "addl %0, %%eax                   \n\t"\r
78       "addl _screen, %%eax              \n\t"\r
79       "movl %%eax, %%edi                \n\t"\r
80       "movl %2, %%ecx                   \n\t"\r
81       "shrl $2, %%ecx                   \n\t"\r
82       "repz                             \n\t"\r
83       "movsl                            \n\t"\r
84       "incl %1                          \n\t"\r
85       "decl %%edx                       \n\t"\r
86       "jnz csl0                         \n\t"\r
87       :\r
88       : "m" (x), "m" (y), "m" (width), "m" (height), "m" (src)\r
89       : "eax","edx","esi","edi","ecx","cc" );\r
90   cpubyte=ETC;\r
91   return 0;\r
92 }\r
93 \r
94 int Mode13hCopyTile(int x, int y, byte *spr)\r
95 {\r
96   cpubyte=RENDER;\r
97 \r
98   asm("movl $16, %%ecx                  \n\t"\r
99       "movl %2, %%esi                   \n\t"\r
100       "movl %1, %%edi                   \n\t"\r
101       "imul $352, %%edi                 \n\t"\r
102       "addl %0, %%edi                   \n\t"\r
103       "addl _screen, %%edi              \n\t"\r
104 " ctl0:                                 \n\t"\r
105       "movl (%%edi), %%eax              \n\t"\r
106       "andl $0, %%eax                   \n\t"\r
107       "orl  (%%esi), %%eax              \n\t"\r
108       "movl %%eax, (%%edi)              \n\t"\r
109       "movl 4(%%edi), %%eax             \n\t"\r
110       "andl $0, %%eax                   \n\t"\r
111       "orl  4(%%esi), %%eax             \n\t"\r
112       "movl %%eax, 4(%%edi)             \n\t"\r
113       "movl 8(%%edi), %%eax             \n\t"\r
114       "andl $0, %%eax                   \n\t"\r
115       "orl  8(%%esi), %%eax             \n\t"\r
116       "movl %%eax, 8(%%edi)             \n\t"\r
117       "movl 12(%%edi), %%eax            \n\t"\r
118       "andl $0, %%eax                   \n\t"\r
119       "orl  12(%%esi), %%eax            \n\t"\r
120       "movl %%eax, 12(%%edi)            \n\t"\r
121       "addl $16, %%esi                  \n\t"\r
122       "addl $352, %%edi                 \n\t"\r
123       "decl %%ecx                       \n\t"\r
124       "jnz ctl0                         \n\t"\r
125       :\r
126       : "m" (x), "m" (y), "m" (spr)\r
127       : "eax","ecx","esi","edi","cc" );\r
128   cpubyte=ETC;\r
129   return 0;\r
130 }\r
131 \r
132 int Mode13hTCopySprite(int x, int y, int width, int height, byte *src)\r
133 {\r
134   cpubyte=RENDER;\r
135 \r
136   asm("movl %3, %%ecx                   \n\t"\r
137       "movl %4, %%esi                   \n\t"\r
138 "tcsl0:                                 \n\t"\r
139       "movl %1, %%eax                   \n\t"\r
140       "imul $352, %%eax                 \n\t"\r
141       "addl %0, %%eax                   \n\t"\r
142       "addl _screen, %%eax              \n\t"\r
143       "movl %%eax, %%edi                \n\t"\r
144       "movl %2, %%edx                   \n\t"\r
145 "drawloop:                              \n\t"\r
146       "lodsb                            \n\t"\r
147       "orb %%al, %%al                   \n\t"\r
148       "jz nodraw                        \n\t"\r
149       "stosb                            \n\t"\r
150       "decl %%edx                       \n\t"\r
151       "jz endline                       \n\t"\r
152       "jmp drawloop                     \n\t"\r
153 "nodraw:                                \n\t"\r
154       "incl %%edi                       \n\t"\r
155       "decl %%edx                       \n\t"\r
156       "jnz drawloop                     \n\t"\r
157 "endline:                               \n\t"\r
158       "incl %1                          \n\t"\r
159       "decl %%ecx                       \n\t"\r
160       "jnz tcsl0                        \n\t"\r
161       :\r
162       : "m" (x), "m" (y), "m" (width), "m" (height), "m" (src)\r
163       : "eax","edx","esi","edi","ecx","cc" );\r
164 \r
165   cpubyte=ETC;\r
166   return 0;\r
167 }\r
168 \r
169 int Mode13hTCopyTile(int x, int y, byte *spr, byte *matte)\r
170 {\r
171   cpubyte=RENDER;\r
172   asm("movl $16, %%ecx                  \n\t"\r
173       "movl %2, %%esi                   \n\t"\r
174       "movl %1, %%edi                   \n\t"\r
175       "imul $352, %%edi                 \n\t"\r
176       "addl %0, %%edi                   \n\t"\r
177       "addl _screen, %%edi              \n\t"\r
178       "movl %3, %%edx                   \n\t"\r
179 "tctl0:                                 \n\t"\r
180       "movl (%%edi), %%eax              \n\t"\r
181       "andl (%%edx), %%eax              \n\t"\r
182       "orl  (%%esi), %%eax              \n\t"\r
183       "movl %%eax, (%%edi)              \n\t"\r
184       "movl 4(%%edi), %%eax             \n\t"\r
185       "andl 4(%%edx), %%eax             \n\t"\r
186       "orl  4(%%esi), %%eax             \n\t"\r
187       "movl %%eax, 4(%%edi)             \n\t"\r
188       "movl 8(%%edi), %%eax             \n\t"\r
189       "andl 8(%%edx), %%eax             \n\t"\r
190       "orl  8(%%esi), %%eax             \n\t"\r
191       "movl %%eax, 8(%%edi)             \n\t"\r
192       "movl 12(%%edi), %%eax            \n\t"\r
193       "andl 12(%%edx), %%eax            \n\t"\r
194       "orl  12(%%esi), %%eax            \n\t"\r
195       "movl %%eax, 12(%%edi)            \n\t"\r
196       "addl $16, %%esi                  \n\t"\r
197       "addl $352, %%edi                 \n\t"\r
198       "addl $16, %%edx                  \n\t"\r
199       "decl %%ecx                       \n\t"\r
200       "jnz tctl0                        \n\t"\r
201       :\r
202       : "m" (x), "m" (y), "m" (spr), "m" (matte)\r
203       : "eax","ecx","edx","esi","edi","cc" );\r
204 \r
205   cpubyte=ETC;\r
206   return 0;\r
207 }\r
208 \r
209 int Mode13hCCopySprite(int x,int y,int width,int height,byte *src)\r
210 {\r
211   byte *s,*d;\r
212   int xl,yl,xs,ys;\r
213   int cx1=0, cy1=0, cx2=tsx-1, cy2=tsy-1;\r
214 \r
215   cpubyte=RENDER;\r
216 \r
217   xl=width;\r
218   yl=height;\r
219   xs=ys=0;\r
220 \r
221   if (x>cx2 || y>cy2 || x+xl<cx1 || y+yl<cy1)\r
222     return 0;\r
223 \r
224   if (x+xl > cx2) xl=cx2-x+1;\r
225   if (y+yl > cy2) yl=cy2-y+1;\r
226 \r
227   if (x<cx1) { xs=cx1-x; xl-=xs; x=cx1; }\r
228   if (y<cy1) { ys=cy1-y; yl-=ys; y=cy1; }\r
229 \r
230   s=src;\r
231   if (ys) s+=(ys*width); // only perform mul if necessary ;)\r
232   if (xs) s+=xs;\r
233   d=screen+(y*tsx)+x;\r
234 \r
235   for (; yl; yl--)\r
236   {\r
237     memcpy(d,s,xl);\r
238     s+=width;\r
239     d+=tsx;\r
240   }\r
241 \r
242   cpubyte=ETC;\r
243   return 0;\r
244 }\r
245 \r
246 int Mode13hTCCopySprite(int x,int y,int width,int height,byte *src)\r
247 {\r
248   byte *s,*d,c;\r
249   int xl,yl,xs,ys;\r
250   int cx1=0, cy1=0, cx2=tsx-1, cy2=tsy-1;\r
251 \r
252   cpubyte=RENDER;\r
253 \r
254   xl=width;\r
255   yl=height;\r
256   xs=ys=0;\r
257 \r
258   if (x>cx2 || y>cy2 || x+xl<cx1 || y+yl<cy1)\r
259     return 0;\r
260 \r
261   if (x+xl > cx2) xl=cx2-x+1;\r
262   if (y+yl > cy2) yl=cy2-y+1;\r
263 \r
264   if (x<cx1) { xs=cx1-x; xl-=xs; x=cx1; }\r
265   if (y<cy1) { ys=cy1-y; yl-=ys; y=cy1; }\r
266 \r
267   s=src;\r
268   if (ys) s+=(ys*width); // only perform mul if necessary ;)\r
269   if (xs) s+=xs;\r
270   d=screen+(y*tsx)+x;\r
271 \r
272   for (; yl; yl--)\r
273   {\r
274     for (x=0; x<xl; x++)\r
275     {\r
276       c=s[x];\r
277       if (c)\r
278         d[x]=c;\r
279     }\r
280     s+=width;\r
281     d+=tsx;\r
282   }\r
283 \r
284   cpubyte=ETC;\r
285   return 0;\r
286 }\r
287 \r
288 int Mode13hScaleSprite(int x, int y, int w, int h, int tw, int th, byte *s)\r
289 { int i,j,xm,ym,xd,yd,sx,sy=0,xs,ys,dys=0;\r
290   unsigned char *d;\r
291 \r
292   cpubyte=RENDER;\r
293 \r
294   if (!tw || !th) return 0;\r
295   tw += sgn(tw); th += sgn(th);\r
296   xm = abs(tw); ym = abs(th);\r
297   xs = (w<<16)/xm; ys = (h<<16)/ym;\r
298   xd = sgn(tw); yd = sgn(th);\r
299 \r
300   if (tw>0 && th>0) dys=tsx-xm;\r
301   else if (tw>0 && th<0) dys=(0-tsx)-xm;\r
302   else if (tw<0 && th>0) dys=tsx+xm;\r
303   else if (tw<0 && th<0) dys=(0-tsx)+xm;\r
304 \r
305   d = screen+(y*tsx)+x;\r
306   for (i=0;i<ym;++i) {\r
307     sx=0;\r
308     for (j=0;j<xm;++j) {\r
309       *d=s[(sx>>16)];\r
310       d+=xd;\r
311       sx+=xs;\r
312       }\r
313     d+=dys;\r
314     sy+=ys;\r
315     s+=(sy>>16)*w;\r
316     sy&=0xffff;\r
317     }\r
318   cpubyte=ETC;\r
319   return 0;\r
320 }\r
321 \r
322 int Mode13hRenderMAPLine(int x, int y, int yofs, word *map)\r
323 {\r
324   cpubyte=RENDER;\r
325   asm("movl %1, %%edi                  \n\t"\r
326       "imul $352, %%edi                \n\t"\r
327       "addl %0, %%edi                  \n\t"\r
328       "addl _screen, %%edi             \n\t"\r
329       "movl $22, %%ebx                 \n\t"\r
330       "movl %3, %%ecx                  \n\t"\r
331 "tileloop:                             \n\t"\r
332       "movw (%%ecx), %%ax              \n\t"\r
333       "movzwl %%ax, %%edx              \n\t"\r
334       "shll $1, %%edx                  \n\t"\r
335       "addl _tileidx, %%edx            \n\t"\r
336       "movw (%%edx), %%ax              \n\t"\r
337       "movzwl %%ax, %%esi              \n\t"\r
338       "shll $8, %%esi                  \n\t"\r
339       "addl _vsp, %%esi                \n\t"\r
340       "movl %2, %%eax                  \n\t"\r
341       "shll $4, %%eax                  \n\t"\r
342       "addl %%eax, %%esi               \n\t"\r
343       "movl (%%esi), %%eax             \n\t"\r
344       "movl %%eax, (%%edi)             \n\t"\r
345       "movl 4(%%esi), %%eax            \n\t"\r
346       "movl %%eax, 4(%%edi)            \n\t"\r
347       "movl 8(%%esi), %%eax            \n\t"\r
348       "movl %%eax, 8(%%edi)            \n\t"\r
349       "movl 12(%%esi), %%eax           \n\t"\r
350       "movl %%eax, 12(%%edi)           \n\t"\r
351       "addl $16, %%edi                 \n\t"\r
352       "addl $2, %%ecx                  \n\t"\r
353       "decl %%ebx                      \n\t"\r
354       "jnz tileloop                    \n\t"\r
355       :\r
356       : "m" (x), "m" (y), "m" (yofs), "m" (map)\r
357       : "eax","ebx","ecx","edx","esi","edi","cc" );\r
358 \r
359   cpubyte=ETC;\r
360   return 0;\r
361 }\r
362 \r
363 int Mode13hTRenderMAPLine(int x, int y, int yofs, word *map)\r
364 {\r
365   cpubyte=RENDER;\r
366   asm("movl %1, %%edi                  \n\t"\r
367       "imul $352, %%edi                \n\t"\r
368       "addl %0, %%edi                  \n\t"\r
369       "addl _screen, %%edi             \n\t"\r
370       "movl $22, %%ebx                 \n\t"\r
371       "movl %3, %%ecx                  \n\t"\r
372 "tileloop1:                            \n\t"\r
373       "movw (%%ecx), %%ax              \n\t"\r
374       "movzwl %%ax, %%edx              \n\t"\r
375       "shll $1, %%edx                  \n\t"\r
376       "addl _tileidx, %%edx            \n\t"\r
377       "movw (%%edx), %%ax              \n\t"\r
378       "orw %%ax, %%ax                  \n\t"\r
379       "jz next1                        \n\t"\r
380       "movzwl %%ax, %%esi              \n\t"\r
381       "shll $8, %%esi                  \n\t"\r
382       "movl %%esi, %%edx               \n\t"\r
383       "addl _vspmask, %%edx            \n\t"\r
384       "addl _vsp, %%esi                \n\t"\r
385       "movl %2, %%eax                  \n\t"\r
386       "shll $4, %%eax                  \n\t"\r
387       "addl %%eax, %%esi               \n\t"\r
388       "addl %%eax, %%edx               \n\t"\r
389       "movl (%%edi), %%eax             \n\t"\r
390       "andl (%%edx), %%eax             \n\t"\r
391       "orl  (%%esi), %%eax             \n\t"\r
392       "movl %%eax, (%%edi)             \n\t"\r
393       "movl 4(%%edi), %%eax            \n\t"\r
394       "andl 4(%%edx), %%eax            \n\t"\r
395       "orl  4(%%esi), %%eax            \n\t"\r
396       "movl %%eax, 4(%%edi)            \n\t"\r
397       "movl 8(%%edi), %%eax            \n\t"\r
398       "andl 8(%%edx), %%eax            \n\t"\r
399       "orl  8(%%esi), %%eax            \n\t"\r
400       "movl %%eax, 8(%%edi)            \n\t"\r
401       "movl 12(%%edi), %%eax           \n\t"\r
402       "andl 12(%%edx), %%eax           \n\t"\r
403       "orl  12(%%esi), %%eax           \n\t"\r
404       "movl %%eax, 12(%%edi)           \n\t"\r
405 "next1:                                \n\t"\r
406       "addl $16, %%edi                 \n\t"\r
407       "addl $2, %%ecx                  \n\t"\r
408       "decl %%ebx                      \n\t"\r
409       "jnz tileloop1                   \n\t"\r
410       :\r
411       : "m" (x), "m" (y), "m" (yofs), "m" (map)\r
412       : "eax","ebx","ecx","edx","esi","edi","cc" );\r
413 \r
414   cpubyte=ETC;\r
415   return 0;\r
416 }\r
417 \r
418 int Mode13hColorField(int x, int y, byte c)\r
419 {\r
420   cpubyte=RENDER;\r
421   asm("movl %1, %%eax                   \n\t"\r
422       "imul $352, %%eax                 \n\t"\r
423       "addl %0, %%eax                   \n\t"\r
424       "addl _screen, %%eax              \n\t"\r
425       "movl %%eax, %%edi                \n\t"\r
426       "movl $8, %%ecx                   \n\t"\r
427       "movb %2, %%al                    \n\t"\r
428 "lineloop1:                             \n\t"\r
429       "stosb                            \n\t"\r
430       "incl %%edi                       \n\t"\r
431       "stosb                            \n\t"\r
432       "incl %%edi                       \n\t"\r
433       "stosb                            \n\t"\r
434       "incl %%edi                       \n\t"\r
435       "stosb                            \n\t"\r
436       "incl %%edi                       \n\t"\r
437       "stosb                            \n\t"\r
438       "incl %%edi                       \n\t"\r
439       "stosb                            \n\t"\r
440       "incl %%edi                       \n\t"\r
441       "stosb                            \n\t"\r
442       "incl %%edi                       \n\t"\r
443       "stosb                            \n\t"\r
444       "incl %%edi                       \n\t"\r
445       "addl $336, %%edi                 \n\t"\r
446       "incl %%edi                       \n\t"\r
447       "stosb                            \n\t"\r
448       "incl %%edi                       \n\t"\r
449       "stosb                            \n\t"\r
450       "incl %%edi                       \n\t"\r
451       "stosb                            \n\t"\r
452       "incl %%edi                       \n\t"\r
453       "stosb                            \n\t"\r
454       "incl %%edi                       \n\t"\r
455       "stosb                            \n\t"\r
456       "incl %%edi                       \n\t"\r
457       "stosb                            \n\t"\r
458       "incl %%edi                       \n\t"\r
459       "stosb                            \n\t"\r
460       "incl %%edi                       \n\t"\r
461       "stosb                            \n\t"\r
462       "addl $336, %%edi                 \n\t"\r
463       "decl %%ecx                       \n\t"\r
464       "jnz lineloop1                    \n\t"\r
465       :\r
466       : "m" (x), "m" (y), "m" (c)\r
467       : "eax","edi","ecx","cc" );\r
468   cpubyte=ETC;\r
469   return 0;\r
470 }\r
471 \r
472 int Mode13hClearScreen()\r
473 {\r
474   cpubyte=RENDER;\r
475   memset(screen+(352*16)+16,0,(352*200));\r
476   cpubyte=ETC;\r
477   return 0;\r
478 }\r
479 \r
480 int Mode13hPutPixel(int x, int y, int color)\r
481 {\r
482   cpubyte=RENDER;\r
483 \r
484   if (x<cx1 || y<cy1 || x>cx2 || y>cy2)\r
485   {\r
486     cpubyte=ETC;\r
487     return 0;\r
488   }\r
489 \r
490   x+=16;\r
491   y+=16;\r
492 \r
493   screen[(y*tsx)+x]=color;\r
494 \r
495   cpubyte=ETC;\r
496   return 0;\r
497 }\r
498 \r
499 int Mode13hGetPixel(int x, int y)\r
500 {\r
501   cpubyte=RENDER;\r
502 \r
503   if (x<cx1 || y<cy1 || x>cx2 || y>cy2)\r
504   {\r
505     cpubyte=ETC;\r
506     return 0;\r
507   }\r
508 \r
509   x+=16;\r
510   y+=16;\r
511 \r
512   cpubyte=ETC;\r
513   return screen[(y*tsx)+x];\r
514 }\r
515 \r
516 int Mode13hHLine(int x, int y, int x2, int color)\r
517 { byte *d;\r
518   int width;\r
519 \r
520   cpubyte=RENDER;\r
521 \r
522   // swap?\r
523   if (x2<x) { int t=x2; x=x2; x2=t; }\r
524 \r
525   width=x2-x+1;\r
526   if (x>cx2 || y>cy2 || x+width<cx1 || y<cy1)\r
527     return 0;\r
528 \r
529   if (x+width > cx2) width=cx2-x+1;\r
530   if (x<cx1) { width-=(cx1-x); x=cx1; }\r
531 \r
532   x+=16;\r
533   y+=16;\r
534   x2+=16;\r
535 \r
536   d=screen+(y*tsx)+x;\r
537   memset(d,color,width);\r
538 \r
539   cpubyte=ETC;\r
540   return 0;\r
541 }\r
542 \r
543 int Mode13hVLine(int x, int y, int y2, int color)\r
544 { byte *d;\r
545   int height;\r
546 \r
547   cpubyte=RENDER;\r
548 \r
549   // swap?\r
550   if (y2<y) { int t=y2; x=y2; y2=t; }\r
551 \r
552   height=y2-y+1;\r
553   if (x>cx2 || y>cy2 || x<cx1 || y+height<cy1)\r
554   {\r
555     cpubyte=ETC;\r
556     return 0;\r
557   }\r
558 \r
559   if (y+height > cy2) height=cy2-y+1;\r
560   if (y<cy1) { height-=(cy1-y); y=cy1; }\r
561 \r
562   x+=16;\r
563   y+=16;\r
564   y2+=16;\r
565 \r
566   d=screen+(y*tsx)+x;\r
567   for (; height; height--)\r
568   {\r
569     *d=color;\r
570     d+=tsx;\r
571   }\r
572 \r
573   cpubyte=ETC;\r
574   return 0;\r
575 }\r
576 \r
577 int Mode13hLine(int x1, int y1, int x2, int y2, int color)\r
578 {\r
579   short i,xc,yc,er,n,m,xi,yi,xcxi,ycyi,xcyi;\r
580   unsigned dcy,dcx;\r
581 \r
582   cpubyte=RENDER;\r
583 \r
584   // check to see if the line is completly clipped off\r
585   if ((x1<cx1 && x2<cx1) || (x1>cx2 && x2>cx2)\r
586   || (y1<cy1 && y2<cy1) || (y1>cy2 && y2>cy2))\r
587   {\r
588     cpubyte=ETC;\r
589     return 0;\r
590   }\r
591 \r
592   if (x1>x2)\r
593   {\r
594     i=x1; x1=x2; x2=i;\r
595     i=y1; y1=y2; y2=i;\r
596   }\r
597 \r
598   // clip the left side\r
599   if (x1<cx1)\r
600   { int myy=(y2-y1);\r
601     int mxx=(x2-x1),b;\r
602     if (!mxx)\r
603     {\r
604       cpubyte=ETC;\r
605       return 0;\r
606     }\r
607     if (myy)\r
608     {\r
609       b=y1-(y2-y1)*x1/mxx;\r
610       y1=myy*cx1/mxx+b;\r
611       x1=cx1;\r
612     }\r
613     else x1=cx1;\r
614   }\r
615 \r
616   // clip the right side\r
617   if (x2>cx2)\r
618   { int myy=(y2-y1);\r
619     int mxx=(x2-x1),b;\r
620     if (!mxx)\r
621     {\r
622       cpubyte=ETC;\r
623       return 0;\r
624     }\r
625     if (myy)\r
626     {\r
627       b=y1-(y2-y1)*x1/mxx;\r
628       y2=myy*cx2/mxx+b;\r
629       x2=cx2;\r
630     }\r
631     else x2=cx2;\r
632   }\r
633 \r
634   if (y1>y2)\r
635   {\r
636     i=x1; x1=x2; x2=i;\r
637     i=y1; y1=y2; y2=i;\r
638   }\r
639 \r
640   // clip the bottom\r
641   if (y2>cy2)\r
642   { int mxx=(x2-x1);\r
643     int myy=(y2-y1),b;\r
644     if (!myy)\r
645     {\r
646       cpubyte=ETC;\r
647       return 0;\r
648     }\r
649     if (mxx)\r
650     {\r
651       b=y1-(y2-y1)*x1/mxx;\r
652       x2=(cy2-b)*mxx/myy;\r
653       y2=cy2;\r
654     }\r
655     else y2=cy2;\r
656   }\r
657 \r
658   // clip the top\r
659   if (y1<cy1)\r
660   { int mxx=(x2-x1);\r
661     int myy=(y2-y1),b;\r
662     if (!myy)\r
663     {\r
664       cpubyte=ETC;\r
665       return 0;\r
666     }\r
667     if (mxx)\r
668     {\r
669       b=y1-(y2-y1)*x1/mxx;\r
670       x1=(cy1-b)*mxx/myy;\r
671       y1=cy1;\r
672     }\r
673     else y1=cy1;\r
674   }\r
675 \r
676   // ???\r
677   // see if it got cliped into the box, out out\r
678   if (x1<cx1 || x2<cx1 || x1>cx2 || x2>cx2 || y1<cy1 || y2 <cy1 || y1>cy2 || y2>cy2)\r
679   {\r
680     cpubyte=ETC;\r
681     return 0;\r
682   }\r
683 \r
684   if (x1>x2)\r
685   { xc=x2; xi=x1; }\r
686   else { xi=x2; xc=x1; }\r
687 \r
688   x1+=16;y1+=16; // aen; adjust these here??\r
689   x2+=16;y2+=16;\r
690 \r
691   // assume y1<=y2 from above swap operation\r
692   yi=y2; yc=y1;\r
693 \r
694   dcx=x1; dcy=y1;\r
695   xc=(x2-x1); yc=(y2-y1);\r
696   if (xc<0) xi=-1; else xi=1;\r
697   if (yc<0) yi=-1; else yi=1;\r
698   n=abs(xc); m=abs(yc);\r
699   ycyi=abs(2*yc*xi);\r
700   er=0;\r
701 \r
702   if (n>m)\r
703   {\r
704     xcxi=abs(2*xc*xi);\r
705     for (i=0;i<=n;i++)\r
706     {\r
707       screen[(dcy*tsx)+dcx]=color;\r
708       if (er>0)\r
709       { dcy+=yi;\r
710         er-=xcxi;\r
711       }\r
712       er+=ycyi;\r
713       dcx+=xi;\r
714     }\r
715   }\r
716   else\r
717   {\r
718     xcyi=abs(2*xc*yi);\r
719     for (i=0;i<=m;i++)\r
720     {\r
721       screen[(dcy*tsx)+dcx]=color;\r
722       if (er>0)\r
723       { dcx+=xi;\r
724         er-=ycyi;\r
725       }\r
726       er+=xcyi;\r
727       dcy+=yi;\r
728     }\r
729   }\r
730 \r
731   cpubyte=ETC;\r
732   return 0;\r
733 }\r
734 \r
735 int Mode13hCircle(int x, int y, int radius, int color)\r
736 { int cx=0;\r
737   int cy=radius;\r
738   int df=1-radius;\r
739   int d_e=3;\r
740   int d_se=-2*radius+5;\r
741 \r
742   cpubyte=RENDER;\r
743 \r
744   do {\r
745     Mode13hPutPixel(x+cx,y+cy,color);\r
746     if (cx) Mode13hPutPixel(x-cx,y+cy,color);\r
747     if (cy) Mode13hPutPixel(x+cx,y-cy,color);\r
748     if ((cx) && (cy)) Mode13hPutPixel(x-cx,y-cy,color);\r
749 \r
750     if (cx != cy)\r
751     {\r
752       Mode13hPutPixel(x+cy,y+cx,color);\r
753       if (cx) Mode13hPutPixel(x+cy,y-cx,color);\r
754       if (cy) Mode13hPutPixel(x-cy,y+cx,color);\r
755       if (cx && cy) Mode13hPutPixel(x-cy,y-cx,color);\r
756     }\r
757 \r
758     if (df<0)\r
759     {\r
760       df+=d_e;\r
761       d_e+=2;\r
762       d_se+=2;\r
763     }\r
764     else\r
765     {\r
766       df+=d_se;\r
767       d_e+=2;\r
768       d_se+=4;\r
769       cy--;\r
770     }\r
771 \r
772     cx++;\r
773 \r
774   } while (cx <= cy);\r
775 \r
776   cpubyte=ETC;\r
777   return 0;\r
778 }\r
779 \r
780 int Mode13hCircleFill(int x, int y, int radius, int color)\r
781 { int cx=0;\r
782   int cy=radius;\r
783   int df=1-radius;\r
784   int d_e=3;\r
785   int d_se=-2*radius+5;\r
786 \r
787   cpubyte=RENDER;\r
788 \r
789   do {\r
790     Mode13hHLine(x-cy,y-cx,x+cy,color);\r
791     if (cx) Mode13hHLine(x-cy,y+cx,x+cy,color);\r
792 \r
793     if (df<0)\r
794     {\r
795       df+=d_e;\r
796       d_e+=2;\r
797       d_se+=2;\r
798     }\r
799     else\r
800     {\r
801       if (cx != cy)\r
802       {\r
803         Mode13hHLine(x-cx,y-cy,x+cx,color);\r
804         if (cy) Mode13hHLine(x-cx,y+cy,x+cx,color);\r
805       }\r
806 \r
807       df+=d_se;\r
808       d_e+=2;\r
809       d_se+=4;\r
810       cy--;\r
811     }\r
812 \r
813     cx++;\r
814 \r
815   } while (cx <= cy);\r
816 \r
817   cpubyte=ETC;\r
818   return 0;\r
819 }\r
820 \r
821 int Mode13hRect(int x, int y, int x2, int y2, int color)\r
822 {\r
823   Mode13hHLine(x,y,x2,color);\r
824   Mode13hHLine(x,y2,x2,color);\r
825   Mode13hVLine(x,y+1,y2-1,color);\r
826   Mode13hVLine(x2,y+1,y2-1,color);\r
827   return 0;\r
828 }\r
829 \r
830 int Mode13hRectFill(int x, int y, int x2, int y2, int color)\r
831 {\r
832   cpubyte=RENDER;\r
833 \r
834   if (y2<y) { int t=y2; y=y2; y2=t; }\r
835 \r
836   for (; y<=y2; y++)\r
837     Mode13hHLine(x,y,x2,color);\r
838 \r
839   cpubyte=ETC;\r
840   return 0;\r
841 }\r
842 \r
843 void InitMode13h(void)\r
844 {\r
845   SetMode(0x13);\r
846 \r
847   screenx=(byte *) 0xA0000 + __djgpp_conventional_base;\r
848   screen=(byte *) malloc(95744);\r
849   memset(screen, 0, 95744);\r
850 \r
851   sx=320;  sy=200;\r
852   tsx=352; tsy=232;\r
853   tx=20;   ty=13;\r
854   cx1=0;   cy1=0;\r
855   cx2=sx-1;cy2=sy-1;\r
856 \r
857   // Mode successfuly set, now lets set up the driver.\r
858   ShutdownVideo = Mode13hShutdown;\r
859   ShowPage = Mode13hShowPage;\r
860   CopySprite = Mode13hCopySprite;\r
861   TCopySprite = Mode13hTCopySprite;\r
862   CCopySprite = Mode13hCCopySprite;\r
863   TCCopySprite = Mode13hTCCopySprite;\r
864   CopyTile = Mode13hCopyTile;\r
865   TCopyTile = Mode13hTCopyTile;\r
866   ScaleSprite = Mode13hScaleSprite;\r
867   RenderMAPLine = Mode13hRenderMAPLine;\r
868   TRenderMAPLine = Mode13hTRenderMAPLine;\r
869   ColorField = Mode13hColorField;\r
870   ClearScreen = Mode13hClearScreen;\r
871   PutPixel = Mode13hPutPixel;\r
872   GetPixel = Mode13hGetPixel;\r
873   HLine = Mode13hHLine;\r
874   VLine = Mode13hVLine;\r
875   Line = Mode13hLine;\r
876   Circle = Mode13hCircle;\r
877   CircleFill = Mode13hCircleFill;\r
878   Rect = Mode13hRect;\r
879   RectFill = Mode13hRectFill;\r
880   DriverDesc = "320x200 (Mode 13h, linear)";\r
881 }\r