home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / imlib / port / svga / video.c < prev   
Encoding:
C/C++ Source or Header  |  1997-07-07  |  4.5 KB  |  200 lines

  1. #include "globals.hpp"
  2. #include "system.h"
  3. #include "video.hpp"
  4. #include "dos.h"
  5. #include "macs.hpp"
  6. #include "image.hpp"
  7. #include "vga.h"
  8. #include "palette.hpp"
  9. #include "jmalloc.hpp"
  10.  
  11.  
  12. unsigned char current_background;
  13. extern unsigned int xres,yres;
  14. extern palette *lastl;
  15. int vmode;
  16. image *screen;
  17. uchar *v_addr;
  18.  
  19. int get_vmode()
  20. { return vmode; }
  21.  
  22. extern void JCMouse_init ();
  23.  
  24. void set_mode(int mode, int argc, char **argv)
  25. {
  26.   int fail=0,i;
  27.   if (mode==19) 
  28.     mode=G320x200x256;
  29.   for (i=1;i<argc;i++)
  30.   {
  31.     if (!strcmp(argv[i],"-vmode"))
  32.     { i++;
  33.       if (!strcmp(argv[i],"G320x200x256")) mode=G320x200x256;
  34.       else if (!strcmp(argv[i],"G640x480x256")) mode=G640x480x256;
  35.       else if (!strcmp(argv[i],"G800x600x256")) mode=G800x600x256;
  36.       else if (!strcmp(argv[i],"G1024x768x256")) mode=G1024x768x256;
  37.       else fail=1;
  38.     }
  39.   }  
  40.   if (fail)
  41.   { printf("Graphics modes supported :\n"
  42.             "  G320x200x256, G640x480x256, G800x600x256, G1024x768x256\n"
  43.             "  usage : %s [program options] [-vmode G....]\n",argv[0]);
  44.     exit(1);
  45.   }
  46.   JCMouse_init();
  47.   vga_init();
  48.   vga_setmode(mode);
  49.   v_addr=vga_getgraphmem();
  50.   xres=vga_getxdim()-1;
  51.   yres=vga_getydim()-1;
  52.  
  53.   vmode=mode;
  54.   screen=new image(xres+1,yres+1,NULL,2);
  55.   screen->clear();
  56.   update_dirty(screen);
  57. }
  58.  
  59. void close_graphics()
  60. {
  61.   if (lastl)
  62.     delete lastl;
  63.   lastl=NULL;
  64.   vga_setmode(0);
  65. }
  66.  
  67. inline void jmemcpy(long *dest, long *src, long size)
  68. {
  69.   register long *s=src;
  70.   register long *d=dest;
  71.  
  72.   size>>=2;  // doing word at a time
  73.   while (size--)
  74.     *(d++)=*(s++);  
  75. }
  76.  
  77. void put_part(image *im, int x, int y, int x1, int y1, int x2, int y2)
  78. {
  79.   unsigned long screen_off;
  80.   int ys,ye,         // ystart, yend
  81.         xs,xe,      
  82.         page,last_page=-1,yy;
  83.   long breaker;
  84.   unsigned char *line_addr;
  85.  
  86.   if (y>(int)yres || x>(int)xres) return ;
  87.   CHECK(y1>=0 && y2>=y1 && x1>=0 && x2>=x1);
  88.  
  89.  
  90.   if (y<0)
  91.   { y1+=-y; y=0; }
  92.   ys=y1;
  93.   if (y+(y2-y1)>=(int)yres)
  94.     ye=(int)yres-y+y1-1;
  95.   else ye=y2;
  96.  
  97.   if (x<0)
  98.   { x1+=-x; x=0; }
  99.   xs=x1;
  100.   if (x+(x2-x1)>=(int)xres)
  101.     xe=(int)xres-x+x1-1;
  102.   else xe=x2;
  103.   if (xs>xe || ys>ye) return ;
  104.  
  105.   // find the memory offset for the scan line of interest
  106.   screen_off=((long)y*(long)(xres+1));
  107.  
  108.  
  109.   for (yy=ys;yy<=ye;yy++,screen_off+=(xres+1))
  110.   {
  111.     page=screen_off>>16;     // which page of 64k are we on?
  112.     if (page!=last_page)     
  113.     { last_page=page;
  114.       vga_setpage(page);     // switch to new bank
  115.     }
  116.  
  117.     line_addr=im->scan_line(yy)+xs;  // get address of image scan line
  118.  
  119.     // breaker is the number of bytes before the page split
  120.     breaker=(long)0xffff-(long)(screen_off&0xffff)+1;
  121.  
  122.  
  123.     // see if the slam gets split by the page break
  124.     if (breaker>x+xe-xs)
  125.     {
  126.       void *dest=v_addr+(screen_off&0xffff)+x;
  127.       int size=xe-xs+1;
  128.       memcpy(dest,line_addr,size);
  129.     }
  130.     else if (breaker<=x)
  131.     { last_page++;
  132.       vga_setpage(last_page);
  133.       memcpy(v_addr+x-breaker,line_addr,xe-xs+1);
  134.     }
  135.     else
  136.     {
  137.       memcpy(v_addr+(screen_off&0xffff)+x,line_addr,breaker-x);
  138.       last_page++;
  139.       vga_setpage(last_page);
  140.       memcpy(v_addr,line_addr+breaker-x,xe-xs-(breaker-x)+1);
  141.     } 
  142.     y++;   
  143.   }
  144. }
  145.  
  146. void put_image(image *im, int x, int y)
  147. { put_part(im,x,y,0,0,im->width()-1,im->height()-1); }
  148.  
  149.  
  150. void update_dirty(image *im, int xoff, int yoff)
  151. {
  152.  
  153.   int count;
  154.   dirty_rect *dr,*q;
  155.   CHECK(im->special);  // make sure the image has the ablity to contain dirty areas
  156.   if (im->special->keep_dirt==0)
  157.     put_image(im,0,0);
  158.   else
  159.   {
  160.     count=im->special->dirties.number_nodes();
  161.     if (!count) return;  // if nothing to update, return
  162.     (linked_node *) dr=im->special->dirties.first();
  163.     while (count>0)
  164.     {
  165.       put_part(im,dr->dx1+xoff,dr->dy1+yoff,dr->dx1,dr->dy1,dr->dx2,dr->dy2);
  166.       q=dr;
  167.       (linked_node *)dr=dr->next();
  168.       im->special->dirties.unlink((linked_node *)q);
  169.       delete q;
  170.       count--;
  171.     }
  172.   }
  173. }
  174.  
  175.  
  176. void palette::load()
  177. {
  178.   if (lastl)
  179.     delete lastl;
  180.   lastl=copy();
  181.   for (int i=0;i<ncolors;i++)
  182.     vga_setpalette(i,red(i)>>2,green(i)>>2,blue(i)>>2);
  183. }
  184.  
  185. void palette::load_nice()
  186. { load(); }
  187.  
  188.  
  189. void image::make_page(short width, short height, unsigned char *page_buffer)
  190. {
  191.   if (page_buffer)
  192.     data=page_buffer;
  193.   else data=(unsigned char *)jmalloc(width*height,"image::data");
  194. }
  195. void image::delete_page()
  196. {
  197.   if (!special || !special->static_mem)
  198.     jfree(data);      
  199. }
  200.