Hi there Folks,
Here's a new piece of code that needs some of your valuable assistance and fixing.
The Aim is to have an image ( for this example a solid filled box is used ) wobble in both directions. Based upon the method used in Blitz Basic, where you would use DrawImageRect which I have added, you'll see it in the listing as a sub called DrawBufferRect.
Individually do a sin / cos on the x and y's and using directly drawing from the box1 image buffer and screenbuffer works. For this Double Wobble I have used temporary buffers to deal with updating the shape changing.
I then found I was having crashes with doing this, and came up with the idea of having the temporary storage buffer bigger than the box's one, and then in the UpdateWobbleWobble, centering it all to avoid the problem with boundary checks. As you'lll see from the code below, the centering isnt quite right, everything to the left is wiggling, but to the right is a solid line down the side.
'
' eXpErImEnTaL
' Wobble Wobble V1.0
' (c) Clyde Radcliffe / Gravity 2009
'
Option explicit
Const As Single PI=3.14159265
Const As Single D2R=PI/180.0
#Include once "tinyptc.bi"
Type gfxbuffer
wwidth as integer
height as integer
pixels as uinteger pointer
End Type
Declare Sub ClearBuffer ( ByVal buffer As gfxbuffer Pointer, ByVal col As UInteger=0)
Declare Sub ClearBuffers( ByVal buffer1 As gfxbuffer Pointer,_
ByVal buffer2 As gfxbuffer Pointer,_
ByVal Col As UInteger=0 )
Declare Sub DrawBuffer( ByVal PosX As Integer,_
ByVal PosY As Integer,_
ByVal Buffer As gfxbuffer Pointer )
Declare Sub DrawBufferRect( ByVal PosX As Integer,_
ByVal PosY As Integer,_
ByVal RectX As Integer,_
ByVal RectY As Integer,_
ByVal RectWWidth As Integer,_
ByVal RectHeight As Integer,_
ByVal Buffer As gfxbuffer Pointer,_
ByVal Dest As gfxbuffer Pointer )
Declare Sub RunWobbleWobble()
Declare Sub UpdateWobbleWobble( ByVal PosX As Integer,_
ByVal PosY As Integer,_
ByVal Mult As Integer,_
ByVal Size As Integer,_
ByVal Angle As Integer,_
ByVal Source As gfxbuffer Pointer,_
ByVal Buffer1 As gfxbuffer Pointer,_
ByVal Buffer2 As gfxbuffer Pointer )
Declare Function CreateBox( ByVal WWidth As Integer,_
ByVal Height As Integer,_
ByVal Col As UInteger=0 ) As gfxbuffer pointer
Declare Function CreateBuffer( ByVal WWidth As Integer,_
ByVal Height As Integer) As gfxbuffer pointer
Declare Function graphics( ByVal Title As String,_
ByVal WWidth As Integer,_
byval Height As Integer ) As gfxbuffer pointer
Dim Shared As gfxbuffer Pointer ScreenBuffer, Box1, BoxTemp1, BoxTemp2
ScreenBuffer=Graphics("Wobble Wobble",640,480)
Box1 =CreateBox ( 64, 64, &HFFFFFF )
BoxTemp1=CreateBuffer( 64*4, 64*4 )
BoxTemp2=CreateBuffer( 64*4, 64*4 )
RunWobbleWobble()
Sub RunWobbleWobble()
Dim As Integer WobAng
Dim Key As String
While Key<>Chr(27)
UpdateWobbleWobble( 0, 0, 1, 32, WobAng, Box1, BoxTemp1, BoxTemp2 )
DrawBuffer( 120, 120, BoxTemp2 )
PTC_Update @ScreenBuffer->Pixels[0]
WobAng+=2 : if WobAng>360 then WobAng=WobAng-360
Key=Inkey
ClearBuffer( ScreenBuffer )
ClearBuffers( BoxTemp1,_
BoxTemp2 )
Wend
End Sub
Sub clearbuffer( Byval buffer As gfxbuffer Pointer,_
ByVal Col As UInteger=0 )
Dim As UInteger Pointer pixel=buffer->pixels
Dim As UInteger Pointer last=pixel+(buffer->WWidth*buffer->Height)
while pixel<last
*pixel=col
pixel+=1
wend
End Sub
Sub clearbuffers( Byval buffer1 As gfxbuffer Pointer,_
ByVal buffer2 As gfxbuffer Pointer,_
ByVal Col As UInteger=0 )
Dim As UInteger Pointer pixel1=buffer1->Pixels, pixel2=buffer2->Pixels
Dim As UInteger Pointer last=pixel1+(buffer1->WWidth*buffer1->Height)
while pixel1<last
*pixel1=col
*pixel2=col
pixel1+=1
pixel2+=1
wend
End Sub
Sub UpdateWobbleWobble( ByVal PosX As Integer,_
ByVal PosY As Integer,_
ByVal Mult As Integer,_
ByVal Size As Integer,_
ByVal Angle As Integer,_
ByVal Source As gfxbuffer Pointer,_ )
ByVal Buffer1 As gfxbuffer Pointer,_)
ByVal Buffer2 As gfxbuffer Pointer )
Dim As Integer x,y
Dim As Integer CX, CY
'
' This works fine.
'
For y=0 to Source->Height-1
CX=( Source->Height\2 ) - (PosX+Cos(( Angle+y*Mult ) * ( D2R )) * ( Size-y*Size\Source->Height ) )'/ 2 )
DrawBufferRect( cx+PosX,_'+Cos( ( Angle+y*Mult ) * (PI/180) )*( Size-y*Size\Source->Height ),_
PosY+y,_
0,_
y,_
Source->WWidth,_
1,_
Source,_
Buffer1)
Next
'
' This is whats up??
'
For x=0 To Source->WWidth-1
CY=( Source->WWidth\2 ) - (PosY+Cos(( Angle+x*Mult ) * ( D2R )) * ( Size-x*Size\Source->WWidth ) )'/ 2 )
DrawBufferRect( PosX+x,_
cy+POsY,_'PosY+Sin( ( Angle+x*Mult ) * (PI/180) )*( Size-x*Size\Source->WWidth ),_
x,_
0,_
1,_
Source->Height,_
Buffer1,_
Buffer2)
Next
End Sub
Function CreateBox( ByVal WWidth As Integer,_
ByVal Height As Integer,_
ByVal Col As UInteger=0 ) As gfxbuffer Pointer
Dim buffer As gfxbuffer Pointer=CreateBuffer(WWidth,Height)
Dim As Integer x,y
For y=0 To Height-1
For x=0 To WWidth-1
Buffer->Pixels[ x+y*WWidth ]=Col
Next
Next
Return Buffer
End Function
Sub DrawBuffer( ByVal PosX As Integer, ByVal PosY As Integer, ByVal Buffer As gfxbuffer Pointer )
Dim As Integer x,y
For y=0 To Buffer->Height-1
For x=0 To Buffer->WWidth-1
ScreenBuffer->Pixels[ (x+PosX)+(y+PosY) * ScreenBuffer->WWidth ]=Buffer->Pixels[ x+y*Buffer->WWidth ]
Next
Next
End Sub
Sub DrawBufferRect( ByVal PosX As Integer,_
ByVal PosY As Integer,_
ByVal RectX As Integer,_
ByVal RectY As Integer,_
ByVal RectWWidth As Integer,_
ByVal RectHeight As Integer,_
ByVal Buffer As gfxBuffer Pointer,_
ByVal Dest As gfxBuffer Pointer )
Dim As Integer x,y
For y=0 to rectheight-1
For x=0 To rectwwidth-1
Dest->Pixels[ ( x+PosX ) + ( y+PosY ) * Dest->WWidth ]=Buffer->Pixels[ ( x+RectX ) + ( y+RectY ) * Buffer->WWidth ]
Next
Next
End Sub
Function CreateBuffer( ByVal WWidth As Integer, ByVal Height As Integer) As gfxbuffer pointer
Dim As gfxbuffer Pointer Buffer=Callocate( Len(gfxbuffer)+Len(UInteger)*WWidth*Height)
Buffer->WWidth=WWidth
Buffer->Height=Height
Buffer->Pixels=Cast( UInteger Pointer, Cast( Byte Pointer, buffer )+Len( gfxbuffer ))
Return Buffer
End function
Function graphics( Byval Title As String, ByVal WWidth As Integer, ByVal Height As Integer ) As gfxbuffer Pointer
ptc_open( Title, WWidth, Height )
Return createbuffer(WWidth,Height)
End Function
Big thanks for you help in correcting it and cheers,
Clyde.