Re: Graphical Bar Code?

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 07/25/02


On Thu, 25 Jul 2002, Mike Stilson wrote:

> (It's amazing the number of li'l routines you add to your libraries over
> some years then forget about)

Why not just:

  char *bar(char *buf, int M, int per)
  {
    const char barChar = '*';
    int len = MIN(M - 1, per / 10);
    memset(buf, barChar, len);
    *(buf + len) = '\0';
    return buf;
  }

  #define PERC(p,w) ((w) > 0 ? ((p) * 100) / (w) : 0)

where buf is a local buffer to write to, M is the maximum length of the
bar plus terminating '\0', and per is a percentage.  A scaling of 1:10 is
automatically applied.  Example:

  int hp = PERC(GET_HIT(ch), GET_MAX_HIT(ch));
  char hbar[11];

  send_to_char(ch, "H: %s\r\n", bar(hbar, 11, hp));

Simple, flexible, and doesn't require you to fiddle with memory
allocation.  A convenience macro like

  #define SBAR(b,v,m) bar((b), sizeof(b), PERC((v), (m)))

can make it even simpler to use for the common case of a percentage bar:

  char hbar[11];
  send_to_char(ch, "H: %s\r\n", SBAR(hbar, GET_HIT(ch), GET_MAX_HIT(ch));

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT