author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
42 |
#include <math.h> |
0 | 43 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
44 |
#include "raycasting.h" |
0 | 45 |
|
46 |
#ifndef M_PI |
|
47 |
#define M_PI 3.14159265358979323846 |
|
48 |
#endif |
|
49 |
||
50 |
#define WORLD_SIZE 8 |
|
51 |
int world_map[WORLD_SIZE][WORLD_SIZE] = { |
|
52 |
{ 1, 1, 1, 1, 6, 1, 1, 1 }, |
|
53 |
{ 1, 0, 0, 1, 0, 0, 0, 7 }, |
|
54 |
{ 1, 1, 0, 1, 0, 1, 1, 1 }, |
|
55 |
{ 6, 0, 0, 0, 0, 0, 0, 3 }, |
|
56 |
{ 1, 8, 8, 0, 8, 0, 8, 1 }, |
|
57 |
{ 2, 2, 0, 0, 8, 8, 7, 1 }, |
|
58 |
{ 3, 0, 0, 0, 0, 0, 0, 5 }, |
|
59 |
{ 2, 2, 2, 2, 7, 4, 4, 4 }, |
|
60 |
}; |
|
61 |
||
62 |
#define TEXTURE_SIZE 64 |
|
63 |
#define TEXTURE_BLOCK (TEXTURE_SIZE * TEXTURE_SIZE) |
|
64 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
Raycasting::Raycasting(QWidget *parent) |
0 | 66 |
: QWidget(parent) |
67 |
, angle(0.5) |
|
68 |
, playerPos(1.5, 1.5) |
|
69 |
, angleDelta(0) |
|
70 |
, moveDelta(0) |
|
71 |
, touchDevice(false) { |
|
72 |
||
73 |
// http://www.areyep.com/RIPandMCS-TextureLibrary.html |
|
74 |
textureImg.load(":/textures.png"); |
|
75 |
textureImg = textureImg.convertToFormat(QImage::Format_ARGB32); |
|
76 |
Q_ASSERT(textureImg.width() == TEXTURE_SIZE * 2); |
|
77 |
Q_ASSERT(textureImg.bytesPerLine() == 4 * TEXTURE_SIZE * 2); |
|
78 |
textureCount = textureImg.height() / TEXTURE_SIZE; |
|
79 |
||
80 |
watch.start(); |
|
81 |
ticker.start(25, this); |
|
82 |
setAttribute(Qt::WA_OpaquePaintEvent, true); |
|
83 |
setMouseTracking(false); |
|
84 |
} |
|
85 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
void Raycasting::updatePlayer() { |
0 | 87 |
int interval = qBound(20, watch.elapsed(), 250); |
88 |
watch.start(); |
|
89 |
angle += angleDelta * interval / 1000; |
|
90 |
qreal step = moveDelta * interval / 1000; |
|
91 |
qreal dx = cos(angle) * step; |
|
92 |
qreal dy = sin(angle) * step; |
|
93 |
QPointF pos = playerPos + 3 * QPointF(dx, dy); |
|
94 |
int xi = static_cast<int>(pos.x()); |
|
95 |
int yi = static_cast<int>(pos.y()); |
|
96 |
if (world_map[yi][xi] == 0) |
|
97 |
playerPos = playerPos + QPointF(dx, dy); |
|
98 |
} |
|
99 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
void Raycasting::showFps() { |
0 | 101 |
static QTime frameTick; |
102 |
static int totalFrame = 0; |
|
103 |
if (!(totalFrame & 31)) { |
|
104 |
int elapsed = frameTick.elapsed(); |
|
105 |
frameTick.start(); |
|
106 |
int fps = 32 * 1000 / (1 + elapsed); |
|
107 |
setWindowTitle(QString("Raycasting (%1 FPS)").arg(fps)); |
|
108 |
} |
|
109 |
totalFrame++; |
|
110 |
} |
|
111 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
112 |
void Raycasting::render() { |
0 | 113 |
|
114 |
// setup the screen surface |
|
115 |
if (buffer.size() != bufferSize) |
|
116 |
buffer = QImage(bufferSize, QImage::Format_ARGB32); |
|
117 |
int bufw = buffer.width(); |
|
118 |
int bufh = buffer.height(); |
|
119 |
if (bufw <= 0 || bufh <= 0) |
|
120 |
return; |
|
121 |
||
122 |
// we intentionally cheat here, to avoid detach |
|
123 |
const uchar *ptr = buffer.bits(); |
|
124 |
QRgb *start = (QRgb*)(ptr); |
|
125 |
QRgb stride = buffer.bytesPerLine() / 4; |
|
126 |
QRgb *finish = start + stride * bufh; |
|
127 |
||
128 |
// prepare the texture pointer |
|
129 |
const uchar *src = textureImg.bits(); |
|
130 |
const QRgb *texsrc = reinterpret_cast<const QRgb*>(src); |
|
131 |
||
132 |
// cast all rays here |
|
133 |
qreal sina = sin(angle); |
|
134 |
qreal cosa = cos(angle); |
|
135 |
qreal u = cosa - sina; |
|
136 |
qreal v = sina + cosa; |
|
137 |
qreal du = 2 * sina / bufw; |
|
138 |
qreal dv = -2 * cosa / bufw; |
|
139 |
||
140 |
for (int ray = 0; ray < bufw; ++ray, u += du, v += dv) { |
|
141 |
// everytime this ray advances 'u' units in x direction, |
|
142 |
// it also advanced 'v' units in y direction |
|
143 |
qreal uu = (u < 0) ? -u : u; |
|
144 |
qreal vv = (v < 0) ? -v : v; |
|
145 |
qreal duu = 1 / uu; |
|
146 |
qreal dvv = 1 / vv; |
|
147 |
int stepx = (u < 0) ? -1 : 1; |
|
148 |
int stepy = (v < 0) ? -1 : 1; |
|
149 |
||
150 |
// the cell in the map that we need to check |
|
151 |
qreal px = playerPos.x(); |
|
152 |
qreal py = playerPos.y(); |
|
153 |
int mapx = static_cast<int>(px); |
|
154 |
int mapy = static_cast<int>(py); |
|
155 |
||
156 |
// the position and texture for the hit |
|
157 |
int texture = 0; |
|
158 |
qreal hitdist = 0.1; |
|
159 |
qreal texofs = 0; |
|
160 |
bool dark = false; |
|
161 |
||
162 |
// first hit at constant x and constant y lines |
|
163 |
qreal distx = (u > 0) ? (mapx + 1 - px) * duu : (px - mapx) * duu; |
|
164 |
qreal disty = (v > 0) ? (mapy + 1 - py) * dvv : (py - mapy) * dvv; |
|
165 |
||
166 |
// loop until we hit something |
|
167 |
while (texture <= 0) { |
|
168 |
if (distx > disty) { |
|
169 |
// shorter distance to a hit in constant y line |
|
170 |
hitdist = disty; |
|
171 |
disty += dvv; |
|
172 |
mapy += stepy; |
|
173 |
texture = world_map[mapy][mapx]; |
|
174 |
if (texture > 0) { |
|
175 |
dark = true; |
|
176 |
if (stepy > 0) { |
|
177 |
qreal ofs = px + u * (mapy - py) / v; |
|
178 |
texofs = ofs - floor(ofs); |
|
179 |
} else { |
|
180 |
qreal ofs = px + u * (mapy + 1 - py) / v; |
|
181 |
texofs = ofs - floor(ofs); |
|
182 |
} |
|
183 |
} |
|
184 |
} else { |
|
185 |
// shorter distance to a hit in constant x line |
|
186 |
hitdist = distx; |
|
187 |
distx += duu; |
|
188 |
mapx += stepx; |
|
189 |
texture = world_map[mapy][mapx]; |
|
190 |
if (texture > 0) { |
|
191 |
if (stepx > 0) { |
|
192 |
qreal ofs = py + v * (mapx - px) / u; |
|
193 |
texofs = ofs - floor(ofs); |
|
194 |
} else { |
|
195 |
qreal ofs = py + v * (mapx + 1 - px) / u; |
|
196 |
texofs = ceil(ofs) - ofs; |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
// get the texture, note that the texture image |
|
203 |
// has two textures horizontally, "normal" vs "dark" |
|
204 |
int col = static_cast<int>(texofs * TEXTURE_SIZE); |
|
205 |
col = qBound(0, col, TEXTURE_SIZE - 1); |
|
206 |
texture = (texture - 1) % textureCount; |
|
207 |
const QRgb *tex = texsrc + TEXTURE_BLOCK * texture * 2 + |
|
208 |
(TEXTURE_SIZE * 2 * col); |
|
209 |
if (dark) |
|
210 |
tex += TEXTURE_SIZE; |
|
211 |
||
212 |
// start from the texture center (horizontally) |
|
213 |
int h = static_cast<int>(bufw / hitdist / 2); |
|
214 |
int dy = (TEXTURE_SIZE << 12) / h; |
|
215 |
int p1 = ((TEXTURE_SIZE / 2) << 12) - dy; |
|
216 |
int p2 = p1 + dy; |
|
217 |
||
218 |
// start from the screen center (vertically) |
|
219 |
// y1 will go up (decrease), y2 will go down (increase) |
|
220 |
int y1 = bufh / 2; |
|
221 |
int y2 = y1 + 1; |
|
222 |
QRgb *pixel1 = start + y1 * stride + ray; |
|
223 |
QRgb *pixel2 = pixel1 + stride; |
|
224 |
||
225 |
// map the texture to the sliver |
|
226 |
while (y1 >= 0 && y2 < bufh && p1 >= 0) { |
|
227 |
*pixel1 = tex[p1 >> 12]; |
|
228 |
*pixel2 = tex[p2 >> 12]; |
|
229 |
p1 -= dy; |
|
230 |
p2 += dy; |
|
231 |
--y1; |
|
232 |
++y2; |
|
233 |
pixel1 -= stride; |
|
234 |
pixel2 += stride; |
|
235 |
} |
|
236 |
||
237 |
// ceiling and floor |
|
238 |
for (; pixel1 > start; pixel1 -= stride) |
|
239 |
*pixel1 = qRgb(0, 0, 0); |
|
240 |
for (; pixel2 < finish; pixel2 += stride) |
|
241 |
*pixel2 = qRgb(96, 96, 96); |
|
242 |
} |
|
243 |
||
244 |
update(QRect(QPoint(0, 0), bufferSize)); |
|
245 |
} |
|
246 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
247 |
void Raycasting::resizeEvent(QResizeEvent*) { |
0 | 248 |
#if defined(Q_OS_WINCE_WM) |
249 |
touchDevice = true; |
|
250 |
#elif defined(Q_OS_SYMBIAN) |
|
251 |
// FIXME: use HAL |
|
252 |
if (width() > 480 || height() > 480) |
|
253 |
touchDevice = true; |
|
254 |
#else |
|
255 |
touchDevice = false; |
|
256 |
#endif |
|
257 |
if (touchDevice) { |
|
258 |
if (width() < height()) { |
|
259 |
trackPad = QRect(0, height() / 2, width(), height() / 2); |
|
260 |
centerPad = QPoint(width() / 2, height() * 3 / 4); |
|
261 |
bufferSize = QSize(width(), height() / 2); |
|
262 |
} else { |
|
263 |
trackPad = QRect(width() / 2, 0, width() / 2, height()); |
|
264 |
centerPad = QPoint(width() * 3 / 4, height() / 2); |
|
265 |
bufferSize = QSize(width() / 2, height()); |
|
266 |
} |
|
267 |
} else { |
|
268 |
trackPad = QRect(); |
|
269 |
bufferSize = size(); |
|
270 |
} |
|
271 |
update(); |
|
272 |
} |
|
273 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
274 |
void Raycasting::timerEvent(QTimerEvent*) { |
0 | 275 |
updatePlayer(); |
276 |
render(); |
|
277 |
showFps(); |
|
278 |
} |
|
279 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
void Raycasting::paintEvent(QPaintEvent *event) { |
0 | 281 |
QPainter p(this); |
282 |
p.setCompositionMode(QPainter::CompositionMode_Source); |
|
283 |
||
284 |
p.drawImage(event->rect(), buffer, event->rect()); |
|
285 |
||
286 |
if (touchDevice && event->rect().intersects(trackPad)) { |
|
287 |
p.fillRect(trackPad, Qt::white); |
|
288 |
p.setPen(QPen(QColor(224, 224, 224), 6)); |
|
289 |
int rad = qMin(trackPad.width(), trackPad.height()) * 0.3; |
|
290 |
p.drawEllipse(centerPad, rad, rad); |
|
291 |
||
292 |
p.setPen(Qt::NoPen); |
|
293 |
p.setBrush(Qt::gray); |
|
294 |
||
295 |
QPolygon poly; |
|
296 |
poly << QPoint(-30, 0); |
|
297 |
poly << QPoint(0, -40); |
|
298 |
poly << QPoint(30, 0); |
|
299 |
||
300 |
p.translate(centerPad); |
|
301 |
for (int i = 0; i < 4; ++i) { |
|
302 |
p.rotate(90); |
|
303 |
p.translate(0, 20 - rad); |
|
304 |
p.drawPolygon(poly); |
|
305 |
p.translate(0, rad - 20); |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
p.end(); |
|
310 |
} |
|
311 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
312 |
void Raycasting::keyPressEvent(QKeyEvent *event) { |
0 | 313 |
event->accept(); |
314 |
if (event->key() == Qt::Key_Left) |
|
315 |
angleDelta = 1.3 * M_PI; |
|
316 |
if (event->key() == Qt::Key_Right) |
|
317 |
angleDelta = -1.3 * M_PI; |
|
318 |
if (event->key() == Qt::Key_Up) |
|
319 |
moveDelta = 2.5; |
|
320 |
if (event->key() == Qt::Key_Down) |
|
321 |
moveDelta = -2.5; |
|
322 |
} |
|
323 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
324 |
void Raycasting::keyReleaseEvent(QKeyEvent *event) { |
0 | 325 |
event->accept(); |
326 |
if (event->key() == Qt::Key_Left) |
|
327 |
angleDelta = (angleDelta > 0) ? 0 : angleDelta; |
|
328 |
if (event->key() == Qt::Key_Right) |
|
329 |
angleDelta = (angleDelta < 0) ? 0 : angleDelta; |
|
330 |
if (event->key() == Qt::Key_Up) |
|
331 |
moveDelta = (moveDelta > 0) ? 0 : moveDelta; |
|
332 |
if (event->key() == Qt::Key_Down) |
|
333 |
moveDelta = (moveDelta < 0) ? 0 : moveDelta; |
|
334 |
} |
|
335 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
336 |
void Raycasting::mousePressEvent(QMouseEvent *event) { |
0 | 337 |
qreal dx = centerPad.x() - event->pos().x(); |
338 |
qreal dy = centerPad.y() - event->pos().y(); |
|
339 |
angleDelta = dx * 2 * M_PI / width(); |
|
340 |
moveDelta = dy * 10 / height(); |
|
341 |
} |
|
342 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
343 |
void Raycasting::mouseMoveEvent(QMouseEvent *event) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
344 |
qreal dx = centerPad.x() - event->pos().x(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
345 |
qreal dy = centerPad.y() - event->pos().y(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
346 |
angleDelta = dx * 2 * M_PI / width(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
347 |
moveDelta = dy * 10 / height(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
348 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
350 |
void Raycasting::mouseReleaseEvent(QMouseEvent*) { |
0 | 351 |
angleDelta = 0; |
352 |
moveDelta = 0; |
|
353 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
354 |