Angel 3.2
A 2D Game Prototyping Engine
GridActor.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Actors/GridActor.h"
32 
33 #include "../Infrastructure/Common.h"
34 
36 {
37  //yay for magic numbers! (default parameters of the grid)
38  _lineColor = Color(.76f, .83f, 1.0f);
39  _axisColor = Color(1.0f, .41f, .6f);
40  _interval = 1.0f;
41  _minCoord = Vector2(-20.0f, -20.0f);
42  _maxCoord = Vector2(20.0f, 20.0f);
43 
44  RecalculatePoints();
45 }
46 
47 GridActor::GridActor(const Color& lines, const Color& axis, float interval, const Vector2& minCoord, const Vector2& maxCoord)
48 {
49  _lineColor = lines;
50  _axisColor = axis;
51  _interval = interval;
52  _minCoord = minCoord;
53  _maxCoord = maxCoord;
54 
55  RecalculatePoints();
56 }
57 
58 void GridActor::SetLineColor(const Color& lineCol)
59 {
60  _lineColor = lineCol;
61 }
62 
64 {
65  return _lineColor;
66 }
67 
68 void GridActor::SetAxisColor(const Color& axisCol)
69 {
70  _axisColor = axisCol;
71 }
72 
74 {
75  return _axisColor;
76 }
77 
78 void GridActor::SetInterval(float interval)
79 {
80  _interval = interval;
81 }
82 
83 const float GridActor::GetInterval() const
84 {
85  return _interval;
86 }
87 
88 void GridActor::SetMinCoord(const Vector2& minCoord)
89 {
90  _minCoord = minCoord;
91  RecalculatePoints();
92 }
93 
95 {
96  return _minCoord;
97 }
98 
99 void GridActor::SetMaxCoord(const Vector2& maxCoord)
100 {
101  _maxCoord = maxCoord;
102  RecalculatePoints();
103 }
104 
106 {
107  return _maxCoord;
108 }
109 
110 void GridActor::RecalculatePoints()
111 {
112  _points.clear();
113 
114  float i;
115  for (i = _minCoord.X; i < _maxCoord.X; i += _interval)
116  {
117  _points.push_back(i);
118  _points.push_back(_minCoord.Y);
119  _points.push_back(i);
120  _points.push_back(_maxCoord.Y);
121  }
122  for (i = _minCoord.Y; i < _maxCoord.Y; i += _interval)
123  {
124  _points.push_back(_minCoord.X);
125  _points.push_back(i);
126  _points.push_back(_maxCoord.X);
127  _points.push_back(i);
128  }
129 
130  _axes[0] = _minCoord.X;
131  _axes[1] = 0.0f;
132  _axes[2] = _maxCoord.X;
133  _axes[3] = 0.0f;
134  _axes[4] = 0.0f;
135  _axes[5] = _minCoord.Y;
136  _axes[6] = 0.0f;
137  _axes[7] = _maxCoord.Y;
138 }
139 
141 {
142  // lines
143  glEnableClientState(GL_VERTEX_ARRAY);
144  glLineWidth(1.0f);
145  glColor4f(_lineColor.R, _lineColor.G, _lineColor.B, 1.0f);
146  glVertexPointer(2, GL_FLOAT, 0, &_points[0]);
147  glDrawArrays(GL_LINES, 0, _points.size() / 2);
148 
149  // axes
150  glColor4f(_axisColor.R, _axisColor.G, _axisColor.B, 1.0f);
151  glVertexPointer(2, GL_FLOAT, 0, _axes);
152  glDrawArrays(GL_LINES, 0, 4);
153 }