home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 635.lha / wbloc_v1.0 / wbloc.c < prev    next >
C/C++ Source or Header  |  1992-01-18  |  5KB  |  296 lines

  1. /*
  2.  * WBLoc.c - places a locator on the Workbench screen
  3.  *
  4.  * Bruno Costa - 22 Dec 90 - 24 Dec 90
  5.  *
  6.  * (compile & link with SAS/C using: lc -L wbloc)
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <graphics/gfxmacros.h>
  12. #include <intuition/intuition.h>
  13. #include <proto/intuition.h>
  14. #include <proto/layers.h>
  15. #include <proto/exec.h>
  16. #include <proto/graphics.h>
  17.  
  18. #include <clib/macros.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define    WIN_WIDTH 148
  23.  
  24. struct NewWindow nw = {
  25.   (588 - WIN_WIDTH), 0, WIN_WIDTH, 12, 3, 2,
  26.   MOUSEMOVE | MOUSEBUTTONS | ACTIVEWINDOW | INACTIVEWINDOW,
  27.   RMBTRAP | SMART_REFRESH,
  28.   NULL, NULL, NULL, NULL, NULL,
  29.   5, 5, 640, 200, WBENCHSCREEN
  30. };
  31.  
  32. struct Library *GfxBase = NULL;
  33. struct Library *LayersBase = NULL;
  34. struct Library *IntuitionBase = NULL;
  35.  
  36. struct RastPort *rp = NULL, *backrp = NULL;
  37. struct Window *wind = NULL;
  38. int maxx, maxy;
  39. int textx, texty;
  40.  
  41.  
  42. UWORD ptrtemplate[] = {
  43.   0x0000, 0x0000,
  44.  
  45.   0x0000, 0x0000,
  46.   0xffff, 0x0000,
  47.   0x0000, 0x0000,
  48.   0x0000, 0x0000,
  49.   0x2000, 0x2000,
  50.   0xf000, 0xf000,
  51.   0x2000, 0x2000,
  52.   0x0000, 0x0000,
  53.   0x0000, 0x0000,
  54.   0x4000, 0x4000,
  55.   0x4000, 0x4000,
  56.   0x4000, 0x4000,
  57.   0xe000, 0xe000,
  58.   0x4000, 0x4000,
  59.   0x0000, 0x0000,
  60.   0xffff, 0x0000,
  61.  
  62.   0x0000, 0x0000
  63. };
  64.  
  65. #define PTRSIZE (sizeof (ptrtemplate) / sizeof (UWORD))
  66.  
  67. UWORD chip pointer[2][PTRSIZE];
  68.  
  69.  
  70. static UBYTE nfont[11][5] = {
  71.   {7, 5, 5, 5, 7},    /* 0 */
  72.   {2, 6, 2, 2, 2},    /* 1 */
  73.   {7, 1, 7, 4, 7},    /* 2 */
  74.   {7, 1, 3, 1, 7},    /* 3 */
  75.   {5, 5, 7, 1, 1},    /* 4 */
  76.   {7, 4, 7, 1, 7},    /* 5 */
  77.   {7, 4, 7, 5, 7},    /* 6 */
  78.   {7, 1, 1, 2, 2},    /* 7 */
  79.   {7, 5, 7, 5, 7},    /* 8 */
  80.   {7, 5, 7, 1, 1},    /* 9 */
  81.   {7, 1, 2, 0, 2}    /* ? */
  82. };
  83.  
  84.  
  85. void putdigit (UWORD *where, int pos, int n)
  86. {
  87.  int i;
  88.  
  89.  ++where;    /* optional */
  90.  
  91.  for (i = 0; i < 5; i++)
  92.    where[i << 1] |= nfont[n][i] << (pos << 2);
  93. }
  94.  
  95.  
  96. void mkbitnumber (int n, UWORD *where)
  97. {
  98.  int i, len;
  99.  char str[20];
  100.  
  101.  sprintf (str, "%d", n);
  102.  len = strlen (str);
  103.  if (len > 3)
  104.  {
  105.    putdigit (where, 0, 10);
  106.    putdigit (where, 1, 10);
  107.    putdigit (where, 2, 10);
  108.  }
  109.  else
  110.    for (i = len - 1; i >= 0; i--)
  111.      putdigit (where, len - i - 1, str[i] - '0');
  112. }
  113.  
  114.  
  115. void setptr (int x, int y)
  116. {
  117.  static int curptr = 0;
  118.  
  119.  CopyMem (ptrtemplate, &pointer[curptr][0], sizeof (ptrtemplate));
  120.  
  121.  mkbitnumber (x, &pointer[curptr][8]);
  122.  mkbitnumber (y, &pointer[curptr][20]);
  123.  
  124.  SetPointer (wind, &pointer[curptr][0], 16, 16, 0, 0);
  125.  
  126.  curptr = 1 - curptr;    /* switch buffers */
  127. }
  128.  
  129.  
  130. void writetext (char *str, int c, int x)
  131. {
  132.  SetRast (wind->RPort, 0);
  133.  SetAPen (wind->RPort, c);
  134.  SetDrMd (wind->RPort, JAM1);
  135.  Move (wind->RPort, x, wind->BorderTop + wind->RPort->TxBaseline);
  136.  Text (wind->RPort, str, strlen (str));
  137.  RefreshWindowFrame (wind);
  138. }
  139.  
  140.  
  141. void writetitle (void)
  142. {
  143.  writetext ("WBLocator", 1, 43);
  144. }
  145.  
  146.  
  147. void writeauthor (void)
  148. {
  149.  writetext ("\2511991 Bruno Costa", 2, 6);
  150. }
  151.  
  152.  
  153. void setwindow (void)
  154. {
  155.  MoveWindow (wind, (2 * maxx / 3) - wind->LeftEdge, 0);
  156. }
  157.  
  158.  
  159. void draw (int x, int y)
  160. {
  161.  SetDrMd (rp, COMPLEMENT);
  162.  Move (rp, 0, y);
  163.  Draw (rp, maxx, y);
  164.  Move (rp, x, 0);
  165.  Draw (rp, x, maxy);
  166.  
  167.  setptr (x, y);
  168. }
  169.  
  170.  
  171. void drawloc (int x, int y)
  172. {
  173.  static int ox = -1, oy = -1;
  174.  
  175.  if (ox >= 0  &&  oy >= 0)
  176.    draw (ox, oy);
  177.  
  178.  if (x >= 0  &&  y >= 0)
  179.    draw (x, y);
  180.  
  181.  ox = x;
  182.  oy = y;
  183. }
  184.  
  185.  
  186. void saverp (void)
  187. {
  188.  CopyMem (rp, backrp, sizeof (*rp));
  189. }
  190.  
  191.  
  192. void restorerp (void)
  193. {
  194.  CopyMem (backrp, rp, sizeof (*rp));
  195. }
  196.  
  197.  
  198. #define abort(code) {err=code; goto abort;}
  199.  
  200. void main (void)
  201. {
  202.  int err = 0;
  203.  struct IntuiMessage *msg;
  204.  int drawing = FALSE, GoOn = TRUE;
  205.  
  206.  GfxBase = OpenLibrary ("graphics.library", 0L);
  207.  LayersBase = OpenLibrary ("layers.library", 0L);
  208.  IntuitionBase = OpenLibrary ("intuition.library", 0L);
  209.  
  210.  wind = OpenWindow (&nw);
  211.  if (wind == NULL)
  212.    abort (20);
  213.  
  214.  rp = &wind->WScreen->RastPort;
  215.  maxx = wind->WScreen->Width - 1;
  216.  maxy = wind->WScreen->Height - 1;
  217.  
  218.  backrp = (struct RastPort *) AllocMem (sizeof (struct RastPort), 0);
  219.  if (backrp == NULL)
  220.    abort (20);
  221.  
  222.  setwindow ();
  223.  writetitle ();
  224.  
  225.  GoOn=TRUE;
  226.  while (GoOn)
  227.  {
  228.    WaitPort (wind->UserPort);
  229.  
  230.    while ((msg = (struct IntuiMessage *) GetMsg (wind->UserPort))  &&  GoOn)
  231.    {
  232.      ULONG class = msg->Class;
  233.      ULONG code= msg->Code;
  234.  
  235.      ReplyMsg (&msg->ExecMessage);
  236.  
  237.      if (drawing)
  238.        drawloc (wind->WScreen->MouseX, wind->WScreen->MouseY);
  239.  
  240.      switch (class)
  241.      {
  242.        case ACTIVEWINDOW:
  243.          if (!drawing)
  244.          {
  245.            writeauthor ();
  246.            drawing = TRUE;
  247.            ReportMouse (wind, TRUE);
  248.            LockLayerInfo (&wind->WScreen->LayerInfo);
  249.            saverp ();
  250.          }
  251.          break;
  252.  
  253.        case INACTIVEWINDOW:
  254.          if (drawing)
  255.          {
  256.            drawloc (-1, -1);
  257.            restorerp ();
  258.            UnlockLayerInfo (&wind->WScreen->LayerInfo);
  259.            ReportMouse (wind, FALSE);
  260.            drawing = FALSE;
  261.            writetitle ();
  262.          }
  263.          break;
  264.  
  265.        case MOUSEBUTTONS:
  266.          if (code == MENUDOWN)
  267.            GoOn = FALSE;
  268.          break;
  269.      }
  270.    }
  271.  }
  272.  
  273.  if (drawing)
  274.  {
  275.    drawloc (-1, -1);
  276.    restorerp ();
  277.    UnlockLayerInfo (&wind->WScreen->LayerInfo);
  278.    ReportMouse (wind, FALSE);
  279.    drawing = FALSE;
  280.  }
  281.  
  282. abort:
  283.  if (backrp)
  284.    FreeMem (backrp, sizeof (struct RastPort));
  285.  if (wind)
  286.    CloseWindow (wind);
  287.  if (IntuitionBase)
  288.    CloseLibrary (IntuitionBase);
  289.  if (LayersBase)
  290.    CloseLibrary (LayersBase);
  291.  if (GfxBase)
  292.    CloseLibrary (GfxBase);
  293.  
  294.  exit (err);
  295. }
  296.