home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xconq55.zip / xc5.5 / ginit.c < prev    next >
C/C++ Source or Header  |  1992-02-15  |  12KB  |  378 lines

  1. /* Copyright (c) 1987, 1988  Stanley T. Shebs. */
  2. /* This program may be used, copied, modified, and redistributed freely */
  3. /* for noncommercial purposes, so long as this notice remains intact. */
  4.  
  5. /* Initialization and random routines for the display part of xconq. */
  6.  
  7. #include "config.h"
  8. #include "misc.h"
  9. #include "dir.h"
  10. #include "period.h"
  11. #include "side.h"
  12. #include "unit.h"
  13. #include "map.h"
  14. #include "global.h"
  15.  
  16. /* Always use four lines for the unit info display, even on small screens. */
  17.  
  18. #define INFOLINES 5
  19.  
  20. int maxnamelen = 0;        /* length of longest side name+host */
  21.  
  22. /* Find a unit and put it at center (or close to it) of first view.  I guess */
  23. /* can't use put_on_screen because not enough stuff is inited yet. */
  24.  
  25. init_curxy(side)
  26. Side *side;
  27. {
  28.     Unit *unit;
  29.     Side *loop_side;
  30.  
  31.     for_all_units(loop_side, unit) {
  32.     if (unit->side == side) {
  33.         side->cx = unit->x;  side->cy = unit->y;
  34.         side->vcx = unit->x;
  35.         side->vcy = max(unit->y, side->vw2+1);
  36.         side->vcy = min(side->vcy, (world.height-1) - side->vh2);
  37.         return;
  38.     }
  39.     }
  40.     side->vcx = side->vw2;  side->vcy = side->vh2;
  41.     side->cx = side->vcx;  side->cy = side->vcy;
  42. }
  43.  
  44. /* open displays early so that the first redraw will find the displays already up. */
  45.  
  46. init_displays1()
  47. {
  48.     int len;
  49.     Side *side;
  50.  
  51.     for_all_sides(side) {
  52.     len = 8 + strlen(side->name) + 1;
  53.     maxnamelen = max(maxnamelen,
  54.              len + (side->host ? hostlen(side->host) + 3 : 0));
  55.     }
  56.     for_all_sides(side) {
  57.     if (side->host != NULL) {
  58.         init_display(side);
  59.     }
  60.     }
  61. }
  62.  
  63. /* The very first step in using X is to open up all the desired displays. */
  64. /* In our case, there are many displays each with many windows.  If the */
  65. /* display opens successfully, do an initial "redraw" to set the screen up. */
  66.  
  67. init_displays2()
  68. {
  69.     Side *side;
  70.  
  71.     for_all_sides(side) {
  72.     if (side->host != NULL) {
  73.         if (active_display(side)) {
  74.         init_curxy(side);
  75. #if 0
  76.         redraw(side);
  77. #endif
  78.         }
  79.     }
  80.     }
  81. }
  82.  
  83. /* The length of the printed host name is the length till the first */
  84.  /* period or the total length */
  85.  
  86. int hostlen(str)
  87. char str[];
  88. {
  89.   int len = 0;
  90.  
  91.   while ((str[len] != '.') && (str[len] != '\0')) len++;
  92.   return len;
  93. }
  94.  
  95. /* Open display, create all the windows we'll need, do misc setup things, */
  96. /* and initialize some globals to out-of-range values for recognition later. */
  97.  
  98. init_display(side)
  99. Side *side;
  100. {
  101.     int i;
  102.  
  103.     if (Debug) printf("Will try to open display \"%s\" ...\n", side->host);
  104.  
  105.     if (!open_display(side)) {
  106.     fprintf(stderr, "Display \"%s\" could not be opened!\n", side->host);
  107.     exit_xconq();
  108.     }
  109.     active_display(side);   /* done for side effect */
  110.     init_colors(side);
  111.     init_misc(side);
  112.     init_sizes(side);
  113.     create_main_window(side);
  114.     side->msg =
  115.     create_window(side, 0, 0,
  116.               side->lw, side->nh * side->fh);
  117.     side->info =
  118.     create_window(side, 0, side->nh * side->fh + side->bd,
  119.               side->lw, INFOLINES * side->fh);
  120.     side->prompt =
  121.     create_window(side, 0, (side->nh + INFOLINES) * side->fh + 2*side->bd,
  122.               side->lw, side->fh);
  123.     side->map =
  124.     create_window(side, 0, side->tlh,
  125.               side->vw * side->hw,
  126.               side->vh * side->hch + (side->hh - side->hch));
  127.     set_retain(side, side->map);
  128.     side->sides =
  129.     create_window(side, side->lw + side->bd + 1, 0,
  130.               side->sw * side->fw, numsides * side->fh);
  131.     side->timemode =
  132.     create_window(side, side->lw + side->fw, side->trh - 4 * side->fh,
  133.               2 * side->margin + 8 * side->fw, 2 * side->fh);
  134.     side->clock =
  135.     create_window(side, side->lw + 13 * side->fw, side->trh - 2 * side->fh,
  136.               8 * side->fw, side->fh);
  137.     side->state =
  138.     create_window(side, side->lw + 1, side->trh,
  139.               22 * side->fw, period.numutypes*max(side->hh, side->fh));
  140.     if (world_display(side)) {
  141.     side->world =
  142.         create_window(side,
  143.               side->lw+side->bd, side->mh-side->mm*world.height,
  144.               world.width * side->mm, world.height * side->mm);
  145.     set_retain(side, side->world);
  146.     }
  147.     create_help_window(side);
  148.     fixup_windows(side);
  149.     enable_input(side);
  150.     for (i = 0; i < MAXNOTES; ++i) side->noticebuf[i] = " ";
  151.     for (i = 0; i < MAXUTYPES; ++i) side->bvec[i] = 0;
  152.     /* Flag some values as uninitialized */
  153.     side->vcx = side->vcy = -1;
  154.     side->lastvcx = side->lastvcy = -1;
  155.     side->lastx = side->lasty = -1;
  156.     if (Debug) printf("Successfully opened \"%s\"!\n", side->host);
  157. }
  158.  
  159. /* Initial sizing tries to take as much screen as possible. */
  160.  
  161. init_sizes(side)
  162. Side *side;
  163. {
  164.   if (!resize_display(side, display_width(side), display_height(side))) {
  165.     fprintf(stderr, "Display \"%s\" is too small!\n", side->host);
  166.     exit_xconq();
  167.   }
  168. }
  169.  
  170. /* Decide/compute all the sizes of things.  Our main problem here is that */
  171. /* the display might be smaller than we really desire, so things have to */
  172. /* be adjusted to fit. */
  173.  
  174. resize_display(side, mw, mh)
  175. Side *side;
  176. int mw, mh;
  177. {
  178.   /* if you adjust this procedure, please make a corresponding
  179.      adjustment to resize_display2() */
  180.     int alw, abh;
  181.  
  182.     alw = max(mw/4, world.width);
  183.     side->sw = min(maxnamelen+5, alw / side->fw);
  184.  
  185.     alw = mw - max((side->sw * side->fw), world.width) - side->bd;
  186.     side->vw = min(world.width, alw / side->hw);
  187.     side->nw = min(BUFSIZE, alw / side->fw);
  188.  
  189.     abh = (2 * mh) / 3;
  190.     side->vh = min(world.height, abh / side->hch);
  191.     side->nh = max(1, min((abh/side->fh)/2 - 5, MAXNOTES));
  192.     side->mm = min(5, (max(world.width, side->fw*side->sw) / world.width));
  193.     set_sizes(side);
  194.     side->mw = mw;
  195.     side->mh = mh;
  196.     return (side->vw >= MINWIDTH && side->vh >= MINHEIGHT);
  197. }
  198.  
  199. /* this version doesn't adjust the map magnification. */
  200. resize_display2(side, mw, mh)
  201. Side *side;
  202. int mw, mh;
  203. {
  204.   /* if you adjust this procedure, please make a corresponding
  205.      adjustment to resize_display() */
  206.     int alw, abh;
  207.  
  208.     alw = max(mw/4, side->mm*world.width);
  209.     side->sw = min(maxnamelen+5, alw / side->fw);
  210.  
  211.     alw = mw - max((side->sw * side->fw), side->mm*world.width) - side->bd;
  212.     side->vw = min(world.width, alw / side->hw);
  213.     side->nw = min(BUFSIZE, alw / side->fw);
  214.  
  215.     abh = (2 * mh) / 3;
  216.     side->vh = min(world.height, abh / side->hch);
  217.     side->nh = max(1, min((abh/side->fh)/2 - 5, MAXNOTES));
  218.     set_sizes(side);
  219.     side->mw = mw;
  220.     side->mh = mh;
  221.     return (side->vw >= MINWIDTH && side->vh >= MINHEIGHT);
  222. }
  223.  
  224. /* This fn is a "ten-line horror"; effectively a mini tiled window manager */
  225. /* that keeps the subwindows from overlapping no matter what the display and */
  226. /* requested sizes are.  Siemens eat your heart out... */
  227.  
  228. set_sizes(side)
  229. Side *side;
  230. {
  231.     int ulhgt, llhgt, urhgt, lrhgt;
  232.  
  233.     /* Make sure map window dimensions are OK */
  234.     side->vw = min(world.width, side->vw);
  235.     side->vh = min(world.height, side->vh);
  236.     side->vw2 = side->vw / 2;  side->vh2 = side->vh / 2;
  237.     side->vw_odd = side->vw % 2;
  238.     /* Compute subregion sizes (left/right upper/lower width/height) */
  239.     side->lw = max(side->nw * side->fw, side->hw * side->vw);
  240.     side->rw = max((world_display(side) ? world.width * side->mm : 0),
  241.            side->sw * side->fw);
  242.  
  243.     side->trh = (numsides + 4) * side->fh + side->bd;
  244.     side->tlh = side->fh * (side->nh + INFOLINES + 1) + 3 * side->bd;
  245.  
  246.     side->brh = period.numutypes * max(side->hh, side->fh);
  247.     if (world_display(side))
  248.       side->brh += side->mm * world.height + 2*side->bd;
  249.     side->blh = side->hch * side->vh + (side->hh - side->hch);
  250.  
  251.     if (side->brh+side->trh > side->blh+side->tlh)
  252.       side->blh = side->brh+side->trh - side->tlh;
  253.  
  254.     if (side->trh > side->tlh) {
  255.       side->brh = side->tlh+side->blh - side->trh;
  256.     } else if (side->brh > side->blh) {
  257.       side->trh = side->tlh+side->blh - side->brh;
  258.     } else {
  259.       side->brh = side->blh;
  260.       side->trh = side->tlh;
  261.     }
  262.  
  263.     side->mw = side->lw + side->bd + side->rw;
  264.     side->mh = side->tlh + 3*side->bd + side->blh;
  265.     /* Only vcy needs adjustment, since vcx can never be close to an edge */
  266.     side->vcy = min(max(side->vh2, side->vcy), (world.height-1)-side->vh2);
  267. }
  268.  
  269. /* Acquire a set of colors.  There are too many to make it feasible to */
  270. /* customize them via .Xdefaults, so we don't even try.  If there aren't */
  271. /* enough colors, drop into monochrome mode.  This doesn't take the window */
  272. /* manager into account - it may have grabbed some color space. */
  273.  
  274. init_colors(side)
  275. Side *side;
  276. {
  277.     if (Debug) printf("%d colors available ...\n", display_colors(side));
  278.     side->monochrome = (display_colors(side) <= 2);
  279.     side->bonw = (side->monochrome ? BLACKONWHITE : FALSE);
  280.     set_colors(side);
  281. }
  282.  
  283. /* This will set up the correct set of colors at any point in the game. */
  284. /* Note that terrain colors get NO value if in monochrome mode. */
  285. /* If the colors requested are not available, it is up to the graphics */
  286. /* interface to supply a substitute. */
  287.  
  288.  
  289. long
  290. get_map_color(side, name, class, fallback)
  291.      Side    *side;
  292.      char    *name, *class, *fallback;
  293. {
  294.   static char    rmName[200], rmClass[200];
  295.   sprintf(rmName, "map.%s.%s", period.name, name);
  296.   sprintf(rmClass, "map.Period.%s", class);
  297.   return request_color(side, rmName, rmClass, fallback);
  298. }
  299.  
  300. set_colors(side)
  301. Side *side;
  302. {
  303.   int    t;
  304.   long fg, bg;
  305.   
  306.   fg = (side->bonw ? black_color(side) : white_color(side));
  307.   bg = (side->bonw ? white_color(side) : black_color(side));
  308.   side->bgcolor = side->owncolor = side->altcolor = side->diffcolor = bg;
  309.   side->fgcolor = side->bdcolor = side->graycolor = side->enemycolor = fg;
  310.   side->neutcolor = side->goodcolor = side->badcolor = fg;
  311.   if (!side->monochrome) {
  312.     static char    *FG="Foreground", *BG="Background";
  313.     for_all_terrain_types(t) {
  314.       side->hexcolor[t] = request_color(side, NULL, NULL, ttypes[t].color);
  315.     }
  316. #if 1
  317.     get_color_resources(side);
  318. #else
  319.     side->owncolor = get_map_color(side,"ownColor",BG,"black");
  320.     side->altcolor = get_map_color(side,"alternateColor",BG,"blue");
  321.     side->diffcolor = get_map_color(side,"differentColor",BG,"maroon");
  322.     side->bdcolor = get_map_color(side,"borderColor",FG,"blue");
  323.     side->graycolor = get_map_color(side,"grayColor",FG,"light gray");
  324.     side->enemycolor = get_map_color(side,"enemyColor",FG,"red");
  325.     side->neutcolor = get_map_color(side,"neutralColor",FG,"light gray");
  326.     side->goodcolor = get_map_color(side,"goodColor",FG,"green");
  327.     side->badcolor = get_map_color(side,"badColor",FG,"red");
  328. #endif
  329.   }
  330. }
  331.  
  332. /* Move windows and change their sizes to correspond with the new sizes of */
  333. /* viewports, etc. */
  334.  
  335. reconfigure_display(side, rzmain)
  336. Side *side;
  337. int rzmain;
  338. {
  339.   int sy, sdy;
  340.   
  341.   if (active_display(side)) {
  342.     if (rzmain)
  343.       set_colors(side);
  344.     reset_misc(side);
  345.     sy = 0;  sdy = side->nh * side->fh;
  346.     change_window(side, side->msg, 0, sy, side->lw, sdy);
  347.     sy += sdy + side->bd;  sdy = INFOLINES * side->fh;
  348.     change_window(side, side->info, 0, sy, side->lw, sdy);
  349.     sy += sdy + side->bd;  sdy = 1 * side->fh;
  350.     change_window(side, side->prompt, 0, sy, side->lw, sdy);
  351.     change_window(side, side->map,
  352.           0, side->tlh,
  353.           side->vw * side->hw,
  354.           side->vh * side->hch + (side->hh - side->hch));
  355.     change_window(side, side->sides,
  356.           side->lw + side->bd, 0, -1, -1);
  357.     change_window(side, side->timemode,
  358.           side->lw + side->fw, side->trh - 4 * side->fh, -1, -1);
  359.     change_window(side, side->clock,
  360.           side->lw + 13 * side->fw, side->trh - side->fh, -1, -1);
  361.     change_window(side, side->state,
  362.           side->lw + side->bd, side->trh + side->bd, -1, -1);
  363.     if (world_display(side)) {
  364.       change_window(side, side->world,
  365.             side->lw+side->bd, side->mh - world.height*side->mm,
  366.             world.width * side->mm, world.height * side->mm);
  367.     }
  368.     if (rzmain) {
  369.       change_window(side, side->main, -1, -1, side->mw, side->mh);
  370.       printf("main window configured to %dx%d\n", side->mw, side->mh);
  371.     }
  372. #if 0
  373.     redraw(side);
  374. #endif
  375.   }
  376. }
  377.  
  378.