2013/05/18

Sprite function test program #2

This is test program #2 for sprite functions. The sp_image() can be defined sprite from the image at once. And, when you move mass sprites at a time, sp_halt() is essential for preventing flickering.
// Sprite test2
// for V2.00 later only

// Set graphics mode
width(0)
int wgx,wgy
getWidth(wgx,wgy):// Get screen width by graphics mode
//locate(0,2):print "Graphic Width:";wgx;"*";wgy
//
sp_init():// Hide sprite screen
width(32)
vpage(B_SPAGE+B_TPAGE,YES):// display sprite screen
vpriority(TPAGE,SPAGE,GPAGE0,GPAGE1,GPAGE2,GPAGE3)
cls()
tborder(1,CTHCYAN)
//
// Display sprite at touched position
//
str exit$,mes$
if isLocalizeJapan() then {
 exit$="中断"
 mes$="画面にタッチ&ドラッグしてください"
} else {
 exit$="exit"
 mes$="Please touch & drag"
}
setFunctionKey(0, exit$ ,'!')
setFunctionKey(1,"SPst" ,'S')
setFunctionKey(2,"BGst" ,'B')
setFunctionKey(3,"Dprw" ,'p')
setFunctionKey(4,"disp" ,'d')
setFunctionKey(5,"OFF" ,'F')
setFunctionKey(6,"ON" ,'O')
setFunctionKey(7,"BGget",'G')
setFunctionKeyBackgroundImage("funcBack.png")
displayFunctionKeyAll(YES,YES):// v2.0
//
setTouchArea(0,0,0,0,0,YES):// Must need for getting any touch coordinate
//
// You can set 512 patterns, but can display 256 planes.
sp_halt(YES)://  Pause sprite display
int pwx=16,pwy=16
int tsp=0,esp=0
esp=sp_image("IMG1.JPG",110,260,pwx,pwy,tsp,tsp,wgx/3,wgy/3,SPRITE_PRW_BG0SPBG1,YES):// pwx,pwy is adjusted
locate(12,0):print "SP:";tsp;"〜";esp-1

//---------------------
// for functions test
//dim int ptn(255)
//sp_pat(tsp,ptn)
//int fp
//fp=fopen("pat.dat","w")
//fwrite(ptn,255,fp)
//fclose(fp)
//---------------------
//
// sprite pattern (X68 type)
dim char ca(255) = { // mesh
     0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
    , 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1
                , 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0
}
int pb1=3:// palette block No.
int pb2=2:// palette block No.
sp_color(0,&h00000000,pb1):// Transparency
sp_color(0,&h00000000,pb2):// Transparency
sp_color(1,&h000080ff,pb1)
sp_color(1,&h800000ff,pb2)
sp_def(esp,ca)
sp_clr(esp+1)
//
int pg0=0:// BG text page number
int pg1=1:// BG text page number
int x,y
bg_fill(pg0,esp,pb1,SPRITE_FLIP_NONE):// once fill all
// wg?/16 : the number of sprite in screen width/height
// pull out the center
int bwx=wgx/16,bwy=wgy/16
for y=bwy/4 to bwy*3/4
 for x=bwx/4 to bwx*3/4
  bg_put(pg0,x,y,esp+1,pb1)
 next
next
bg_set(1,pg0,YES)
//
bg_fill(pg1,esp+1,pb1,SPRITE_FLIP_NONE):// once clear all
// draw center
for y=bwy*4/10 to bwy*5/10
 for x=0 to bwx
  bg_put(pg1,x,y,esp,pb2)
 next
next
bg_set(0,pg1,YES)
//
//mapSprite(wgx/3,wgy/3,tsp,pwx,pwy)
sp_halt(NO):// Resume sprite display

//
dim float tpoint(2)
int touchCnt
str cs
int dt=0,t
int ts=time():/* start time
locate(0,1):print mes$
//
int i
int x0=-1,y0=-1
int fDisableBG0SPBG1=NO
prfDisableBG0SPBG1(fDisableBG0SPBG1)
int fdisp=YES
sp_disp(fdisp)
repeat
 // timer count until key input
 while (not kbhit())
  t=time()
  if t-ts>dt then { :/* change second
   dt=dt+1
  // locate(20,0):print dt;
  // beep2(0)
  // locate(20,1)
  }
  touchCnt=getTouchPoint(tpoint)
  if (touchCnt>0) then {
   x=tpoint(0):// float → int
   y=tpoint(1)
   if x<>x0 or y<>y0 then {
    locate(0,0):print x;",";y
    mapSprite(x,y,tsp,pwx,pwy)
    //
    x0=x
    y0=y
   }
   // Do not re-displayed when the coordinate is same. Sprite would flicker Failure to do so. 
  }
 endwhile
 // key operation
 cs=inkey$()
 switch cs
  case "S":// Sprite stat
   for i=0 to 5
    locate(0,2+i)
    print "i=";i;":";sp_stat(tsp,i)
   next
   break
  case "B":// BG stat
   for i=0 to 3
    locate(16,2+i)
    print "i=";i;":";bg_stat(0,i)
   next
   break
  case "p":// Disable BG0SPBG1
   if fDisableBG0SPBG1 then fDisableBG0SPBG1=NO else fDisableBG0SPBG1=YES
   prfDisableBG0SPBG1(fDisableBG0SPBG1)
   break
  case "d":// sp_disp
   if fdisp then fdisp=NO else fdisp=YES
   sp_disp(fdisp)
   break
  case "F":// sp_off
   sp_off(tsp,esp-1):// In this program, plane number is same as pattern number
   break
  case "O":// sp_on
   sp_on(tsp,esp-1,SPRITE_PRW_BG0SPBG1):// In this program, plane number is same as pattern number
   break
  case "G":// bg_get
   getBG(pg0,bwx,bwy)
   getBG(pg1,bwx,bwy)
   break
 endswitch
until cs="!"

print "Sprite version=";versionXBiOSSprite$()
//
end

func mapSprite(gx;int,gy;int,tsp;int,pwx;int,pwy;int)
int wx,wy
 sp_halt(YES):// Halt screen update until finish to change the values.
 for wy=0 to pwy-1
  for wx=0 to pwx-1
   // sp_image() ignores palette (because of iOS type)
   sp_setting(tsp,gx+wx*16,gy+wy*16)
   tsp=tsp+1
  next
 next
 sp_halt(NO):// Resume display
endfunc

func prfDisableBG0SPBG1(fDisableBG0SPBG1;int)
 dim str strfDisableBG0SPBG1(1)={"-","D"}
 sp_disableBG0SPBG1(fDisableBG0SPBG1)
 locate(31,0):print strfDisableBG0SPBG1(-fDisableBG0SPBG1)
endfunc

func getBG(pg;int,x;int,y;int)
 int pb,flip,ptn
 ptn=bg_get(pg,x,y,pb,flip)
 locate(0,10+pg*2)  :print "pg=";pg;":x=";x;"/y=";y
 locate(0,10+pg*2+1):print "pb=";pb;"/flip=";flip;"/ptn=";ptn
endfunc



Zip archive file within sample image: XBsprite.zip

No comments:

Post a Comment