simple.c revision 28328
128328Ssos/*-
228328Ssos * Copyright (c) 1991-1997 S�ren Schmidt
328328Ssos * All rights reserved.
428328Ssos *
528328Ssos * Redistribution and use in source and binary forms, with or without
628328Ssos * modification, are permitted provided that the following conditions
728328Ssos * are met:
828328Ssos * 1. Redistributions of source code must retain the above copyright
928328Ssos *    notice, this list of conditions and the following disclaimer
1028328Ssos *    in this position and unchanged.
1128328Ssos * 2. Redistributions in binary form must reproduce the above copyright
1228328Ssos *    notice, this list of conditions and the following disclaimer in the
1328328Ssos *    documentation and/or other materials provided with the distribution.
1428328Ssos * 3. The name of the author may not be used to endorse or promote products
1528328Ssos *    derived from this software withough specific prior written permission
1628328Ssos *
1728328Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1828328Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1928328Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2028328Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2128328Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2228328Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2328328Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2428328Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2528328Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2628328Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2728328Ssos *
2828328Ssos *  $Id: simple.c,v 1.8 1997/08/15 12:32:59 sos Exp $
2928328Ssos */
3028328Ssos
3128328Ssos#include <signal.h>
3228328Ssos#include <machine/console.h>
3328328Ssos#include "vgl.h"
3428328Ssos
3528328Ssosstatic byte VGLSavePaletteRed[256];
3628328Ssosstatic byte VGLSavePaletteGreen[256];
3728328Ssosstatic byte VGLSavePaletteBlue[256];
3828328Ssos
3928328Ssos#define ABS(a)		(((a)<0) ? -(a) : (a))
4028328Ssos#define SGN(a)		(((a)<0) ? -1 : 1)
4128328Ssos
4228328Ssos
4328328Ssosvoid
4428328SsosVGLSetXY(VGLBitmap *object, int x, int y, byte color)
4528328Ssos{
4628328Ssos  VGLCheckSwitch();
4728328Ssos  if (x>=0 && x<object->Xsize && y>=0 && y<object->Ysize) {
4828328Ssos    if (!VGLMouseFreeze(x, y, 1, 1, color)) {
4928328Ssos      switch (object->Type) {
5028328Ssos      case MEMBUF:
5128328Ssos      case VIDBUF8:
5228328Ssos        object->Bitmap[y*object->Xsize+x]=(color);
5328328Ssos        break;
5428328Ssos      case VIDBUF8X:
5528328Ssos        outb(0x3c4, 0x02);
5628328Ssos        outb(0x3c5, 0x01 << (x&0x3));
5728328Ssos	object->Bitmap[(unsigned)(object->Xsize/2*y)+(x/4)] = (color);
5828328Ssos	break;
5928328Ssos      case VIDBUF4:
6028328Ssos          outb(0x3c4, 0x02); outb(0x3c5, 0x01);
6128328Ssos          outb(0x3ce, 0x04); outb(0x3cf, 0x00);
6228328Ssos	  object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] =
6328328Ssos            ( object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] & ~(0x80>>(x%8)) )
6428328Ssos             |  ((color & 0x01) ? (0x80>>(x%8)) : 0);
6528328Ssos          outb(0x3c4, 0x02); outb(0x3c5, 0x02);
6628328Ssos          outb(0x3ce, 0x04); outb(0x3cf, 0x01);
6728328Ssos	  object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] =
6828328Ssos            ( object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] & ~(0x80>>(x%8)) )
6928328Ssos             |  ((color & 0x02) ? (0x80>>(x%8)) : 0);
7028328Ssos          outb(0x3c4, 0x02); outb(0x3c5, 0x04);
7128328Ssos          outb(0x3ce, 0x04); outb(0x3cf, 0x02);
7228328Ssos	  object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] =
7328328Ssos            ( object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] & ~(0x80>>(x%8)) )
7428328Ssos             |  ((color & 0x04) ? (0x80>>(x%8)) : 0);
7528328Ssos          outb(0x3c4, 0x02); outb(0x3c5, 0x08);
7628328Ssos          outb(0x3ce, 0x04); outb(0x3cf, 0x03);
7728328Ssos	  object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] =
7828328Ssos            ( object->Bitmap[(y*object->Xsize/8+x/8)&0xffff] & ~(0x80>>(x%8)) )
7928328Ssos             |  ((color & 0x08) ? (0x80>>(x%8)) : 0);
8028328Ssos      }
8128328Ssos    }
8228328Ssos    VGLMouseUnFreeze();
8328328Ssos  }
8428328Ssos}
8528328Ssos
8628328Ssosbyte
8728328SsosVGLGetXY(VGLBitmap *object, int x, int y)
8828328Ssos{
8928328Ssos  VGLCheckSwitch();
9028328Ssos  switch (object->Type) {
9128328Ssos    case MEMBUF:
9228328Ssos    case VIDBUF8:
9328328Ssos      return object->Bitmap[((y*object->Xsize)+x)];
9428328Ssos      break;
9528328Ssos    case VIDBUF8X:
9628328Ssos      outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
9728328Ssos      return object->Bitmap[(unsigned)(object->Xsize/4*y)+(x/4)];
9828328Ssos      break;
9928328Ssos    case VIDBUF4:
10028328Ssos      return (object->Bitmap[((y*object->Xsize/8)+x/8)]&(0x80>>(x%8))) ? 1 : 0;
10128328Ssos      break;
10228328Ssos  }
10328328Ssos  return 0;
10428328Ssos}
10528328Ssos
10628328Ssosvoid
10728328SsosVGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
10828328Ssos{
10928328Ssos  int d, x, y, ax, ay, sx, sy, dx, dy;
11028328Ssos
11128328Ssos  dx = x2-x1; ax = ABS(dx)<<1; sx = SGN(dx); x = x1;
11228328Ssos  dy = y2-y1; ay = ABS(dy)<<1; sy = SGN(dy); y = y1;
11328328Ssos
11428328Ssos  if (ax>ay) {					/* x dominant */
11528328Ssos    d = ay-(ax>>1);
11628328Ssos    for (;;) {
11728328Ssos      VGLSetXY(object, x, y, color);
11828328Ssos      if (x==x2)
11928328Ssos	break;
12028328Ssos      if (d>=0) {
12128328Ssos	y += sy; d -= ax;
12228328Ssos      }
12328328Ssos      x += sx; d += ay;
12428328Ssos    }
12528328Ssos  }
12628328Ssos  else {					/* y dominant */
12728328Ssos    d = ax-(ay>>1);
12828328Ssos    for (;;) {
12928328Ssos      VGLSetXY(object, x, y, color);
13028328Ssos      if (y==y2)
13128328Ssos	break;
13228328Ssos      if (d>=0) {
13328328Ssos	x += sx; d -= ay;
13428328Ssos      }
13528328Ssos      y += sy; d += ax;
13628328Ssos    }
13728328Ssos  }
13828328Ssos}
13928328Ssos
14028328Ssosvoid
14128328SsosVGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
14228328Ssos{
14328328Ssos  VGLLine(object, x1, y1, x2, y1, color);
14428328Ssos  VGLLine(object, x2, y1, x2, y2, color);
14528328Ssos  VGLLine(object, x2, y2, x1, y2, color);
14628328Ssos  VGLLine(object, x1, y2, x1, y1, color);
14728328Ssos}
14828328Ssos
14928328Ssosvoid
15028328SsosVGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
15128328Ssos{
15228328Ssos  int y;
15328328Ssos
15428328Ssos  for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
15528328Ssos}
15628328Ssos
15728328Ssosvoid
15828328Ssosinline set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, byte color)
15928328Ssos{
16028328Ssos  if (x!=0) {
16128328Ssos    VGLSetXY(object, xc+x, yc+y, color);
16228328Ssos    VGLSetXY(object, xc-x, yc+y, color);
16328328Ssos    if (y!=0) {
16428328Ssos      VGLSetXY(object, xc+x, yc-y, color);
16528328Ssos      VGLSetXY(object, xc-x, yc-y, color);
16628328Ssos    }
16728328Ssos  }
16828328Ssos  else {
16928328Ssos    VGLSetXY(object, xc, yc+y, color);
17028328Ssos    if (y!=0)
17128328Ssos      VGLSetXY(object, xc, yc-y, color);
17228328Ssos  }
17328328Ssos}
17428328Ssos
17528328Ssosvoid
17628328SsosVGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, byte color)
17728328Ssos{
17828328Ssos  int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
17928328Ssos  int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
18028328Ssos
18128328Ssos  while (dx<dy) {
18228328Ssos    set4pixels(object, x, y, xc, yc, color);
18328328Ssos    if (d>0) {
18428328Ssos      y--; dy-=asq2; d-=dy;
18528328Ssos    }
18628328Ssos    x++; dx+=bsq2; d+=bsq+dx;
18728328Ssos  }
18828328Ssos  d+=(3*(asq-bsq)/2-(dx+dy))/2;
18928328Ssos  while (y>=0) {
19028328Ssos    set4pixels(object, x, y, xc, yc, color);
19128328Ssos    if (d<0) {
19228328Ssos      x++; dx+=bsq2; d+=dx;
19328328Ssos    }
19428328Ssos    y--; dy-=asq2; d+=asq-dy;
19528328Ssos  }
19628328Ssos}
19728328Ssos
19828328Ssosvoid
19928328Ssosinline set2lines(VGLBitmap *object, int x, int y, int xc, int yc, byte color)
20028328Ssos{
20128328Ssos  if (x!=0) {
20228328Ssos    VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
20328328Ssos    if (y!=0)
20428328Ssos      VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
20528328Ssos  }
20628328Ssos  else {
20728328Ssos    VGLLine(object, xc, yc+y, xc, yc-y, color);
20828328Ssos  }
20928328Ssos}
21028328Ssos
21128328Ssosvoid
21228328SsosVGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, byte color)
21328328Ssos{
21428328Ssos  int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
21528328Ssos  int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
21628328Ssos
21728328Ssos  while (dx<dy) {
21828328Ssos    set2lines(object, x, y, xc, yc, color);
21928328Ssos    if (d>0) {
22028328Ssos      y--; dy-=asq2; d-=dy;
22128328Ssos    }
22228328Ssos    x++; dx+=bsq2; d+=bsq+dx;
22328328Ssos  }
22428328Ssos  d+=(3*(asq-bsq)/2-(dx+dy))/2;
22528328Ssos  while (y>=0) {
22628328Ssos    set2lines(object, x, y, xc, yc, color);
22728328Ssos    if (d<0) {
22828328Ssos      x++; dx+=bsq2; d+=dx;
22928328Ssos    }
23028328Ssos    y--; dy-=asq2; d+=asq-dy;
23128328Ssos  }
23228328Ssos}
23328328Ssos
23428328Ssosvoid
23528328SsosVGLClear(VGLBitmap *object, byte color)
23628328Ssos{
23728328Ssos  VGLCheckSwitch();
23828328Ssos  VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color);
23928328Ssos  switch (object->Type) {
24028328Ssos  case MEMBUF:
24128328Ssos  case VIDBUF8:
24228328Ssos    memset(object->Bitmap, color, object->Xsize*object->Ysize);
24328328Ssos    break;
24428328Ssos  case VIDBUF8X:
24528328Ssos    /* XXX works only for Xsize % 4 = 0 */
24628328Ssos    outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
24728328Ssos    memset(object->Bitmap, color, object->Xsize*object->Ysize/4);
24828328Ssos    break;
24928328Ssos
25028328Ssos  case VIDBUF4:
25128328Ssos    /* XXX works only for Xsize % 8 = 0 */
25228328Ssos    memset(object->Bitmap, color, object->Xsize/8*object->Ysize);
25328328Ssos    break;
25428328Ssos  }
25528328Ssos  VGLMouseUnFreeze();
25628328Ssos}
25728328Ssos
25828328Ssosvoid
25928328SsosVGLRestorePalette()
26028328Ssos{
26128328Ssos  int i;
26228328Ssos
26328328Ssos  outb(0x3C6, 0xFF);
26428328Ssos  inb(0x3DA);
26528328Ssos  outb(0x3C8, 0x00);
26628328Ssos  for (i=0; i<256; i++) {
26728328Ssos    outb(0x3C9, VGLSavePaletteRed[i]);
26828328Ssos    inb(0x84);
26928328Ssos    outb(0x3C9, VGLSavePaletteGreen[i]);
27028328Ssos    inb(0x84);
27128328Ssos    outb(0x3C9, VGLSavePaletteBlue[i]);
27228328Ssos    inb(0x84);
27328328Ssos  }
27428328Ssos  inb(0x3DA);
27528328Ssos  outb(0x3C0, 0x20);
27628328Ssos}
27728328Ssos
27828328Ssosvoid
27928328SsosVGLSavePalette()
28028328Ssos{
28128328Ssos  int i;
28228328Ssos
28328328Ssos  outb(0x3C6, 0xFF);
28428328Ssos  inb(0x3DA);
28528328Ssos  outb(0x3C7, 0x00);
28628328Ssos  for (i=0; i<256; i++) {
28728328Ssos    VGLSavePaletteRed[i] = inb(0x3C9);
28828328Ssos    inb(0x84);
28928328Ssos    VGLSavePaletteGreen[i] = inb(0x3C9);
29028328Ssos    inb(0x84);
29128328Ssos    VGLSavePaletteBlue[i] = inb(0x3C9);
29228328Ssos    inb(0x84);
29328328Ssos  }
29428328Ssos  inb(0x3DA);
29528328Ssos  outb(0x3C0, 0x20);
29628328Ssos}
29728328Ssos
29828328Ssosvoid
29928328SsosVGLSetPalette(byte *red, byte *green, byte *blue)
30028328Ssos{
30128328Ssos  int i;
30228328Ssos
30328328Ssos  for (i=0; i<256; i++) {
30428328Ssos    VGLSavePaletteRed[i] = red[i];
30528328Ssos    VGLSavePaletteGreen[i] = green[i];
30628328Ssos    VGLSavePaletteBlue[i] = blue[i];
30728328Ssos  }
30828328Ssos  VGLCheckSwitch();
30928328Ssos  outb(0x3C6, 0xFF);
31028328Ssos  inb(0x3DA);
31128328Ssos  outb(0x3C8, 0x00);
31228328Ssos  for (i=0; i<256; i++) {
31328328Ssos    outb(0x3C9, VGLSavePaletteRed[i]);
31428328Ssos    inb(0x84);
31528328Ssos    outb(0x3C9, VGLSavePaletteGreen[i]);
31628328Ssos    inb(0x84);
31728328Ssos    outb(0x3C9, VGLSavePaletteBlue[i]);
31828328Ssos    inb(0x84);
31928328Ssos  }
32028328Ssos  inb(0x3DA);
32128328Ssos  outb(0x3C0, 0x20);
32228328Ssos}
32328328Ssos
32428328Ssosvoid
32528328SsosVGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
32628328Ssos{
32728328Ssos  VGLSavePaletteRed[color] = red;
32828328Ssos  VGLSavePaletteGreen[color] = green;
32928328Ssos  VGLSavePaletteBlue[color] = blue;
33028328Ssos  VGLCheckSwitch();
33128328Ssos  outb(0x3C6, 0xFF);
33228328Ssos  inb(0x3DA);
33328328Ssos  outb(0x3C8, color);
33428328Ssos  outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
33528328Ssos  inb(0x3DA);
33628328Ssos  outb(0x3C0, 0x20);
33728328Ssos}
33828328Ssos
33928328Ssosvoid
34028328SsosVGLSetBorder(byte color)
34128328Ssos{
34228328Ssos  VGLCheckSwitch();
34328328Ssos  inb(0x3DA);
34428328Ssos  outb(0x3C0,0x11); outb(0x3C0, color);
34528328Ssos  inb(0x3DA);
34628328Ssos  outb(0x3C0, 0x20);
34728328Ssos}
34828328Ssos
34928328Ssosvoid
35028328SsosVGLBlankDisplay(int blank)
35128328Ssos{
35228328Ssos  byte val;
35328328Ssos
35428328Ssos  VGLCheckSwitch();
35528328Ssos  outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
35628328Ssos  outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
35728328Ssos}
358