Welcome fellow delphite, I’m very pleased to have another delphi user join us in the ranks of TrueVision 3D. I’m here to teach you how to use TrueVision 3D in Delphi. I’ll cover installing it, and making a Hello World application as well.
Let’s get started with installing TrueVision 3D for use with delphi. First of install TrueVision 3D SDK, then you want to browse to the folder you installed the SDK to. Open up the Delphi folder, then the System folder, and run TV3DInstaller.exe after this you’ll good to go.
Now the fun part, creating the Hello World application. First thing is first, every delphite knows how to Uses works, so we’re going to add two new ones in there for this application. These two are TrueVision3D, and Direct3D8 now although Direct3D8 may not be used in this application, its functions will most likely be used in all your projects. So now that we have that setup, we’re ready to code. First we need to define our Engine and our Scene. The engine is the core, without this TrueVision 3D would not function, and the scene is pretty self explanitory, it allows us to draw some things on the screen, however different defintions are needed for more specific drawings, but scene will allow us todo our Hello World application nicely. So lets define those:
FEngine: TTVEngine;
FScene: TTVScene;
Running: boolean;
We also threw in a boolean, Running, which we will use in our MainLoop. So now that we have that much done, we’re ready to create everything. In the OnCreate event, we’re going to put the following:
FEngine := TTVEngine.Create(Self); FEngine.Initialize(Handle, True, 1); FScene := TTVScene.Create(Self); Show; Running := True; MainLoop;
Now, the first 2 lines will create our engine, and initialize it to our form, and make it Windowed Mode. You can play with Fullscreen later if you wish. The line after that will create our Scene, we don’t need to initialize our scene however. The Show; will make our form visible before starting the MainLoop, if we don’t do this the application will get into the loop before getting a chance to show the form, and thus we won’t see anything. The last 2 lines set Running to true, and call a procedure we’re going to create next, MainLoop.
MainLoop is used because we need to erase the screen, then update it, constantly, so that it is always up to date, and if things are moved over it, it will redraw after they are removed, ect. So make a new procedure, it needs no params, call it MainLoop, obviously :D Now put the following code within it:
while Running do begin Application.ProcessMessages; FEngine.Clear(False); FScene.DrawText('Hello World!', 10, 10, -1); FEngine.RenderToScreen; end; FreeAndNil(FScene); FreeAndNil(FEngine); Close;
Woah, that’s a big chunk of code right? Don’t worry it wont bite. I’ll explain it now. We create our loop to run as long as Running is true, the next line Application.ProcessMessages; will allow the program to carry about its normal actions, this way it will not freeze up. After this we need the erase the screen, this is done using FEngine.Clear(False); the false makes it clear the whole screen not just the Z buffer, Z buffer will be explained in a later tutorial. The line after that is the best line in the whole tutorial, its draws the words “Hello World!” to the window. the 10, 10 is the X and Y positions, and the -1 is the color. -1 is white, I’ll fully explain drawing different colored text to the screen in another tutorial, for now, lets just use white. The Line after that will draw everything to the screen. Pretty straight forward right?
Now, the reason we have code outside the loop is because that code will not be called until the loop has stopped. This way if at any point in the application if Running does not = True everything will be properly destroyed. So the next 2 lines will destroy everything we created properly. Then the last line will terminate the application.
Now, theres only one thing we’re missing, a way to stop the application from running. Now in a later tutorial you will be taught how to use Input to detect keys being pressed, this is a good way of ending the application, but im going to show you how todo it with just pressing the X button.
Its actually very simple, it uses the FormCloseQuery event, which is called when the X button is pressed, or the form is closed otherwise. Now when this happens we want it todo the following, get ready for this, its alot of code:
Running := False;
Now, test out the application. It should draw the words “Hello World!” and close out without any errors upon pressing the X button. Well, that concludes installing TrueVision 3D, using it with Delphi, and making a Hello World application. Below I will show the entire code so you can compare if you did something wrong. Good Luck!
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, TrueVision3D, Direct3D8; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure MainLoop(); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); private FEngine: TTVEngine; FScene: TTVScene; Running: boolean; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin FEngine := TTVEngine.Create(Self); FEngine.Initialize(Handle, True, 1); FScene := TTVScene.Create(Self); Show; Running := True; MainLoop; end; procedure TForm1.MainLoop(); begin while Running do begin Application.ProcessMessages; FEngine.Clear(False); FScene.DrawText('Hello World!', 10, 10, -1); FEngine.RenderToScreen; end; FreeAndNil(FScene); FreeAndNil(FEngine); Close; end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin Running := False; end; end.