# What's the most efficient way to define a sprite in basic?

Page 1/2
| 2

I'm working on a game in basic, and the graphics are taking up a lot of data.
It's time to optimize, and I'd like to know if anyone knows the most efficient way to define a sprite in Basic.

Here's what I'm using right now:

```FOR I=0 TO 31
B\$=B\$+CHR\$(VAL("&H"+A\$))
NEXT
SPRITE\$(0)=B\$
DATA 3C,70,E0,E0,E0,E0,70,3E,1F,E,3,D,E,1E,1E,3C,3C,E,7,7,7,7,E,7C,B8,D0,E0,F0,F0,78,78,3C```

I prefer using HEX codes for the graphics, as they are generally shorter and you can delete all the leading 0's to save a little extra space, but I don't know if this is the most efficient way of defining a sprite in HEX.
Is there a totally different way of defining a sprite that is even better than using HEX, or can my code be optimized even more?

Best way is not using functions and string variables.
If you use DATA, store direct HEX values (including '&h')
Then you not need VAL("&h"+a\$), just READ A, and B\$=B\$+chr\$(A)

And more efficient method, is using direct writing to SPT with VPOKE by VRAM address:

```for i=0 to 31:read A:vpoke&h1800+i,A:next i
DATA &h3C,&h70,&hE0....```

where &h1800 - is sample address of your SPT (you may obtain it over BASE operator, or over default values of VDP for you mode)

PS: Its all for speed optimizations.
If you change addresses of SPT and SAT in graphics modes, just remember: using of SET PAGE, restore this addresses to MSX Basic defaults.

hex values are shorter only on listing. each one is 1 byte long as if it was decimal. so it can be useful only on 10liner contest with char per row limit.
if you haven't changed nothing (and I think you haven't) sprite pattern data start address is 3800h (&h3800) = 14336 decimal

so a simple way to shorten your listing is:

```s=14336
fori=0to31
vpokes+i,val("&h"+a\$))
next
data .....
```

if you use decimal value (the bytes of data will be the same quantity) you can save some bytes avoiding hex "translation":

```s=14336
fori=0to31
vpokes+i,a
next
data .....
```

oh, Dolphin was faster and what I said is valid only for msx1, don't know (still) if msx2 default values are the same

Hello, this example for Basic to see two sprite and two colors on line.

```10 SCREEN 4,1
20 DATA 18,3c,ff,99,99,ff,81,ff
21 DATA 0e,0e,0a,0a,0a,0a,0a,0a
22 REM
30 DATA 7e,ff,ff,99,99,ff,81,ff
32 DATA 04,04,0b,0b,0b,0b,0b,0b
40 FOR K=0 TO 1:FOR Z=0 TO 1:A\$="":FOR J=0 TO 7:READ X\$
50 A\$=A\$+CHR\$(VAL("&H"+X\$))
60 NEXT J
70 REM
80 IF Z=0 THEN SPRITE\$(K)=A\$ ELSE COLOR SPRITE\$(K)=A\$
90 NEXT Z
95 NEXT K
100 A\$="":B\$=""
110 PUTSPRITE0,(100,100),,0
120 PUTSPRITE1,(120,100),,1
130 I\$=INPUT\$(1)
```

This data line 20-21 and 30-31 for sprite and color 0-15.

Note:
The colors for COLOR SPRITE is limit of 0-15 ( &H00 - &H0F)
For information see https://www.msx.org/wiki/PUT_SPRITE

DanySoft

For msx2, sorry...

Thanks for the replies, but I am trying to optimize more for size than for speed.

try something like data "183cff9999ff81ff" and val("&h"+mid\$(a\$,1+i*2,2)) - i don't have a ready code here, but i think you can get the idea

Yep, I did something like this in one of my tenliners last year.
I don't know if it is too cryptic for you, but you can take a look at this thread.
You will find the code of my tenliner game and a full code explanation

I think the best size / speed ratio is :

```10 SCREEN2,2
20 FOR I=&H3800 TO &H381F
40 NEXT I
50 DATA 3C,70,E0,E0,E0,E0,70,3E,1F,E,3,D,E,1E,1E,3C,3C,E,7,7,7,7,E,7C,B8,D0,E0,F0,F0,78,78,3C```

If you make a program on HDD (flash card interface) or ROM using DSK2ROM you can use directly BLOAD"SPRITE",S. It will be fast and little greedy in memory.

PS: Put the DATAs always at the end of the program.

In the same token as Nitrofurano, You can try that:

```10 SCREEN2,2
20 A\$="3C70E0E0E0E0703E1F0E030D0E1E1E3C3C0E070707070E7CB8D0E0F0F078783C"
30 FOR I=0 TO 30 STEP 2: B\$=B\$=CHR\$("&H"+MID\$(a\$,I,2)): NEXT I
40 SPRITE\$(0)=B\$
```

A\$ must always be 64 characters long, and keep as much as possible A\$ (or another same variable) to put the data of the sprites there because the more you create variable the less memory you will have.

Page 1/2
| 2