-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicObject.cpp
More file actions
131 lines (112 loc) · 2.72 KB
/
GraphicObject.cpp
File metadata and controls
131 lines (112 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// GraphicObject.cpp
// Template Project
//
// Created by Jean-Yves Hervé on 2023-10-05.
//
#include <iostream>
#include "glPlatform.h"
#include "GraphicObject.h"
using namespace earshooter;
using namespace std;
bool GraphicObject::drawAbsoluteBoxes_ = false;
bool GraphicObject::drawRelativeBoxes_ = false;
GraphicObject::GraphicObject(float x, float y, float angle)
: Object(x, y, angle),
absoluteBox_(nullptr),
relativeBox_(nullptr)
{}
GraphicObject::GraphicObject(const WorldPoint& pt, float angle)
: Object(pt, angle),
absoluteBox_(nullptr),
relativeBox_(nullptr)
{}
void GraphicObject::draw() const
{
draw_();
// Unfortunately, we are back in the parent reference frame, so we need to
// apply the object's relative transformation again
if (drawAbsoluteBoxes_)
{
// forces an update of the box
getAbsoluteBox();
glPushMatrix();
glTranslatef(getX(), getY(), 0.f);
// no rotation for the absolute box
absoluteBox_->draw();
glPopMatrix();
}
if (drawRelativeBoxes_ && relativeBox_ != nullptr)
{
glPushMatrix();
glTranslatef(getX(), getY(), 0.f);
glRotatef(getAngle(), 0.f, 0.f, 1.f);
relativeBox_->draw();
glPopMatrix();
}
}
void GraphicObject::setAbsoluteBoundingBox_(float xmin, float xmax, float ymin, float ymax) const
{
if (absoluteBox_ != nullptr)
{
absoluteBox_->setDimensions(xmin, xmax, ymin, ymax);
}
else
{
absoluteBox_ = make_shared<BoundingBox>(xmin, xmax, ymin, ymax);
}
}
void GraphicObject::setAbsoluteBoundingBox_(const WorldPoint& cornerUL, const WorldPoint& cornerLR) const
{
if (absoluteBox_ != nullptr)
{
absoluteBox_->setDimensions(cornerUL, cornerLR);
}
else
{
absoluteBox_ = make_shared<BoundingBox>(cornerUL, cornerLR);
}
}
void GraphicObject::setRelativeBoundingBox_(float xmin, float xmax, float ymin, float ymax) const
{
if (relativeBox_ != nullptr)
{
relativeBox_->setDimensions(xmin, xmax, ymin, ymax);
}
else
{
relativeBox_ = make_shared<BoundingBox>(xmin, xmax, ymin, ymax);
}
}
void GraphicObject::setRelativeBoundingBox_(const WorldPoint& cornerUL, const WorldPoint& cornerLR) const
{
if (relativeBox_ != nullptr)
{
relativeBox_->setDimensions(cornerUL, cornerLR);
}
else
{
relativeBox_ = make_shared<BoundingBox>(cornerUL, cornerLR);
}
}
void GraphicObject::setAbsoluteBoundingBoxColor_(ColorIndex color) const
{
if (absoluteBox_ != nullptr)
{
absoluteBox_->setColor(color);
}
}
void GraphicObject::setRelativeBoundingBoxColor_(ColorIndex color) const
{
if (relativeBox_ != nullptr)
{
relativeBox_->setColor(color);
}
}
const std::shared_ptr<BoundingBox> GraphicObject::getAbsoluteBox() const
{
if (absoluteBox_ == nullptr)
absoluteBox_ = make_shared<BoundingBox>();
updateAbsoluteBox_();
return absoluteBox_;
}