Starting with Truevision 3D on Visual Basic 6.0

This tutorial assumes you have used Visual Basic 6 before. In this tutorial I will explain how to install TrueVision 3D in Visual Basic 6, as well as how to make a simple Hello World application. Installing and using TrueVision3D in Visual Basic 6 is very easy. All you need todo is download Version 6.2 off the website, then install it. After this the hardest part is over. The next step will be to open up Visual Basic 6, a new project will already be made for you. Now, you want to add 3 new references to the project. Todo this, click the Project menu up the top, and click Refernces. You will be givin a giant list. You’re looking for 3 things.

  • DirectX 8 for Visual Basic Type Library
  • TV3D SDK 6.1 - TV3DMedia
  • TV3D SDK 6.2 - TV3DEngine

After finding, and checking off each one of these, press Ok. You’re now ready to use the engine. Keep in mind, for each TrueVision 3D enabled project you wish to create, you will need to enable these 3 references. Now that you’ve got your references enabled, you need to actually write some code. Open up the code for your form. I will now teach you a few things you will need to know in order to make any TrueVision 3D project. I will teach you, how to initilize your engine, how to make a scene, and how to make a mainloop, as well as how to clean up after yourself.

So lets get started. First you need to define your engine, as well as your scene. The engine and scene are nessecary for any game/application, they will allow you draw things to the screen. So lets define them:

Private Engine as New TVEngine
Private Scene as New TVScene

Alrighty, now were going to need 1 more variable before we can continue, this variable is going to be a Boolean named Running, it tells us whether our MainLoop should continue to loop or not, so lets throw that in there:

Private Engine as New TVEngine
Private Scene as New TVScene
Private Running as Boolean

Okay, no time like the present to create our loading code, so open up the Form_Load sub, and add this in there (Don’t worry, I’ll explain it all afterwords):

    If Engine.Init3DWindowedMode(Form1.hWnd) = True Then
        Me.Show
        MainLoop
    End If

What this does, is asks the engine if it can successfully create itself on Me.hWnd, meaning, the form your putting the code into. If you want you can use TV.Init3DFullscreen which will work similar but make it fullscreen instead. Now, if that returns true we know everything went okay, and we can show our form (Me.Show) as well as begin our MainLoop. I guess it’s time I explain what a MainLoop is. What the MainLoop does is erases the draw area and then draws everything back there again. The purpose of this is to keep everything up to date, like if you move something over the window, such as the mouse, and it doesn’t redraw, you will see the mouse over and over again, this isn’t cool, therefore we erase and redraw the screen. Which brings us to our next chunk of code, the MainLoop:

Private Sub MainLoop()
    Do While Running
        Engine.Clear
            DoEvents
            Scene.DrawText "Hello World!", 10, 10
        Engine.RenderToScreen
    Loop
End Sub

Okay, first we define the sub, then we make a loop, it will continue to repeat the actions within until Running = False. Now the actions within you may not understand, because you new, therefore I will explain them. Engine.Clear is the erasing part I spoke of, and Engine.RenderToScreen is the drawing. Each time it erases the screen, then draws to it. The things that happen in between those to are what will be drawn to the screen. If you used Visual Basic before you already know what DoEvents does, it will stop the loop from freezing up the application. Now, this is where we get to the fun part, actually drawing something to the form, Scene.DrawText will allow you to draw text to the screen. The first paramiter is Text, which is what it will say, and the next two are X and Y, which are the position in which the text will be drawn. You can play with this and you’ll catch on rather quickly.

Okay, now the application will open, draw “Hello World!” to the screen, and thats it, now you need one last thing, and this is destroy everything you’ve created. The reason for this is so you don’t get memory leaks, which are when things are created without being properly disposed of. Also, we need to stop our MainLoop, so what we’re going to use is the Forms QueryUnload event, which is triggered when you click the X in the corner of the form. Now insert the following code into it:

    Running = False
    Set Engine = Nothing
    Set Scene = Nothing
    End

Okay this is pretty straight forward, we stop the loop, Set the Engine to nothing, Set the Scene to nothing, then End the program.

Congradulations if all this worked fine for you, you’ve just created you’re first TrueVision 3D Visual Basic 6 application. You’ve got along way to go yet, but this is a good start. I will show the entire code in a second, in case you missing something, or put something in the wrong place. Anyhow, good luck with TrueVision 3D!

Entire Code:

Private Engine as New TVEngine
Private Scene as New TVScene
Private Running As Boolean
 
Private Sub Form_Load()
    If Engine.Init3DWindowedMode(Form1.hWnd) = True Then
        Running = True
        Form1.Show
        MainLoop
    End If
End Sub
 
Private Sub MainLoop()
    Do While Running
        Engine.Clear
            DoEvents
            Scene.DrawText "Hello World!", 10, 10
        Engine.RenderToScreen
    Loop
End Sub
 
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Running = False
    Set Engine = Nothing
    Set Scene = Nothing
    End
End Sub
 
tv3d/getting_started_with_visual_basic_6.txt · Last modified: 2013/11/22 13:31