-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbitmapDrawing.cpp
More file actions
497 lines (418 loc) · 18.4 KB
/
bitmapDrawing.cpp
File metadata and controls
497 lines (418 loc) · 18.4 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "bitTables.h"
#include "dungeon.h"
#include "monsterBitmaps.h"
#include "objectBitmaps.h"
#include "wallBitmaps.h"
#include "TinyJoypadUtils.h"
/*--------------------------------------------------------*/
uint8_t Dungeon::getLightingMask( const uint8_t &viewDistance )
{
return( pgm_read_byte( lightingTable + _dungeon.lightingOffset + viewDistance ) );
}
/*--------------------------------------------------------*/
// Processes a complete vertical column at position x
void Dungeon::renderDungeonColumn( const uint8_t x )
{
memset( _dungeon.lineBuffer, 0, sizeof( _dungeon.lineBuffer ) / sizeof(_dungeon.lineBuffer[0] ) );
#if 0
// use mirror effect depending on position and orientation to create walking illusion (this works quite well!)
bool mirror = (_dungeon.playerX + _dungeon.playerY + _dungeon.dir) & 0x01;
#ifdef ENABLE_FLOOR_RENDERING
memcpy_P( _dungeon.lineBuffer + 5, dungeonFloor + ( mirror ? 96 - x : x ) * 3, 3 );
#endif
// draw dungeon from distant to close
// - find all walls that are visible in column x and draw them from distant to close
// - apply wall shading
// - draw objects with mask (and shade objects)
SIMPLE_WALL_INFO wallInfo;
const SIMPLE_WALL_INFO* wallInfoPtr = arrayOfWallInfo;
const SIMPLE_WALL_INFO* lastWallInfoPtr = nullptr;
NON_WALL_OBJECT object;
// start at full distance
uint8_t distance = MAX_VIEW_DISTANCE;
// walls are ordered by distance, the last object has distance 1
while ( true )
{
// the structure resides in PROGMEM, so we need to copy it to RAM first...
memcpy_P( &wallInfo, wallInfoPtr, sizeof( wallInfo ) );
/////////////////////////////////////////////////////////////////////////
// Place NWOs (Non Wall Objects)
// NWOs need to be placed after all wall elements for the appropriate distance have been processed.
// The trigger event is a change in distance to an even value.
// distance has changed?
if ( wallInfo.viewDistance != distance )
{
// switching from normal to diagonal walls?
if ( ( distance & 0x01 ) == 0x00 )
{
// object distance is measured in cells (equals to half the wall distance rounded down)
distance >>= 1;
// search for NWOs that could be visible in this column
for ( uint8_t n = 0; n < sizeof( objectList ) / sizeof( objectList[0] ); n++ )
{
// get object from PROGMEM
memcpy_P( &object, &objectList[n], sizeof( object ) );
const uint8_t objectWidth = object.bitmapWidth >> distance;
// offset table for center positions at the corresponding distance
const int8_t* offsets = objectCenterPositions + distance * objCenterPosPerLine;
// start at the leftmost position
int8_t leftRightOffset = objCenterPosStartOffset;
// try to find the correct column
while ( leftRightOffset <= objCenterPosEndOffset )
{
int8_t offset = pgm_read_byte( offsets );
// is this a valid place for the object?
if ( ( x >= offset - objectWidth ) && ( x < offset + objectWidth ) )
{
// is there such an object at the target position?
uint8_t cellValue = *( getCellRaw( _dungeon.playerX, _dungeon.playerY, distance, leftRightOffset, _dungeon.dir ) );
if ( ( cellValue & OBJECT_MASK ) == object.itemType )
{
// some objects must not be visibile from orthogonal directions
if (cellValue & FLAG_LIMITED_VISIBILITY)
{
// forbidden direction?
if ( ( cellValue & LIMITED_VISIBILITY_MASK ) == ( _dungeon.dir & LIMITED_VISIBILITY_MASK ) )
{
// not visible
break;
}
}
// reset buffer pointer
uint8_t* buffer = _dungeon.lineBuffer;
// walk over all vertical pixels in the current column
for ( uint8_t y = 0; y < DUNGEON_WINDOW_SIZE_Y / 8; y++ )
{
uint8_t posX = x - offset + objectWidth;
// free background
uint8_t mask = getDownScaledBitmapData( posX, y, distance, &object, true );
*buffer &= mask;
// and overlay scaled bitmap
uint8_t scaledBitmap = getDownScaledBitmapData( posX, y, distance, &object, false );
#ifdef _ENABLE_OBJECT_SHADING_
// object "shading" is only required for fading in and out
scaledBitmap &= getLightingMask( distance );
#endif
// is it the object right in front of the player (must be, if distance is 1)
if ( distance == 1 )
{
// apply inversion effect if the monster was hit...
scaledBitmap ^= ( _dungeon.invertMonsterEffect & ~mask );
}
*buffer++ |= scaledBitmap;
} // for y
serialPrint(F(", [ object = ")); serialPrint(n); serialPrint(F(", distance = ")); serialPrint( distance ); serialPrint(F(" ]"));
break;
}
}
// next column
offsets++;
leftRightOffset++;
} // while
} // for n
}
distance = wallInfo.viewDistance;
if (distance == 0)
{
#ifdef _ENABLE_DUNGEON_FLOOR_
if (_dungeon.lightingOffset < 2)
{
/// apply background
uint8_t* buffer = _dungeon.lineBuffer + 5;
for (uint8_t y = 0; y < 3; y++)
{
uint8_t pixels = *buffer;
// no pixels set?
if (!pixels)
{
// fill in the background, simple version without wall mask
pixels = pgm_read_byte(dungeonFloor + (mirror ? 96 - x : x) * 3 + y);
}
// store pixels
*buffer++ = pixels;
}
}
#endif
// exit loop, all walls and NWOs have been processed
serialPrint(F(", wall = "));
if (lastWallInfoPtr)
{
serialPrint(lastWallInfoPtr - arrayOfWallInfo); serialPrint(F(", distance = ")); serialPrintln(lastWallInfoPtr->viewDistance);
}
else
{
serialPrintln(F("null"));
}
break;
}
}
/////////////////////////////////////////////////////////////////////////
// Place walls
// is this wall element visible in this column?
if ( ( x >= wallInfo.startPosX ) && ( x <= wallInfo.endPosX ) )
{
// check if there is a wall object at that distance and position offset
if ( ( *( getCellRaw( _dungeon.playerX, _dungeon.playerY, wallInfo.viewDistance >> 1, wallInfo.leftRightOffset, _dungeon.dir ) ) & WALL_MASK ) == ( WALL & ~FLAG_SOLID ) )
{
lastWallInfoPtr = wallInfoPtr;
// split combined positions into start and end
int8_t startPosY = wallInfo.posStartEndY >> 4;
int8_t endPosY = wallInfo.posStartEndY & 0x0f;
int8_t sizeY = endPosY - startPosY + 1;
// positions are to be considered relative to the bitmap
int8_t posX = x - wallInfo.startPosX;
// calculate the x offset depending on the mirror flag
uint8_t offsetX = mirror ? wallInfo.width - 1 - posX - wallInfo.relPos
: posX + wallInfo.relPos;
// copy wall data to the right position
const uint8_t* bitmapData = wallInfo.wallBitmap + offsetX * sizeY;
uint8_t* buffer = _dungeon.lineBuffer + startPosY;
while ( sizeY-- )
{
uint8_t pixels = pgm_read_byte( bitmapData++ );
#ifdef _ENABLE_WALL_SHADING_
// Lighting could be optimized further by integrating the shading directly
// into the wall bitmaps,but at the cost of losing the fading effect
pixels &= getLightingMask( ( wallInfo.viewDistance & 0xfe ) + ( x & 0x01 ) );
#endif
*buffer++ = pixels;
}
}
}
// next wall object
wallInfoPtr++;
}
#else
// here is what we're gonna do:
// - find the frontmost wall in this column and determine the max. view distance
// start a y-loop
// - get the wall pixels and apply shading (if enabled)
// - place all visible NWOs (Non Wall Objects) over the wall pixels from back to front and apply shading
SIMPLE_WALL_INFO wallInfo;
const SIMPLE_WALL_INFO* wallInfoPtr = arrayOfWallInfo;
// all objects are visible
int8_t maxObjectDistance = MAX_VIEW_DISTANCE;
// use mirror effect depending on position and orientation to create walking illusion (this works quite well!)
bool mirror = ( _dungeon.playerX + _dungeon.playerY + _dungeon.dir ) & 0x01;
// iterate through the whole list (at least as long as it's necessary)
while( true )
{
// the structure resides in PROGMEM, so we need to copy it to RAM first...
memcpy_P( &wallInfo, wallInfoPtr, sizeof( wallInfo ) );
// end of list reached?
if ( wallInfo.wallBitmap == nullptr ) { break; }
// check conditions
if ( ( x >= wallInfo.startPosX ) && ( x <= wallInfo.endPosX ) )
{
// is there a wall object?
if ( ( *( getCellRaw( _dungeon.playerX, _dungeon.playerY, wallInfo.viewDistance, wallInfo.leftRightOffset, _dungeon.dir ) ) & WALL_MASK ) == ( WALL & ~FLAG_SOLID ) )
{
// split combined positions into start and end
int8_t startPosY = wallInfo.posStartEndY >> 4;
int8_t endPosY = wallInfo.posStartEndY & 0x0f;
int8_t sizeY = endPosY - startPosY + 1;
// positions are to be considered relative to the bitmap
int8_t posX = x - wallInfo.startPosX;
// calculate the x offset depending on the mirror flag
uint8_t offsetX = mirror ? wallInfo.width - 1 - posX - wallInfo.relPos
: posX + wallInfo.relPos;
// copy wall data to the right position
const uint8_t *bitmapData = wallInfo.wallBitmap + offsetX * sizeY;
uint8_t *buffer = _dungeon.lineBuffer + startPosY;
while ( sizeY-- )
{
*buffer++ = pgm_read_byte( bitmapData++ );
}
// objects behind walls are not visible, but doors or switches might be placed *on* walls
maxObjectDistance = wallInfo.viewDistance;
// that's it!
break;
}
}
// move to next entry
wallInfoPtr++;
}
/// apply background
uint8_t* buffer = _dungeon.lineBuffer;
for ( uint8_t y = 0; y < 8; y++ )
{
uint8_t pixels = *buffer;
#ifdef _ENABLE_WALL_SHADING_
// TODO: this could be optimized further:
// - integrate shading into wall bitmaps
// - or use simple 'if ( wallInfo.viewDistance > 2 )...' instead of switch statement
// - OR use lookup table for dynamic lighting!
//uint8_t lightMask = pgm_read_byte( lightingTable + _dungeon.lightingOffset + wallInfo.viewDistance * 2 + ( x & 0x01 ) );
pixels&= getLightingMask( wallInfo.viewDistance * 2 + ( x & 0x01 ) );
#endif
#ifdef _ENABLE_DUNGEON_FLOOR_
// lower part and no pixels set?
if ( ( y >= 5 ) && !pixels )
{
// fill in the background, simple version without wall mask
pixels |= pgm_read_byte( dungeonFloor + (mirror ? 96 - x : x) * 3 + y - 5 );
}
#endif
// store pixels
*buffer++ = pixels;
}
NON_WALL_OBJECT object;
uint8_t cellValue;
// draw NWOs (Non Wall Objects) over the background pixels (with mask!)
for ( uint8_t distance = maxObjectDistance; distance > 0; distance-- )
{
for ( uint8_t n = 0; n < sizeof( objectList ) / sizeof( objectList[0] ); n++ )
{
memcpy_P( &object, &objectList[n], sizeof(object) );
uint8_t objectWidth = object.bitmapWidth >> distance;
// offset table for center positions at the corresponding distance
const int8_t* offsets = objectCenterPositions + distance * objCenterPosPerLine;
// start at the leftmost position
int8_t leftRightOffset = objCenterPosStartOffset;
// try to find the correct column
while ( leftRightOffset <= objCenterPosEndOffset )
{
int8_t offset = pgm_read_byte( offsets );
// is this a valid place for the object?
if ( ( x >= offset - objectWidth ) && ( x < offset + objectWidth ) )
{
// is there such an object at the target position?
cellValue = *( getCellRaw( _dungeon.playerX, _dungeon.playerY, distance, leftRightOffset, _dungeon.dir ) );
if ( ( cellValue & OBJECT_MASK ) == object.itemType )
{
// some objects must not be visibile from orthogonal directions
if ( cellValue & FLAG_LIMITED_VISIBILITY )
{
// forbidden direction?
if ( ( cellValue & LIMITED_VISIBILITY_MASK ) == ( _dungeon.dir & LIMITED_VISIBILITY_MASK ) )
{
// not visible
break;
}
}
// if the object is in the same distance as a wall object and not placed on a wall,
// it must not be rendered
if ((wallInfo.viewDistance == distance)
&& !(cellValue & WALL_MASK))
{
if ( !leftRightOffset )
{
return;
}
}
// reset buffer pointer
buffer = _dungeon.lineBuffer;
// walk over all vertical pixels in the current column
for ( uint8_t y = 0; y < DUNGEON_WINDOW_SIZE_Y / 8; y++ )
{
uint8_t posX = x - offset + objectWidth;
// free background
uint8_t mask = getDownScaledBitmapData(posX, y, distance, &object, true);
*buffer &= mask;
// and overlay scaled bitmap
uint8_t scaledBitmap = getDownScaledBitmapData(posX, y, distance, &object, false);
#ifdef _ENABLE_OBJECT_SHADING_
// use shading effect to pronounce the distance of an object (at the cost of clarity)
//uint8_t lightMask = pgm_read_byte( lightingTable + _dungeon.lightingOffset + wallInfo.viewDistance );
scaledBitmap &= getLightingMask(wallInfo.viewDistance);
#endif
// is it the object right in front of the player (must be, if distance is 1)
if (distance == 1)
{
// apply inversion effect if the monster was hit...
scaledBitmap ^= (_dungeon.invertMonsterEffect & ~mask);
}
*buffer++ |= scaledBitmap;
} // for y
break;
}
}
// next column
offsets++;
leftRightOffset++;
} // while
} // for n
} // for distance
#endif
}
/*--------------------------------------------------------*/
// Returns the downscaled bitmap data at position x,y.
// Supported distance values are 1, 2, 3.
// Note that x is scaled by the scale factor for
// the current distance, while y remains unscaled!
uint8_t Dungeon::getDownScaledBitmapData( const uint8_t x, // already downscaled by 1 << ( distance - 1 )
uint8_t y, // unscaled vertical position
const uint8_t distance, // supported values are 1..3
const NON_WALL_OBJECT *object, // current non wall object
const bool useMask // if true returns the down scaled mask instead of the bitmap
)
{
uint8_t pixels = 0;
// get start address
const uint8_t *bitmapData = object->bitmapData;
// (add optional offset for mask if required)
if ( useMask ) { bitmapData += object->bitmapHeightInBytes * object->bitmapWidth; }
// Get scaling factor from LUT (efficient and still flexible).
const uint8_t scaleFactor = pgm_read_byte( scalingFactorFromDistance + distance );
const uint8_t squaredScaleFactor = scaleFactor * scaleFactor;
// get threshold (distance is 1..3, so subtract 1 (at no cost!))
const uint8_t threshold = object->scalingThreshold[distance - 1];
// is there anything to be done?
uint8_t startOffsetY = pgm_read_byte( verticalStartOffset + distance );
uint8_t endOffsetY = pgm_read_byte( verticalEndOffset + distance );
if ( ( y >= startOffsetY ) && ( y <= endOffsetY ) )
{
// modify positions in source bitmap by scaling factor
//x = x * scaleFactor * object->bitmapHeightInBytes;
// correct y position by start offset
y -= startOffsetY;
// get associated bit mask
uint8_t bitMask = pgm_read_byte( bitMaskFromScalingFactor + scaleFactor );
// calculate the first and last bit to be processed
uint8_t startBitNo = object->bitmapVerticalOffsetInBits;
uint8_t endBitNo = startBitNo + object->bitmapHeightInBytes * 8;
// but we are starting with bit 0 (and its friends)
uint8_t bitNo = y * 8 * scaleFactor;
// We need to calculate 8 vertical output bits...
// NOTE: Because the Tiny85 only supports shifting by 1 bit, it is
// more efficient to do the shifting in the 'for' loop instead
// of using a ( 1 << n ) construct.
for ( uint8_t bitValue = 1; bitValue != 0; bitValue <<= 1 )
{
uint8_t bitSum = 0;
if ( ( bitNo >= startBitNo ) && ( bitNo < endBitNo ) )
{
// calculate start address
uint8_t row = ( bitNo - startBitNo ) / 8;
const uint8_t *data = bitmapData + x * scaleFactor * object->bitmapHeightInBytes + row;
// go over the columns - all required bits always are in one row
for ( uint8_t col = 0; col < scaleFactor; col++ )
{
// to get the output value, we will sum all the bits up (using a lookup table saves time and flash space)
bitSum += pgm_read_byte( nibbleBitCount + ( ( pgm_read_byte( data ) >> ( bitNo & 0x07 ) ) & bitMask ) );
data += object->bitmapHeightInBytes;
}
}
else if ( useMask )
{
// make bitsum count - otherwise we will erase the backgound ;)
//bitSum += pgm_read_byte( scalingFactorFromDistance2 + distance );
bitSum += squaredScaleFactor;
}
// next bit position
bitNo += scaleFactor;
// calculate output pixel
if ( bitSum >= threshold )
{
pixels |= bitValue;
}
} // for
}
// no bits here, set mask to 0xff
else if ( useMask )
{
// 0x00-- -> 0xff
pixels--;
}
return( pixels );
}