summaryrefslogtreecommitdiffstats
path: root/pacman-c++/main.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2011-04-03 17:01:08 +0200
committermanuel <manuel@mausz.at>2011-04-03 17:01:08 +0200
commit11e767ea030cbdab3f0374f61efc3a813746fad5 (patch)
tree8351c8b61092874286f9f78f96ad8b5d86ca66e6 /pacman-c++/main.cpp
parent818383fec4f220a2410177b58518797e81d8eab3 (diff)
downloadfoop-11e767ea030cbdab3f0374f61efc3a813746fad5.tar.gz
foop-11e767ea030cbdab3f0374f61efc3a813746fad5.tar.bz2
foop-11e767ea030cbdab3f0374f61efc3a813746fad5.zip
extreme cleanup
Diffstat (limited to 'pacman-c++/main.cpp')
-rw-r--r--pacman-c++/main.cpp269
1 files changed, 3 insertions, 266 deletions
diff --git a/pacman-c++/main.cpp b/pacman-c++/main.cpp
index 5fa9c1c..ac55036 100644
--- a/pacman-c++/main.cpp
+++ b/pacman-c++/main.cpp
@@ -1,286 +1,23 @@
1#include "actor.h"
1#include "pixmapitem.h" 2#include "pixmapitem.h"
2#include <QtCore> 3#include <QtCore>
3#include <QtGui> 4#include <QtGui>
4 5
5//![15]
6class StateSwitchEvent: public QEvent
7{
8public:
9 StateSwitchEvent()
10 : QEvent(Type(StateSwitchType))
11 {
12 }
13
14 StateSwitchEvent(int rand)
15 : QEvent(Type(StateSwitchType)),
16 m_rand(rand)
17 {
18 }
19
20 enum { StateSwitchType = QEvent::User + 256 };
21
22 int rand() const { return m_rand; }
23
24private:
25 int m_rand;
26};
27//![15]
28
29//![16]
30class QGraphicsRectWidget : public QGraphicsWidget
31{
32public:
33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *,
34 QWidget *)
35 {
36 painter->fillRect(rect(), Qt::blue);
37 }
38};
39//![16]
40
41class StateSwitchTransition: public QAbstractTransition
42{
43public:
44 StateSwitchTransition(int rand)
45 : QAbstractTransition(),
46 m_rand(rand)
47 {
48 }
49
50protected:
51//![14]
52 virtual bool eventTest(QEvent *event)
53 {
54 return (event->type() == QEvent::Type(StateSwitchEvent::StateSwitchType))
55 && (static_cast<StateSwitchEvent *>(event)->rand() == m_rand);
56 }
57//![14]
58
59 virtual void onTransition(QEvent *) {}
60
61private:
62 int m_rand;
63};
64
65//![10]
66class StateSwitcher : public QState
67{
68 Q_OBJECT
69public:
70 StateSwitcher(QStateMachine *machine)
71 : QState(machine), m_stateCount(0), m_lastIndex(0)
72 { }
73//![10]
74
75//![11]
76 virtual void onEntry(QEvent *)
77 {
78 int n;
79 while ((n = (qrand() % m_stateCount + 1)) == m_lastIndex)
80 { }
81 m_lastIndex = n;
82 machine()->postEvent(new StateSwitchEvent(n));
83 }
84 virtual void onExit(QEvent *) {}
85//![11]
86
87//![12]
88 void addState(QState *state, QAbstractAnimation *animation) {
89 StateSwitchTransition *trans = new StateSwitchTransition(++m_stateCount);
90 trans->setTargetState(state);
91 addTransition(trans);
92 trans->addAnimation(animation);
93 }
94//![12]
95
96private:
97 int m_stateCount;
98 int m_lastIndex;
99};
100
101//![13]
102QState *createGeometryState(QObject *w1, const QRect &rect1,
103 QObject *w2, const QRect &rect2,
104 QObject *w3, const QRect &rect3,
105 QObject *w4, const QRect &rect4,
106 QState *parent)
107{
108 QState *result = new QState(parent);
109 result->assignProperty(w1, "geometry", rect1);
110 result->assignProperty(w2, "geometry", rect2);
111 result->assignProperty(w3, "geometry", rect3);
112 result->assignProperty(w4, "geometry", rect4);
113
114 return result;
115}
116//![13]
117
118int main(int argc, char **argv) 6int main(int argc, char **argv)
119{ 7{
120 QApplication app(argc, argv); 8 QApplication app(argc, argv);
121 9
122#if 0
123 QWidget window;
124 QPalette palette;
125 palette.setBrush(QPalette::Window, Qt::black);
126 window.setPalette(palette);
127
128 QPushButton *button1 = new QPushButton("A", &window);
129 button1->setObjectName("button1");
130
131 QPushButton *button2 = new QPushButton("B", &window);
132 button2->setObjectName("button2");
133
134 QPushButton *button3 = new QPushButton("C", &window);
135 button3->setObjectName("button3");
136
137 QPushButton *button4 = new QPushButton("D", &window);
138 button4->setObjectName("button4");
139
140#else
141//![1]
142 //QGraphicsRectWidget *button1 = new QGraphicsRectWidget;
143 QGraphicsRectWidget *button2 = new QGraphicsRectWidget;
144 QGraphicsRectWidget *button3 = new QGraphicsRectWidget;
145 QGraphicsRectWidget *button4 = new QGraphicsRectWidget;
146 button2->setZValue(1);
147 button3->setZValue(2);
148 button4->setZValue(3);
149
150 //QPixmap pixmap(":/pacman10-hp-sprite-2");
151
152 QGraphicsScene scene(0, 0, 300, 300); 10 QGraphicsScene scene(0, 0, 300, 300);
153 scene.setBackgroundBrush(Qt::black); 11 scene.setBackgroundBrush(Qt::black);
154 12
155 PixmapItem *button1 = new PixmapItem("google-pacman-sprite", &scene); 13 Actor *actor1 = new Actor(Actor::Player1);
156 button1->setSprite(2, 2, 16, 16); 14 scene.addItem(actor1);
157 15
158 //scene.addItem(button2);
159 //scene.addItem(button3);
160 //scene.addItem(button4);
161//![1]
162 QGraphicsView window(&scene); 16 QGraphicsView window(&scene);
163 window.setFrameStyle(0); 17 window.setFrameStyle(0);
164 window.setAlignment(Qt::AlignLeft | Qt::AlignTop); 18 window.setAlignment(Qt::AlignLeft | Qt::AlignTop);
165 window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 19 window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
166 window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 20 window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
167#endif
168
169#if 0
170//![2]
171 QStateMachine machine;
172
173 QState *group = new QState();
174 group->setObjectName("group");
175 QTimer timer;
176 timer.setInterval(1250);
177 timer.setSingleShot(true);
178 QObject::connect(group, SIGNAL(entered()), &timer, SLOT(start()));
179//![2]
180
181//![3]
182 QState *state1;
183 QState *state2;
184 QState *state3;
185 QState *state4;
186 QState *state5;
187 QState *state6;
188 QState *state7;
189
190 state1 = createGeometryState(button1, QRect(100, 0, 50, 50),
191 button2, QRect(150, 0, 50, 50),
192 button3, QRect(200, 0, 50, 50),
193 button4, QRect(250, 0, 50, 50),
194 group);
195//![3]
196 state2 = createGeometryState(button1, QRect(250, 100, 50, 50),
197 button2, QRect(250, 150, 50, 50),
198 button3, QRect(250, 200, 50, 50),
199 button4, QRect(250, 250, 50, 50),
200 group);
201 state3 = createGeometryState(button1, QRect(150, 250, 50, 50),
202 button2, QRect(100, 250, 50, 50),
203 button3, QRect(50, 250, 50, 50),
204 button4, QRect(0, 250, 50, 50),
205 group);
206 state4 = createGeometryState(button1, QRect(0, 150, 50, 50),
207 button2, QRect(0, 100, 50, 50),
208 button3, QRect(0, 50, 50, 50),
209 button4, QRect(0, 0, 50, 50),
210 group);
211 state5 = createGeometryState(button1, QRect(100, 100, 50, 50),
212 button2, QRect(150, 100, 50, 50),
213 button3, QRect(100, 150, 50, 50),
214 button4, QRect(150, 150, 50, 50),
215 group);
216 state6 = createGeometryState(button1, QRect(50, 50, 50, 50),
217 button2, QRect(200, 50, 50, 50),
218 button3, QRect(50, 200, 50, 50),
219 button4, QRect(200, 200, 50, 50),
220 group);
221//![4]
222 state7 = createGeometryState(button1, QRect(0, 0, 50, 50),
223 button2, QRect(250, 0, 50, 50),
224 button3, QRect(0, 250, 50, 50),
225 button4, QRect(250, 250, 50, 50),
226 group);
227 group->setInitialState(state1);
228//![4]
229
230//![5]
231 QParallelAnimationGroup animationGroup;
232 QSequentialAnimationGroup *subGroup;
233
234 QPropertyAnimation *anim = new QPropertyAnimation(button4, "geometry");
235 anim->setDuration(1000);
236 anim->setEasingCurve(QEasingCurve::OutElastic);
237 animationGroup.addAnimation(anim);
238//![5]
239
240//![6]
241 subGroup = new QSequentialAnimationGroup(&animationGroup);
242 subGroup->addPause(100);
243 anim = new QPropertyAnimation(button3, "geometry");
244 anim->setDuration(1000);
245 anim->setEasingCurve(QEasingCurve::OutElastic);
246 subGroup->addAnimation(anim);
247//![6]
248
249 subGroup = new QSequentialAnimationGroup(&animationGroup);
250 subGroup->addPause(150);
251 anim = new QPropertyAnimation(button2, "geometry");
252 anim->setDuration(1000);
253 anim->setEasingCurve(QEasingCurve::OutElastic);
254 subGroup->addAnimation(anim);
255
256 subGroup = new QSequentialAnimationGroup(&animationGroup);
257 subGroup->addPause(200);
258 anim = new QPropertyAnimation(button1, "geometry");
259 anim->setDuration(1000);
260 anim->setEasingCurve(QEasingCurve::OutElastic);
261 subGroup->addAnimation(anim);
262
263//![7]
264 StateSwitcher *stateSwitcher = new StateSwitcher(&machine);
265 stateSwitcher->setObjectName("stateSwitcher");
266 group->addTransition(&timer, SIGNAL(timeout()), stateSwitcher);
267 stateSwitcher->addState(state1, &animationGroup);
268 stateSwitcher->addState(state2, &animationGroup);
269//![7]
270 stateSwitcher->addState(state3, &animationGroup);
271 stateSwitcher->addState(state4, &animationGroup);
272 stateSwitcher->addState(state5, &animationGroup);
273 stateSwitcher->addState(state6, &animationGroup);
274//![8]
275 stateSwitcher->addState(state7, &animationGroup);
276//![8]
277
278//![9]
279 machine.addState(group);
280 machine.setInitialState(group);
281 machine.start();
282//![9]
283#endif
284 21
285 window.resize(300, 300); 22 window.resize(300, 300);
286 window.show(); 23 window.show();