Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Omar Chehab on April 11, 2015, 11:11:46 am

Title: HELP - Tilemap fill black outside the map
Post by: Omar Chehab on April 11, 2015, 11:11:46 am
Problem: Area outside the tilemap is filled with the TI's memory.
Objective: Fill the area outside my tilemap with black.
Image:
(http://i.imgur.com/vErLeuH.png?1)
Thanks in advance.
Title: Re: HELP - Tilemap fill black outside the map
Post by: ACagliano on April 11, 2015, 11:20:21 am
What is the data structure for your tilemap? Can we see how it looks in the program?
Title: Re: HELP - Tilemap fill black outside the map
Post by: Omar Chehab on April 11, 2015, 11:27:31 am
Code: [Select]
.TITLE
[SPRITES]

[HEX MAP]

5->X
3->Y
Repeat getKey(15)
sub(MOVE)
sub(SCREEN)
End

Lbl MOVE
If getKey(4):Y--
ElseIf getKey(3):X++
ElseIf getKey(1):Y++
ElseIf getKey(2):X--:End
Return

Lbl SCREEN
ClrDraw
ClrDraw^^r
For(B,0,7)
For(A,0,11)
Pt-On(A*8,B*8,{32*(B+Y)+A+X+GDB0}*8+Pic0)
Pt-On(A*8,B*8,{32*(B+Y)+A+X+GDB0}*8+Pic1)^^r
End
End
Text(0,0,(32*(B+Q)+A+P+GDB0))
Pt-Off(40,24,Pic2+8)
Pt-Off(40,24,Pic2)^^r
DispGraph
DispGraph^^r
Return
Title: Re: HELP - Tilemap fill black outside the map
Post by: ACagliano on April 11, 2015, 11:30:34 am
Is your tilemap a set area in memory? Like do you actually have tile data stored somewhere in memory?
Title: Re: HELP - Tilemap fill black outside the map
Post by: Sorunome on April 11, 2015, 11:49:32 am
Do you always know the tilemaps size?
Title: Re: HELP - Tilemap fill black outside the map
Post by: Runer112 on April 11, 2015, 11:55:18 am
There are two solutions to this problem:

Code: [Select]
Pt-On(A*8,B*8,((B+Y→D<32?A+X→C<32)?{D*32+C+GDB0}*8,BLACK_TILE_INDEX)+Pic0→C)
Pt-On(A*8,B*8,C-Pic0+Pic1)ʳ

Unrelated to your question, but there's also a good potential for optimization (and a bit of a bug) with how you display each frame and prepare the next. The optimization is that DispGraphʳ : ClrDraw : ClrDrawʳ can all be done in one command with DispGraphClrDrawʳ. And the bug is that the DispGraph without the ʳ shouldn't be there, as you have two display commands fighting between monochrome and grayscale graphics.
Title: Re: HELP - Tilemap fill black outside the map
Post by: Omar Chehab on April 11, 2015, 03:52:35 pm
Is your tilemap a set area in memory? Like do you actually have tile data stored somewhere in memory?
Yes, GDB0

Do you always know the tilemaps size?
Yes, 32x32

There are two solutions to this problem:

  • Add enough black tiles around the edges of your tilemap so that the camera can never move far enough to go past them. This only costs as much extra time as it takes to perform the larger Y-coordinate multiplication to look up a tile, but does cost a fair amount of extra map space, so might not be ideal.
  • Check if a tile to render's coordinates are outside of the map, and if so, produce a black tile instead. This only costs a small amount of time and requires no extra map data, so is probably the desired solution. Your map tile drawing code should then look something like the following (untested!), which should look up the tile index normally if both coordinates are between 0 and 31 or return the black tile index if not:
Code: [Select]
Pt-On(A*8,B*8,((B+Y→D<32?A+X→C<32)?{D*32+C+GDB0}*8,BLACK_TILE_INDEX)+Pic0→C)
Pt-On(A*8,B*8,C-Pic0+Pic1)ʳ

Unrelated to your question, but there's also a good potential for optimization (and a bit of a bug) with how you display each frame and prepare the next. The optimization is that DispGraphʳ : ClrDraw : ClrDrawʳ can all be done in one command with DispGraphClrDrawʳ. And the bug is that the DispGraph without the ʳ shouldn't be there, as you have two display commands fighting between monochrome and grayscale graphics.
Thanks, i will begin testing.
Title: Re: HELP - Tilemap fill black outside the map
Post by: Omar Chehab on April 11, 2015, 03:58:57 pm
Okay, this issue is solved. Thanks, runner's second solution worked perfectly. Although i understand the concept, I dont understand how and why it works. Could you explain it please?
Title: Re: HELP - Tilemap fill black outside the map
Post by: Runer112 on April 11, 2015, 04:34:36 pm
Sure, I can break down the code a bit. Since only the tile lookup logic changed, I'm just going to focus on that. Also, I realized I could make the logic a bit nicer, so I'm going to explain this slightly different version:

Code: [Select]
Pt-On(A*8,B*8,((B+Y→D<32?A+X→C<32)?{D*32+C+GDB0},BLACK_TILE_INDEX)*8→C+Pic0)
Pt-On(A*8,B*8,C+Pic1)ʳ

((B+Y→D<32If the Y-coordinate (saved for later in D) is between 0 and 31 (this is an unsigned comparison, so negative coordinates are interpreted as really large positive coordinates and are not less than 32)
?And (a ternary operator with only one following clause is like a short-circuit AND operator)
A+X→C<32)If the X-coordinate (saved for later in C) is between 0 and 31
?Then:
{D*32+C+GDB0}Look up the tile index at (C, D) in the map GDB0
,Else:
BLACK_TILE_INDEX)Use the black tile index
*8→CCalculate the pointer offset of this tile index (saved for later in C)
+Pic0Add the pointer offset to the tileset primary data pointer to obtain a pointer to the tile's primary sprite
... In the next sprite command...
C+Pic1Add the pointer offset to the tileset secondary data pointer to obtain a pointer to the tile's secondary sprite
Title: Re: HELP - Tilemap fill black outside the map
Post by: Omar Chehab on April 12, 2015, 03:07:06 am
Sweet, case closed thanks for your help.