Line data Source code
1 : #include <stdio.h>
2 : #include <assert.h>
3 :
4 : #include "Util.h"
5 : #include "Config.h"
6 :
7 : #include "Block.h"
8 :
9 9 : Block::Block() {
10 :
11 : #ifdef PRINT
12 : puts("Block::Block()");
13 : #endif
14 :
15 : // Default constructor creates a block of size 50
16 : // with a random location.
17 9 : size(Config::BLOCK_SIZE, Config::BLOCK_SIZE);
18 9 : randomLocation();
19 :
20 : // Calculate edges accordingly.
21 9 : setEdges();
22 :
23 9 : }
24 :
25 0 : Block::Block(int x, int y, int w, int h) {
26 :
27 0 : location(x, y);
28 0 : size(w, h);
29 0 : }
30 :
31 0 : Block::~Block() {
32 :
33 0 : deleteEdges();
34 :
35 : #ifdef PRINT
36 : puts("Block::~Block()");
37 : #endif
38 0 : }
39 :
40 0 : void Block::deleteEdges() {
41 :
42 : // Delete edges and free pointers.
43 0 : std::vector< Edge* >::iterator iEdge;
44 0 : for(iEdge = edges_.begin(); iEdge != edges_.end(); ++iEdge) {
45 0 : Edge* anEdge = *iEdge;
46 0 : delete anEdge;
47 : }
48 :
49 0 : edges_.clear();
50 0 : }
51 :
52 9 : void Block::setEdges() {
53 :
54 : // Edges must be empty to set edges.
55 9 : assert( edges_.size() == 0 );
56 :
57 : // Set/Create edges.
58 9 : Vector2f A(x_, y_);
59 9 : Vector2f B(x_ + width_, y_);
60 9 : Vector2f C(x_ + width_, y_ + height_);
61 9 : Vector2f D(x_, y_ + height_);
62 :
63 9 : edges_.push_back( new Edge(A, B) );
64 9 : edges_.push_back( new Edge(B, C) );
65 9 : edges_.push_back( new Edge(C, D) );
66 9 : edges_.push_back( new Edge(D, A) );
67 9 : }
68 :
69 9 : void Block::location(int x, int y) {
70 :
71 9 : x_ = x;
72 9 : y_ = y;
73 :
74 : #ifdef PRINT
75 : printf("Block::location(%d, %d)\n", x_, y_);
76 : #endif
77 9 : }
78 :
79 9 : void Block::size(int width, int height) {
80 :
81 9 : width_ = width;
82 9 : height_ = height;
83 :
84 : #ifdef PRINT
85 : printf("Block::size(%d, %d)\n", width_, height_);
86 : #endif
87 9 : }
88 :
89 9 : void Block::randomLocation() {
90 :
91 : // Initialize with a random location within boundaries.
92 9 : int margin = 10;
93 :
94 : int x = Util::randomInRange(
95 : margin,
96 : Config::SCREEN_WIDTH - width_ - margin
97 9 : );
98 :
99 : int y = Util::randomInRange(
100 : margin,
101 : Config::SCREEN_HEIGHT - height_ - margin
102 9 : );
103 :
104 9 : location(x, y);
105 9 : }
106 :
107 45 : std::vector< Edge* >& Block::getEdges() {
108 :
109 45 : return edges_;
110 : }
|