Sunday, 29 March 2020

NextBASIC - How to display a sprite (ZX Spectrum Next quick and easy guide)

You've just got your Next. You don't want to read through the large, very technical manual to get to the juicy bits...you just want to get right in there and put a hardware sprite on the screen. Here's how:

Preprep 

++SKIP THIS IF YOU KNOW HOW TO CREATE A DIRECTORY OR DON'T CARE++

First, go into the browser and make a new directory by pushing 'K'.

Name it binthis because I doubt you'll want to keep this test permanently.

Go into NextBASIC

type CD "binthis"

CD means 'Change Directory" and this is to make sure we save everything in here to keep things tidy.

Draw your sprite


in NextBASIC type: .spredit mysprite.spr

.spredit invokes the sprite editor.
mysprite.spr is the filename of your sprites (this doesn't actually create the file until you save it from the sprite editor. Also, don't put the name in quotes or it won't save).

The editor loads. Feel free to scribble over the top of the first sprite, or if you just want to get on with things, just hit Shift-S to save.

Exit the editor with Symbol Shift and Space.

Displaying the Sprite in NextBASIC


Here's the meat of it.

10 BANK NEW spritey

This tells the Spectrum to reserve a bank in memory to load your sprite data into. "spritey" is just a variable name created with this command.

20 LOAD "mysprite.spr" BANK spritey,0,256

OK so this loads the sprite file into a memory bank. In this case, the one called spritey.
The 0 is the where in the bank it loads the data - in this case 0 loads into the start of the bank.
The 256 is and amount of data to load. Each 16x16 sprite is 256 bytes. If you want to load in more than 1 sprite, just multiple the number by 256. We are loading just one.

30 SPRITE BANK spritey

This tells the Next to use the data in bank spritey for sprites.

40 SPRITE CLEAR: SPRITE PRINT 1

This clears the screen of any sprites that might be there from other programs, then it sets all sprites to visible. Setting the value to 0 makes all sprites invisible.

50 SPRITE 0,80,80,0,1

This is the command to draw a sprite onto the screen. The variables are:
0 (The number of the sprite...this is an identifier and has no bearing on what graphic is used)
80,80 (The x,y position of the sprite on screen)
0 (The image/frame/pattern to use. This corresponds to the actual graphic data you saved in the sprite editor. We loaded one image. Images always start at 0. If we loaded 4 sprites, this number could be 0-3)
1 (This makes sure the sprite is visible)

RUN the program.

That's it, simple as that.

Feel free to save this example with SAVE "spritetest.bas" (use .bas as an easy identifier for basic programs).

Also feel free to go back into the browser and erase the whole folder with 'E'.


*Note. It's good practice to have the command BANK spritey CLEAR at an exit point in your program, otherwise if you keep running it, the Next will keep allocating new areas and you'll get at Out of Memory error.