Debian Codegraph @

src/level/FinderField.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "FinderField.h"
10
11 #include "V2.h"
12
13 #include <string.h> // memset()
14
15 //-----------------------------------------------------------------
16 /**
17 * Two dimensional array of booleans.
18 */
19 FinderField::FinderField(int w, int h)
20 {
21 m_w = w;
22 m_h = h;
23
24 //NOTE: [y][x] indexes
25 m_closed = new bool*[m_h];
26 for (int y = 0; y < m_h; ++y) {
27 m_closed[y] = new bool[m_w];
28 memset(m_closed[y], false, sizeof(bool) * m_w);
29 }
30 }
31 //-----------------------------------------------------------------
32 FinderField::~FinderField()
33 {
34 for (int y = 0; y < m_h; ++y) {
35 delete[] m_closed[y];
36 }
37 delete[] m_closed;
38 }
39 //-----------------------------------------------------------------
40 /**
41 * Erase all marks.
42 */
43 void
44 FinderField::reset()
45 {
46 for (int y = 0; y < m_h; ++y) {
47 memset(m_closed[y], false, sizeof(bool) * m_w);
48 }
49 }
50 //-----------------------------------------------------------------
51 /**
52 * Mark given place as closed.
53 */
54 void
55 FinderField::markClosed(const V2 &loc)
56 {
57 int x = loc.getX();
58 int y = loc.getY();
59
60 if ((0 <= x && x < m_w) && (0 <= y && y < m_h)) {
61 m_closed[y][x] = true;
62 }
63 }
64 //-----------------------------------------------------------------
65 /**
66 * Returns true when place is closed.
67 * NOTE: all places outside array are marked as closed
68 */
69 bool
70 FinderField::isClosed(const V2 &loc) const
71 {
72 int x = loc.getX();
73 int y = loc.getY();
74
75 bool result = true;
76 if ((0 <= x && x < m_w) && (0 <= y && y < m_h)) {
77 result = m_closed[y][x];
78 }
79 return result;
80 }
81
82