Ok so i made a very small demo. All it does is draw an 8x8 sprite and lets you move it around the screen until you press CLEAR.
The files can be found here:
http://tr1p1ea.net/files/downloads/xsprtut.zip
Here is the sprite sheet for the appvar (also in the zip):
And here is the source code with comments:
Quote:
"SET LCD 2X MODE
real(0,1,1,0
"CLEAR SCREEN (BOTH SIDES)
real(0,3,4,255,1
real(0,3,4,255,0
"LOAD APPVAR XSPRITE INTO MEM
"XSPRITES
real(5,0,0
"SET VARS FOR SPRITE TO CENTRE SCREEN
real(1,1,0,160/2
real(1,1,1,120/2-4
"INIT BACKUP VARS FOR SPRITE CLEARING. XVARS 0,1=X,Y AND 2,3=BACKUP X,Y
real(1,1,2,160/2
real(1,1,3,120/2-4
"SET TIOSVAR S TO BE THE SPRITE INDEX IN THE APPVAR
"CHANGE THIS IF YOU WANT TO DRAW A DIFFERENT SPRITE
0→S
"MAIN LOOP TO DRAW+MOVE SPRITE UNTIL CLEAR IS PRESSED
Repeat real(2,0,0)=15
"DRAW SPRITE AND UPDATELCD
real(4,1,0,1,1,1,0,0,1,1,0,S
"DRAW OVER OLD SPRITE POSITION TO CLEAR IT ON OLD LCD SIDE
real(7,9,real(1,0,2),real(1,0,3),8,8,255,0
"COPY CURRENT XY TO BACKUP XY
real(1,1,2,real(1,0,0
real(1,1,3,real(1,0,1
"USE XLIBC INPUT FUNCTION TO UPDATE USERVARS BASED ON ARROW PRESS
real(2,1,0,1,8,8
End
"CLEAR SCREEN (BOTH SIDES)
real(0,3,4,255,1
real(0,3,4,255,0
"RESTORE LCD MODE
real(0,1,0,1
There are a few things that you need to know about xLIBC. The first is that the drawing functions all work in Half-Res mode, meaning that the screen is divided in 2, with only 1 half visible at any time. This enables you to draw to the 'non-visible' side of the screen and properly prepare it before display - reducing flicker and such; like double buffering. This is why we see some of the commands issued twice in the code, because it needs to operate on both sides like:
Quote:
"CLEAR SCREEN (BOTH SIDES)
real(0,3,4,255,1
real(0,3,4,255,0
The code pretty much readies the screen, sets up some initial XY values for the sprite, then in the main loop it does the following:
Draws the sprite and updates the LCD so its visible to the user
Draws an 8x8 white rectangle at the 'OLD' XY position on the 'non-visible' side of the screen, effective clearing the sprite
Copies the current XY to the old XY for later use
Uses the built in xLIBC GetKey command to test for arrow presses and update the required XY uservars the line:
Quote:
real(2,1,0,1,8,8
This will test for UP,DOWN,LEFT,RIGHT and update uservars 0,1 by +/-8 pixels if pressed, all in 1 line! There are other GetKey functions as well that can speed up some operations (tile collisions etc)
As a bonus the TIOS VAR 'S' holds the index for the TILE/SPRITE index in the appvar to draw. In the sprite sheet there are 2 sprites that occupy index's 0 & 1 (0=blue, 1 = red). If you change this line from:
Quote:
"CHANGE THIS IF YOU WANT TO DRAW A DIFFERENT SPRITE
0→S
to
Quote:
"CHANGE THIS IF YOU WANT TO DRAW A DIFFERENT SPRITE
1→S
It will draw the red sprite instead!
*NOTE* That all the text comments in the code slow down the program a fair bit, but its a mini-tutorial afterall. Here is a screenie!:
Sorry if its a bit rushed, happy to take any questions.
Of course you will need the latest version of DCSE8 on your calc to run the example.
EDIT - Just for those interested, and to ensure that advertising of DCSE libs is accurate, here is a screenshot of the same program with the text comments removed from the main loop in the code:
Same code, much speedier program .