Sprite to Bitmap

Page 1/3
| 2 | 3

By Chilly Willy

Expert (103)

Chilly Willy's picture

20-06-2021, 19:18

Can someone post some code.

Say I make a pattern bitmap of 768 bytes in memory

Map
db 20,10,20,30,40----
0-31
.....
23......

Of course each one corresponds to a pattern in the pattern table

Well if I give the y and x position of the sprite I need the tile that is at that y and x position.

This is what I use to get it from VRAM but I want to move away from that and grab it directly from a pointer.
This will save on resources and speed up access

LD A, (PLAYER_Y)
AND $F8
LD H, 0
LD L, A
ADD HL, HL
ADD HL, HL
LD DE, $1800
ADD HL, DE
LD A, (PLAYER_X)
SRL A
SRL A
SRL A
OR L
LD L, A

DI
LD A,L
OUT (98h),A
LD A,H
AND 3FH
OUT (99h),A
EI

IN A, (98h)

I know L gives 0-255 and H will return 0, 1 or 2

If someone has already figured out the math then please post this.
Someone else gave me this formula so I am lost on such things.

TIA

Login or register to post comments

By PingPong

Enlighted (4138)

PingPong's picture

20-06-2021, 21:18

your question is unclear: what do you need?
do you need to get the vram addr of the tile at a given x,y position?
say something like VRAMADDR = XYTOADDR(X,Y)
or whatever?

By Chilly Willy

Expert (103)

Chilly Willy's picture

20-06-2021, 21:44

No..
I have a screen table I can send directly to VRAM that displays the screen.

I want to parse the table while it is in Ram or ROM, NOT VRAM

So if the table name is
DANNY:
and Danny is 0-767 positions
I want to give it my y/x position of my sprite and have it return which value is at DANNY+???

So after I divide y/8+x I should get a value I can add to Danny: and get whatever data is in that position.

So if y/x of my sprite equals to position 730 in the table and 48 is at position 730 then it should return 48

The routine I provided will grab from VRAM but I need it to grab from a Table in Ram or Rom.

I know I am over explaining it but somehow I did not get that across the first time.
Maybe my English is off but I am learning.

By thegeps

Paragon (1189)

thegeps's picture

20-06-2021, 22:48

you mean you want a collision detection between a sprite and a tile in the nametable, so you know wich tile is in that position...

this routine isn't mine (but I use it a lot). It is a jltursan routine I found time ago on Karoshi forum

	ld	hl,sat+1				;point to player (sprite 0) xpos
	ld	e,(hl)					;xpos in E
	ld	l,0					;point to player ypos
	ld	d,(hl)					;ypos in D
	ld	a,d					;ypos in A
	and	0F8h					
	ld	h,0					
	ld	l,a					
	add	hl,hl					
	add	hl,hl 					;hl=(Y/8)*32
	ld	a,e					;xpos in A
	and	0F8h
	rrca
	rrca
	rrca						; X/8
	add	a,l
	ld	l,a
	adc	a,h
	sub	l
	ld	h,a					;hl=(x/8)+(y/8)*32
	ld 	de,video_buffer				;video_buffer start address in DE
	add	hl,de					;add our position to our buffer
	ld	a,(hl)					;check actual position content

By thegeps

Paragon (1189)

thegeps's picture

20-06-2021, 22:51

obviously sat is128 bytes buffer and its address is aligned aligned (forgot to tell it before)
video buffer is 768 bytes buffer in RAM

By theNestruo

Champion (421)

theNestruo's picture

20-06-2021, 23:13

Take a look at COORDS_TO_OFFSET and OFFSET_TO_COORDS in https://github.com/theNestruo/msx-msxlib/blob/master/lib/gam...

They doesn't use/return addresses but offsets. Add them to the $1800 to get the NAMTBL address; or add them to the address of your buffer and that's it!

By Chilly Willy

Expert (103)

Chilly Willy's picture

21-06-2021, 00:44

After I rewrote the routine it looks like it works but if something is off please let me know.

LD A, (PLAYER_Y) ; POINT TO PLAYER YPOS
AND $F8
LD H, 0
LD L, A
ADD HL, HL
ADD HL, HL ; HL=(Y/8)*32

LD A, (PLAYER_X) ; POINT TO PLAYER (SPRITE 0) XPOS
AND $F8
RRCA
RRCA
RRCA ; X/8
ADD A, L
LD L, A
ADC A, H
SUB L
LD H, A ; HL=(X/8)+(Y/8)*32

LD DE, PLAYFIELD ; VIDEO_BUFFER START ADDRESS IN DE
ADD HL, DE ; ADD OUR POSITION TO OUR BUFFER
LD A, (HL) ; CHECK ACTUAL POSITION CONTENT

By Grauw

Ascended (10768)

Grauw's picture

21-06-2021, 01:39

Chilly Willy wrote:

SUB L

Clever Smile.

By Guillian

Prophet (3517)

Guillian's picture

21-06-2021, 09:01

This is what Konami used:
(buffer must be aligned)

; In: HL = XY
; Out: HL = Pointer to buffer

ld	    a, l
rra
rra
rra
rra
rr	    h
rra
rr	    h
rra
rr	    h
ld	    l, h
and	    3
add	    a, TileBuffer/256
ld	    h, a

By Metalion

Paragon (1625)

Metalion's picture

21-06-2021, 10:39

Grauw wrote:
Chilly Willy wrote:

SUB L

Clever Smile.

Yes, I also used this clever ADD HL,A operation a few times myself.
But I found out it is quicker to do :

ADD A,L
LD L,A
JR NC,$+3
INC H

By Metalion

Paragon (1625)

Metalion's picture

21-06-2021, 20:11

I don't understand at all how someone who is able to program in assembly, is unable to figure the maths behind this.

address = pointer + (Y/8)*32+(X/8)

How hard is that ?????

Page 1/3
| 2 | 3