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
62 //color
\82Ä
\82·
\82Æ
\r
66 int bakax = 0, bakay = 0;
\r
67 int xx = rand()&0%320, yy = rand()&0%240, sx = 0, sy = 0;
\r
71 * Comment out the following #define if you don't want the testing main()
\r
77 * Define the port addresses of some VGA registers.
\r
79 #define CRTC_ADDR 0x3d4 /* Base port of the CRT Controller (color) */
\r
81 #define SEQU_ADDR 0x3c4 /* Base port of the Sequencer */
\r
82 #define GRAC_ADDR 0x3ce /* Base port of the Graphics Controller */
\r
86 * Make a far pointer to the VGA graphics buffer segment. Your compiler
\r
87 * might not have the MK_FP macro, but you'll figure something out.
\r
89 byte *vga = (byte *) MK_FP(0xA000, 0);
\r
91 //fontAddr = getFont();
\r
94 * width and height should specify the mode dimensions. widthBytes
\r
95 * specify the width of a line in addressable bytes.
\r
97 unsigned width, height, widthBytes;
\r
100 * actStart specifies the start of the page being accessed by
\r
101 * drawing operations. visStart specifies the contents of the Screen
\r
102 * Start register, i.e. the start of the visible page.
\r
104 unsigned actStart, visStart;
\r
107 * set320x200x256_X()
\r
108 * sets mode 13h, then turns it into an unchained (planar), 4-page
\r
109 * 320x200x256 mode.
\r
111 void set320x200x256_X(void)
\r
115 /* Set VGA BIOS mode 13h: */
\r
117 int86(0x10, &r, &r);
\r
119 /* Turn off the Chain-4 bit (bit 3 at index 4, port 0x3c4): */
\r
120 outpw(SEQU_ADDR, 0x0604);
\r
122 /* Turn off word mode, by setting the Mode Control register
\r
123 of the CRT Controller (index 0x17, port 0x3d4): */
\r
124 outpw(CRTC_ADDR, 0xE317);
\r
126 /* Turn off doubleword mode, by setting the Underline Location
\r
127 register (index 0x14, port 0x3d4): */
\r
128 outpw(CRTC_ADDR, 0x0014);
\r
130 /* Clear entire video memory, by selecting all four planes, then
\r
131 writing 0 to entire segment. */
\r
132 outpw(SEQU_ADDR, 0x0F02);
\r
133 memset(vga+1, 0, 0xffff); /* stupid size_t exactly 1 too small */
\r
136 /* Update the global variables to reflect dimensions of this
\r
137 mode. This is needed by most future drawing operations. */
\r
141 /* Each byte addresses four pixels, so the width of a scan line
\r
142 in *bytes* is one fourth of the number of pixels on a line. */
\r
143 widthBytes = width / 4;
\r
145 /* By default we want screen refreshing and drawing operations
\r
146 to be based at offset 0 in the video segment. */
\r
147 actStart = visStart = 0;
\r
151 * setActiveStart() tells our graphics operations which address in video
\r
152 * memory should be considered the top left corner.
\r
154 void setActiveStart(unsigned offset)
\r
160 * setVisibleStart() tells the VGA from which byte to fetch the first
\r
161 * pixel when starting refresh at the top of the screen. This version
\r
162 * won't look very well in time critical situations (games for
\r
163 * instance) as the register outputs are not synchronized with the
\r
164 * screen refresh. This refresh might start when the high byte is
\r
165 * set, but before the low byte is set, which produces a bad flicker.
\r
167 void setVisibleStart(unsigned offset)
\r
170 outpw(CRTC_ADDR, 0x0C); /* set high byte */
\r
171 outpw(CRTC_ADDR+1, visStart >> 8);
\r
172 outpw(CRTC_ADDR, 0x0D); /* set low byte */
\r
173 outpw(CRTC_ADDR+1, visStart & 0xff);
\r
177 * setXXXPage() sets the specified page by multiplying the page number
\r
178 * with the size of one page at the current resolution, then handing the
\r
179 * resulting offset value over to the corresponding setXXXStart()
\r
180 * function. The first page is number 0.
\r
182 void setActivePage(int page)
\r
184 setActiveStart(page * widthBytes * height);
\r
187 void setVisiblePage(int page)
\r
189 setVisibleStart(page * widthBytes * height);
\r
192 void putPixel_X(int x, int y, byte color)
\r
194 /* Each address accesses four neighboring pixels, so set
\r
195 Write Plane Enable according to which pixel we want
\r
196 to modify. The plane is determined by the two least
\r
197 significant bits of the x-coordinate: */
\r
199 outp(0x3c5, 0x01 << (x & 3));
\r
201 /* The offset of the pixel into the video segment is
\r
202 offset = (width * y + x) / 4, and write the given
\r
203 color to the plane we selected above. Heed the active
\r
204 page start selection. */
\r
205 vga[(unsigned)(widthBytes * y) + (x / 4) + actStart] = color;
\r
209 byte getPixel_X(int x, int y)
\r
211 /* Select the plane from which we must read the pixel color: */
\r
212 outpw(GRAC_ADDR, 0x04);
\r
213 outpw(GRAC_ADDR+1, x & 3);
\r
215 return vga[(unsigned)(widthBytes * y) + (x / 4) + actStart];
\r
219 void set320x240x256_X(void)
\r
221 /* Set the unchained version of mode 13h: */
\r
222 set320x200x256_X();
\r
224 /* Modify the vertical sync polarity bits in the Misc. Output
\r
225 Register to achieve square aspect ratio: */
\r
228 /* Modify the vertical timing registers to reflect the increased
\r
229 vertical resolution, and to center the image as good as
\r
231 outpw(0x3D4, 0x2C11); /* turn off write protect */
\r
232 outpw(0x3D4, 0x0D06); /* vertical total */
\r
233 outpw(0x3D4, 0x3E07); /* overflow register */
\r
234 outpw(0x3D4, 0xEA10); /* vertical retrace start */
\r
235 outpw(0x3D4, 0xAC11); /* vertical retrace end AND wr.prot */
\r
236 outpw(0x3D4, 0xDF12); /* vertical display enable end */
\r
237 outpw(0x3D4, 0xE715); /* start vertical blanking */
\r
238 outpw(0x3D4, 0x0616); /* end vertical blanking */
\r
240 /* Update mode info, so future operations are aware of the
\r
248 void putColorBox_X(int x, int y, int w, int h, byte color) {
253 for (curx=x; curx<(x+w); curx++) {
254 outp(0x3c5, 0x01 << (curx & 3));
255 drawptr = (unsigned)(widthBytes * y) + (curx / 4) + actStart;
256 for (cury=0; cury<h; cury++) {
257 vga[drawptr] = color;
258 drawptr += widthBytes;
264 /*-----------XXXX-------------*/
\r
265 //---------------------------------------------------
\r
267 // Use the bios to get the address of the 8x8 font
\r
269 // You need a font if you are going to draw text.
\r
278 memset(&rg, 0, sizeof(rg));
\r
287 return (int far *)MK_FP(seg, off);
\r
290 void drawChar(int x, int y, int color, byte c)
\r
294 int far *font = getFont() + (c * 8);
\r
296 for (i = 0; i < 8; i++)
\r
299 for (j = 0; j < 8; j++)
\r
303 //pixel(x + j, y + i, color);
\r
304 putPixel_X(x + j, y + i, color);
\r
312 void drawText(int x, int y, int color, byte string)
\r
316 drawChar(x, y, color, string);
\r
322 /////////////////////////////////////////////////////////////////////////////
\r
324 // setvideo() - This function Manages the video modes //
\r
326 /////////////////////////////////////////////////////////////////////////////
\r
327 void setvideo(/*byte mode, */int vq){
\r
328 union REGS in, out;
\r
330 if(!vq){ // deinit the video
\r
331 // change to the video mode we were in before we switched to mode 13h
\r
333 in.h.al = old_mode;
\r
334 int86(0x10, &in, &out);
\r
336 }else if(vq == 1){ // init the video
\r
337 // get old video mode
\r
339 int86(0x10, &in, &out);
\r
340 old_mode = out.h.al;
\r
343 set320x240x256_X();
\r
347 /////////////////////////////////////////////////////////////////////////////
\r
349 // cls() - This clears the screen to the specified color, on the VGA or on //
\r
350 // the Virtual screen. //
\r
352 /////////////////////////////////////////////////////////////////////////////
\r
353 void cls(byte color, byte *Where){
\r
354 _fmemset(Where, color, width*(height*17));
357 //color
\82Ä
\82·
\82Æ
\r
359 if(gq < NUM_COLORS){
\r
366 //color
\82Ä
\82·
\82Æ
\r
369 //---- cls(gq, vaddr);
\r
376 //slow spectrum down
\r
380 //plotpixel(xx, yy, coor, vga);
\r
381 //ppf(sx, sy, coor, vga);
\r
382 putPixel_X(sx, sy, coor);
\r
383 //printf("%d %d %d %d\n", sx, sy, svq, coor);
\r
388 if(svq == 7) coor++;
\r
389 if(sy == height && svq == 8) coor = rand()%NUM_COLORS;
\r
394 /*-----------ding-------------*/
\r
400 //++++ if(q <= 4 && q!=2 && gq == BONK-1) coor = rand()%HGQ;
\r
404 ) && gq == BONK-1){
\r
405 if(coor < HGQ && coor < LGQ) coor = LGQ;
\r
409 bakax = rand()%3; bakay = rand()%3;
\r
413 if(q == 5){ colortest(); return gq; }
\r
414 if(q == 10){ colorz(); return gq; }
\r
415 if(q == 11){ colorz(); delay(100); return gq; }
\r
416 if(q == 8){ ssd(q); /*printf("%d\n", coor);*/ }
\r
418 coor = rand()%NUM_COLORS;
\r
419 //---- cls(coor, vaddr);
\r
424 if(q == 7 || q== 9){
\r
427 if(q == 9){ ssd(q); coor++; }
\r
431 if((q<5 && gq<BONK) || (q==16 && gq<BONK)){ // the number variable make the colors more noticable
\r
433 if(xx==width){bakax=0;}
\r
434 if(xx==0){bakax=1;}
\r
435 if(yy==height){bakay=0;}
\r
436 if(yy==0){bakay=1;}
\r
438 if(xx!=width||yy!=height){
\r
439 if(xx==0){bakax=1;bakay=-1;d3y=1;}
\r
440 if(yy==0){bakax=1;bakay=0;d3y=1;}
\r
441 if(xx==width){bakax=-1;bakay=-1;d3y=1;}
\r
442 if(yy==height){bakax=1;bakay=0;d3y=1;}
\r
443 }else if(xx==width&&yy==height) xx=yy=0;
\r
491 if(yy<0) yy=height;
\r
493 if(yy>height) yy=0;
\r
496 //interesting effects
\r
502 putPixel_X(tx, ty, coor);
\r
503 //drawrect(tx, ty, tx+TILEWH, ty+TILEWH, coor);
\r
504 //printf("%d %d %d %d %d %d\n", xx, yy, tx, ty, TILEWH);
\r
507 //---- ppf(xx, yy, coor, vga);
\r
508 //++++0000 putPixel_X(xx, yy, coor);
\r
509 }else putColorBox_X(xx, yy, TILEWH, TILEWH, coor);
510 //drawrect(xx, yy, xx+TILEWH-1, yy+TILEWH-1, coor);
\r
511 //---- if(q==2) ppf(rand()%, rand()%height, 0, vga);
\r
512 if(q==2||q==16) putPixel_X(rand()%width, rand()%height, 0);
\r
513 if(q==2||q==4||q==16){ bakax = rand()%3; bakay = rand()%3; }
\r
515 //if(xx<0||xx>320||yy<0||yy>240)
\r
516 // printf("%d %d %d %d %d %d\n", xx, yy, coor, bakax, bakay, getPixel_X(xx,yy));
\r
517 // printf("%d\n", getPixel_X(xx,yy));
\r
519 // drawText(0, 0, 15, getPixel_X(xx,yy));
\r
526 * The library testing routines follows below.
\r
537 int p, x, y, pages;
\r
539 /* This is the way to calculate the number of pages available. */
\r
540 pages = 65536L/(widthBytes*height); // apparently this takes the A000 address
\r
542 printf("%d\n", pages);
\r
544 for (p = 0; p <= pages; ++p)
\r
548 /* On each page draw a single colored border, and dump the palette
\r
549 onto a small square about the middle of the page. */
\r
552 for (x = 0; x <= width; ++x)
\r
554 putPixel_X(x, 0, p+1);
\r
555 if(p!=pages) putPixel_X(x, height-1, p+1);
\r
556 else putPixel_X(x, 99-1, p+1);
\r
559 for (y = 0; y <= height; ++y)
\r
561 putPixel_X(0, y, p+1);
\r
562 if(p!=pages) putPixel_X(width-1, y, p+1);
\r
563 else putPixel_X(width-1, y, p+1);
\r
566 for (x = 0; x < 16; ++x)
\r
567 for (y = 0; y < 16; ++y)
\r
568 putPixel_X(x+(p+2)*16, y+(p+2)*16, x + y*16);
\r
571 drawText(0, 0, 15, p);
\r
575 /* Each pages will now contain a different image. Let the user cycle
\r
576 through all the pages by pressing a key. */
\r
577 for (p = 0; p <= pages; ++p)
\r
580 //drawText(0, 240, 15, "bakapi");
\r
587 * Library test (program) entry point.
\r
594 d=1; // switch variable
\r
595 key=4; // default screensaver number
\r
596 // puts("First, have a look at the 320x200 mode. I will draw some rubbish");
\r
597 // puts("on all of the four pages, then let you cycle through them by");
\r
598 // puts("hitting a key on each page.");
\r
599 // puts("Press a key when ready...");
\r
604 // puts("Then, check out Mode X, 320x240 with 3 (and a half) pages.");
\r
605 // puts("Press a key when ready...");
\r
611 /*while(d!=0){ // on!
\r
612 if(!kbhit()){ // conditions of screen saver
\r
616 // user imput switch
\r
617 printf("Enter 1, 2, 3, 4, or 6 to run a screensaver, or enter 5 to quit.\n", getch()); // prompt the user
\r
619 //if(key==3){xx=yy=0;} // crazy screen saver wwww
\r
624 while(!kbhit()){ // conditions of screen saver
\r
627 //end of screen savers
\r
630 puts("Where to next? It's your move! wwww");
\r
631 printf("bakapi ver. 1.04.09a\nis made by sparky4
\81i
\81\86\83Ö
\81\85\81j feel free to use it ^^\nLicence: GPL v2\n");
\r