summaryrefslogtreecommitdiff
path: root/noncore/games/zsame/StoneWidget.cpp
Unidiff
Diffstat (limited to 'noncore/games/zsame/StoneWidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/zsame/StoneWidget.cpp350
1 files changed, 350 insertions, 0 deletions
diff --git a/noncore/games/zsame/StoneWidget.cpp b/noncore/games/zsame/StoneWidget.cpp
new file mode 100644
index 0000000..49fa1a4
--- a/dev/null
+++ b/noncore/games/zsame/StoneWidget.cpp
@@ -0,0 +1,350 @@
1/*
2 * ksame 0.4 - simple Game
3 * Copyright (C) 1997,1998 Marcus Kreutzberger
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <qpainter.h>
25#include <qpixmap.h>
26#include <qbitmap.h>
27#include <qimage.h>
28#include <qcursor.h>
29
30
31#include <qpe/resource.h>
32
33#include <time.h>
34#include <assert.h>
35
36#include "StoneWidget.h"
37
38
39
40struct StoneSlice {
41 QPixmap stone;
42};
43
44
45StoneWidget::StoneWidget( QWidget *parent, int x, int y )
46 : QWidget(parent,"StoneWidget"), stonefield(x,y)
47{
48// setBackgroundPixmap(QPixmap(locate("wallpaper", "Time-For-Lunch-2.jpg")));
49// QPixmap stonemap(locate("appdata", "stones.png"));
50
51 QPixmap stonemap = Resource::loadPixmap("zsame/stones" );
52 assert(!stonemap.isNull());
53
54 slice=0;
55 maxslices=30;
56 maxcolors=4;
57
58 sizex=x;
59 sizey=y;
60
61 stone_width=stonemap.width()/(maxslices+1);
62 stone_height=stonemap.height()/maxcolors;
63
64 map = new StoneSlice*[maxcolors];
65 QBitmap mask;
66 for (int c = 0; c < maxcolors; c++) {
67 map[c] = new StoneSlice[maxslices];
68
69 for (int s = 0; s < maxslices; s++) {
70 map[c][s].stone.resize(stone_width, stone_height);
71 assert(!map[c][s].stone.isNull());
72 bitBlt(&map[c][s].stone, 0, 0,
73 &stonemap, stone_width * s,
74 c*stone_height,
75 stone_width,stone_height,CopyROP,false);
76 QImage im = map[c][s].stone.convertToImage();
77 mask = im.createHeuristicMask();
78 map[c][s].stone.setMask(mask);
79 }
80 }
81
82 field_height=stone_height*sizey;
83 field_width=stone_width*sizex;
84
85 setMouseTracking(true);
86
87 // QColor c(115,115,115);
88 // setBackgroundColor(c);
89
90 // emit s_sizechanged();
91 startTimer( 100 );
92 history.setAutoDelete(true);
93}
94
95StoneWidget::~StoneWidget() {
96 for (int c = 0; c < maxcolors; c++) {
97 delete [] map[c];
98 }
99 delete [] map;
100
101 setMouseTracking(false);
102 killTimers();
103}
104
105unsigned int
106StoneWidget::board() {
107 return stonefield.getBoard();
108}
109
110int
111StoneWidget::score() {
112 return stonefield.getScore();
113}
114
115int
116StoneWidget::marked() {
117 return stonefield.getMarked();
118}
119
120QSize
121StoneWidget::size() {
122 return QSize(sizex,sizey);
123}
124
125int
126StoneWidget::colors() {
127 return stonefield.getColors();
128}
129
130QSize
131StoneWidget::sizeHint () const {
132 return QSize(field_width,field_height);
133}
134
135void
136StoneWidget::newGame(unsigned int board,int colors) {
137 stonefield.newGame(board,colors);
138 history.clear();
139 modified= false;
140 emit s_newgame();
141 emit s_colors(stonefield.getColors());
142 emit s_board(stonefield.getBoard());
143}
144
145void
146StoneWidget::reset() {
147 stonefield.reset();
148 history.clear();
149 emit s_newgame();
150}
151
152void
153StoneWidget::unmark() {
154 stonefield.unmark();
155 emit s_marked(0);
156}
157
158bool StoneWidget::undoPossible() const {
159 if (stonefield.isGameover()) return false;
160 return stonefield.undoPossible();
161}
162
163int
164StoneWidget::undo(int count) {
165 if (stonefield.isGameover()) return 0;
166
167 int ret_val=stonefield.undo(count);
168
169 QPoint p=mapFromGlobal(cursor().pos());
170 int x=p.x();
171 int y=p.y();
172 if (x<0||y<0||x>=field_width||y>=field_height) {
173 emit s_score(stonefield.getMarked());
174 return ret_val;
175 }
176
177 int marked=stonefield.mark(x/stone_width,y/stone_height);
178 emit s_marked(marked);
179 slice=0;
180 emit s_score(stonefield.getScore());
181 modified= (stonefield.getScore()>0);
182 return ret_val;
183}
184
185bool StoneWidget::isGameover() {
186 return stonefield.isGameover();
187}
188
189bool StoneWidget::hasBonus() {
190 return stonefield.gotBonus(); // don't ask me why the names differ... ;-| [hlm]
191}
192
193bool StoneWidget::isOriginalBoard() {
194 return !modified;
195}
196
197void StoneWidget::readProperties(Config *) {
198/* Q_ASSERT(conf);
199
200 history.clear();
201
202 if (!conf->hasKey("Board")||
203 !conf->hasKey("Colors")||
204 !conf->hasKey("Stones")) {
205 return;
206 }
207 newGame(conf->readNumEntry("Board"),conf->readNumEntry("Colors"));
208
209 QStrList list;
210 conf->readListEntry("Stones",list);
211
212 for (const char *item=list.first();item;item=list.next()) {
213 int x=-1,y=-1;
214 if (sscanf(item,"%02X%02X",&x,&y)!=2) break;
215 history.append(new QPoint(x,y));
216 stonefield.remove(x,y);
217 }
218*/
219}
220
221
222void
223StoneWidget::saveProperties(Config *) {
224/*
225 Q_ASSERT(conf);
226
227 QStrList list(true);
228 QString tmp;
229
230 for (QPoint *item=history.first();item;item=history.next()) {
231 tmp.sprintf("%02X%02X",item->x(),item->y());
232 list.append(tmp.ascii());
233 }
234
235 conf->writeEntry("Stones",list);
236 conf->writeEntry("Board",stonefield.getBoard());
237 conf->writeEntry("Colors",stonefield.getColors());
238*/
239}
240
241void
242StoneWidget::timerEvent( QTimerEvent * ) {
243 QPoint p=mapFromGlobal(cursor().pos());
244 int x=p.x();
245 int y=p.y();
246 if (x<0||y<0||x>=field_width||y>=field_height)
247 stonefield.unmark();
248 slice=(slice+1)%maxslices;
249 paintEvent(0);
250}
251
252void
253StoneWidget::paintEvent( QPaintEvent *e ) {
254
255 Stone *stone=stonefield.getField();
256
257 for (int y=0;y<sizey;y++) {
258 int cy = y * stone_height;
259
260 for (int x=0;x<sizex;x++) {
261 int cx = stone_width * x;
262
263 bool redraw=stone->marked||stone->changed;
264
265 if (!redraw&&e) {
266 QRect r(cx,cy,stone_width,stone_height);
267 redraw=r.intersects(e->rect());
268 }
269 if (redraw) {
270 stone->changed=false;
271 if (stone->color) {
272
273 int tslice = stone->marked?slice:0;
274 bitBlt(this,cx,cy,
275 &map[stone->color-1][tslice].stone,
276 0, 0,
277 stone_width,stone_height,CopyROP,FALSE);
278
279 } else {
280 erase(cx, cy, stone_width, stone_height);
281 }
282 }
283 stone++; // naechster Stein.
284 }
285 }
286}
287
288void
289StoneWidget::mousePressEvent ( QMouseEvent *e) {
290
291 if (stonefield.isGameover()) return;
292
293 int x=e->pos().x();
294 int y=e->pos().y();
295 if (x<0||y<0||x>=field_width||y>=field_height) return;
296
297 int sx=x/stone_width;
298 int sy=y/stone_height;
299
300 int mar =stonefield.mark(sx, sy);
301
302 if ( mar != -1 ) {
303 myMoveEvent(e);
304 return;
305 }
306
307
308 if (stonefield.remove(sx, sy)) {
309 history.append(new QPoint(sx, sy));
310
311 emit s_remove(sx, sy);
312
313 stonefield.mark(sx,sy);
314 emit s_marked(stonefield.getMarked());
315 modified= true;
316
317 emit s_score(stonefield.getScore());
318 if (stonefield.isGameover()) emit s_gameover();
319 }
320}
321
322void
323StoneWidget::myMoveEvent ( QMouseEvent *e)
324{
325 return;
326
327 if (stonefield.isGameover()) {
328 stonefield.unmark();
329 emit s_marked(0);
330 return;
331 }
332
333 int x=e->pos().x();
334 int y=e->pos().y();
335 if (x<0||y<0||x>=field_width||y>=field_height) return;
336
337 int marked=stonefield.mark(x/stone_width,y/stone_height);
338 if (marked>=0) {
339 emit s_marked(marked);
340 slice=0;
341 }
342}
343
344
345
346
347
348
349
350