Diffstat (limited to 'noncore/games/kpacman/painter.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/kpacman/painter.cpp | 972 |
1 files changed, 972 insertions, 0 deletions
diff --git a/noncore/games/kpacman/painter.cpp b/noncore/games/kpacman/painter.cpp new file mode 100644 index 0000000..16fed55 --- a/dev/null +++ b/noncore/games/kpacman/painter.cpp | |||
@@ -0,0 +1,972 @@ | |||
1 | |||
2 | #include "portable.h" | ||
3 | |||
4 | #if defined( KDE2_PORT ) | ||
5 | #include <kapp.h> | ||
6 | #include <kconfig.h> | ||
7 | #include <kstddirs.h> | ||
8 | #elif defined( QPE_PORT ) | ||
9 | #include <qpe/qpeapplication.h> | ||
10 | #include "config.h" | ||
11 | #endif | ||
12 | |||
13 | #include <qcolor.h> | ||
14 | #include <qpainter.h> | ||
15 | #include <qpixmap.h> | ||
16 | #include <qbitmap.h> | ||
17 | #include <qrect.h> | ||
18 | #include <qstring.h> | ||
19 | |||
20 | #include <qmessagebox.h> | ||
21 | #include <qfileinfo.h> | ||
22 | |||
23 | #include "painter.h" | ||
24 | #include "board.h" | ||
25 | |||
26 | Painter::Painter( Board *b, QWidget *parent, int Scheme, int Mode, Bitfont *font) | ||
27 | { | ||
28 | w = parent; | ||
29 | board = b; | ||
30 | |||
31 | pointPix = NULL; | ||
32 | wallPix = NULL; | ||
33 | prisonPix = NULL; | ||
34 | energizerPix = NULL; | ||
35 | fruitPix = NULL; | ||
36 | pacmanPix = NULL; | ||
37 | dyingPix = NULL; | ||
38 | eyesPix = NULL; | ||
39 | monsterPix = NULL; | ||
40 | fruitScorePix = NULL; | ||
41 | monsterScorePix = NULL; | ||
42 | |||
43 | lastPointPixmapName = ""; | ||
44 | lastWallPixmapName = ""; | ||
45 | lastPrisonPixmapName = ""; | ||
46 | lastEnergizerPixmapName = ""; | ||
47 | lastFruitPixmapName = ""; | ||
48 | lastPacmanPixmapName = ""; | ||
49 | lastDyingPixmapName = ""; | ||
50 | lastEyesPixmapName = ""; | ||
51 | lastMonsterPixmapName = ""; | ||
52 | lastFruitScorePixmapName = ""; | ||
53 | lastMonsterScorePixmapName = ""; | ||
54 | |||
55 | bitfont = font; | ||
56 | |||
57 | scheme = Scheme; | ||
58 | mode = Mode; | ||
59 | level = 0; | ||
60 | |||
61 | confScheme(); | ||
62 | } | ||
63 | |||
64 | QList<QPixmap> *Painter::loadPixmap(QWidget*, QString pixmapName, | ||
65 | QList<QPixmap> *pixmaps) | ||
66 | { | ||
67 | if (pixmaps == NULL) { | ||
68 | pixmaps = new QList<QPixmap>; | ||
69 | pixmaps->setAutoDelete(TRUE); | ||
70 | } | ||
71 | |||
72 | if (!pixmaps->isEmpty()) | ||
73 | pixmaps->clear(); | ||
74 | |||
75 | QPixmap PIXMAP(pixmapName); | ||
76 | if (PIXMAP.isNull() || PIXMAP.mask() == NULL) { | ||
77 | QString msg = i18n("The pixmap could not be contructed.\n\n" | ||
78 | "The file '@PIXMAPNAME@' does not exist,\n" | ||
79 | "or is of an unknown format."); | ||
80 | msg.replace(QRegExp("@PIXMAPNAME@"), pixmapName); | ||
81 | // QMessageBox::critical(parent, i18n("Initialization Error"), msg); | ||
82 | printf("%s\n", msg.data()); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | int height = PIXMAP.height(); | ||
87 | int width = (height == 0) ? 0 : PIXMAP.width()/(PIXMAP.width()/height); | ||
88 | |||
89 | QBitmap BITMAP; | ||
90 | QBitmap MASK; | ||
91 | |||
92 | BITMAP = *PIXMAP.mask(); | ||
93 | MASK.resize(width, height); | ||
94 | |||
95 | for (int x = 0; x < PIXMAP.width()/width; x++) { | ||
96 | QPixmap *pixmap = new QPixmap(width, height); | ||
97 | pixmaps->append(pixmap); | ||
98 | bitBlt(pixmap, 0, 0, &PIXMAP, x*width, 0, width, height, QPixmap::CopyROP, TRUE); | ||
99 | bitBlt(&MASK, 0, 0, &BITMAP, x*width, 0, width, height, QPixmap::CopyROP, TRUE); | ||
100 | pixmap->setMask(MASK); | ||
101 | } | ||
102 | |||
103 | return pixmaps; | ||
104 | } | ||
105 | |||
106 | QList<QPixmap> *Painter::textPixmap(QStrList &str, QList<QPixmap> *pixmaps, | ||
107 | QColor fg, QColor bg) | ||
108 | { | ||
109 | if (pixmaps == NULL) { | ||
110 | pixmaps = new QList<QPixmap>; | ||
111 | pixmaps->setAutoDelete(TRUE); | ||
112 | } | ||
113 | |||
114 | if (!pixmaps->isEmpty()) | ||
115 | pixmaps->clear(); | ||
116 | |||
117 | for (uint s = 0; s < str.count(); s++) { | ||
118 | QPixmap *pixmap = new QPixmap(bitfont->text(str.at(s), fg, bg)); | ||
119 | pixmaps->append(pixmap); | ||
120 | } | ||
121 | |||
122 | return pixmaps; | ||
123 | } | ||
124 | |||
125 | QList<QPixmap> *Painter::textPixmap(QString str, QList<QPixmap> *pixmaps, | ||
126 | QColor fg, QColor bg) | ||
127 | { | ||
128 | if (pixmaps == NULL) { | ||
129 | pixmaps = new QList<QPixmap>; | ||
130 | pixmaps->setAutoDelete(TRUE); | ||
131 | } | ||
132 | |||
133 | if (!pixmaps->isEmpty()) | ||
134 | pixmaps->clear(); | ||
135 | |||
136 | QPixmap *pixmap = new QPixmap(bitfont->text(str, fg, bg)); | ||
137 | pixmaps->append(pixmap); | ||
138 | |||
139 | return pixmaps; | ||
140 | } | ||
141 | |||
142 | /* Return the point of the upperleft pixel of the block representing that position | ||
143 | * on the board. | ||
144 | */ | ||
145 | QPoint Painter::point(int pos) | ||
146 | { | ||
147 | return QPoint((board->x(pos)-1)*BlockWidth, (board->y(pos)-1)*BlockHeight); | ||
148 | } | ||
149 | |||
150 | |||
151 | QRect Painter::rect(int pos, PixMap pix, uint i) | ||
152 | { | ||
153 | if (pos == OUT) | ||
154 | return QRect(); | ||
155 | |||
156 | QPixmap *PIXMAP = NULL; | ||
157 | switch (pix) { | ||
158 | case PacmanPix : PIXMAP = pacmanPix-> | ||
159 | at(checkRange(i, pacmanPix->count()-1)); | ||
160 | break; | ||
161 | case DyingPix : PIXMAP = dyingPix-> | ||
162 | at(checkRange(i, dyingPix->count()-1)); | ||
163 | break; | ||
164 | case MonsterPix : PIXMAP = monsterPix-> | ||
165 | at(checkRange(i, monsterPix->count()-1)); | ||
166 | break; | ||
167 | case EyesPix : PIXMAP = eyesPix-> | ||
168 | at(checkRange(i, eyesPix->count()-1)); | ||
169 | break; | ||
170 | case FruitPix : PIXMAP = fruitPix-> | ||
171 | at(checkRange(i, fruitPix->count()-1)); | ||
172 | break; | ||
173 | case PointPix : PIXMAP = pointPix-> | ||
174 | at(checkRange(i, pointPix->count()-1)); | ||
175 | break; | ||
176 | case EnergizerPix : PIXMAP = energizerPix-> | ||
177 | at(checkRange(i, energizerPix->count()-1)); | ||
178 | break; | ||
179 | case FruitScorePix : PIXMAP = fruitScorePix-> | ||
180 | at(checkRange(i, fruitScorePix->count()-1)); | ||
181 | break; | ||
182 | case MonsterScorePix : PIXMAP = monsterScorePix-> | ||
183 | at(checkRange(i,monsterScorePix->count()-1)); | ||
184 | break; | ||
185 | default : PIXMAP = wallPix-> | ||
186 | at(checkRange(i, wallPix->count()-1)); | ||
187 | } | ||
188 | if (PIXMAP == NULL) | ||
189 | return QRect(); | ||
190 | |||
191 | QRect rect = PIXMAP->rect(); | ||
192 | QPoint point = this->point(pos); | ||
193 | rect.moveCenter(QPoint(point.x()-1, point.y()-1)); | ||
194 | |||
195 | return rect; | ||
196 | } | ||
197 | |||
198 | QRect Painter::rect(int pos, QString str, int align) | ||
199 | { | ||
200 | if (pos == OUT) // return an empty rect if the position | ||
201 | return QRect(); // is invalid | ||
202 | QPoint point = this->point(pos); | ||
203 | QRect rect = bitfont->rect(str); | ||
204 | |||
205 | rect.moveCenter(QPoint(point.x()-1, point.y()-1)); | ||
206 | |||
207 | int dx = 0; | ||
208 | int dy = 0; | ||
209 | |||
210 | if (align & QLabel::AlignLeft || align & QLabel::AlignRight) { | ||
211 | dx = (str.length()-1) * (bitfont->width()/2); | ||
212 | if (align & QLabel::AlignRight) | ||
213 | dx *= -1; | ||
214 | } | ||
215 | |||
216 | if (align & QLabel::AlignTop || align & QLabel::AlignBottom) { | ||
217 | dy = bitfont->height()/2; | ||
218 | if (align & QLabel::AlignBottom) | ||
219 | dy *= -1; | ||
220 | } | ||
221 | |||
222 | if (dx != 0 || dy != 0) | ||
223 | rect.moveBy(dx, dy); | ||
224 | |||
225 | return rect; | ||
226 | } | ||
227 | |||
228 | QRect Painter::rect(QRect r1, QRect r2) | ||
229 | { | ||
230 | QRect rect; | ||
231 | rect.setLeft(r1.left() < r2.left() ? r1.left() : r2.left()); | ||
232 | rect.setTop(r1.top() < r2.top() ? r1.top() : r2.top()); | ||
233 | rect.setRight(r1.right() > r2.right() ? r1.right() : r2.right()); | ||
234 | rect.setBottom(r1.bottom() > r2.bottom() ? r1.bottom() : r2.bottom()); | ||
235 | |||
236 | return rect; | ||
237 | } | ||
238 | |||
239 | void Painter::erase(int pos, PixMap pix, uint i) | ||
240 | { | ||
241 | if (pos == OUT) | ||
242 | return; | ||
243 | QRect rect = this->rect(pos, pix, i); | ||
244 | bitBlt(&roomPix, rect.x(), rect.y(), &backPix, | ||
245 | rect.x(), rect.y(), rect.width(), rect.height()); | ||
246 | } | ||
247 | |||
248 | int Painter::maxPixmaps(PixMap pix) | ||
249 | { | ||
250 | switch (pix) { | ||
251 | case WallPix : return (int) wallPix->count(); | ||
252 | case PrisonPix : return (int) prisonPix->count(); | ||
253 | case PointPix : return (int) pointPix->count(); | ||
254 | case EnergizerPix : return (int) energizerPix->count(); | ||
255 | case FruitPix : return (int) fruitPix->count(); | ||
256 | case PacmanPix : return (int) pacmanPix->count(); | ||
257 | case DyingPix : return (int) dyingPix->count(); | ||
258 | case EyesPix : return (int) eyesPix->count(); | ||
259 | case MonsterPix : return (int) monsterPix->count(); | ||
260 | case FruitScorePix : return (int) fruitScorePix->count(); | ||
261 | case MonsterScorePix : return (int) monsterScorePix->count(); | ||
262 | default : return 0; | ||
263 | } | ||
264 | } | ||
265 | |||
266 | void Painter::draw(QPoint point, DrawWidget where, QPixmap pix) | ||
267 | { | ||
268 | switch (where) { | ||
269 | case Widget : bitBlt(w, point.x(), point.y(), &pix); | ||
270 | break; | ||
271 | case RoomPix : bitBlt(&roomPix, point.x(), point.y(), &pix); | ||
272 | break; | ||
273 | case BackPix : bitBlt(&backPix, point.x(), point.y(), &pix); | ||
274 | break; | ||
275 | } | ||
276 | } | ||
277 | |||
278 | void Painter::draw(QRect rect, DrawWidget where, QPixmap pix) | ||
279 | { | ||
280 | draw(QPoint(rect.x(), rect.y()), where, pix); | ||
281 | } | ||
282 | |||
283 | void Painter::draw(int pos, DrawWidget where, PixMap pix, uint i) | ||
284 | { | ||
285 | QPixmap *PIXMAP = NULL; | ||
286 | switch (pix) { | ||
287 | case PacmanPix : PIXMAP = pacmanPix-> | ||
288 | at(checkRange(i, pacmanPix->count()-1)); | ||
289 | break; | ||
290 | case DyingPix : PIXMAP = dyingPix-> | ||
291 | at(checkRange(i, dyingPix->count()-1)); | ||
292 | break; | ||
293 | case MonsterPix : PIXMAP = monsterPix-> | ||
294 | at(checkRange(i, monsterPix->count()-1)); | ||
295 | break; | ||
296 | case EyesPix : PIXMAP = eyesPix-> | ||
297 | at(checkRange(i, eyesPix->count()-1)); | ||
298 | break; | ||
299 | case FruitPix : PIXMAP = fruitPix-> | ||
300 | at(checkRange(i, fruitPix->count()-1)); | ||
301 | break; | ||
302 | case EnergizerPix : PIXMAP = energizerPix-> | ||
303 | at(checkRange(i, energizerPix->count()-1)); | ||
304 | break; | ||
305 | case FruitScorePix : PIXMAP = fruitScorePix-> | ||
306 | at(checkRange(i, fruitScorePix->count()-1)); | ||
307 | break; | ||
308 | case MonsterScorePix : PIXMAP = monsterScorePix-> | ||
309 | at(checkRange(i,monsterScorePix->count()-1)); | ||
310 | break; | ||
311 | default : ; | ||
312 | } | ||
313 | |||
314 | if (PIXMAP == NULL) | ||
315 | return; | ||
316 | |||
317 | QRect rect = PIXMAP->rect(); | ||
318 | QPoint point = this->point(pos); | ||
319 | rect.moveCenter(QPoint(point.x()-1, point.y()-1)); | ||
320 | |||
321 | switch (where) { | ||
322 | case Widget : bitBlt(w, rect.x(), rect.y(), PIXMAP); | ||
323 | break; | ||
324 | case RoomPix : bitBlt(&roomPix, rect.x(), rect.y(), PIXMAP); | ||
325 | break; | ||
326 | case BackPix : bitBlt(&backPix, rect.x(), rect.y(), PIXMAP); | ||
327 | break; | ||
328 | } | ||
329 | } | ||
330 | |||
331 | QPixmap Painter::draw(int pos, DrawWidget where, | ||
332 | QString str, QColor fg, QColor bg, int align) | ||
333 | { | ||
334 | QPixmap TEXT = bitfont->text(str, fg, bg); | ||
335 | |||
336 | QRect rect = this->rect(pos, str, align); | ||
337 | QPixmap SAVE = QPixmap(rect.width(), rect.height()); | ||
338 | |||
339 | switch (where) { | ||
340 | case Widget : bitBlt(&SAVE, 0, 0, w, rect.x(), rect.y()); | ||
341 | bitBlt(w, rect.x(), rect.y(), &TEXT); | ||
342 | break; | ||
343 | case RoomPix : bitBlt(&SAVE, 0, 0, &roomPix, rect.x(), rect.y()); | ||
344 | bitBlt(&roomPix, rect.x(), rect.y(), &TEXT); | ||
345 | break; | ||
346 | case BackPix : bitBlt(&SAVE, 0, 0, &backPix, rect.x(), rect.y()); | ||
347 | bitBlt(&backPix, rect.x(), rect.y(), &TEXT); | ||
348 | break; | ||
349 | } | ||
350 | |||
351 | return SAVE; | ||
352 | } | ||
353 | |||
354 | QRect Painter::draw(int col, int row, DrawWidget where, | ||
355 | QString str, QColor fg, QColor bg, int align) | ||
356 | { | ||
357 | QPixmap TEXT = bitfont->text(str, fg, bg); | ||
358 | |||
359 | QRect rect = this->rect(row*BoardWidth+col, str, align); | ||
360 | draw(rect, where, TEXT); | ||
361 | |||
362 | return rect; | ||
363 | } | ||
364 | |||
365 | void Painter::initPixmaps() | ||
366 | { | ||
367 | if (lastPointPixmapName != pointPixmapName.at(level)) { | ||
368 | pointPix = loadPixmap(w, pointPixmapName.at(level), pointPix); | ||
369 | lastPointPixmapName = pointPixmapName.at(level); | ||
370 | } | ||
371 | if (lastPrisonPixmapName != prisonPixmapName.at(level)) { | ||
372 | prisonPix = loadPixmap(w, prisonPixmapName.at(level), prisonPix); | ||
373 | lastPrisonPixmapName = prisonPixmapName.at(level); | ||
374 | } | ||
375 | if (lastEnergizerPixmapName != energizerPixmapName.at(level)) { | ||
376 | energizerPix = loadPixmap(w, energizerPixmapName.at(level), energizerPix); | ||
377 | lastEnergizerPixmapName = energizerPixmapName.at(level); | ||
378 | } | ||
379 | if (lastFruitPixmapName != fruitPixmapName.at(level)) { | ||
380 | fruitPix = loadPixmap(w, fruitPixmapName.at(level), fruitPix); | ||
381 | lastFruitPixmapName = fruitPixmapName.at(level); | ||
382 | } | ||
383 | if (lastPacmanPixmapName != pacmanPixmapName.at(level)) { | ||
384 | pacmanPix = loadPixmap(w, pacmanPixmapName.at(level), pacmanPix); | ||
385 | lastPacmanPixmapName = pacmanPixmapName.at(level); | ||
386 | } | ||
387 | if (lastDyingPixmapName != dyingPixmapName.at(level)) { | ||
388 | dyingPix = loadPixmap(w, dyingPixmapName.at(level), dyingPix); | ||
389 | lastDyingPixmapName = dyingPixmapName.at(level); | ||
390 | } | ||
391 | if (lastEyesPixmapName != eyesPixmapName.at(level)) { | ||
392 | eyesPix = loadPixmap(w, eyesPixmapName.at(level), eyesPix); | ||
393 | lastEyesPixmapName = eyesPixmapName.at(level); | ||
394 | } | ||
395 | if (lastMonsterPixmapName != monsterPixmapName.at(level)) { | ||
396 | monsterPix = loadPixmap(w, monsterPixmapName.at(level), monsterPix); | ||
397 | lastMonsterPixmapName = monsterPixmapName.at(level); | ||
398 | } | ||
399 | |||
400 | if (lastFruitScorePixmapName != fruitScorePixmapName.at(level) || | ||
401 | (const char *) *fruitScorePixmapName.at(level) == '\0') { | ||
402 | if ((const char *) *fruitScorePixmapName.at(level) == '\0') { | ||
403 | fruitScorePix = textPixmap(fruitScoreString, fruitScorePix, PINK); | ||
404 | } else { | ||
405 | fruitScorePix = loadPixmap(w, fruitScorePixmapName.at(level), | ||
406 | fruitScorePix); | ||
407 | lastFruitScorePixmapName = fruitScorePixmapName.at(level); | ||
408 | } | ||
409 | } | ||
410 | |||
411 | if (lastMonsterScorePixmapName != monsterScorePixmapName.at(level) || | ||
412 | (const char *) *monsterScorePixmapName.at(level) == '\0') { | ||
413 | if ((const char *) *monsterScorePixmapName.at(level) == '\0') { | ||
414 | monsterScorePix = textPixmap(monsterScoreString, monsterScorePix, CYAN); | ||
415 | } else { | ||
416 | monsterScorePix = loadPixmap(w, monsterScorePixmapName.at(level), | ||
417 | monsterScorePix); | ||
418 | lastMonsterScorePixmapName = monsterScorePixmapName.at(level); | ||
419 | } | ||
420 | } | ||
421 | |||
422 | if (lastWallPixmapName != wallPixmapName.at(level)) { | ||
423 | wallPix = loadPixmap(w, wallPixmapName.at(level), wallPix); | ||
424 | if (wallPix->isEmpty()) { | ||
425 | BlockWidth = 0; | ||
426 | BlockHeight = 0; | ||
427 | } else { | ||
428 | BlockWidth = wallPix->at(0)->width(); | ||
429 | BlockHeight = wallPix->at(0)->height(); | ||
430 | } | ||
431 | lastWallPixmapName = wallPixmapName.at(level); | ||
432 | } | ||
433 | } | ||
434 | |||
435 | void Painter::initbackPixmaps() | ||
436 | { | ||
437 | backgroundColor = BLACK; | ||
438 | |||
439 | backPix.resize((BoardWidth-3)*BlockWidth, (BoardHeight-3)*BlockHeight ); | ||
440 | backPix.fill(backgroundColor); | ||
441 | } | ||
442 | |||
443 | void Painter::initRoomPixmap() | ||
444 | { | ||
445 | roomPix.resize((BoardWidth-3)*BlockWidth, (BoardHeight-3)*BlockHeight ); | ||
446 | bitBlt(&roomPix,0,0, &backPix); | ||
447 | |||
448 | for (unsigned int x = 0; x < board->size(); x++) { | ||
449 | if (board->isBrick(x)) | ||
450 | drawBrick(x); | ||
451 | if (board->isPrison(x) || board->isGate(x)) | ||
452 | drawPrison(x); | ||
453 | if (board->isPoint(x)) | ||
454 | drawPoint(x); | ||
455 | } | ||
456 | } | ||
457 | |||
458 | void Painter::drawBrick(int pos) | ||
459 | { | ||
460 | int border = 0; | ||
461 | if (board->isBrick(board->move(pos, N ))) border |= (1 << 0); | ||
462 | if (board->isBrick(board->move(pos, NE))) border |= (1 << 1); | ||
463 | if (board->isBrick(board->move(pos, E ))) border |= (1 << 2); | ||
464 | if (board->isBrick(board->move(pos, SE))) border |= (1 << 3); | ||
465 | if (board->isBrick(board->move(pos, S ))) border |= (1 << 4); | ||
466 | if (board->isBrick(board->move(pos, SW))) border |= (1 << 5); | ||
467 | if (board->isBrick(board->move(pos, W ))) border |= (1 << 6); | ||
468 | if (board->isBrick(board->move(pos, NW))) border |= (1 << 7); | ||
469 | |||
470 | if (board->isOut(board->move(pos, N ))) border |= (1 << 8); | ||
471 | if (board->isOut(board->move(pos, NE))) border |= (1 << 9); | ||
472 | if (board->isOut(board->move(pos, E ))) border |= (1 << 10); | ||
473 | if (board->isOut(board->move(pos, SE))) border |= (1 << 11); | ||
474 | if (board->isOut(board->move(pos, S ))) border |= (1 << 12); | ||
475 | if (board->isOut(board->move(pos, SW))) border |= (1 << 13); | ||
476 | if (board->isOut(board->move(pos, W ))) border |= (1 << 14); | ||
477 | if (board->isOut(board->move(pos, NW))) border |= (1 << 15); | ||
478 | |||
479 | switch (border & 0xFF) { | ||
480 | case 31 : border = 0; break; | ||
481 | case 159 : border = 0; break; | ||
482 | case 63 : border = 0; break; | ||
483 | case 191 : border = 0; break; | ||
484 | case 241 : border = 1; break; | ||
485 | case 249 : border = 1; break; | ||
486 | case 243 : border = 1; break; | ||
487 | case 251 : border = 1; break; | ||
488 | case 124 : border = 2; break; | ||
489 | case 252 : border = 2; break; | ||
490 | case 126 : border = 2; break; | ||
491 | case 199 : border = 3; break; | ||
492 | case 231 : border = 3; break; | ||
493 | case 207 : border = 3; break; | ||
494 | case 28 : if ((border >> 8) > 0) | ||
495 | border = 24; | ||
496 | else | ||
497 | border = 4; | ||
498 | break; | ||
499 | case 112 : if ((border >> 8) > 0) | ||
500 | border = 27; | ||
501 | else | ||
502 | border = 5; | ||
503 | break; | ||
504 | case 7 : if ((border >> 8) > 0) | ||
505 | border = 25; | ||
506 | else | ||
507 | border = 6; | ||
508 | break; | ||
509 | case 193 : if ((border >> 8) > 0) | ||
510 | border = 26; | ||
511 | else | ||
512 | border = 7; | ||
513 | break; | ||
514 | case 247 : if ((border & (1 << 11)) > 0) | ||
515 | border = 23; | ||
516 | else | ||
517 | border = 8; | ||
518 | break; | ||
519 | case 119 : if ((border & (1 << 15)) > 0) | ||
520 | border = 8; | ||
521 | if ((border & (1 << 11)) > 0) | ||
522 | border = 11; | ||
523 | break; | ||
524 | case 223 : if ((border & (1 << 13)) > 0) | ||
525 | border = 20; | ||
526 | else | ||
527 | border = 9; | ||
528 | break; | ||
529 | case 221 : if ((border & (1 << 13)) > 0) | ||
530 | border = 10; | ||
531 | if ((border & (1 << 9)) > 0) | ||
532 | border = 9; | ||
533 | break; | ||
534 | case 253 : if ((border & (1 << 9)) > 0) | ||
535 | border = 21; | ||
536 | else | ||
537 | border = 10; | ||
538 | break; | ||
539 | case 127 : if ((border & (1 << 15)) > 0) | ||
540 | border = 22; | ||
541 | else | ||
542 | border = 11; | ||
543 | break; | ||
544 | case 30 : border = 12; break; | ||
545 | case 240 : border = 13; break; | ||
546 | case 15 : border = 14; break; | ||
547 | case 225 : border = 15; break; | ||
548 | case 135 : border = 16; break; | ||
549 | case 195 : border = 17; break; | ||
550 | case 60 : border = 18; break; | ||
551 | case 120 : border = 19; break; | ||
552 | case 255 : border = 28; break; | ||
553 | default : border = -1; | ||
554 | } | ||
555 | if (border != -1 && border < (int) wallPix->count()) { | ||
556 | QRect rect = this->rect(pos, WallPix); | ||
557 | bitBlt(&roomPix, rect.x(), rect.y(), wallPix->at((uint) border)); | ||
558 | } | ||
559 | } | ||
560 | |||
561 | void Painter::drawPrison(int pos) | ||
562 | { | ||
563 | int border = 0; | ||
564 | if (board->isPrison(board->move(pos, N ))) border |= (1 << 0); | ||
565 | if (board->isPrison(board->move(pos, NE))) border |= (1 << 1); | ||
566 | if (board->isPrison(board->move(pos, E ))) border |= (1 << 2); | ||
567 | if (board->isPrison(board->move(pos, SE))) border |= (1 << 3); | ||
568 | if (board->isPrison(board->move(pos, S ))) border |= (1 << 4); | ||
569 | if (board->isPrison(board->move(pos, SW))) border |= (1 << 5); | ||
570 | if (board->isPrison(board->move(pos, W ))) border |= (1 << 6); | ||
571 | if (board->isPrison(board->move(pos, NW))) border |= (1 << 7); | ||
572 | |||
573 | if (board->isGate(board->move(pos, N ))) border |= (1 << 8); | ||
574 | if (board->isGate(board->move(pos, NE))) border |= (1 << 9); | ||
575 | if (board->isGate(board->move(pos, E ))) border |= (1 << 10); | ||
576 | if (board->isGate(board->move(pos, SE))) border |= (1 << 11); | ||
577 | if (board->isGate(board->move(pos, S ))) border |= (1 << 12); | ||
578 | if (board->isGate(board->move(pos, SW))) border |= (1 << 13); | ||
579 | if (board->isGate(board->move(pos, W ))) border |= (1 << 14); | ||
580 | if (board->isGate(board->move(pos, NW))) border |= (1 << 15); | ||
581 | |||
582 | switch (border & 0xFF) { | ||
583 | case 31 : border = 0; break; | ||
584 | case 159 : border = 0; break; | ||
585 | case 63 : border = 0; break; | ||
586 | case 241 : border = 1; break; | ||
587 | case 249 : border = 1; break; | ||
588 | case 243 : border = 1; break; | ||
589 | case 124 : border = 2; break; | ||
590 | case 252 : border = 2; break; | ||
591 | case 126 : border = 2; break; | ||
592 | case 199 : border = 3; break; | ||
593 | case 231 : border = 3; break; | ||
594 | case 207 : border = 3; break; | ||
595 | case 28 : if ((border >> 8) != 0) | ||
596 | border = 12; | ||
597 | else | ||
598 | border = 4; | ||
599 | break; | ||
600 | case 112 : if ((border >> 8) != 0) | ||
601 | border = 13; | ||
602 | else | ||
603 | border = 5; | ||
604 | break; | ||
605 | case 7 : if ((border >> 8) != 0) | ||
606 | border = 14; | ||
607 | else | ||
608 | border = 6; | ||
609 | break; | ||
610 | case 193 : if ((border >> 8) != 0) | ||
611 | border = 15; | ||
612 | else | ||
613 | border = 7; | ||
614 | break; | ||
615 | case 247 : border = 8; break; | ||
616 | case 223 : border = 9; break; | ||
617 | case 253 : border = 10; break; | ||
618 | case 127 : border = 11; break; | ||
619 | default : border = -1; | ||
620 | } | ||
621 | if (board->isGate(pos)) { | ||
622 | if (board->isGate(board->move(pos, N))) | ||
623 | border = 17; | ||
624 | else | ||
625 | border = 16; | ||
626 | } | ||
627 | |||
628 | if (border != -1 && border < (int) prisonPix->count()) { | ||
629 | QRect rect = this->rect(pos, PrisonPix); | ||
630 | bitBlt(&roomPix, rect.x(), rect.y(), prisonPix->at((uint) border)); | ||
631 | } | ||
632 | } | ||
633 | |||
634 | void Painter::drawPoint(int pos) | ||
635 | { | ||
636 | if (!pointPix->isEmpty()) { | ||
637 | QRect rect = this->rect(pos, PointPix); | ||
638 | bitBlt(&roomPix, rect.x(), rect.y(), pointPix->at(0)); | ||
639 | } | ||
640 | } | ||
641 | |||
642 | QString Painter::decodeHexOctString(QString s) | ||
643 | { | ||
644 | QString value; | ||
645 | QString valids; | ||
646 | int pos, xpos = 0, opos = 0; | ||
647 | int v, len, leadin; | ||
648 | const char *ptr; | ||
649 | uchar c; | ||
650 | |||
651 | while (((xpos = s.find(QRegExp("\\\\x[0-9a-fA-F]+"), xpos)) != -1) || | ||
652 | ((opos = s.find(QRegExp("\\\\[0-7]+"), opos)) != -1)) { | ||
653 | if (xpos != -1) { | ||
654 | valids = "0123456789abcdef"; | ||
655 | leadin = 2; | ||
656 | pos = xpos; | ||
657 | } else { | ||
658 | valids = "01234567"; | ||
659 | leadin = 1; | ||
660 | pos = opos; | ||
661 | } | ||
662 | |||
663 | c = '\0'; | ||
664 | len = 0; | ||
665 | value = s.mid(pos+leadin, 3); | ||
666 | ptr = (const char *) value; | ||
667 | |||
668 | while (*ptr != '\0' && (v = valids.find(*ptr++, 0, FALSE)) != -1) { | ||
669 | c = (c * valids.length()) + v; | ||
670 | len++; | ||
671 | } | ||
672 | |||
673 | value.fill(c, 1); | ||
674 | s.replace(pos, len+leadin, value); | ||
675 | } | ||
676 | |||
677 | return s; | ||
678 | } | ||
679 | |||
680 | void Painter::fillScoreString(QStrList &list, QArray<int> &values) | ||
681 | { | ||
682 | if( !list.isEmpty()) | ||
683 | list.clear(); | ||
684 | |||
685 | QString s; | ||
686 | |||
687 | for (uint i = 0; i < values.size(); i++) { | ||
688 | |||
689 | if (values[i] < 10 || values[i] > 10000) | ||
690 | s = "?"; | ||
691 | else if (values[i] == 1600) | ||
692 | s = "\x1a\x1b"; | ||
693 | else if (values[i] < 100) { | ||
694 | s = "\x0e"; | ||
695 | s.insert(0, char (values[i] / 10 + 0x10)); | ||
696 | } else if (values[i] < 1000) { | ||
697 | s = "\x0f"; | ||
698 | s.insert(0, char (values[i] / 100 + 0x10)); | ||
699 | } else { | ||
700 | s = "\x0f\x10"; | ||
701 | s.insert(0, char (values[i] / 1000 + 0x10)); | ||
702 | } | ||
703 | |||
704 | list.append(s.data()); | ||
705 | } | ||
706 | } | ||
707 | |||
708 | void Painter::fillArray(QArray<int> &array, QString values, int max) | ||
709 | { | ||
710 | array.resize(max); | ||
711 | int last = 0; | ||
712 | bool ok; | ||
713 | QString value; | ||
714 | |||
715 | for (uint i = 0; i < array.size(); i++) { | ||
716 | if (values.find(',') < 0 && values.length() > 0) { | ||
717 | value = values; | ||
718 | values = ""; | ||
719 | } | ||
720 | if (values.find(',') >= 0) { | ||
721 | value = values.left(values.find(',')); | ||
722 | values.remove(0,values.find(',')+1); | ||
723 | } | ||
724 | array[i] = value.toInt(&ok); | ||
725 | if (ok) | ||
726 | last = array[i]; | ||
727 | else | ||
728 | array[i] = last; | ||
729 | } | ||
730 | } | ||
731 | |||
732 | void Painter::fillStrList(QStrList &list, QString values, int max) | ||
733 | { | ||
734 | if (!list.isEmpty()) | ||
735 | list.clear(); | ||
736 | |||
737 | QString last = ""; | ||
738 | QString value; | ||
739 | |||
740 | for (uint i = 0; i < (uint) max; i++) { | ||
741 | if (values.find(',') < 0 && values.length() > 0) { | ||
742 | value = values; | ||
743 | values = ""; | ||
744 | } | ||
745 | if (values.find(',') >= 0) { | ||
746 | value = values.left(values.find(',')); | ||
747 | values.remove(0,values.find(',')+1); | ||
748 | } | ||
749 | if (!value.isEmpty()) | ||
750 | last = decodeHexOctString(value); | ||
751 | list.append(last); | ||
752 | } | ||
753 | } | ||
754 | |||
755 | void Painter::fillPixmapName(QStrList &pixmapName) | ||
756 | { | ||
757 | QStrList list = pixmapName; | ||
758 | |||
759 | if (!pixmapName.isEmpty()) | ||
760 | pixmapName.clear(); | ||
761 | |||
762 | QString pixmap; | ||
763 | |||
764 | QFileInfo fileInfo; | ||
765 | |||
766 | for (uint i = 0; i < list.count(); i++) { | ||
767 | pixmap = list.at(i); | ||
768 | |||
769 | if (pixmap.left(1) != "/" && pixmap.left(1) != "~") | ||
770 | pixmap = FIND_APP_DATA( pixmapDirectory+pixmap ); | ||
771 | |||
772 | fileInfo.setFile(pixmap); | ||
773 | if (!fileInfo.isReadable() || !fileInfo.isFile()) | ||
774 | pixmap = ""; | ||
775 | |||
776 | pixmapName.append(pixmap); | ||
777 | } | ||
778 | } | ||
779 | |||
780 | void Painter::confLevels(bool defGroup) | ||
781 | { | ||
782 | APP_CONFIG_BEGIN( cfg ); | ||
783 | if (defGroup || cfg->hasKey("Levels")) | ||
784 | maxLevel = cfg->readNumEntry("Levels", 13); | ||
785 | APP_CONFIG_END( cfg ); | ||
786 | } | ||
787 | |||
788 | void Painter::confMisc(bool defGroup) | ||
789 | { | ||
790 | APP_CONFIG_BEGIN( cfg ); | ||
791 | if (defGroup || cfg->hasKey("PixmapDirectory")) { | ||
792 | pixmapDirectory = cfg->readEntry("PixmapDirectory"); | ||
793 | |||
794 | if (pixmapDirectory.left(1) != "/" && pixmapDirectory.left(1) != "~") | ||
795 | pixmapDirectory.insert(0, "pics/"); | ||
796 | if (pixmapDirectory.right(1) != "/") | ||
797 | pixmapDirectory.append("/"); | ||
798 | } | ||
799 | |||
800 | if (defGroup || cfg->hasKey("PointPixmapName")) | ||
801 | fillStrList(pointPixmapName, | ||
802 | cfg->readEntry("PointPixmapName", "point.xpm"), maxLevel+1); | ||
803 | if (defGroup || cfg->hasKey("WallPixmapName")) | ||
804 | fillStrList(wallPixmapName, | ||
805 | cfg->readEntry("WallPixmapName", "wall.xpm"), maxLevel+1); | ||
806 | if (defGroup || cfg->hasKey("PrisonPixmapName")) | ||
807 | fillStrList(prisonPixmapName, | ||
808 | cfg->readEntry("PrisonPixmapName", "prison.xpm"), maxLevel+1); | ||
809 | if (defGroup || cfg->hasKey("EnergizerPixmapName")) | ||
810 | fillStrList(energizerPixmapName, | ||
811 | cfg->readEntry("EnergizerPixmapName", "switch.xpm"),maxLevel+1); | ||
812 | if (defGroup || cfg->hasKey("FruitPixmapName")) | ||
813 | fillStrList(fruitPixmapName, | ||
814 | cfg->readEntry("FruitPixmapName", "fruit.xpm"), maxLevel+1); | ||
815 | if (defGroup || cfg->hasKey("PacmanPixmapName")) | ||
816 | fillStrList(pacmanPixmapName, | ||
817 | cfg->readEntry("PacmanPixmapName", "pacman.xpm"), maxLevel+1); | ||
818 | if (defGroup || cfg->hasKey("DyingPixmapName")) | ||
819 | fillStrList(dyingPixmapName, | ||
820 | cfg->readEntry("DyingPixmapName", "dying.xpm"), maxLevel+1); | ||
821 | if (defGroup || cfg->hasKey("EyesPixmapName")) | ||
822 | fillStrList(eyesPixmapName, | ||
823 | cfg->readEntry("EyesPixmapName", "eyes.xpm"), maxLevel+1); | ||
824 | if (defGroup || cfg->hasKey("MonsterPixmapName")) | ||
825 | fillStrList(monsterPixmapName, | ||
826 | cfg->readEntry("MonsterPixmapName", "monster.xpm"), maxLevel+1); | ||
827 | |||
828 | if (defGroup || cfg->hasKey("FruitScorePixmapName")) | ||
829 | fillStrList(fruitScorePixmapName, | ||
830 | cfg->readEntry("FruitScorePixmapName"), maxLevel+1); | ||
831 | if (defGroup || cfg->hasKey("MonsterScorePixmapName")) | ||
832 | fillStrList(monsterScorePixmapName, | ||
833 | cfg->readEntry("MonsterScorePixmapName"), maxLevel+1); | ||
834 | APP_CONFIG_END( cfg ); | ||
835 | } | ||
836 | |||
837 | void Painter::confScoring(bool defGroup) | ||
838 | { | ||
839 | APP_CONFIG_BEGIN( cfg ); | ||
840 | if (defGroup || cfg->hasKey("FruitScore")) | ||
841 | fillArray(fruitScore, | ||
842 | cfg->readEntry("FruitScore", | ||
843 | "100,300,500,,700,,1000,,2000,,3000,,5000"), | ||
844 | maxLevel+1); | ||
845 | if (defGroup || cfg->hasKey("MonsterScore")) | ||
846 | fillArray(monsterScore, | ||
847 | cfg->readEntry("MonsterScore", "200,400,800,1600"), 4); | ||
848 | |||
849 | if (defGroup || cfg->hasKey("FruitScoreString")) | ||
850 | fillStrList(fruitScoreString, | ||
851 | cfg->readEntry("FruitScoreString"), maxLevel+1); | ||
852 | if (defGroup || cfg->hasKey("MonsterScoreString")) | ||
853 | fillStrList(monsterScoreString, | ||
854 | cfg->readEntry("MonsterScoreString"), 4); | ||
855 | APP_CONFIG_END( cfg ); | ||
856 | } | ||
857 | |||
858 | void Painter::confScheme() | ||
859 | { | ||
860 | APP_CONFIG_BEGIN( cfg ); | ||
861 | SAVE_CONFIG_GROUP( cfg, oldgroup ); | ||
862 | QString newgroup; | ||
863 | |||
864 | // if not set, read mode and scheme from the configfile | ||
865 | if (mode == -1 && scheme == -1) { | ||
866 | scheme = cfg->readNumEntry("Scheme", -1); | ||
867 | mode = cfg->readNumEntry("Mode", -1); | ||
868 | |||
869 | // if mode is not set in the defGroup-group, lookup the scheme group | ||
870 | if (scheme != -1 || mode == -1) { | ||
871 | newgroup.sprintf("Scheme %d", scheme); | ||
872 | cfg->setGroup(newgroup); | ||
873 | |||
874 | mode = cfg->readNumEntry("Mode", -1); | ||
875 | RESTORE_CONFIG_GROUP( cfg, oldgroup ); | ||
876 | } | ||
877 | } | ||
878 | |||
879 | confLevels(); | ||
880 | |||
881 | if (mode != -1) { | ||
882 | newgroup.sprintf("Mode %d", mode); | ||
883 | cfg->setGroup(newgroup); | ||
884 | |||
885 | confLevels(FALSE); | ||
886 | } | ||
887 | |||
888 | if (scheme != -1) { | ||
889 | newgroup.sprintf("Scheme %d", scheme); | ||
890 | cfg->setGroup(newgroup); | ||
891 | |||
892 | confLevels(FALSE); | ||
893 | } | ||
894 | |||
895 | RESTORE_CONFIG_GROUP( cfg, oldgroup ); | ||
896 | |||
897 | confMisc(); | ||
898 | confScoring(); | ||
899 | |||
900 | if (mode != -1) { | ||
901 | newgroup.sprintf("Mode %d", mode); | ||
902 | cfg->setGroup(newgroup); | ||
903 | |||
904 | confMisc(FALSE); | ||
905 | confScoring(FALSE); | ||
906 | } | ||
907 | |||
908 | if (scheme != -1) { | ||
909 | newgroup.sprintf("Scheme %d", scheme); | ||
910 | cfg->setGroup(newgroup); | ||
911 | |||
912 | confMisc(FALSE); | ||
913 | confScoring(FALSE); | ||
914 | } | ||
915 | |||
916 | if ((const char *) *fruitScoreString.at(0) == '\0') | ||
917 | fillScoreString(fruitScoreString, fruitScore); | ||
918 | if ((const char *) *monsterScoreString.at(0) == '\0') | ||
919 | fillScoreString(monsterScoreString, monsterScore); | ||
920 | |||
921 | fillPixmapName(pointPixmapName); | ||
922 | fillPixmapName(wallPixmapName); | ||
923 | fillPixmapName(prisonPixmapName); | ||
924 | fillPixmapName(energizerPixmapName); | ||
925 | fillPixmapName(fruitPixmapName); | ||
926 | fillPixmapName(pacmanPixmapName); | ||
927 | fillPixmapName(dyingPixmapName); | ||
928 | fillPixmapName(eyesPixmapName); | ||
929 | fillPixmapName(monsterPixmapName); | ||
930 | fillPixmapName(fruitScorePixmapName); | ||
931 | fillPixmapName(monsterScorePixmapName); | ||
932 | |||
933 | initPixmaps(); | ||
934 | initbackPixmaps(); | ||
935 | initRoomPixmap(); | ||
936 | |||
937 | RESTORE_CONFIG_GROUP( cfg, oldgroup ); | ||
938 | APP_CONFIG_END( cfg ); | ||
939 | } | ||
940 | |||
941 | void Painter::setScheme(int Scheme, int Mode, Bitfont *font) | ||
942 | { | ||
943 | bitfont = font; | ||
944 | |||
945 | mode = Mode; | ||
946 | scheme = Scheme; | ||
947 | |||
948 | confScheme(); | ||
949 | } | ||
950 | |||
951 | void Painter::setLevel(int Level) | ||
952 | { | ||
953 | level = Level; | ||
954 | |||
955 | initPixmaps(); | ||
956 | initbackPixmaps(); | ||
957 | initRoomPixmap(); | ||
958 | } | ||
959 | |||
960 | int Painter::checkRange(int value, int max, int min) | ||
961 | { | ||
962 | if (value < min) { | ||
963 | printf("Painter::checkRange (value = %d, max = %d, min = %d)\n", | ||
964 | value, max, min); | ||
965 | return min; | ||
966 | } else if (value > max) { | ||
967 | printf("Painter::checkRange (value = %d, max = %d, min = %d)\n", | ||
968 | value, max, min); | ||
969 | return max; | ||
970 | } else | ||
971 | return value; | ||
972 | } | ||