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
| 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 |