home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff368.lzh / GraphicsPak / graphics_pak.c < prev    next >
C/C++ Source or Header  |  1990-08-15  |  5KB  |  185 lines

  1. /***************************************************************************
  2.  * graphics_pak.c - general-purpose graphics functions to make programming *
  3.  *               alot easier!                                              *
  4.  * ----------------------------------------------------------------------- *
  5.  * Author: Paul T. Miller                                                  *
  6.  * ----------------------------------------------------------------------- *
  7.  * Modification History:                                                   *
  8.  * ---------------------                                                   *
  9.  * Date     Comment                                                        *
  10.  * -------- -------                                                        *
  11.  * 05-09-90 Bring AllocBitMap into the graphics package
  12.  * 05-18-90 DrawLine()
  13.  *
  14.  ***************************************************************************/
  15.  
  16. #ifndef GRAPHICS_PAK_H
  17. #include "graphics_pak.h"
  18. #endif
  19.  
  20. #include <proto/graphics.h>
  21.  
  22. extern struct IntuitionBase *IntuitionBase;
  23. extern struct DiskfontBase *DiskfontBase;
  24. struct Library *LayersBase;
  25.  
  26. static int openflags;
  27.  
  28. OpenLibraries(flags)
  29. UWORD flags;
  30. {
  31.    openflags = NULL;
  32.  
  33.    if (flags & INTUITIONBASE)
  34.    {
  35.       IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  36.       openflags |= INTUITIONBASE;
  37.    }
  38.    if (flags & GFXBASE)
  39.    {
  40.       GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  41.       openflags |= GFXBASE;
  42.    }
  43.    if (flags & LAYERSBASE)
  44.    {
  45.       LayersBase = (struct LayersBase *)OpenLibrary("layers.library",0);
  46.       openflags |= LAYERSBASE;
  47.    }
  48.    if (flags & DISKFONTBASE)
  49.    {
  50.       DiskfontBase = (struct DiskfontBase *)OpenLibrary("diskfont.library",0);
  51.       if (DiskfontBase)
  52.          openflags |= DISKFONTBASE;
  53.    }
  54.    return(openflags);
  55. }
  56.  
  57. void CloseLibraries()
  58. {
  59.    if (openflags & GFXBASE)
  60.       if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  61.    if (openflags & INTUITIONBASE)
  62.       if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  63.    if (openflags & LAYERSBASE)
  64.       if (LayersBase) CloseLibrary(LayersBase);
  65.    if (openflags & DISKFONTBASE)
  66.       if (DiskfontBase) CloseLibrary((struct Library *)DiskfontBase);
  67.  
  68.    openflags = NULL;
  69. }
  70.  
  71. void DrawPixel(rp, x, y, c)
  72. register struct RastPort *rp;
  73. register int x, y, c;
  74. {
  75.    SetAPen(rp, c);
  76.    WritePixel(rp, x, y);
  77. }
  78.  
  79. void DrawLine(rp, x1, y1, x2, y2, c)
  80. struct RastPort *rp;
  81. int x1, y1, x2, y2, c;
  82. {
  83.    SetAPen(rp, c);
  84.    Move(rp, x1, y1);
  85.    Draw(rp, x2, y2);
  86. }
  87.  
  88. void DrawBox(rp, x, y, w, h, c)
  89. struct RastPort *rp;
  90. int x, y, w, h, c;
  91. {
  92.    SetAPen(rp, c);
  93.    Move(rp, x, y);
  94.    Draw(rp, x+w, y);
  95.    Draw(rp, x+w, y+h);
  96.    Draw(rp, x, y+h);
  97.    Draw(rp, x, y);
  98. }
  99.  
  100. void FillBox(rp, x, y, w, h, c)
  101. struct RastPort *rp;
  102. int x, y, w, h, c;
  103. {
  104.    SetAPen(rp, c);
  105.    RectFill(rp, x, y, x+w, y+h);
  106. }
  107.  
  108. void WriteText(rport, x, y, text, color)
  109. struct RastPort *rport;
  110. long x, y, color;
  111. char *text;
  112. {
  113.    Move(rport, x, y);
  114.    SetAPen(rport, color);
  115.    SetDrMd(rport, JAM1);
  116.    Text(rport, text, strlen(text));
  117. }
  118.  
  119. struct BitMap *AllocBitMap(width, height, depth, flags)
  120. USHORT width, height;
  121. UBYTE depth, flags;
  122. {
  123.    struct BitMap *bm;
  124.    register int i;
  125.    long memsize = RASSIZE(width, height);
  126.  
  127.    bm = (struct BitMap *)AllocMem(sizeof(struct BitMap), MEMF_CLEAR);
  128.    if (bm)
  129.    {
  130.       InitBitMap(bm, (long)depth, (long)width, (long)height);
  131.  
  132.       for (i = 0; i < depth; i++)
  133.       {
  134.          if (flags & FASTMEM)
  135.             bm->Planes[i] = (PLANEPTR)AllocMem(memsize, MEMF_CLEAR);
  136.          else
  137.             bm->Planes[i] = (PLANEPTR)AllocMem(memsize, MEMF_CHIP|MEMF_CLEAR);
  138.          if (!bm->Planes[i])
  139.          {
  140.             FreeBitMap(bm);
  141.             return(NULL);
  142.          }
  143.       }
  144.    }
  145.    return(bm);
  146. }
  147.  
  148. void FreeBitMap(bm)
  149. struct BitMap *bm;
  150. {
  151.    register int i;
  152.  
  153.    if (bm)
  154.    {
  155.       for (i = 0; i < bm->Depth; i++)
  156.          if (bm->Planes[i])
  157.             FreeMem(bm->Planes[i], (long)(bm->BytesPerRow * bm->Rows));
  158.       FreeMem(bm, sizeof(struct BitMap));
  159.    }
  160. }
  161.  
  162. void MoveBitMap(source, x, y, w, h, dest, dx, dy)
  163. struct BitMap *source, *dest;
  164. int x, y, w, h, dx, dy;
  165. {
  166.    if (source && dest)
  167.       BltBitMap(source, x, y, dest, dx, dy, w, h, 0xc0, 0xff, NULL);
  168. }
  169.  
  170. void DrawBitMap(source, x, y, w, h, dest)
  171. struct BitMap *source, *dest;
  172. int x, y, w, h;
  173. {
  174.    if (source && dest)
  175.       BltBitMap(source, 0, 0, dest, x, y, w, h, 0xc0, 0xff, NULL);
  176. }
  177.  
  178. void CopyBitMap(source, x, y, w, h, dest)
  179. struct BitMap *source, *dest;
  180. int x, y, w, h;
  181. {
  182.    if (source && dest)
  183.       BltBitMap(source, x, y, dest, 0, 0, w, h, 0xc0, 0xff, NULL);
  184. }
  185.