home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 286.lha / PxlScope.c < prev    next >
C/C++ Source or Header  |  1989-08-21  |  6KB  |  234 lines

  1. /*
  2.  * P x l S c o p e
  3.  * 
  4.  * Opens a window in the workbench screen and shows a magnified view of
  5.  * the pixels at the mouse pointer location.
  6.  *
  7.  * May be freely distributed.
  8.  *
  9.  * Ed Karlo
  10.  * CIS 76657,1445
  11.  * November, 1986
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. #include "proto/exec.h"
  19. #include "exec/types.h"
  20. #include "exec/devices.h"
  21.  
  22. #include "proto/graphics.h"
  23. #include "graphics/copper.h"
  24. #include "graphics/gels.h"
  25. #include "graphics/gfxbase.h"
  26. #include "graphics/regions.h"
  27.  
  28. #include "hardware/blit.h"
  29.  
  30. #include "proto/intuition.h"
  31. #include "intuition/intuition.h"
  32.  
  33. #include "devices/keymap.h"
  34.  
  35. struct IntuitionBase *IntuitionBase;
  36. struct GfxBase       *GfxBase;
  37.  
  38. #define BIGPIXW 8
  39. #define BIGPIXH 7
  40.  
  41. struct NewWindow nw = {
  42.        324 , 10 ,            /* LeftEdge, TopEdge   */
  43.        316 , 181 ,           /* Width, Height       */
  44.        0 , 1 ,               /* DetailPen, BlockPen */
  45.        CLOSEWINDOW |         /* IDCMPFlags          */       
  46.        NEWSIZE ,
  47.        WINDOWCLOSE |         /* Flags               */
  48.        WINDOWDRAG |
  49.        WINDOWDEPTH |
  50.        WINDOWSIZING |
  51.        SMART_REFRESH ,
  52.        NULL,                 /* *FirstGadget        */
  53.        NULL,                 /* *CheckMark          */
  54.        "PxlScope" ,          /* Title               */
  55.        NULL ,                /* *Screen             */
  56.        NULL ,                /* *BitMap             */
  57.        121 , 28 ,            /* MinWidth, MinHeight */
  58.        640 , 200 ,           /* MaxWidth, MaxHeight */
  59.        WBENCHSCREEN          /* Type                */
  60. };
  61.  
  62. struct Screen       *S;
  63. struct Window       *W;
  64. struct IntuiMessage *message;
  65.  
  66. ULONG  class;
  67. USHORT code;
  68.  
  69. int   old_mx, old_my,        /* save mouse pos */
  70.       wxmaxpix, wymaxpix,    /* max big pixels that will fit in window */
  71.                              /* (includes any partial ones at borders) */
  72.       forsx, forsy,          /* for loop start x and y pos */
  73.       pen,                   /* hold pixel color */
  74.       locsx, locsy;          /* local use start x and y locs */
  75.  
  76. char wkstr[15];              /* build text */
  77.  
  78. /* function prototypes */
  79. void checkmsg(void);
  80. void windowmax(void);
  81. void splat(int, int);
  82. void putrect(struct Window *, int, int, int, int, int);
  83.  
  84. void main(argc,argv)
  85. int argc;
  86. char **argv;
  87. {
  88.    IntuitionBase = (struct IntuitionBase *)
  89.                       OpenLibrary("intuition.library", 0);
  90.    if (IntuitionBase == NULL) exit (1);
  91.    
  92.    GfxBase = (struct GfxBase *)
  93.                 OpenLibrary("graphics.library", 0);
  94.    if (GfxBase == NULL) exit (2);
  95.  
  96.    W = (struct Window *)OpenWindow(&nw);
  97.    if (W == NULL) exit (3);
  98.  
  99.    S = W->WScreen;
  100.  
  101.    SetDrMd(W->RPort, JAM2); /* draw mode */
  102.    SetBPen(W->RPort, 2); /* and backround for text */
  103.  
  104.    windowmax();
  105.  
  106.    splat(old_mx = S->MouseX, old_my = S->MouseY);
  107.  
  108.  
  109.    for (;;) {
  110.  
  111.       checkmsg();
  112.  
  113.       if ( (old_mx != S->MouseX || old_my != S->MouseY) )
  114.          splat(old_mx = S->MouseX, old_my = S->MouseY);
  115.  
  116.    }
  117.  
  118. }
  119.  
  120.  
  121. /*
  122.  * any messages ?
  123.  */
  124. void checkmsg()
  125. {
  126.    while ( message = (struct IntuiMessage *)
  127.               GetMsg(W->UserPort) ) {
  128.       class = message->Class;
  129.       code  = message->Code;
  130.       ReplyMsg(message);
  131.       if (class == CLOSEWINDOW) { /* bye */
  132.          CloseWindow (W);
  133.          CloseLibrary((struct Library *)IntuitionBase);
  134.          CloseLibrary((struct Library *)GfxBase);
  135.          exit (0);
  136.       }
  137.  
  138.       if (class == NEWSIZE)
  139.          windowmax();
  140.  
  141.     } /* while loop for window messages */
  142. }
  143.  
  144.  
  145. /*
  146.  * how many pixels can dance on the head of a pin ?
  147.  */
  148. void windowmax()
  149. {
  150.    wxmaxpix = (W->Width - 4) / BIGPIXW;
  151.    if ( (W->Width - 4) % BIGPIXW ) wxmaxpix++;
  152.    wymaxpix = (W->Height - 20) / BIGPIXH;
  153.    if ( (W->Height - 20) % BIGPIXH ) wymaxpix++; 
  154. }
  155.  
  156.  
  157. /*
  158.  * fills the window with big pixels
  159.  */
  160. void splat(mx, my)
  161. int mx, my;
  162. {
  163.    putrect(W, 2, 2, 10, W->Width - 3, 18);
  164.    sprintf(wkstr, "X=%4d  Y=%4d", mx, my);
  165.    SetAPen(W->RPort, 1);
  166.    Move(W->RPort, 5, 17);
  167.    Text(W->RPort, wkstr, 14);
  168.  
  169.    for (forsy=0; forsy<=wymaxpix; forsy++) {
  170.       for (forsx=0; forsx<=wxmaxpix; forsx++) {
  171.          checkmsg();
  172.          if ( (old_mx != S->MouseX || old_my != S->MouseY) )
  173.             return; /* still moving */
  174.          if ( mx+forsx > 639 || my+forsy > 199 ) { /* in the rough */
  175.             putrect(W, 3,  2 + (forsx*BIGPIXW), 19 + (forsy*BIGPIXH),
  176.                            9 + (forsx*BIGPIXW), 25 + (forsy*BIGPIXH) );
  177.          } else { /* in the ball park */
  178.             /* draw big pixel and blank out right and bottom of it */
  179.             pen = ReadPixel(&S->RastPort, mx+forsx, my+forsy);
  180.             locsx = forsx * BIGPIXW;
  181.             locsy = forsy * BIGPIXH;
  182.             putrect( W, pen,  2 + locsx, 19 + locsy,
  183.                               6 + locsx, 24 + locsy );
  184.             putrect( W, 0,    7 + locsx, 19 + locsy,
  185.                               9 + locsx, 24 + locsy );
  186.             putrect( W, 0,    2 + locsx, 25 + locsy,
  187.                               9 + locsx, 25 + locsy );
  188.          } /* drawing the big pixel */
  189.       } /* for x loop */
  190.    } /* for y loop */
  191. }
  192.  
  193.  
  194. /*
  195.  * draws a rectangle in window w in pen color penn taking care
  196.  * not to damage right and bottom border and size gadget
  197.  */
  198. void putrect(w, penn, sx, sy, ex, ey)
  199. struct Window *w;
  200. int penn, sx, sy, ex, ey;
  201. {
  202. int sgx, sgy; /* size gadget border pos */
  203.  
  204.    if ( sx > w->Width - 3 || sy > w->Height - 2 )   /* out of sight */
  205.       return; 
  206.  
  207.    if ( ex > w->Width - 3 )   /* don't overwrite right border */
  208.       ex = (w->Width) - 3; 
  209.  
  210.    if ( ey > w->Height - 2 )   /* don't overwrite bottom border */
  211.       ey = w->Height - 2; 
  212.  
  213.    sgx = w->Width - 17;   /* compute boundary of size gadget */
  214.    sgy = w->Height - 10;
  215.  
  216.    if ( sx > sgx && sy > sgy )   /* entirely behind size gadget */
  217.       return;
  218.  
  219.    SetAPen(w->RPort, penn);
  220.  
  221.    if ( ex > sgx && ey > sgy ) { /* ends within size gadget */
  222.       if ( sx > sgx )   /* entirely above */
  223.          RectFill(w->RPort, sx, sy, ex, sgy);
  224.       else
  225.       if ( sy > sgy )   /* entirely beside */
  226.          RectFill(w->RPort, sx, sy, sgx, ey);
  227.       else { /* on the corner */
  228.          RectFill(w->RPort, sx, sy, ex, sgy);
  229.          RectFill(w->RPort, sx, sgy+1, sgx, ey);
  230.       }
  231.    } else
  232.       RectFill(w->RPort, sx, sy, ex, ey);
  233. }
  234.