Code archives/3D Graphics - Effects/Gradually shrink camera viewport.

This code has been declared by its author to be Public Domain code.

Download source code

Gradually shrink camera viewport. by Bill Stanbrook
(Posted 23 years ago)
The first function gradually shrinks the camera viewport(s) to give the effect that the viewport is moving away from you. The aspect ratio of the viewport is preserved as it shrinks. You can specify a parameter (in seconds) which determines how long the shrinking process takes.

The effect is probably best suited to end of game, or death, scenes.

This function is meant to be used as a self contained routine, but it could be adapted to work as inline code in your main loop. You could also adapt it for use with 2D viewports.

The second function simply resets the viewport(s) to the specified size. If you only had one viewport to reset and no 'tidy up' rendering to do then you could simply use the 'CameraViewport' command directly.

Function shrink_camera_viewports_to_zero_size( the_vertical_shrink_time#, the_viewport_width, the_viewport_height )
; NOTES:-
; 'the_vertical_shrink_time#' is the time, in seconds, that the effect should take to complete, assuming that the viewport is wider than it is high.
; 'the_viewport_width' and 'the_viewport_height' are the viewport width and height.
; This code is designed to be used with viewports that take up all the window or full screen drawing area. The code should be adapted for other arrangements.

x# = 0
y# = 0
w# = the_viewport_width
h# = the_viewport_height

the_main_step# = the_viewport_height / the_vertical_shrink_time#
the_aspect_ratio# = Float ( the_viewport_width ) / Float ( the_viewport_height )

ClsColor 0, 0, 0 ; The background color. You can remove this if you don't use 'Cls' below.

the_old_millisecs_time = MilliSecs ()

While ( w# > 0 ) And ( h# > 0 ) And ( Not KeyHit ( 1 ) )
	CameraViewport camera_1, x#, y#, w#, h# ; Changes the viewport size of your main camera.
	CameraViewport HUD_camera, x#, y#, w#, h# ; Changes the viewport size of your HUD camera, if you've got one. Remove if not.
	Cls ; Comment this out to get a paint streak effect.
	RenderWorld
	Delay ( 1 ) ; This is here to give Windows some breathing room.
	VWait
	Flip False

	; -- Calculate the delta time to regulate the speed of the effect.
	the_millisecs_time = MilliSecs ()
	the_time_taken = the_millisecs_time - the_old_millisecs_time
	If the_time_taken > 100 Then the_time_taken = 100 ; Stop abrupt jumps from occurring due to disk accesses, etc.
	the_delta_time# = the_time_taken / 1000.0
	the_old_millisecs_time = the_millisecs_time
	;^^^^^^


	the_step# = the_main_step# * the_delta_time#

	h# = h# - the_step#
	w# = h# * the_aspect_ratio#
	x# = ( the_viewport_width - w# ) / 2.0
	y# = ( the_viewport_height - h# ) / 2.0

Wend

End Function

; --- Reset the viewports to their original size. ---
Function reset_the_camera_viewports( the_viewport_width, the_viewport_height )
; NOTES:-
; 'the_viewport_width' and 'the_viewport_height' are the viewport width and height.

	CameraViewport camera_1, 0, 0, the_viewport_width, the_viewport_height ; Changes the viewport size of your main camera.
	CameraViewport HUD_camera, 0, 0, the_viewport_width, the_viewport_height ; Changes the viewport size of your HUD camera, if you've got one. Remove if not.
	RenderWorld : VWait : Flip False ; You can remove this line if you want to do the rendering elsewhere.
End Function
; The code is at: http://www.blitzbasic.com/codearcs/codearcs.php?code=909