Software Archive
Read-only legacy content

Animation

bob_johnson
Beginner
804 Views
I am tring to animate graphics. When I run my program it executes the code but only the last frame shows up. There is a delay which I assume is because it is "drawing" the frames. So I must not be doing something that will update the window for each frame.

Here is a portion of my code that does the animation. The routine gtest does the actual drawing of the frame.

case (IDM_GRAPH_CYCLE)
do i=1,nhours
pdate = dates1(nhours)
ptime = times1(nhours)
newdata = .true.
call gtest (hWnd)
retlog = UpdateWindow(hWnd)
call sleep(100)
enddo
ret = InvalidateRect (hWnd, NULL_RECT, .TRUE.)
MainWndProc = 0
return
0 Kudos
4 Replies
bob_johnson
Beginner
804 Views
OK here is the code again. I hope it comes out better.

 
           case (IDM_GRAPH_CYCLE) 
	do i=1,nhours 
		pdate = dates1(nhours) 
		ptime = times1(nhours) 
		newdata = .true. 
		call gtest (hWnd) 
		retlog = UpdateWindow(hWnd) 
		call sleep(100) 
	enddo 
	ret = InvalidateRect (hWnd, NULL_RECT, .TRUE.) 
                MainWndProc = 0 
                return 
   
0 Kudos
Jugoslav_Dujic
Valued Contributor II
804 Views
It's almost good -- you should move InvalidateRect before UpdateWindow; UpdateWindow updates only if there's something to update -- you have to tell what it is with InvalildateRect.
0 Kudos
bob_johnson
Beginner
804 Views
Jugoslav,

That worked great !! It is now cycling through the frames. However it is not smooth. It shows each frame as it is drawn. Do I need to do some sort of double buffering ? If so how do I do that. Thanks
0 Kudos
Jugoslav_Dujic
Valued Contributor II
804 Views
Double-buffering improves the animation, surely. You should use:

 
hBufferDC = CreateCompatibleDC(hWindowDC, iWidth, iHeight) 
hBmp = CreateCompatibleBitmap(hWindowDC) 
iSt = SelectObject(hBufferDC, hBmp) 


to create buffer DC. Your WM_PAINT handler should have, apart from
BeginPaint/EndPaint, only:

 
hDC = BeginPaint(hWnd, ps) 
iSt = BitBlt(hDC, ps%rcPaint%Left, ps%RcPaint%Top,  & 
ps%rcPaint%Right-ps%rcPaint%Left, ps%rcPaint%Bottom-ps%rcPaint%Top, & 
hBufferDC, ps%rcPaint%Left, ps%rcPaint%Top,SRCCOPY) 


Always draw on hBufferDC and you can do it whenever you need update (not on WM_PAINT). Only ensure that you call InvalidateRect when you do the drawing.
Of course, take care that of proper creation/destruction (DeleteDC) of hBufferDC -- usually, you create it only once and destroy it at program end.

HTH

Jugoslav
0 Kudos
Reply