5 * (C)1993 Ztiff Zox Softwear
\r
7 * Simple graphics library to accompany the article
\r
9 * INTRODUCTION TO MODE X.
\r
11 * This library provides the basic functions for initializing and using
\r
12 * unchained (planar) 256-color VGA modes. Currently supported are:
\r
17 * Functions are provided for:
\r
19 * - initializing one of the available modes
\r
20 * - setting the start address of the VGA refresh data
\r
21 * - setting active and visible display pages
\r
22 * - writing and reading a single pixel to/from video memory
\r
24 * The library is provided as a demonstration only, and is not claimed
\r
25 * to be particularly efficient or suited for any purpose. It has only
\r
26 * been tested with Borland C++ 3.1 by the author. Comments on success
\r
27 * or disaster with other compilers are welcome.
\r
29 * This file is public domain. Do with it whatever you'd like, but
\r
30 * please don't distribute it without the article.
\r
32 * Thanks go out to various helpful netters who spotted the 0xE7 bug
\r
33 * in the set320x240x256() function!
\r
35 * Modified by sparky4 so it can be compiled in open watcom ^^
\r
42 * We 'require' a large data model simply to get rid of explicit 'far'
\r
43 * pointers and compiler specific '_fmemset()' functions and the likes.
\r
45 #if !defined(__COMPACT__)
\r
46 # if !defined(__LARGE__)
\r
47 # if !defined(__HUGE__)
\r
48 # error Large data model required! Try compiling with 'bcc -ml lib.c'.
\r
57 //code from old library!
\r
59 #include "dos_gfx.h"
\r
60 #include "lib\x\modex.h"
\r
63 //color
\82Ä
\82·
\82Æ
\r
67 int bakax = 0, bakay = 0;
\r
68 int xx = rand()&0%320, yy = rand()&0%240, sx = 0, sy = 0;
\r
72 * Comment out the following #define if you don't want the testing main()
\r
78 * Define the port addresses of some VGA registers.
\r
80 #define CRTC_ADDR 0x3d4 /* Base port of the CRT Controller (color) */
\r
82 #define SEQU_ADDR 0x3c4 /* Base port of the Sequencer */
\r
83 #define GRAC_ADDR 0x3ce /* Base port of the Graphics Controller */
\r
87 * Make a far pointer to the VGA graphics buffer segment. Your compiler
\r
88 * might not have the MK_FP macro, but you'll figure something out.
\r
90 byte *vga = (byte *) MK_FP(0xA000, 0);
\r
92 //fontAddr = getFont();
\r
95 * width and height should specify the mode dimensions. widthBytes
\r
96 * specify the width of a line in addressable bytes.
\r
98 unsigned width, height, widthBytes;
\r
101 * actStart specifies the start of the page being accessed by
\r
102 * drawing operations. visStart specifies the contents of the Screen
\r
103 * Start register, i.e. the start of the visible page.
\r
105 unsigned actStart, visStart;
\r
108 * set320x200x256_X()
\r
109 * sets mode 13h, then turns it into an unchained (planar), 4-page
\r
110 * 320x200x256 mode.
\r
112 void set320x200x256_X(void)
\r
116 /* Set VGA BIOS mode 13h: */
\r
118 int86(0x10, &r, &r);
\r
120 /* Turn off the Chain-4 bit (bit 3 at index 4, port 0x3c4): */
\r
121 outpw(SEQU_ADDR, 0x0604);
\r
123 /* Turn off word mode, by setting the Mode Control register
\r
124 of the CRT Controller (index 0x17, port 0x3d4): */
\r
125 outpw(CRTC_ADDR, 0xE317);
\r
127 /* Turn off doubleword mode, by setting the Underline Location
\r
128 register (index 0x14, port 0x3d4): */
\r
129 outpw(CRTC_ADDR, 0x0014);
\r
131 /* Clear entire video memory, by selecting all four planes, then
\r
132 writing 0 to entire segment. */
\r
133 outpw(SEQU_ADDR, 0x0F02);
\r
134 memset(vga+1, 0, 0xffff); /* stupid size_t exactly 1 too small */
\r
137 /* Update the global variables to reflect dimensions of this
\r
138 mode. This is needed by most future drawing operations. */
\r
142 /* Each byte addresses four pixels, so the width of a scan line
\r
143 in *bytes* is one fourth of the number of pixels on a line. */
\r
144 widthBytes = width / 4;
\r
146 /* By default we want screen refreshing and drawing operations
\r
147 to be based at offset 0 in the video segment. */
\r
148 actStart = visStart = 0;
\r
152 * setActiveStart() tells our graphics operations which address in video
\r
153 * memory should be considered the top left corner.
\r
155 void setActiveStart(unsigned offset)
\r
161 * setVisibleStart() tells the VGA from which byte to fetch the first
\r
162 * pixel when starting refresh at the top of the screen. This version
\r
163 * won't look very well in time critical situations (games for
\r
164 * instance) as the register outputs are not synchronized with the
\r
165 * screen refresh. This refresh might start when the high byte is
\r
166 * set, but before the low byte is set, which produces a bad flicker.
\r
168 void setVisibleStart(unsigned offset)
\r
171 outpw(CRTC_ADDR, 0x0C); /* set high byte */
\r
172 outpw(CRTC_ADDR+1, visStart >> 8);
\r
173 outpw(CRTC_ADDR, 0x0D); /* set low byte */
\r
174 outpw(CRTC_ADDR+1, visStart & 0xff);
\r
178 * setXXXPage() sets the specified page by multiplying the page number
\r
179 * with the size of one page at the current resolution, then handing the
\r
180 * resulting offset value over to the corresponding setXXXStart()
\r
181 * function. The first page is number 0.
\r
183 void setActivePage(int page)
\r
185 setActiveStart(page * widthBytes * height);
\r
188 void setVisiblePage(int page)
\r
190 setVisibleStart(page * widthBytes * height);
\r
193 void vScroll(int rows)
\r
195 // Scrolling = current start + (rows * bytes in a row)
\r
196 setVisibleStart(visStart + (rows * width));
\r
199 void putPixel_X(int x, int y, byte color)
\r
201 /* Each address accesses four neighboring pixels, so set
\r
202 Write Plane Enable according to which pixel we want
\r
203 to modify. The plane is determined by the two least
\r
204 significant bits of the x-coordinate: */
\r
206 outp(0x3c5, 0x01 << (x & 3));
\r
208 /* The offset of the pixel into the video segment is
\r
209 offset = (width * y + x) / 4, and write the given
\r
210 color to the plane we selected above. Heed the active
\r
211 page start selection. */
\r
212 vga[(unsigned)(widthBytes * y) + (x / 4) + actStart] = color;
\r
216 byte getPixel_X(int x, int y)
\r
218 /* Select the plane from which we must read the pixel color: */
\r
219 outpw(GRAC_ADDR, 0x04);
\r
220 outpw(GRAC_ADDR+1, x & 3);
\r
222 return vga[(unsigned)(widthBytes * y) + (x / 4) + actStart];
\r
226 void set320x240x256_X(void)
\r
228 /* Set the unchained version of mode 13h: */
\r
229 set320x200x256_X();
\r
231 /* Modify the vertical sync polarity bits in the Misc. Output
\r
232 Register to achieve square aspect ratio: */
\r
235 /* Modify the vertical timing registers to reflect the increased
\r
236 vertical resolution, and to center the image as good as
\r
238 outpw(0x3D4, 0x2C11); /* turn off write protect */
\r
239 outpw(0x3D4, 0x0D06); /* vertical total */
\r
240 outpw(0x3D4, 0x3E07); /* overflow register */
\r
241 outpw(0x3D4, 0xEA10); /* vertical retrace start */
\r
242 outpw(0x3D4, 0xAC11); /* vertical retrace end AND wr.prot */
\r
243 outpw(0x3D4, 0xDF12); /* vertical display enable end */
\r
244 outpw(0x3D4, 0xE715); /* start vertical blanking */
\r
245 outpw(0x3D4, 0x0616); /* end vertical blanking */
\r
247 /* Update mode info, so future operations are aware of the
\r
254 /*-----------XXXX-------------*/
\r
256 void putColorBox_X(int x, int y, int w, int h, byte color) {
\r
261 for (curx=x; curx<(x+w); curx++) {
\r
262 outp(0x3c5, 0x01 << (curx & 3));
\r
263 drawptr = (unsigned)(widthBytes * y) + (curx / 4) + actStart;
\r
264 for (cury=0; cury<h; cury++) {
\r
265 vga[drawptr] = color;
\r
266 drawptr += widthBytes;
\r
271 WHILE NOT FINISHED DO
\r
272 OFFSET = OFFSET + 80
\r
273 IF OFFSET >= (200 * 80) THEN OFFSET = 0
\r
275 SET VGA OFFSET = OFFSET
\r
276 DRAW TO ROW -1 (was row 0 before scroll)
\r
280 WHILE NOT FINISHED DO
\r
281 OFFSET = OFFSET + 80
\r
282 IF OFFSET >= (240 * 80) THEN OFFSET = 0
\r
284 SET VGA OFFSET = OFFSET
\r
285 DRAW TO ROW -1 (was row 0 before scroll)
\r
289 //---------------------------------------------------
\r
291 // Use the bios to get the address of the 8x8 font
\r
293 // You need a font if you are going to draw text.
\r
302 memset(&rg, 0, sizeof(rg));
\r
311 return (int far *)MK_FP(seg, off);
\r
314 void drawChar(int x, int y, int color, byte c)
\r
318 int far *font = getFont() + (c * 8);
\r
320 for (i = 0; i < 8; i++)
\r
323 for (j = 0; j < 8; j++)
\r
327 //pixel(x + j, y + i, color);
\r
328 putPixel_X(x + j, y + i, color);
\r
336 void drawText(int x, int y, int color, byte string)
\r
340 drawChar(x, y, color, string);
\r
346 /////////////////////////////////////////////////////////////////////////////
\r
348 // setvideo() - This function Manages the video modes //
\r
350 /////////////////////////////////////////////////////////////////////////////
\r
351 void setvideo(/*byte mode, */int vq){
\r
352 union REGS in, out;
\r
354 if(!vq){ // deinit the video
\r
355 // change to the video mode we were in before we switched to mode 13h
\r
357 in.h.al = old_mode;
\r
358 int86(0x10, &in, &out);
\r
360 }else if(vq == 1){ // init the video
\r
361 // get old video mode
\r
363 int86(0x10, &in, &out);
\r
364 old_mode = out.h.al;
\r
367 set320x240x256_X();
\r
371 /////////////////////////////////////////////////////////////////////////////
\r
373 // cls() - This clears the screen to the specified color, on the VGA or on //
\r
374 // the Virtual screen. //
\r
376 /////////////////////////////////////////////////////////////////////////////
\r
377 void cls(byte color, byte *Where){
\r
378 _fmemset(Where, color, width*(height*17));
\r
381 //color
\82Ä
\82·
\82Æ
\r
383 if(gq < NUM_COLORS){
\r
390 //color
\82Ä
\82·
\82Æ
\r
393 //---- cls(gq, vaddr);
\r
400 //slow spectrum down
\r
404 //plotpixel(xx, yy, coor, vga);
\r
405 //ppf(sx, sy, coor, vga);
\r
406 putPixel_X(sx, sy, coor);
\r
407 //printf("%d %d %d %d\n", sx, sy, svq, coor);
\r
412 if(svq == 7) coor++;
\r
413 if(sy == height && svq == 8) coor = rand()%NUM_COLORS;
\r
418 /*-----------ding-------------*/
\r
424 //++++ if(q <= 4 && q!=2 && gq == BONK-1) coor = rand()%HGQ;
\r
428 ) && gq == BONK-1){
\r
429 if(coor < HGQ && coor < LGQ) coor = LGQ;
\r
433 bakax = rand()%3; bakay = rand()%3;
\r
437 if(q == 8){ colorz(); return gq; }else
\r
438 if(q == 10){ ssd(q); /*printf("%d\n", coor);*/ }else
\r
439 if(q == 5){ colortest(); return gq; }else
\r
440 if(q == 11){ colorz(); delay(100); return gq; }
\r
442 coor = rand()%NUM_COLORS;
\r
443 //---- cls(coor, vaddr);
\r
448 if(q == 7 || q== 9){
\r
451 if(q == 9){ ssd(q); coor++; }
\r
455 if((q<5 && gq<BONK) || (q==16 && gq<BONK)){ // the number variable make the colors more noticable
\r
457 if(xx==width){bakax=0;}
\r
458 if(xx==0){bakax=1;}
\r
459 if(yy==height){bakay=0;}
\r
460 if(yy==0){bakay=1;}
\r
462 if(xx!=width||yy!=height){
\r
463 if(xx==0){bakax=1;bakay=-1;d3y=1;}
\r
464 if(yy==0){bakax=1;bakay=0;d3y=1;}
\r
465 if(xx==width){bakax=-1;bakay=-1;d3y=1;}
\r
466 if(yy==height){bakax=1;bakay=0;d3y=1;}
\r
467 }else if(xx==width&&yy==height) xx=yy=0;
\r
514 // if(xx<0) xx=width;
\r
515 // if(yy<0) yy=height;
\r
516 // if(xx>width) xx=0;
\r
517 // if(yy>height) yy=0;
\r
520 //interesting effects
\r
526 putPixel_X(tx, ty, coor);
\r
527 //drawrect(tx, ty, tx+TILEWH, ty+TILEWH, coor);
\r
528 //printf("%d %d %d %d %d %d\n", xx, yy, tx, ty, TILEWH);
\r
531 //---- ppf(xx, yy, coor, vga);
\r
532 }else if(xx>=0 && xx<width && yy>=0 && yy<height){
\r
533 putColorBox_X(xx, yy, TILEWH, TILEWH, coor);
\r
534 //++++0000 putPixel_X(xx, yy, coor);
\r
537 //---- if(q==2) ppf(rand()%, rand()%height, 0, vga);
\r
538 if(q==2) putColorBox_X(rand()%width, rand()%height, TILEWH, TILEWH, 0);
\r
539 if(q==16) putPixel_X(rand()%width, rand()%height, 0);
\r
540 if(q==2||q==4||q==16){ bakax = rand()%3; bakay = rand()%3; }
\r
542 //if(xx<0||xx>320||yy<0||yy>240)
\r
543 // printf("%d %d %d %d %d %d\n", xx, yy, coor, bakax, bakay, getPixel_X(xx,yy));
\r
544 // printf("%d\n", getPixel_X(xx,yy));
\r
546 // drawText(0, 0, 15, getPixel_X(xx,yy));
\r
553 * The library testing routines follows below.
\r
564 int p, x, y, pages;
\r
566 /* This is the way to calculate the number of pages available. */
\r
567 pages = 65536L/(widthBytes*height); // apparently this takes the A000 address
\r
569 printf("%d\n", pages);
\r
571 for (p = 0; p <= pages; ++p)
\r
575 /* On each page draw a single colored border, and dump the palette
\r
576 onto a small square about the middle of the page. */
\r
579 for (x = 0; x <= width; ++x)
\r
581 putPixel_X(x, 0, p+1);
\r
582 if(p!=pages) putPixel_X(x, height-1, p+1);
\r
583 else putPixel_X(x, 99-1, p+1);
\r
586 for (y = 0; y <= height; ++y)
\r
588 putPixel_X(0, y, p+1);
\r
589 if(p!=pages) putPixel_X(width-1, y, p+1);
\r
590 else putPixel_X(width-1, y, p+1);
\r
593 for (x = 0; x < 16; ++x)
\r
594 for (y = 0; y < 16; ++y)
\r
595 putPixel_X(x+(p+2)*16, y+(p+2)*16, x + y*16);
\r
598 drawText(0, 0, 15, p);
\r
602 /* Each pages will now contain a different image. Let the user cycle
\r
603 through all the pages by pressing a key. */
\r
604 for (p = 0; p <= pages; ++p)
\r
607 //drawText(0, 240, 15, "bakapi");
\r
614 * Library test (program) entry point.
\r
621 d=1; // switch variable
\r
622 key=4; // default screensaver number
\r
623 // puts("First, have a look at the 320x200 mode. I will draw some rubbish");
\r
624 // puts("on all of the four pages, then let you cycle through them by");
\r
625 // puts("hitting a key on each page.");
\r
626 // puts("Press a key when ready...");
\r
631 // puts("Then, check out Mode X, 320x240 with 3 (and a half) pages.");
\r
632 // puts("Press a key when ready...");
\r
640 /*while(d!=0){ // on!
\r
641 if(!kbhit()){ // conditions of screen saver
\r
645 // user imput switch
\r
646 printf("Enter 1, 2, 3, 4, or 6 to run a screensaver, or enter 5 to quit.\n", getch()); // prompt the user
\r
648 //if(key==3){xx=yy=0;} // crazy screen saver wwww
\r
653 while(!kbhit()){ // conditions of screen saver
\r
656 //end of screen savers
\r
659 while(!kbhit()){ // conditions of screen saver
\r
666 puts("Where to next? It's your move! wwww");
\r
667 printf("bakapi ver. 1.04.09.01\nis made by sparky4
\81i
\81\86\83Ö
\81\85\81j feel free to use it ^^\nLicence: GPL v2\n");
\r