This tutorial was written by Anti-Gremlin and posted on the forums.
Before you can start programming using the TV3D Engine you must first set up your project. This process is fairly simple but is very important to be able to properly run the engine within your project.
The StdAfx.h file is used for loading up all required include header files for your project. To start off with, we need to add windows.h (needed by TV3D) and the TV3D header files. The example below loads up all available header files from TV3D. Realistically you only need to load up the header files that you will actually use within your application.
Sample (StdAfx.h):
// Windows Header Files #include < windows.h > // TrueVision3D Header Files (For Reference) #include "CTVActor.h" #include "CTVAI.h" #include "CTVAtmosphere.h" #include "CTVBitmapParts.h" #include "CTVCamera.h" #include "CTVCameraFactory.h" #include "CTVCollisionResult.h" #include "CTVDeviceInfo.h" #include "CTVEngine.h" #include "CTVGameController.h" #include "CTVGameControllers.h" #include "CTVGlobals.h" #include "CTVGraphicEffect.h" #include "CTVInputEngine.h" #include "CTVInternalObjects.h" #include "CTVLandscape.h" #include "CTVLightEngine.h" #include "CTVMaterialFactory.h" #include "CTVMathLibrary.h" #include "CTVMesh.h" #include "CTVMiniMesh.h" #include "CTVNode.h" #include "CTVOctree.h" #include "CTVParticleSystem.h" #include "CTVPath.h" #include "CTVPhysics.h" #include "CTVRenderSurface.h" #include "CTVScene.h" #include "CTVScreen2DImmediate.h" #include "CTVScreen2DText.h" #include "CTVShader.h" #include "CTVTextureFactory.h" #include "CTVTileMap.h" #include "CTVViewport.h" #include "HelperFunctions.h" #include "tv_types.h"
Below is a sample of how to create the basic elements of your appilcation to use TV3D. This sample is usually sufficient as is, but you are more than welcome to change it to your needs.
Sample (Tutorial 01.h):
int LoadTV3D(); void GameLoop(); CTVEngine *pEngine; CTVScene *pScene; CTVInputEngine *pInput; bool bDoLoop; HWND frmHWND;
Inside the function BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) you will have to add under the line
if (!hWnd) { return FALSE; }
this:
frmHWND = hWnd;
This is essential if you want your engine to correctly load up.
Sample (Tutorial 01.cpp):
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString( hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING ); LoadString( hInstance, IDC_TUTORIAL01, szWindowClass, MAX_LOADSTRING ); MyRegisterClass( hInstance ); // Perform application initialization: if( !InitInstance( hInstance, nCmdShow ) ) { return FALSE; } hAccelTable = LoadAccelerators( hInstance, (LPCTSTR)IDC_TUTORIAL01 ); LoadTV3D(); // Main message loop: while( bDoLoop ) { // Render Loop if( GetFocus() == frmHWND ) { pEngine->Clear( false ); // Render Everything pScene->RenderAll( true ); pEngine->RenderToScreen(); // If the ESC key is pressed stop the application. if( pInput->IsKeyPressed( cTV_KEY_ESCAPE ) ) { SendMessage( frmHWND, WM_DESTROY, 0, 0 ); } } else { // So we dont process the messages to many times if we are not rendering. Sleep( 100 ); } BOOL peek = PeekMessage( &msg, NULL, 0, 0, TRUE ); if( !TranslateAccelerator( msg.hwnd, hAccelTable, &msg ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } return (int) msg.wParam; } int LoadTV3D() { // Create the pTV Interface first: pEngine = new CTVEngine(); // Set the debug file/options. // Do this before the 3D init so it can log any errors found during init. pEngine->SetDebugMode( true, true ); pEngine->SetDebugFile( "C:\\debugfile.txt" ); // Set your beta-key/license: // pTV->SetLicenseKey(TV_LICENSE_COMMERCIAL, "username", "key"); pEngine->SetBetaKey( "UserName", "BETA-CODE-GOES-IN-HERE" ); // After setting the beta-key/license its time to init the engine: pEngine->Init3DWindowed( frmHWND, true ); // Something good to do is to enable the auto-resize feature: // Get the default viewport and set autoresize to true for it: pEngine->GetViewport()->SetAutoResize( true ); // Lets display the FPS: pEngine->DisplayFPS( true ); // Set the prefered angle system: pEngine->SetAngleSystem( cTV_ANGLE_DEGREE ); // Now after we are done initializing the TVEngine component lets continue: // Create any other components after pTV init-> pScene = new CTVScene(); // Input has an additional init method to call-> pInput = new CTVInputEngine(); // Lets init both keyboard and mouse: pInput->Initialize( true, true ); // Setup the boolean for the loop. bDoLoop = true; return 0; }
Last but not least, we must make sure we clean up after ourselves to stop any form of memory leaks occuring. In doing so, you are successfully unloading everything that you have loaded. Every good programmer cleans up their objects.
Sample (Tutorial 01.cpp :: WndProc()):
case WM_DESTROY: // Stop the loop. bDoLoop = false; // Clean up the scene if( pScene ) { pScene->DestroyAllMeshes(); delete( pScene ); pScene = NULL; } // Destroy the engine last if( pEngine ) { delete( pEngine ); pEngine = NULL; } // Continue normal application quit PostQuitMessage(0); break;