'My' Sprite Control...

Blitz3D Forums/Blitz3D Programming/'My' Sprite Control...

WillKoh(Posted 2004) [#1]
This is my own little ver of spritecontrol, but obviously I've missed something (only tiled bkg shows). The "Local..." is for a pre-processor *I'm planning* to make so wrong var-names is still a possible cause (but I've tried to check). The images "boxes_r" and "boxes_a" are not the problem... "boxes_a" represents alpha-values (per pixel) of the image "boxes_r" which is just 4x4 solid color squares... Frame 0,0,first box is what should be showing at MouseX,MouseY in this example. The prog gets & frees texture each time dealing with it (in case that is better somehow)... I also made a ver that stored it in s\tex but that didn't work either.

The variable names are short but do make sense: xfs (x-frames[count]) yfs (y-frames[count]). xf (x-frame) etc...

	
	
	Include "color.bb"
	Include "string.bb"
	
	Global GFW, GFH, GFD, BPP, GFM
	Global SPV, SCM, SFX, SFY
	Global SPF = %1100000011
	
	Type sprtp
	  Field ent ; the mesh of the sprite
	  Field tex ; it's texture (will div into frames)
          Field fgs ; flags used with texture
          Field xfs ; frames on x-axis
	  Field yfs ; frames on y-axis
	  Field xf  ; current x-frame
          Field yf  ; current y-frame
          Field tw ; texture-width
	  Field th ; texture-height
	  Field iw ; image width (if other than texture's, may leave part of texture blank)
	  Field ih; image height (if other than texture's, may leave part of texture blank)
	  Field fw ; frame width
	  Field fh ; frame height
	  Field tag$
	End Type
	
	
	
	;==== TEST
	SpriteGfx3D 640, 480, 32, 2
	img = LoadImage("boxes_r.bmp")
	aph = LoadImage("boxes_a.bmp")
	my.sprtp = JoinAsSprite(img,aph,4,4)
	bkg = LoadImage("c:\windows\santa fe stucco.bmp"):	Rgb $FF0000
	 SetFont LoadFont("Times New Roman",40,True): HidePointer()
	Repeat
		TileBlock bkg, 00, 00
		; on pivot-check removed, it *is* on pivot
		PlaceSprite my, MouseX(), MouseY()
		RenderWorld()
		Flip
	Until KeyHit(1)
	;=========
	
	
	
Function EmptySprite.sprtp(iw,ih,xfs=1,yfs=1,fgs=-1,par=-1)
	
	Local s.sprtp = New sprtp
	Local luc, ruc, ldc, rdc	
	Local tw#, th#, fw, fh
	Local ent, sfc, tex
	
	If fgs = (-1) fgs = SPF
	If par = (-1) par = SPV
	
	ent = CreateMesh(par)
	sfc = CreateSurface(ent)
	tex = CreateTexture(iw,ih,fgs)
	
	tw# = TextureWidth(tex)
	th# = TextureHeight(tex)
	
	fw = Int(Floor(iw / xfs))
	fh = Int(Floor(ih / yfs))
	
	luc = AddVertex(sfc, 0, 0, (z), 0, 0)
	ruc = AddVertex(sfc, 1, 0, (z), 1, 0)
	ldc = AddVertex(sfc, 0,-1, (z), 0, 1)
	rdc = AddVertex(sfc, 1,-1, (z), 1, 1)
	
	AddTriangle (sfc), luc, ruc, rdc
	AddTriangle (sfc), luc, rdc, ldc
	
	ScaleTexture tex, (tw# / fw), (th# / fh)
	ScaleEntity ent, fw, fh, 1.0
	EntityTexture ent, tex
	
	PositionEntity ent, -1000, -1000, 0
	EntityOrder ent, -100
	EntityFX ent, 16 + 1
	
	s\ent = ent: s\tex = tex:	s\fgs = fgs
	s\xfs = xfs: s\yfs = yfs
	
	s\tw = tw: s\th = th
	s\iw = iw: s\ih = ih
	s\fw = fw: s\fh = fh
	
	Return s
	
End Function



Function JoinAsSprite.sprtp(rimg,aimg=0,xfs=1,yfs=1,fgs=-1,par=-1)
	
	If flags = (-1) flags = SPF
	If parent = (-1) parent = SPV
	
	iw = ImageWidth(rimg): ih = ImageHeight(rimg)
	s.sprtp = EmptySprite(iw,ih,xfs,yfs,fgs,par)	
	
	gb = GraphicsBuffer()
	rb = ImageBuffer(rimg)
	ab = ImageBuffer(aimg)
	sb = SpriteBuffer(s)
	
	SetBuffer sb
	 LockBuffer()
	
	If aimg
	 	If ImageWidth(aimg) <> iw Or ImageHeight(aimg) <> ih
			DebugLog "JoinAsSprite: RegImage <> AlphaMap size!"
		End If
		 lx = (w - 1)
		 ly = (h - 1)
		LockBuffer rb
		 LockBuffer ab
		For y = 0 To ly
			For x = 0 To lx
				pixr = ReadPixelFast(x,y,rb) And $00FFFFFF
				pixa = ReadPixelFast(x,y,ab) And $0000FF00
				WritePixelFast x,y, pixr Or (pixa Shl 16)
			Next
		Next
		UnlockBuffer rb
		UnlockBuffer ab
	Else
	 	DrawBlock rimg, 0, 0
	End If
	
	 UnlockBuffer()
	
	SetBuffer gb
	 Return s
	
End Function



Function PlaceSprite(s.sprtp,x#,y#,z#=0,xf=-1,yf=-1)
	
	Local nx#, ny#, u#, v#
	
	If xf = (-1) xf = s\xf
	If yf = (-1) yf = s\yf	
	
	If xf <> s\xf Or yf <> s\yf
		brs = GetEntityBrush(s\ent)
		tex = GetBrushTexture(brs)
		u# = (xf * s\fw) / s\tw
		v# = (yf * s\fh) / s\th
		 PositionTexture s\tex, -u#, -v#
		s\xf = xf: s\xf = yf
		 FreeBrush brs
		 FreeTexture tex
	End If
	
	nx# = +(x# - 0.5): ny# = -(y# - 0.5)
	PositionEntity s\ent, nx#, ny#, z#
	
End Function



Function SpriteBuffer(s.sprtp,xf=0,yf=0)
	
	brs = GetEntityBrush(s\ent)
	tex = GetBrushTexture(brs)
	
	SFX = xf * s\tw: SFY = yf * s\th
	
	Return TextureBuffer(tex)
        FreeBrush brs
	FreeTexture tex
	
End Function



Function SpriteGfx3D(w,h,d=0,m=0,pds#=0)
	
	;Glob SCM, SPV, GFM
	Graphics3D w, h, d, m
	SCM = CreateCamera()
	SPV = SpritePivot(SCM,pds#)
	CameraClsMode SCM, False, True
	GetGfxVars(): GFM = m
	
End Function



Function SpritePivot(cam,dist#=0)
	
	Local piv, asp#, sc#
	
	piv = CreatePivot(cam)
	asp# = GH / Float(GW): sc# = 2.0 / GW
	PositionEntity piv, -1,asp#,dist#
	ScaleEntity piv, sc#,sc#,sc#
	Return piv
	
End Function



Function GetGfxVars()
	
	GFW = GraphicsWidth()
	GFH = GraphicsHeight()
	GFD = GraphicsDepth()
	BPP = GFD
	
End Function 


'string.bb' is not used yet

Just to supply all; Rgb from color.bb:

Function Rgb(value)
	Color (value Shl 8 Shr 24), (value Shl 16 Shr 24), (value Shl 24 Shr 24)
End Function



slenkar(Posted 2004) [#2]
how does it improve on the orgiinal?


WillKoh(Posted 2004) [#3]
Not much... it's suppost the use a 'map' (an image) for alpha values of the pixels in the regular image. And there's little nice commands like SpriteBuffer, and the offset for that frame: SFX,SFY. The sprtp type fits with object-based programming style...