home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / semaph / sem_pnt.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  15KB  |  304 lines

  1. /*static char *SCCSID = "@(#)sem_pnt.c    6.12 92/02/19";*/
  2. /*==============================================================*\
  3.  *  SEM_PNT.C - routines for the painting of the main window    *
  4.  *      (C) Copyright IBM Corporation 1992.                     *
  5.  *--------------------------------------------------------------*
  6.  *                                                              *
  7.  *  This module contains the code for the main client window    *
  8.  *  painting                                                    *
  9.  *                                                              *
  10.  *--------------------------------------------------------------*
  11.  *                                                              *
  12.  *  This source file contains the following functions:          *
  13.  *                                                              *
  14.  *      DrawResource(PRECTL, COLOR) - Draws colored square      *
  15.  *      DrawStats(ULONG)  update the statistics for a thread    *
  16.  *      ChangeResource(COLOR) - Changes resource color          *
  17.  *      InitSemaphExample(VOID) - Sets initial colors           *
  18.  *      SetRectPositions(HWND) - Calculate rectangle locations  *
  19.  *      MyOffsetRect(PRECTL, LONG, LONG) - Offsets consumer     *
  20.  *                                         rectangles from      *
  21.  *                                         resource             *
  22.  *      MainPaint(hwnd) - main WM_PAINT processing routine      *
  23.  *                                                              *
  24. \*==============================================================*/
  25.  
  26. /*--------------------------------------------------------------*\
  27.  *  Include files, macros, defined constants, and externs       *
  28. \*--------------------------------------------------------------*/
  29. #include "semaph.h"
  30.  
  31. /*--------------------------------------------------------------*\
  32.  *  Global variables                                            *
  33. \*--------------------------------------------------------------*/
  34. /* static variables - only visible within this file */
  35. static LONG  yStatsTop, yStatsBtm, xStatsRight, yBoardTop, yDescGlobal;
  36. SHORT  xWidth;
  37.  
  38. LONG colors[MAXUSERS]= {
  39.     CLR_WHITE, CLR_BLACK, CLR_BLUE, CLR_RED, CLR_PINK, CLR_GREEN,
  40.     CLR_CYAN, CLR_YELLOW, CLR_DARKGRAY, CLR_DARKBLUE, CLR_DARKRED,
  41.     CLR_DARKPINK, CLR_DARKGREEN, CLR_DARKCYAN, CLR_BROWN, CLR_PALEGRAY};
  42.  
  43. LONG clrText[MAXUSERS]= {
  44.     CLR_BLACK, CLR_WHITE, CLR_YELLOW, CLR_GREEN, CLR_BLACK, CLR_YELLOW,
  45.     CLR_WHITE, CLR_RED, CLR_WHITE, CLR_BLUE, CLR_RED,
  46.     CLR_PINK, CLR_GREEN, CLR_CYAN, CLR_WHITE, CLR_DARKGRAY};
  47.  
  48. /****************************************************************\
  49.  * Routine to draw the resource rectangle.                      *
  50.  *--------------------------------------------------------------*
  51.  *                                                              *
  52.  *  Name:     DrawResource(PRECTL, COLOR)                       *
  53.  *                                                              *
  54.  *  Purpose:  Color 1 of the 64 rectangles on the playing board.*
  55.  *                                                              *
  56.  *  Usage:    Called from ThreadConsumer in semaph.c when       *
  57.  *            resource becomes owned by a thread.               *
  58.  *                                                              *
  59.  *  Method:   Get the PS from global hwndMain and  WinFillRect. *
  60.  *            This rountine is included so that no access       *
  61.  *            to the resouce is made outside of paint.c.        *
  62.  *            This is to produce code which is more             *
  63.  *            object orientated.                                *
  64.  *                                                              *
  65.  *  Returns:  none                                              *
  66.  *                                                              *
  67. \****************************************************************/
  68. VOID DrawResource(PRECTL prcl, COLOR color)
  69. {
  70.    HPS hps;
  71.  
  72.    hps = WinGetPS(hwndMain);
  73.    /* this code is related to the WinFillRect in DrawRects.
  74.     * they should stay in synch with each other.
  75.     */
  76.    WinFillRect(hps,prcl,color);
  77.    WinReleasePS(hps);
  78.    return;
  79. }
  80.  
  81. /****************************************************************\
  82.  * update the statistics (number of hits) for a thread          *
  83.  *--------------------------------------------------------------*
  84.  *                                                              *
  85.  *  Name:     DrawStats(ULONG)                                  *
  86.  *                                                              *
  87.  *  Purpose:  update statistics display for for a thread        *
  88.  *                                                              *
  89.  *  Usage:    Called from ThreadConsumer in semaph.c when       *
  90.  *            resource becomes owned by a thread.               *
  91.  *                                                              *
  92.  *                                                              *
  93.  *  Method:   Get the PS, set colors, print number of hits for  *
  94.  *            the thread identified by usMyID.                  *
  95.  *            This rountine is included so that no access       *
  96.  *            to the resouce is made outside of paint.c.        *
  97.  *                                                              *
  98.  *  Returns:                                                    *
  99.  *                                                              *
  100. \****************************************************************/
  101. VOID DrawStats(ULONG usMyID)
  102. {
  103.    HPS hps;
  104.    POINTL  ptl;
  105.    THRDATA *pThr = &thrConsumers[usMyID];
  106.    CHAR    szBuf [CNT_SZHITS];
  107.  
  108.    ptl.x = pThr->rcl.xLeft;
  109.    ptl.y = pThr->rcl.yBottom;
  110.    /* sprintf is non-reentrant, so it is used only when
  111.     * have the mutex.
  112.     */
  113.    sprintf (szBuf, "%lu", pThr->lHits);
  114.    hps = WinGetPS(hwndMain);
  115.    GpiSetBackColor (hps, colors [usMyID]);
  116.    GpiSetColor (hps, clrText [usMyID]);
  117.    GpiCharStringPosAt (hps, &ptl, &pThr->rcl, CHS_CLIP | CHS_OPAQUE,
  118.     strlen (szBuf), szBuf, NULL);
  119.    WinReleasePS(hps);
  120.    return;
  121. }
  122.  
  123. /****************************************************************\
  124.  *  Routine to paint rectangles in main window.                 *
  125.  *--------------------------------------------------------------*
  126.  *                                                              *
  127.  *  Name:     DrawRects(HPS)                                    *
  128.  *                                                              *
  129.  *  Purpose:  Does painting of rectangles.                      *
  130.  *                                                              *
  131.  *  Usage:    Called from MainPaint when a paint message        *
  132.  *            is received after the semaphore sample has        *
  133.  *            been started.                                     *
  134.  *                                                              *
  135.  *  Method:   Uses WinFillRect API to color all owned squares on*
  136.  *            the square "playing board". Updates stats for     *
  137.  *            every active consumer thread. Prints the static   *
  138.  *            informational text.                               *
  139.  *                                                              *
  140.  *  Returns:  none.                                             *
  141.  *                                                              *
  142. \****************************************************************/
  143. VOID DrawRects(HPS hps)
  144. {
  145.    INT i;
  146.    POINTL  ptl;
  147.                             /* Fill all rectangles in consumer array....
  148.                                 as well as rectangle for resource.       */
  149.    if (fSemaphoreWasStarted)
  150.       {
  151.        for ( i = 0; i < MAXRESOURCES; i++ )
  152.        {
  153.            if (aSquares[i].usOwner < MAXUSERS &&
  154.             aSquares[i].usOwner != (USHORT)UNOWNED) {
  155.                /* this line is identical to that in DrawResource.
  156.                * they should stay in synch with each other.
  157.                */
  158.                WinFillRect (hps, &aSquares[i].rcl, colors[aSquares[i].usOwner]);
  159.            }
  160.        }
  161.    }
  162.                         /* print dividing line at bottom of stat display */
  163.    ptl.x = 0;
  164.    ptl.y = yStatsBtm - 2;
  165.    GpiMove (hps, &ptl);
  166.    ptl.x = xWidth;
  167.    GpiLine (hps, &ptl);
  168.  
  169.    /* print statistics for each user */
  170.    for (i = 0; i < (INT) cNumUsers; i++)
  171.    {
  172.        DrawStats ((USHORT) i);
  173.    }
  174.    return;
  175. }
  176.  
  177. /****************************************************************\
  178.  * Routine to size and set the positions of rectangle.          *
  179.  *--------------------------------------------------------------*
  180.  *                                                              *
  181.  * Name:    SetRectPositions (xClient, yClient, yChar, yDesc)   *
  182.  *          SHORT   xClient;    width of client window          *
  183.  *          SHORT   yClient;    height of client window         *
  184.  *          SHORT   yChar;      maximum baseline extender       *
  185.  *          SHORT   yDesc;      maximum char descender          *
  186.  *                                                              *
  187.  * Purpose: Given above information about the window, calculate *
  188.  *          the area need for the square game board, the        *
  189.  *          statistics display, and informative text.           *
  190.  *                                                              *
  191.  * Usage:   Called from MainWndProc whenever a size message     *
  192.  *          is received.                                        *
  193.  *                                                              *
  194.  * Method:  Determines space needed for statistics display from *
  195.  *          the font information given as parameters to this    *
  196.  *          procedure.  Then calculates the 18 values which     *
  197.  *          describe a chess by distances from a corner along 2 *
  198.  *          adjacent sides. These are stored in arrays aX and aY*
  199.  *          Given these values, rectangles are calculated for   *
  200.  *          each square on the grid, and stored as part of the  *
  201.  *          information about those squares. Then the size of   *
  202.  *          the rectangles used for the stat display at the top *
  203.  *          of the window are calculated, and stored in the     *
  204.  *          struct describing the thread. Finally, the position *
  205.  *          of the line of informative text which divides the   *
  206.  *          two regions of colored squares is derived.          *
  207.  *                                                              *
  208.  * Returns: none                                                *
  209.  *                                                              *
  210. \****************************************************************/
  211. VOID SetRectPositions(SHORT xClient, SHORT yClient, SHORT yChar, SHORT yDesc)
  212. {
  213.    INT     i;
  214.    INT     dx;
  215.    LONG aX [CNT_POINTS_EDGE];
  216.    LONG aY [CNT_POINTS_EDGE];
  217.    SHORT xScale, yScale;
  218.                                                  /* top of stat display */
  219.    yStatsTop = yClient;
  220.                                               /* bottom of stat display */
  221.    yStatsBtm = yClient - yChar;
  222.                                                /* width of stat display */
  223.    xStatsRight = xClient;
  224.  
  225.                /* dx is the "scaling factor" for the statistics display */
  226.    dx = (INT) ( (double)xStatsRight / (double)cNumUsers );
  227.  
  228.                                      /* rectangles for the stat display */
  229.    for (i = 0; i < (INT) cNumUsers; i++)
  230.    {
  231.       thrConsumers[i].rcl.yBottom = yStatsBtm;
  232.       thrConsumers[i].rcl.yTop = yStatsTop;
  233.       thrConsumers[i].rcl.xLeft = i * dx;
  234.       thrConsumers[i].rcl.xRight = (i+1) * dx;
  235.    }
  236.                                               /* consume any unused edge */
  237.    thrConsumers [cNumUsers - 1].rcl.xRight = xClient;
  238.    yClient = yStatsBtm - 2;
  239.           /* the scaling factors determine how large each square will be */
  240.    xScale =  xClient / CNT_SQUARES_EDGE;
  241.    yScale =  yClient / CNT_SQUARES_EDGE;
  242.  
  243.                                 /* the entire game board is described by
  244.                                    the points that are calculated here.  */
  245.    for (i = 0; i < CNT_POINTS_EDGE; i++)
  246.    {
  247.        aX[i] = i * xScale;
  248.        aY[i] = i * yScale;
  249.    }
  250.                                            /* consume any unused portion */
  251.    aX[CNT_SQUARES_EDGE] = aX[CNT_SQUARES_EDGE] + xClient % CNT_SQUARES_EDGE;
  252.    aY[CNT_SQUARES_EDGE] = aY[CNT_SQUARES_EDGE] + yClient % CNT_SQUARES_EDGE - 1;
  253.  
  254.                                 /* now plug the points just calculated into
  255.                                    the rectangles that make up the board */
  256.    for (i = 0; i < MAXRESOURCES; i++)
  257.    {
  258.        aSquares[i].rcl.xLeft = aX [i % CNT_SQUARES_EDGE];
  259.        aSquares[i].rcl.xRight = aX [(i % CNT_SQUARES_EDGE) + 1];
  260.        aSquares[i].rcl.yBottom = aY [i / CNT_SQUARES_EDGE];
  261.        aSquares[i].rcl.yTop = aY [(i / CNT_SQUARES_EDGE) + 1];
  262.    }
  263.                                            /* store the descender for later */
  264.    yDescGlobal = yDesc;
  265.                                   /* store top of board for later reference */
  266.    yBoardTop = aY[CNT_SQUARES_EDGE];
  267.        /* finally, store the width of the client window for later reference */
  268.    xWidth = xClient;
  269.    return;
  270. }
  271.  
  272. /****************************************************************\
  273.  *   client painting routine                                    *
  274.  *--------------------------------------------------------------*
  275.  *                                                              *
  276.  *  Name:   MainPaint(hwnd)                                     *
  277.  *                                                              *
  278.  *  Purpose: Paints the main client window.                     *
  279.  *                                                              *
  280.  *  Usage:  Routine is called whenver the client window         *
  281.  *          procedure receives a WM_PAINT message               *
  282.  *                                                              *
  283.  *  Method:                                                     *
  284.  *          - begins painting by calling WinBeginPaint          *
  285.  *            and retriving the HPS for the window              *
  286.  *          - performs any painting desired.  Currently the     *
  287.  *            only painting it does is to draw rectangles on    *
  288.  *            the screen if semaphore is started.               *
  289.  *          - ends painting by calling WinEndPaint              *
  290.  *                                                              *
  291.  *  Returns:                                                    *
  292.  *                                                              *
  293. \****************************************************************/
  294. VOID MainPaint( HWND hwnd )
  295. {
  296.    HPS hps=0L;
  297.  
  298.    hps = WinBeginPaint(hwnd, 0L, 0L);
  299.    GpiErase (hps);
  300.    DrawRects (hps);
  301.    WinEndPaint(hps);
  302.    return;
  303. }                                               /* MainPaint() */
  304.