-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicRender.cpp
157 lines (131 loc) · 4.45 KB
/
GraphicRender.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <QtGui>
#include <math.h>
#include "GameInfo.h"
#include "Map.h"
#include "GameUnit.h"
#include "GraphicRender.h"
GraphicRender::GraphicRender(GameInfo *info, QWidget *parent)
: QWidget(parent)
{
Info = info;
Info->GameUserView_Size.setRect(0.0, 0.0, (qreal)width(), (qreal)height());
TimerID = startTimer(Info->FrameTime);
}
GraphicRender::~GraphicRender()
{
killTimer(TimerID);
}
QSize GraphicRender::minimumSizeHint() const
{
return QSize(100, 100);
}
QSize GraphicRender::sizeHint() const
{
return QSize(1400, 600);
}
void GraphicRender::paintEvent(QPaintEvent * /* event */)
{
long CurrentFrameTime = Info->Global_Time; // Çíà÷åíèå âðåìåíè äëÿ ðåíäåðèíãà òåêóùåãî êàäðà
/* SetUserView */
Info->GameUserView_Size.setRect(0.0, 0.0, (qreal)width(), (qreal)height());///
Info->GameUserView_Position.rx() += Info->ScrollHorizontal*Info->ZoomFactor*Info->ScrollSpeed;
Info->GameUserView_Position.ry() += Info->ScrollVertical*Info->ZoomFactor*Info->ScrollSpeed;
if (Info->GameUserView_Position.rx() > Info->Map->Size.width()-Info->GameUserView_Size.width())
Info->GameUserView_Position.rx() = Info->Map->Size.width()-Info->GameUserView_Size.width();
if (Info->GameUserView_Position.rx() < 0.0)
Info->GameUserView_Position.rx() = 0.0;
if (Info->GameUserView_Position.ry() > Info->Map->Size.height()-Info->GameUserView_Size.height())
Info->GameUserView_Position.ry() = Info->Map->Size.height()-Info->GameUserView_Size.height();
if (Info->GameUserView_Position.ry() < 0.0)
Info->GameUserView_Position.ry() = 0.0;
/* */
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, false);
painter.setPen(palette().dark().color());
painter.setBrush(QBrush(QColor(0xF2,0xEF,0xE8), Qt::SolidPattern));
painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
if (Info->antialiased) painter.setRenderHint(QPainter::Antialiasing, true);
painter.save();
painter.translate(-Info->GameUserView_Position.rx(), -Info->GameUserView_Position.ry());
// Îòðèñîâêà âîäîåìîâ
painter.setPen(QColor(0xBE,0xCD,0xCA));
painter.setBrush(QBrush(QColor(0xB4,0xD0,0xD1), Qt::SolidPattern));
for (int i=0; i<Info->Map->Waters->length(); i++)
{
painter.drawPolygon((*((*(Info->Map->Waters))[i])));
}
// Îòðèñîâêà äîðîã
painter.setPen(QColor(0xBE,0xBD,0xBB));
painter.setBrush(QBrush(QColor(0xFF,0xFF,0xFF), Qt::SolidPattern));
for (int i=0; i<Info->Map->Roads->length(); i++)
{
painter.drawPolygon((*((*(Info->Map->Roads))[i])));
}
// Îòðèñîâêà çäàíèé
painter.setPen(QColor(0xBE,0xBD,0xBB));
painter.setBrush(QBrush(QColor(0xBC,0xAA,0xAA), Qt::SolidPattern));
for (int i=0; i<Info->Map->Buildings->length(); i++)
{
painter.drawPolygon((*((*(Info->Map->Buildings))[i])));
}
// Îòðèñîâêà þíèòîâ
for (int i=0; i<Info->GameUnits->length(); i++)
{
painter.save();
((*(Info->GameUnits))[i])->Draw(&painter);
painter.restore();
}
painter.restore();
// Îòðèñîâêà ðàìêè âûäåëåíèÿ
painter.setRenderHint(QPainter::Antialiasing, false);
if (Info->DrawSelection == 1)
{
painter.setPen(QColor(0xF0,0x80,0x80));
painter.setPen(Qt::DashDotLine);
painter.setBrush(Qt::NoBrush);
painter.drawRect(Info->SelectionArea);
}
/***/
painter.setPen(QColor(0x00,0xFF,0x00));
painter.setBrush(Qt::NoBrush);
painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
/***/
}
void GraphicRender::timerEvent(QTimerEvent *event)
{
Draw();
}
void GraphicRender::Draw()
{
update();
}
void GraphicRender::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{ // Íà÷àëî âûäåëåíèÿ ãðóïïû îáúåêòîâ
Info->UnSelectAllUnits();
Info->SelectionArea.setRect(event->x(), event->y(), 0.0, 0.0);
Info->SelectUnitsInArea(&Info->SelectionArea);
Info->DrawSelection = 1;
}
else if (event->buttons() & Qt::RightButton) {} /// Send order
}
void GraphicRender::mouseReleaseEvent(QMouseEvent *event)
{
/// if (event->buttons() & Qt::LeftButton)
{
if (Info->DrawSelection)
{ // Êîíåö âûäåëåíèÿ ãðóïïû îáúåêòîâ
Info->DrawSelection = 0;
Info->SelectionArea.setRect(Info->SelectionArea.x(), Info->SelectionArea.y(), event->x()-Info->SelectionArea.x(), event->y()-Info->SelectionArea.y());
}
}
}
void GraphicRender::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{ // Èçìåíåíèå ðàìêè âûäåëåíèÿ
Info->SelectionArea.setRect(Info->SelectionArea.x(), Info->SelectionArea.y(), event->x()-Info->SelectionArea.x(), event->y()-Info->SelectionArea.y());
Info->SelectUnitsInArea(&Info->SelectionArea);
}
}