PDA

View Full Version : Animation within a Callback



marcuslee
29-03-2009, 17:28
Hey everyone! 8)

I was playing with an old idea of mine, and I came up with an animation script. I put the animation script in a callback, so it plays when the window opens. Of course, you could have it play at any time, really. But, anyway, the animation is long enough that the user could do something else, like click close or so other user interaction you have going on at this time, which as you can see isn't allowed by the script.

I have thought of a couple of scenarios with this idea. One, prevent the user from doing anything. This would only work with shorter animations. Two, have the animation running in the background but allow user input elsewhere. I like this option because you could have a spinning logo going the whole time (which, I know would get kinda of annoying ... but that's okay)

So, I guess my actual question is this: Is there a way to have two parts of the script execute either at the same time or one after another but before the first one is finished?

Second question: If double execution isn't possible, is there a way to have an animation (setup the way I have it set up here with the indiv frames as image files) play in the background, letting the user do something else?

These two questions may be asking the same thing, I am not sure. Anyway, any ideas?



Mark

P.S. - Here is the code of the Flipping Coin animation:




#MINVERSION 1.7.0.0
#SCRIPTVERSION 1.0.0.1

USES "UI"
USES "FILE"

DECLARE FUNCTION GetDC LIB "USER32.DLL" ALIAS "GetDC" ( BYVAL hWnd AS DWORD ) AS DWORD

GLOBAL hDlg AS LONG

DIM PNGFileName AS STRING = APP_SOURCEPATH + "Images\Coin.PNG"
DIM PNGFileName2 AS STRING = APP_SOURCEPATH + "Images\Coin2.PNG"
DIM PNGFileName3 AS STRING = APP_SOURCEPATH + "Images\Coin3.PNG"
DIM PNGFileName4 AS STRING = APP_SOURCEPATH + "Images\Coin4.PNG"
DIM PNGFileName5 AS STRING = APP_SOURCEPATH + "Images\Coin5.PNG"
DIM PNGFileName6 AS STRING = APP_SOURCEPATH + "Images\Coin6.PNG"
DIM PNGFileName7 AS STRING = APP_SOURCEPATH + "Images\Coin7.PNG"
DIM PNGFileName8 AS STRING = APP_SOURCEPATH + "Images\Coin8.PNG"
DIM PNGFileName9 AS STRING = APP_SOURCEPATH + "Images\Coin9.PNG"
DIM PNGFileName10 AS STRING = APP_SOURCEPATH + "Images\Coin10.PNG"
DIM PNGFileName11 AS STRING = APP_SOURCEPATH + "Images\Coin11.PNG"
DIM PNGFileName12 AS STRING = APP_SOURCEPATH + "Images\Coin12.PNG"
DIM PNGFileName13 AS STRING = APP_SOURCEPATH + "Images\Coin13.PNG"
DIM PNGFileName14 AS STRING = APP_SOURCEPATH + "Images\Coin14.PNG"
DIM Status AS DWORD
DIM FileName AS STRING
DIM RecordNum AS LONG
DIM BodyPart AS STRING
DIM LoadedFile AS STRING
DIM n, Repeat AS INTEGER
DIM pixel2, xVar, yVar, x AS LONG
DIM hBitmap, hGpBitmap, hBitmap2, hGpBitmap2 AS LONG

FUNCTION TBMAIN( )
IF USES( "GDIP" ) <= 0 THEN
MSGBOX 0, "GDI+ lib not found"
STOP
END IF
DIALOG NEW PIXELS, 0, "Flipping Coin", - 1, - 1, 200, 200, _
%WS_DLGFRAME OR _
%DS_CENTER OR _
%WS_CAPTION OR _
%WS_SYSMENU OR _
%WS_OVERLAPPEDWINDOW, _
0 TO hDlg
DIALOG SHOW MODAL hDlg, CALL mainProc( )
END FUNCTION

CALLBACK FUNCTION mainProc( ) AS LONG
STATIC FileContents AS STRING
SELECT CASE CBMSG
CASE %WM_INITDIALOG
' -- Loading images using our functions
tImage_Load( Image1, PNGFileName )
tImage_Load( Image2, PNGFileName2 )
tImage_Load( Image3, PNGFileName3 )
tImage_Load( Image4, PNGFileName4 )
tImage_Load( Image5, PNGFileName5 )
tImage_Load( Image6, PNGFileName6 )
tImage_Load( Image7, PNGFileName7 )
tImage_Load( Image8, PNGFileName8 )
tImage_Load( Image9, PNGFileName9 )
tImage_Load( Image10, PNGFileName10 )
tImage_Load( Image11, PNGFileName11 )
tImage_Load( Image12, PNGFileName12 )
tImage_Load( Image13, PNGFileName13 )
tImage_Load( Image14, PNGFileName14 )
' -- Positioning them
tImage_SetPos( Image1, 10, 10 )
tImage_SetPos( Image2, 10, 10 )
tImage_SetPos( Image3, 10, 10 )
tImage_SetPos( Image4, 10, 10 )
tImage_SetPos( Image5, 10, 10 )
tImage_SetPos( Image6, 10, 10 )
tImage_SetPos( Image7, 10, 10 )
tImage_SetPos( Image8, 10, 10 )
tImage_SetPos( Image9, 10, 10 )
tImage_SetPos( Image10, 10, 10 )
tImage_SetPos( Image11, 10, 10 )
tImage_SetPos( Image12, 10, 10 )
tImage_SetPos( Image13, 10, 10 )
tImage_SetPos( Image14, 10, 10 )
CASE %WM_PAINT
' -- Rendering Image1
tImage_Render( Image1, CBHNDL )
FOR Repeat = 1 TO 2
tImage_Render( Image2, CBHNDL )
n = SLEEP 100
tImage_Render( Image3, CBHNDL )
n = SLEEP 100
tImage_Render( Image4, CBHNDL )
n = SLEEP 100
tImage_Render( Image5, CBHNDL )
n = SLEEP 100
tImage_Render( Image6, CBHNDL )
n = SLEEP 100
tImage_Render( Image7, CBHNDL )
n = SLEEP 100
tImage_Render( Image8, CBHNDL )
n = SLEEP 100
tImage_Render( Image9, CBHNDL )
n = SLEEP 100
tImage_Render( Image10, CBHNDL )
n = SLEEP 100
tImage_Render( Image11, CBHNDL )
n = SLEEP 100
tImage_Render( Image12, CBHNDL )
n = SLEEP 100
tImage_Render( Image13, CBHNDL )
n = SLEEP 100
tImage_Render( Image14, CBHNDL )
n = SLEEP 100
NEXT Repeat
tImage_Render( Image1, CBHNDL )
CASE %WM_LBUTTONDOWN
CASE %WM_LBUTTONUP
CASE %WM_SYSCOMMAND
SELECT CASE CBCTL
CASE %SC_CLOSE
IF MSGBOX( 0, "Are you sure you want to exit the program?", %MB_YESNO OR %MB_ICONQUESTION, "CONFIRM EXIT" ) = %IDYES THEN
' -- Do not forget to clean up resources
tImage_Clear( Image1 )
tImage_Clear( Image2 )
tImage_Clear( Image3 )
tImage_Clear( Image4 )
tImage_Clear( Image5 )
tImage_Clear( Image6 )
tImage_Clear( Image7 )
tImage_Clear( Image8 )
tImage_Clear( Image9 )
tImage_Clear( Image10 )
tImage_Clear( Image11 )
tImage_Clear( Image12 )
tImage_Clear( Image13 )
tImage_Clear( Image14 )
DIALOG END CBHNDL
END IF
RETURN( 1 )
END SELECT
END SELECT
END FUNCTION

' -- Image handling functions

TYPE tImage
fileName AS STRING
pImage AS DWORD
posX AS LONG
posY AS LONG
END TYPE

GLOBAL Image1, Image2, Image3, Image4, Image5, Image6 AS tImage
GLOBAL Image7, Image8, Image9, Image10, Image11, Image12 AS tImage
GLOBAL Image13, Image14 AS tImage

FUNCTION tImage_Load( myimage AS tImage, fileName AS STRING ) AS LONG
myImage.fileName = fileName
myImage.pImage = Gdip_LoadImageFromFile( fileName )
END FUNCTION

FUNCTION tImage_SetPos( myimage AS tImage, x AS LONG, y AS LONG )
myImage.posX = x
myImage.posY = y
END FUNCTION

FUNCTION tImage_Render( myimage AS tImage, hDlg AS DWORD )
DIM pGraphics AS DWORD
pGraphics = Gdip_CreateFromHDC( GetDC( hDlg ))
Gdip_DrawImage( pGraphics, myimage.pImage, myImage.posX, myImage.posY )
IF pGraphics THEN Gdip_DeleteGraphics( pGraphics )
END FUNCTION

SUB tImage_Clear( myimage AS tImage )
IF myimage.pImage THEN Gdip_DisposeImage( myimage.pImage )
END SUB

peter
29-03-2009, 21:35
Hi Marcuslee,

this is a big source code and was thinking whether i could write
a smaller coin program.
this is of course not what you meant with your source code.
but i was a little bit animated thereof.
please, take a look into my source code.

Peter

zak
03-09-2010, 16:57
the program file "Flipping Coin.tbasic" in the list and inside "FlippingCoin.zip" contains in line 32:
DIM n, Repeat AS INTEGER
and in line 98 :
For Repeat = 1 to 2
...

line 127:
Next Repeat
this gives an error since repeat is a thinbasic keyword, we can replace it by r to work in v 1.8.6, if possible to correct this.
a beautifull code thanks marcuslee
also thanks for peter for his another beautifull coin program