Code archives/3D Graphics - Misc/Commented 3D game framework

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

Download source code

Commented 3D game framework by BlitzSupport2001
This code provides a framework for new Blitz 3D programmers to start from, and will keep your game running at the same frame-rate on any machine, regardless of speed; those that can't keep up will simply 'drop' excess frames to keep up.

This includes Mark Sibly's frame-tweening code -- just set the desired FPS at the top (the 'gameFPS' global variable); note that there's not much point in going much beyond 60-85FPS due to standard monitor frequency settings (and if you ask me, 30FPS is just fine).

Just replace the UpdateGame () function with your own game's update code (ie. keychecks, positioning of entities, etc).

Simon Armstrong ('Skidmarks' developer) describes Mark's code as 'the Bible' on 3D frame-tweening, so you *know* it doesn't get any better than this! ;)
; ------------------------------------------------------------------
; 	GameCore -- support@blitzbasic.com
; ------------------------------------------------------------------
; The basics of a frame-limited Blitz 3D game, ready to rock
; ------------------------------------------------------------------
;             Adapted from Mark Sibly's code
; ------------------------------------------------------------------



; ------------------------------------------------------------------
;	Game's frames-per-second setting
; ------------------------------------------------------------------

Global gameFPS = 50

; ------------------------------------------------------------------
;	Open 3D display mode
; ------------------------------------------------------------------

Graphics3D 640, 480

; ------------------------------------------------------------------
; Single camera setup
; ------------------------------------------------------------------

cam = CreateCamera ()
CameraViewport cam, 0, 0, GraphicsWidth (), GraphicsHeight ()

; ------------------------------------------------------------------
; General setup
; ------------------------------------------------------------------

; Load and arrange objects, textures, etc here...

	; Quick example (just delete this)...
	
	Global box = CreateCube ()
	MoveEntity box, 0, 0, 5

; ------------------------------------------------------------------
;	Frame limiting code setup
; ------------------------------------------------------------------

framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod

Repeat

	; --------------------------------------------------------------
	; Frame limiting
	; --------------------------------------------------------------

	Repeat
		frameElapsed = MilliSecs () - frameTime
	Until frameElapsed

	frameTicks = frameElapsed / framePeriod
	
	frameTween# = Float (frameElapsed Mod framePeriod) / Float (framePeriod)

	; --------------------------------------------------------------
	; Update game and world state
	; --------------------------------------------------------------
	
	For frameLimit = 1 To frameTicks
	
		If frameLimit = frameTicks Then CaptureWorld
		frameTime = frameTime + framePeriod
		
		UpdateGame ()

		UpdateWorld
			
	Next

	; --------------------------------------------------------------
	; **** Wireframe for DEBUG only -- remove before release! ****
	; --------------------------------------------------------------
		
	If KeyHit (17): w = 1 - w: WireFrame w: EndIf ; Press 'W'
	
	; --------------------------------------------------------------
	; Draw 3D world
	; --------------------------------------------------------------

	RenderWorld frameTween

	; --------------------------------------------------------------
	; Show result
	; --------------------------------------------------------------

	Flip

Until KeyHit (1)

End

; ------------------------------------------------------------------
; Game update routine, called from frame limiting code
; ------------------------------------------------------------------

Function UpdateGame ()

	; Get keypresses, move entities, etc

	; EXAMPLE CODE -- REMOVE! Uses cursors...
	If KeyDown (203) TurnEntity box, 0, 0.5, 0
	If KeyDown (205) TurnEntity box, 0, -0.5, 0
	
End Function

Comments

mrjh2006
That's very usefull to me. I was just wondering how to do that. Thanks!


mrjh2006
This is great, but what if you need to use 2d and 3d code, forcing you to place some code after the RenderWorld command so that some of the game updating code is not in the updateGame() function. Would this still work? And if not, could you tell me how to make it work? Please?


Trader35642007
nice! i would say make this a sticky


Kryzon2008
It's not the best option. If you take a look at this section here:
Repeat
   frameElapsed = MilliSecs () - frameTime
Until frameElapsed

Here you are halting the program execution, yes, but at the cost of the CPU going through a useless loop.

Frame limiting should be about releasing the CPU to do other things rather than waste it on a loop.


BIG BUG2008
If you had understood the code, you would know that this code waits only up to 1 / 1000 of a second.
On the other hand you can add a Delay(1) of course...


Kryzon2008
Okay, thanks for the heads up :)


EDIT: Don't act like "it's ok, it's just 1 millisecond and it doesn't make a difference".

It doesn't matter if it's 1 millisecond or 1 thousand.
Wasting CPU in a loop is always a waste, logically.


Code Archives Forum