home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / GFX / Misc / FASTMC.LHA / fastmc / src / wkmc.h < prev   
Encoding:
C/C++ Source or Header  |  1995-11-14  |  10.2 KB  |  462 lines

  1. /*
  2.  
  3.  wkmc is a simple true-color-display using a 8-bit-screen,
  4.  the idea behind this came from Stefan Kost and Smack/IFT
  5.  
  6.  NOTE: I think it's fast beeing pure C !
  7.  
  8.  
  9.  Advantages (compared against MultiColor):
  10.   -terrible high speed
  11.   -picture-size and -aspect isn't changed
  12.   -no ugly diagonal-dithering
  13.   -no flickering (uses Multiscan)
  14.   -uses all 256 registers (not only 255)
  15.   -every color-channel (RGB) has a diffent number of
  16.    shades ("eye-sensitive")
  17. */
  18.  
  19.  
  20. /* start independant mc-stuff */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <exec/types.h>
  25. #include <graphics/displayinfo.h>
  26. #include <intuition/intuition.h>
  27. #include <exec/memory.h>
  28. #include <time.h>
  29. static double tm,dt;
  30. static time_t tm1,tm2;
  31.  
  32. #define FAST
  33.  
  34. #ifdef __GNUC__
  35. #else
  36.  #include <clib/exec_protos.h>
  37.  #include <clib/graphics_protos.h>
  38.  #include <clib/intuition_protos.h>
  39. #endif
  40.  
  41. struct GfxBase *GfxBase=NULL;
  42. struct IntuitionBase *IntuitionBase=NULL;
  43. struct Screen *theScreen=NULL;
  44. struct Window *theWindow=NULL;
  45. struct RastPort *rp=NULL;
  46. struct BitMap *bm=NULL;
  47.  
  48. static ULONG ct[770],cvals[256];
  49. static UBYTE pt[256],rt[256],gt[256],bt[256],rbit[256][8],gbit[256][8],bbit[256][8],*ppptr;
  50. static UBYTE entry[9];
  51. static ULONG ind,bsize,psize,lbpr,lwidth,lwidth2,displayID=0x39024;
  52. static double f256=1.0/256.0;
  53.  
  54. extern void SHOWLINES(UBYTE *data,UBYTE *bm0,ULONG line,ULONG bpr,ULONG psize,ULONG width);
  55. extern void SHOWLINE(UBYTE *data,UBYTE *bm0,ULONG line,ULONG bpr,ULONG psize,ULONG width);
  56.  
  57. void FreeBitMap2(struct BitMap **wbm) {
  58.  if((*wbm)==NULL) return;
  59.  if((*wbm)->Planes[0]!=NULL) FreeMem((*wbm)->Planes[0],bsize);
  60.  free((*wbm));
  61.  (*wbm)=NULL;
  62. }
  63.  
  64. void cleanup(void) {
  65.  if(theScreen!=NULL) ScreenToBack(theScreen);
  66.  if(theWindow!=NULL) {CloseWindow(theWindow);theWindow=NULL;}
  67.  if(theScreen!=NULL) {CloseScreen(theScreen);theScreen=NULL;}
  68.  FreeBitMap2(&bm);
  69.  if(GfxBase!=NULL) {CloseLibrary((struct Library*)GfxBase);GfxBase=NULL;}
  70.  if(IntuitionBase!=NULL) {CloseLibrary((struct Library*)IntuitionBase);IntuitionBase=NULL;}
  71. }
  72.  
  73. UBYTE getbit(UBYTE val,UBYTE bit) {
  74.  if(bit!=0) return((val>>bit)%2);
  75.  else return(val%2);
  76. }
  77.  
  78. void AllocBitMap2(struct BitMap **wbm,int width,int height,int depth) {
  79.  int i;
  80.  ULONG ptr;
  81.  (*wbm)=(struct BitMap*)malloc(sizeof(struct BitMap));
  82.  if((*wbm)==NULL) return;
  83.  InitBitMap((struct BitMap*)(*wbm),depth,((width+63)/64)*64,height);
  84.  
  85.  if((*wbm)->Depth!=depth) (*wbm)->Depth=depth;
  86.  
  87.  psize=(ULONG)((*wbm)->BytesPerRow)*(ULONG)((*wbm)->Rows);
  88.  bsize=psize*(ULONG)depth;
  89.  if(bsize<=0) {free((*wbm));(*wbm)=NULL;return;}
  90.  (*wbm)->Planes[0]=(UBYTE*)AllocMem(bsize,MEMF_CHIP | MEMF_CLEAR);
  91.  if((*wbm)->Planes[0]==NULL) {
  92.   free((*wbm));
  93.   (*wbm)=NULL;
  94.  }
  95.  ptr=(ULONG)(*wbm)->Planes[0];
  96.  for(i=0;i<depth;i++) {
  97.   (*wbm)->Planes[i]=(PLANEPTR)ptr;ptr+=psize;
  98.  }
  99. }
  100.  
  101. int init(int width,int height) {
  102.  int i,rs=76,gs=150,bs=30;
  103.  float cscl,cl,cmode=1;
  104.  ULONG lval,lv;
  105.  UBYTE j;
  106.  GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",39L);
  107.  if(GfxBase==NULL) {printf(" Couldn't open graphics.library V39+.\n");return(1);}
  108.  IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",39L);
  109.  if(IntuitionBase==NULL) {printf(" Couldn't open intuition.library V39+.\n");cleanup();return(2);}
  110.  
  111.  if(cmode==0) {
  112.   rs=85;gs=86;bs=85;
  113.  }
  114.  else if(cmode==1) {
  115.   rs=76,gs=150,bs=30;
  116.  }
  117.  
  118.  ct[0]=(ULONG)256<<16;ct[769]=0;
  119.  lval=1;
  120.  
  121.  cscl=256.0/(float)rs;
  122.  for(i=0;i<rs;i++) {
  123.   cl=(float)i*cscl+0.5;
  124.   lv=(ULONG)cl;if(lv>255) lv=255;
  125.   cvals[i]=lv;
  126.   pt[i]=lv;
  127.  }
  128.  for(i=0;i<rs;i++) {
  129.   ct[lval]=cvals[i]<<24;lval++;
  130.   ct[lval]=0;lval++;
  131.   ct[lval]=0;lval++;
  132.  }
  133.  
  134.  cscl=256.0/(float)gs;
  135.  for(i=0;i<gs;i++) {
  136.   cl=(float)i*cscl+0.5;
  137.   lv=(ULONG)cl;if(lv>255) lv=255;
  138.   cvals[i]=lv;
  139.   pt[i+rs]=lv;
  140.  }
  141.  for(i=0;i<gs;i++) {
  142.   ct[lval]=0;lval++;
  143.   ct[lval]=cvals[i]<<24;lval++;
  144.   ct[lval]=0;lval++;
  145.  }
  146.  
  147.  cscl=256.0/(float)bs;
  148.  for(i=0;i<bs;i++) {
  149.   cl=(float)i*cscl+0.5;
  150.   lv=(ULONG)cl;if(lv>255) lv=255;
  151.   cvals[i]=lv;
  152.   pt[i+rs+gs]=lv;
  153.  }
  154.  for(i=0;i<bs;i++) {
  155.   ct[lval]=0;lval++;
  156.   ct[lval]=0;lval++;
  157.   ct[lval]=cvals[i]<<24;lval++;
  158.  }
  159.  
  160.  cscl=(float)rs/256.0;
  161.  for(i=0;i<256;i++) {
  162.   cl=(float)i*cscl+0.5;
  163.   lval=(ULONG)cl;if(lval>(rs-1)) lval=rs-1;
  164.   rt[i]=(UBYTE)lval;
  165.  }
  166.  
  167.  cscl=(float)gs/256.0;
  168.  for(i=0;i<256;i++) {
  169.   cl=(float)i*cscl+0.5;
  170.   lval=(ULONG)cl;if(lval>(gs-1)) lval=gs-1;
  171.   gt[i]=(UBYTE)lval+(UBYTE)rs;
  172.  }
  173.  
  174.  cscl=(float)bs/256.0;
  175.  for(i=0;i<256;i++) {
  176.   cl=(float)i*cscl+0.5;
  177.   lval=(ULONG)cl;if(lval>(bs-1)) lval=bs-1;
  178.   bt[i]=(UBYTE)lval+(UBYTE)rs+(UBYTE)gs;
  179.  }
  180.  
  181.  for(i=0;i<256;i++) {
  182.   for(j=0;j<8;j++) {
  183.    rbit[i][j]=getbit(rt[i],j);
  184.    gbit[i][j]=getbit(gt[i],j);
  185.    bbit[i][j]=getbit(bt[i],j);
  186.   }
  187.  }
  188.  
  189.  AllocBitMap2(&bm,width,height,8);if(bm==NULL) return(1);
  190.  
  191.  theScreen=(struct Screen*)OpenScreenTags(NULL,
  192.   SA_DisplayID,displayID,
  193.   SA_Overscan,OSCAN_TEXT,
  194.   SA_Width,width,
  195.   SA_Height,height,
  196.   SA_Depth,8,
  197.   SA_BitMap,bm,
  198.   SA_AutoScroll,1,
  199.   SA_Title,"WKMC-Screen",
  200.   SA_Colors32,ct,
  201.   SA_Behind,TRUE,
  202.   TAG_DONE);
  203.  if(theScreen==NULL) {printf(" Couln't open screen.\n");cleanup();return(3);}
  204.  theWindow=(struct Window*)OpenWindowTags(NULL,
  205.   WA_Left,0,
  206.   WA_Top,0,
  207.   WA_Width,width,
  208.   WA_Height,height,
  209.   WA_MinWidth,50,
  210.   WA_MinHeight,40,
  211.   WA_MaxWidth,-1,
  212.   WA_MaxHeight,-1,
  213.   WA_IDCMP,VANILLAKEY,
  214.   WA_SizeGadget,FALSE,
  215.   WA_DepthGadget,FALSE,
  216.   WA_DragBar,FALSE,
  217.   WA_CloseGadget,FALSE,
  218.   WA_Borderless,TRUE,
  219.   WA_NoCareRefresh,TRUE,
  220.   WA_SmartRefresh,TRUE,
  221.   WA_CustomScreen,theScreen,
  222.   WA_Activate,TRUE,
  223.   TAG_DONE);
  224.  if(theWindow==NULL) {printf(" Couldn't open window.\n");cleanup();return(4);}
  225.  rp=theWindow->RPort;
  226.  ScreenToFront(theScreen);
  227.  lwidth=(ULONG)width;lwidth2=lwidth/2;
  228.  lbpr=(ULONG)bm->BytesPerRow;
  229.  return(0);
  230. }
  231.  
  232. void SetPixelS(int x,int y,UBYTE r,UBYTE g,UBYTE b) {
  233.  if((x%2==0) && (y%2==0)) {
  234.   SetAPen(rp,rt[r]);
  235.   WritePixel(rp,x,y);
  236.   SetAPen(rp,gt[g]);
  237.   WritePixel(rp,x+1,y);
  238.   SetAPen(rp,bt[b]);
  239. /*  if(y%4==0)*/ WritePixel(rp,x,y+1);
  240. /*  else WritePixel(rp,x+1,y+1);*/
  241.  }
  242. }
  243.  
  244. void SetPixel(int x,int y,UBYTE r,UBYTE g,UBYTE b) {
  245.  register int xx,yy;
  246.  xx=x+x;yy=y+y;
  247.  SetAPen(rp,rt[r]);
  248.  WritePixel(rp,xx,yy);
  249.  SetAPen(rp,gt[g]);
  250.  WritePixel(rp,xx+1,yy);
  251.  SetAPen(rp,bt[b]);
  252. /* if(yy%4==0) */WritePixel(rp,xx,yy+1);
  253. /* else WritePixel(rp,xx+1,yy+1);*/
  254. }
  255.  
  256. void GetPixel(int x,int y,UBYTE *r,UBYTE *g,UBYTE *b) {
  257.  register int xx,yy;
  258.  xx=x+x;yy=y+y;
  259.  (*r)=pt[ReadPixel(rp,xx,yy)];
  260.  (*g)=pt[ReadPixel(rp,xx+1,yy)];
  261. /* if(yy%4==0)*/ (*b)=pt[ReadPixel(rp,xx,yy+1)];
  262. /* else (*b)=pt[ReadPixel(rp,xx+1,yy+1)];*/
  263. }
  264.  
  265. void SetLine(int i,UBYTE* line,int width,int bn,UBYTE **bl) {
  266.  #ifndef FAST
  267.  register int k,act,l,act3,ii;
  268.  ii=i+i;
  269.  
  270.  /* rg */
  271.  act=0;
  272.  for(k=0;k<bn;k++) {
  273.   act3=act+act+act;
  274.   for(l=0;l<8;l++) {
  275.    entry[l]=rbit[line[act3]][l]<<7;
  276.    entry[l]+=gbit[line[act3+1]][l]<<6;
  277.   }
  278.   act++;
  279.   act3=act+act+act;
  280.   for(l=0;l<8;l++) {
  281.    entry[l]+=rbit[line[act3]][l]<<5;
  282.    entry[l]+=gbit[line[act3+1]][l]<<4;
  283.   }
  284.   act++;
  285.   act3=act+act+act;
  286.   for(l=0;l<8;l++) {
  287.    entry[l]+=rbit[line[act3]][l]<<3;
  288.    entry[l]+=gbit[line[act3+1]][l]<<2;
  289.   }
  290.   act++;
  291.   act3=act+act+act;
  292.   for(l=0;l<8;l++) {
  293.    entry[l]+=rbit[line[act3]][l]<<1;
  294.    entry[l]+=gbit[line[act3+1]][l];
  295.   }
  296.   act++;
  297.   for(l=0;l<8;l++) bl[l][k]=entry[l];
  298.  }
  299.  ind=(ULONG)ii*(ULONG)bn;
  300.  for(k=0;k<8;k++) memcpy(&(theScreen->BitMap.Planes[k][ind]),bl[k],bn);
  301.  
  302.  /* b */
  303. /* if(i%4==0) {*/
  304.   act=0;
  305.   for(k=0;k<bn;k++) {
  306.    act3=act+act+act+2;
  307.    for(l=0;l<8;l++) {
  308.     entry[l]=bbit[line[act3]][l]<<7;
  309.    }
  310.    act++;
  311.    act3=act+act+act+2;
  312.    for(l=0;l<8;l++) {
  313.     entry[l]+=bbit[line[act3]][l]<<5;
  314.    }
  315.    act++;
  316.    act3=act+act+act+2;
  317.    for(l=0;l<8;l++) {
  318.     entry[l]+=bbit[line[act3]][l]<<3;
  319.    }
  320.    act++;
  321.    act3=act+act+act+2;
  322.    for(l=0;l<8;l++) {
  323.     entry[l]+=bbit[line[act3]][l]<<1;
  324.    }
  325.    act++;
  326.    for(l=0;l<8;l++) bl[l][k]=entry[l];
  327.   }
  328.  }  /*
  329.  else {
  330.   act=0;
  331.   for(k=0;k<bn;k++) {
  332.    act3=act+act+act+2;
  333.    for(l=0;l<8;l++) {
  334.     entry[l]=bbit[line[act3]][l]<<6;
  335.    }
  336.    act++;
  337.    act3=act+act+act+2;
  338.    for(l=0;l<8;l++) {
  339.     entry[l]+=bbit[line[act3]][l]<<4;
  340.    }
  341.    act++;
  342.    act3=act+act+act+2;
  343.    for(l=0;l<8;l++) {
  344.     entry[l]+=bbit[line[act3]][l]<<2;
  345.    }
  346.    act++;
  347.    act3=act+act+act+2;
  348.    for(l=0;l<8;l++) {
  349.     entry[l]+=bbit[line[act3]][l];
  350.    }
  351.    act++;
  352.    for(l=0;l<8;l++) bl[l][k]=entry[l];
  353.   }
  354.  }*/
  355.  ind+=(ULONG)bn;
  356.  for(k=0;k<8;k++) memcpy(&(theScreen->BitMap.Planes[k][ind]),bl[k],bn);
  357.  #else
  358.  SHOWLINE(line,bm->Planes[0],(ULONG)(i+i),lbpr,psize,lwidth2);
  359.  #endif
  360. }
  361.  
  362. void SetLineS(int i,UBYTE *line,int width,int bn,UBYTE **bl) {
  363.  #ifndef FAST
  364.  register int k,act,l,act3;
  365.  #endif
  366.  if(i%2!=0) return;
  367.  #ifndef FAST
  368.  /* rg */
  369.  
  370.  act=0;
  371.  for(k=0;k<bn;k++) {
  372.   act3=act+act+act;
  373.   for(l=0;l<8;l++) {
  374.    entry[l]=rbit[line[act3]][l]<<7;
  375.    entry[l]+=gbit[line[act3+1]][l]<<6;
  376.   }
  377.   act+=2;
  378.   act3=act+act+act;
  379.   for(l=0;l<8;l++) {
  380.    entry[l]+=rbit[line[act3]][l]<<5;
  381.    entry[l]+=gbit[line[act3+1]][l]<<4;
  382.   }
  383.   act+=2;
  384.   act3=act+act+act;
  385.   for(l=0;l<8;l++) {
  386.    entry[l]+=rbit[line[act3]][l]<<3;
  387.    entry[l]+=gbit[line[act3+1]][l]<<2;
  388.   }
  389.   act+=2;
  390.   act3=act+act+act;
  391.   for(l=0;l<8;l++) {
  392.    entry[l]+=rbit[line[act3]][l]<<1;
  393.    entry[l]+=gbit[line[act3+1]][l];
  394.   }
  395.   act+=2;
  396.   for(l=0;l<8;l++) bl[l][k]=entry[l];
  397.  }
  398.  ind=(ULONG)i*(ULONG)bn;
  399.  for(k=0;k<8;k++) memcpy(&(theScreen->BitMap.Planes[k][ind]),bl[k],bn);
  400.  
  401.  /* b */
  402. /* if(i%4==0) {*/
  403.   act=0;
  404.   for(k=0;k<bn;k++) {
  405.    act3=act+act+act+2;
  406.    for(l=0;l<8;l++) {
  407.     entry[l]=bbit[line[act3]][l]<<7;
  408.    }
  409.    act+=2;
  410.    act3=act+act+act+2;
  411.    for(l=0;l<8;l++) {
  412.     entry[l]+=bbit[line[act3]][l]<<5;
  413.    }
  414.    act+=2;
  415.    act3=act+act+act+2;
  416.    for(l=0;l<8;l++) {
  417.     entry[l]+=bbit[line[act3]][l]<<3;
  418.    }
  419.    act+=2;
  420.    act3=act+act+act+2;
  421.    for(l=0;l<8;l++) {
  422.     entry[l]+=bbit[line[act3]][l]<<1;
  423.    }
  424.    act+=2;
  425.    for(l=0;l<8;l++) bl[l][k]=entry[l];
  426.   }
  427. /* }
  428.  else {
  429.   act=0;
  430.   for(k=0;k<bn;k++) {
  431.    act3=act+act+act+2;
  432.    for(l=0;l<8;l++) {
  433.     entry[l]=bbit[line[act3]][l]<<6;
  434.    }
  435.    act+=2;
  436.    act3=act+act+act+2;
  437.    for(l=0;l<8;l++) {
  438.     entry[l]+=bbit[line[act3]][l]<<4;
  439.    }
  440.    act+=2;
  441.    act3=act+act+act+2;
  442.    for(l=0;l<8;l++) {
  443.     entry[l]+=bbit[line[act3]][l]<<2;
  444.    }
  445.    act+=2;
  446.    act3=act+act+act+2;
  447.    for(l=0;l<8;l++) {
  448.     entry[l]+=bbit[line[act3]][l];
  449.    }
  450.    act+=2;
  451.    for(l=0;l<8;l++) bl[l][k]=entry[l];
  452.   }
  453.  }*/
  454.  ind+=(ULONG)bn;
  455.  for(k=0;k<8;k++) memcpy(&(theScreen->BitMap.Planes[k][ind]),bl[k],bn);
  456.  #else
  457.  SHOWLINES(line,bm->Planes[0],(ULONG)i,lbpr,psize,lwidth);
  458.  #endif
  459. }
  460.  
  461. /* end independant mc-stuff */
  462.