Extracting ink, paper etc. from ATTR value?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Phil2
Drutt
Posts: 25
Joined: Sun Nov 25, 2018 5:43 pm

Extracting ink, paper etc. from ATTR value?

Post by Phil2 »

Hello everyone,

Does anyone know in Basic how to extract the ink, paper, bright and flash values from an ATTR value?

I'm trying to copy a character from one character square to another along with its attributes. The problem is that the attributes of the character I want to copy are not constant.

I remember that Bright adds 64 to the value and Flash adds 128.

Can a formula or formulas be applied a formula or formulas to ATTR?
Something like If ATTR(y,x)=n Then... let ink= :let paper= :let Bright= :Let Flash= etc.

Thank you for your help.

Phil
Andrew771
Drutt
Posts: 42
Joined: Mon May 04, 2020 9:59 pm
Location: Moscow, Russia

Re: Extracting ink, paper etc. from ATTR value?

Post by Andrew771 »

Attr.Byte = FLASH x 128 + BRIGHT x 64 + PAPER x 8 + INK
Coder of ZXOOM, Euphoria 2D, ZX Like Pascal, Russian Railway Magnate...
Phil2
Drutt
Posts: 25
Joined: Sun Nov 25, 2018 5:43 pm

Re: Extracting ink, paper etc. from ATTR value?

Post by Phil2 »

Thank you Andrew. That' s great. Phil
Phil2
Drutt
Posts: 25
Joined: Sun Nov 25, 2018 5:43 pm

Re: Extracting ink, paper etc. from ATTR value?

Post by Phil2 »

Using Andrew's formula, I wrote a program to do it. First you enter in values for ATTR and then extract the values out again...

Code: Select all

10 INPUT "ink?";i;"paper?";p;"bright?";b;"flash?";f
15 PRINT AT 0,0; INK i; PAPER p; BRIGHT b; FLASH f;"a"
20 LET a=ATTR (0,0)
25 PRINT a
30 LET af=0: IF a>127 THEN LET af=1: LET a=a-128
35 LET ab=0: IF a>63 THEN LET ab=1: LET a=a-64
40 IF a>7 THEN LET ap=INT (a/8): LET ai=a-(ap*8)
45 IF a<8 THEN LET ap=0: LET ai=a
50 PRINT "ink ";ai
55 PRINT "paper ";ap
60 PRINT "bright ";ab
65 PRINT "flash ";af
70 PAUSE 0: CLS : GO TO 10
User avatar
MonkZy
Manic Miner
Posts: 279
Joined: Thu Feb 08, 2018 1:01 pm

Re: Extracting ink, paper etc. from ATTR value?

Post by MonkZy »

The ATTR command converts the x,y into a memory address and PEEK's it and returns the data.

you could use a DEF FN to do to the same conversion

Code: Select all

10 DEF FN a(y,x)=22528+x+(y*32)
20 LET att=PEEK FN a(0,0)
This code will yield the same result as LET att=ATTR (0,0)

You can then set INK/PAPER/FLASH/BRIGHT by POKE'ing attributes directly to the memory.

Code: Select all

10 DEF FN a(y,x)=22528+x+(y*32)
20 POKE FN a(0,0),129
This code will set the attribute at 0,0 to INK 1; PAPER 0; FLASH 1. If you just need to copy the attributes you can just PEEK and POKE without breaking the data into each component.
Phil2
Drutt
Posts: 25
Joined: Sun Nov 25, 2018 5:43 pm

Re: Extracting ink, paper etc. from ATTR value?

Post by Phil2 »

Thank you very much MonkZy. That's an efficient piece of code. I'll try it out.

Phil
Phil2
Drutt
Posts: 25
Joined: Sun Nov 25, 2018 5:43 pm

Re: Extracting ink, paper etc. from ATTR value?

Post by Phil2 »

That worked a treat. Copying the attributes from 0,0 to 1,0.
Perfect. Thanks again.

Code: Select all

10 INPUT "ink?";i;"paper?";p;"bright?";b;"flash?";f
15 PRINT INK i; PAPER p; BRIGHT b; FLASH f;"a"
20 DEF FN a(y,x)=22528+x+(y*32)
25 LET att=PEEK FN a(0,0)
30 PRINT "a"
35 POKE FN a(1,0),att
40 PAUSE 0: CLS : GO TO 10
Post Reply