It is currently Tue Nov 13, 2018 12:51 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sat Oct 20, 2018 3:40 am 
Offline

Joined: Tue Jun 28, 2011 2:39 pm
Posts: 192
This is somewhat of a noob thread so posting here. Does anyone has code for rendering numbers and text into nametable? Numbers for stuff like score and so on (or debug info) and text, because you know, text is important in games.

Assuming obviously font tiles are uppercase only and ordered in a sane way (0-9, A-Z, punctuation)


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 4:06 am 
Offline
User avatar

Joined: Thu Mar 31, 2016 11:15 am
Posts: 416
Well, to display a number you have to know all of the base-10 digits. If your number is in binary, you'd have to do a conversion to get it in base 10. Here's optimized code to do that by omegamatrix: http://forums.nesdev.com/viewtopic.php?p=130363

Alternatively you can store your numbers as one byte per base-10 digit. Instead of bytes being in the range [0, 255], you'd cap them to [0, 9]. This makes arithmetic more difficult, but it makes displaying numbers trivial. Personally, I use this technique most of the time.

Quote:
text into nametable?

What have you tried? I'm sure you can figure this out :wink:


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 4:28 am 
Offline

Joined: Tue Jun 28, 2011 2:39 pm
Posts: 192
pubby wrote:
Alternatively you can store your numbers as one byte per base-10 digit. Instead of bytes being in the range [0, 255], you'd cap them to [0, 9]. This makes arithmetic more difficult

And wastes precious RAM. No, thanks. I know you're trying to help and thanks for that, but this simply won't work for me.


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 4:54 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10960
Location: Rio de Janeiro - Brazil
I store things like scores and lives as individual base 10 digits myself. Addition and subtraction are easy (simpler than converting binary to decimal, IMO). It may seem like a waste of precious RAM at first, but realistically speaking, how many displayable numbers do you need? Most games only have a handful of values to display in the status bar.

If you're really hung up on converting binary to decimal though, this has been discussed countless times in the forums, so try searching for these discussions (the most recent one is just a few days old).

As for displaying text, there's really no secret behind it: each symbol you can display is a tile (you can only display symbols you've included in your CHR data, along with the other background elements of your game), so in order to display a letter you just need to write the index of its tile to VRAM, using PPU registers $2006/$2007, like you would for any other background element.


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 5:06 am 
Offline
User avatar

Joined: Thu Mar 31, 2016 11:15 am
Posts: 416
darkhog wrote:
And wastes precious RAM. No, thanks. I know you're trying to help and thanks for that, but this simply won't work for me.

if you're new enough to be asking how to make text appear on the screen, do you really think you're at the point where memory micro-optimizations matter?

:thinking:


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 6:51 am 
Offline
User avatar

Joined: Thu Aug 13, 2015 4:40 pm
Posts: 305
Location: Rio de Janeiro - Brazil
There's plenty of RAM. You don't need to use zp for this. Store each digit in one byte like 99.9% of games do.

_________________
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 8:30 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2329
Location: DIGDUG
Some 6502 number systems use 0-99 per RAM address. While slightly less efficient than 0-255, the code involved to translate from RAM to on-screen decimal is fairly easy.

I use 0-9 myself for all numbers that appear on screen. 6 digits for score, 2 for lives left, 2 for some other thing, is really only 10 RAM address used. Not too bad. And it's the easiest.

If your number tiles start at $f0, then you just need to add $f0 to each digit to get its tile value.

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 2:33 pm 
Offline

Joined: Tue Jun 28, 2011 2:39 pm
Posts: 192
Okay, you convinced me. How does arithmetic for 0-9 bytes work then? And how much is it slower compared to standard ADC/SBC?

Additionally what's the fastest way to convert from normal bytes to 0-9 format 3 bytes for stuff like debug values that I may want to put on screen?


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 3:37 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2329
Location: DIGDUG
Edit. I think I misunderstood your question.

Let's say you are adding 1 to a 2 digit number, max 99

LDX highDigit
LDA lowDigit
clc
adc #1
cmp #10
bcc +
inx ;low digit over 9
LDA #0
+
CPX #10
bcc +
LDA #9 ;max 99 reached
TAX
+
STA lowDigit
STX highDigit


Something like that.

Subtract 1 from a 2 digit number

LDA highDigit ;if both zero, skip
ORA lowDigit
bne +
rts
+
LDX highDigit
LDA lowDigit
Sec
SBC #1
bcs +
dex
lda #9
+
sta lowDigit
stx highDigit

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Sat Oct 20, 2018 11:42 pm 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 819
Perhaps you should consider C at your level.


Top
 Profile  
 
PostPosted: Sun Oct 21, 2018 4:06 am 
Offline
User avatar

Joined: Thu Oct 26, 2017 12:29 pm
Posts: 71
Double dabble algorithm

Each decimal digit is stored in a nibble.


https://en.m.wikipedia.org/wiki/Double_dabble

_________________
...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group