Home | History | Annotate | Line # | Download | only in dab
box.cc revision 1.2.18.1
      1 /*	$NetBSD: box.cc,v 1.2.18.1 2008/05/18 12:29:48 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * box.C: Box computations
     34  */
     35 #include "defs.h"
     36 RCSID("$NetBSD: box.cc,v 1.2.18.1 2008/05/18 12:29:48 yamt Exp $")
     37 
     38 #include "box.h"
     39 #include "board.h"
     40 #include "gamescreen.h"
     41 #include <curses.h>
     42 
     43 const POINT BOX::edges[BOX::last] =
     44     { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
     45 const POINT BOX::corners[BOX::last] =
     46     { { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } };
     47 const int  BOX::syms[BOX::last] =
     48     { GAMESCREEN::GS_HLINE, GAMESCREEN::GS_HLINE,
     49       GAMESCREEN::GS_VLINE, GAMESCREEN::GS_VLINE };
     50 
     51 BOX::BOX(size_t py, size_t px, BOARD& b) :
     52     _b(b)
     53 {
     54     _centery = py * 2 + 1;
     55     _centerx = px * 2 + 1;
     56 }
     57 
     58 void BOX::addcorner(size_t y, size_t x)
     59 {
     60     char sym;
     61     _b.getScrn()->moveto(y, x);
     62     if (x == 0) {
     63 	if (y == 0)
     64 	    sym = GAMESCREEN::GS_ULCORNER;
     65 	else if (y == _b.ty() - 1)
     66 	    sym = GAMESCREEN::GS_LLCORNER;
     67 	else
     68 	    sym = GAMESCREEN::GS_LTEE;
     69     } else if (x == _b.tx() - 1) {
     70 	if (y == 0)
     71 	    sym = GAMESCREEN::GS_URCORNER;
     72 	else if (y == _b.ty() - 1)
     73 	    sym = GAMESCREEN::GS_LRCORNER;
     74 	else
     75 	    sym = GAMESCREEN::GS_RTEE;
     76     } else if (y == 0)
     77 	sym = GAMESCREEN::GS_TTEE;
     78     else if (y == _b.ty() - 1)
     79 	sym = GAMESCREEN::GS_BTEE;
     80     else
     81 	sym = GAMESCREEN::GS_PLUS;
     82 
     83     _b.getScrn()->addedge(sym);
     84 }
     85 
     86 // Paint a box
     87 void BOX::paint(void)
     88 {
     89     int e;
     90     if (_b.getScrn() == NULL)
     91 	return;
     92 
     93     _b.getScrn()->moveto(_centery, _centerx);
     94     _b.getScrn()->addsym(name());
     95 
     96     for (e = BOX::first; e < BOX::last; e++) {
     97 	addcorner(_centery + corners[e].y, _centerx + corners[e].x);
     98 	_b.getScrn()->moveto(_centery + edges[e].y, _centerx + edges[e].x);
     99 	_b.getScrn()->addedge(edge(static_cast<EDGE>(e)));
    100     }
    101     _b.getScrn()->redraw();
    102 }
    103 
    104 // Return the name
    105 int& BOX::name(void)
    106 {
    107     return _b.data(_centery, _centerx);
    108 }
    109 
    110 // Set an edge
    111 void BOX::set(int e)
    112 {
    113     _b.data(_centery + edges[e].y, _centerx + edges[e].x) = syms[e];
    114 }
    115 
    116 // Clear an edge
    117 void BOX::clr(int e)
    118 {
    119     _b.data(_centery + edges[e].y, _centerx + edges[e].x) = ' ';
    120 }
    121 
    122 // Test an edge
    123 int BOX::isset(int e) const
    124 {
    125     return _b.data(_centery + edges[e].y, _centerx + edges[e].x) != ' ';
    126 }
    127 
    128 // Return the edge
    129 int& BOX::edge(int e)
    130 {
    131     return _b.data(_centery + edges[e].y, _centerx + edges[e].x);
    132 }
    133 
    134 // Count the number of edges set in the box
    135 int BOX::count(void) const
    136 {
    137     int cnt = 0;
    138 
    139     for (int e = BOX::first; e < BOX::last; e++)
    140 	cnt += isset(static_cast<EDGE>(e));
    141     return cnt;
    142 }
    143 
    144 // Clear the box
    145 void BOX::reset(void)
    146 {
    147     for (int e = BOX::first; e < BOX::last; e++)
    148 	clr(static_cast<EDGE>(e));
    149     name() = ' ';
    150 }
    151