C++ into VB?
hi there
i have a problem where to understand how a simple game loop works under visual basic programming
i understand that by using c++ programming , the game loop looks like this init()->playerinput()->AI()->Collision->graphics->sound->loopback
but how this represent in visual basic program??
Warning! Warning! The following is a major hack. But it''s the way I do it. If anybody has a better way, tell us about it..
Well, you want an infinite loop, so set one up. However, starting it before the form finishes loading leads to problems, so I put it in a timer. As soon as your in the timer, set its enabled property to false. (we don''t want the timer to be called more than once, we just want our code to start some time (anytime) AFTER the form is loaded. After disabling the timer, put your loop.
Now... in your loop (probably at the top) put a doEvents keyword. This will cause the program to go out of the loop, check if there was any events (keypress for example) and do those events. Other wise, your loop will go on forever and you''ll have to Ctrl-alt-delete it...
If you have questions about keyboard input, just give me a shout.
Also, check out Nehe''s tutorials on openGl. He has vb examples, which are done better than mine... he uses a main function, which seems more straight-forward, but I don''t know much about it.
Well, you want an infinite loop, so set one up. However, starting it before the form finishes loading leads to problems, so I put it in a timer. As soon as your in the timer, set its enabled property to false. (we don''t want the timer to be called more than once, we just want our code to start some time (anytime) AFTER the form is loaded. After disabling the timer, put your loop.
Now... in your loop (probably at the top) put a doEvents keyword. This will cause the program to go out of the loop, check if there was any events (keypress for example) and do those events. Other wise, your loop will go on forever and you''ll have to Ctrl-alt-delete it...
If you have questions about keyboard input, just give me a shout.
Also, check out Nehe''s tutorials on openGl. He has vb examples, which are done better than mine... he uses a main function, which seems more straight-forward, but I don''t know much about it.
November 16, 2000 07:48 AM
''Main'' function in VB explained:
A VB project can startup in a code module rather than a form.
To demonstrate this do the following:
1. Create a new standard exe project
2. Add a module (Project menu or second icon in from left)
3. Add a main() procedure to the module:
private sub main()
''...Your code goes here...
end sub
4. Go to the properties box of the project and select the Startup Object dropdown list and change it from Form1 (assuming you''ve not changed the default form''s name) to Sub Main
5. Run the project and you''ll see that the form does not appear. It just executes your code in sub main and terminates.
This could be used in a DirectX app by including your initialisation, game loop and shut down code in the main() sub. Just make sure that you load the form before using it.
For a good example of this, see the SpaceShooter example in the DX7 SDK. It can be found in the DXMisc folder.
Email me if you don''t have the SDK and I''ll email you the SpaceShooter source code.
MisterMoot@hotmail.com
Hope this helps
A VB project can startup in a code module rather than a form.
To demonstrate this do the following:
1. Create a new standard exe project
2. Add a module (Project menu or second icon in from left)
3. Add a main() procedure to the module:
private sub main()
''...Your code goes here...
end sub
4. Go to the properties box of the project and select the Startup Object dropdown list and change it from Form1 (assuming you''ve not changed the default form''s name) to Sub Main
5. Run the project and you''ll see that the form does not appear. It just executes your code in sub main and terminates.
This could be used in a DirectX app by including your initialisation, game loop and shut down code in the main() sub. Just make sure that you load the form before using it.
For a good example of this, see the SpaceShooter example in the DX7 SDK. It can be found in the DXMisc folder.
Email me if you don''t have the SDK and I''ll email you the SpaceShooter source code.
MisterMoot@hotmail.com
Hope this helps
thanks for all your advice and guide..
i have one problem in mind...
if i put the main code into module...
how about the interface part?
just?
call form2.show?
i have one problem in mind...
if i put the main code into module...
how about the interface part?
just?
call form2.show?
Yes, you have to load the form. You can do this by either using "formname.show", which loads and displays the form, or "load formname" which loads the form but does not display it.
It doesn''t really matter which method you use if you''re going to be using DirectX, as all you need to do is pass a windows handle to the SetCooperativeLevel method of the DirectDraw object (This is the .hwnd property of your form)
It doesn''t really matter which method you use if you''re going to be using DirectX, as all you need to do is pass a windows handle to the SetCooperativeLevel method of the DirectDraw object (This is the .hwnd property of your form)
VB will even the load the form automatically, when you access one of its properties. This is how I set up a game loop in VB:
I add a module and a form (Form1) to my project. This code will then be pasted in the module:
And this could be in the form:
Hope this helps
Sorry for all the edits, but it just never seems right .
Edited by - soehrimnir on November 20, 2000 6:12:15 AM
Edited by - soehrimnir on November 20, 2000 6:14:11 AM
Edited by - soehrimnir on November 20, 2000 6:15:49 AM
I add a module and a form (Form1) to my project. This code will then be pasted in the module:
Public bRunning As BooleanPublic hWnd As LongSub Main() bRunning = Init Do While bRunning ' graphics, ai, ... DoEvents Loop DeInitEnd SubPublic Function Init() As Boolean ' The Form will be loaded, because you access a property, in ' other words, this will call the Form_Load event in Form1. hWnd = Form1.hWnd ' Do all the initializations you need here Form1.Show ' Everything went well Init = TrueEnd FunctionPublic Sub DeInit() ' DeInitialize all your stuff here Unload Form1 EndEnd Sub
And this could be in the form:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyEscape Then bRunning = FalseEnd Sub
Hope this helps
Sorry for all the edits, but it just never seems right .
Edited by - soehrimnir on November 20, 2000 6:12:15 AM
Edited by - soehrimnir on November 20, 2000 6:14:11 AM
Edited by - soehrimnir on November 20, 2000 6:15:49 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement