box.cc 4.4 KB

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