Any idea how to integrate with C# or VB.net GUI?

Archives Forums/Blitz3D SDK Programming/Any idea how to integrate with C# or VB.net GUI?

ziggy(Posted 2007) [#1]
I see no way to do that. any ideas anyone?


Rone(Posted 2007) [#2]
Hi,

maybe make your own UserControl and set the handle to B3DSdk...

private void UserControl1_Load(object sender, EventArgs e)
{
bbSetBlitz3DHWND(this.Handle.ToInt32);
}

Also you could try it with a picturebox...

bbSetBlitz3DHWND( pictureBox1.Handle.ToInt32 );

Otherwise yust copy the pixels into a picturebox if possible which something like memcpy or besides per hand, but in that case I would make it unsafe.

Unfortunately I have no Blitz3dSdk to test it...

regards
Rone


Daz(Posted 2007) [#3]
Hi Ziggy,

Here is some example code in C# I posted in another thread. This example uses a timer control and a panel control.

When running this you have to make sure the timer is initially set to enabled = false otherwise the Timer1_Tick event will trigger before the Blitz canvas is created, thus causing the error message to be displayed which you describe in your bug forum post.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using bb = Blitz3DSDK;

namespace MyBB3DSDKTest
{
    
    public partial class Form1 : Form
    {
        int cube, light, camera;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bb.SetBlitz3DHWND(this.panel1.Handle.ToInt32());
            bb.BeginBlitz3D();
            bb.Graphics3D(panel1.Width, panel1.Height, 0, 2);

            cube = bb.CreateCube(0);
            light = bb.CreateLight(1, 0);
            camera = bb.CreateCamera(0);
            bb.PositionEntity(camera, 0, 0, -4, 1);

            timer1.Enabled = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            bb.EndBlitz3D();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            bb.UpdateWorld(1);
            bb.RenderWorld(0);
            bb.TurnEntity(cube, .1f, .2f, .3f, 1);
            bb.Text(20, bb.GraphicsHeight() - 20, "Blitz3d SDK C# example to use a panel control", 0, 0);
            bb.Flip(1);
        }
    }
}



ziggy(Posted 2007) [#4]
Ok, thanks, I've got it. Now the problem I'm having is..
Any idea on how to make the commands like bbKeyDown work when the DX window is created from a pannel handler, like in the example? The real problem I'm gettin is that I see no way to get keystrokes, or mouse events.


Staton_Richardson(Posted 2007) [#5]
That's the same question I had in the purebasic thread I posted.


dynaman(Posted 2007) [#6]
for C# and VB.NET try the trick I used in the following thread. Instead of using blitz event handling you will need to use C# or VB events though.

http://www.blitzbasic.com/Community/posts.php?topic=70250


dynaman(Posted 2007) [#7]
Also for VB and C#. Set the form's keypreview property to true and then do something similar to this in the keydown handler. (in this case g_key is a global string which holds the value of the key that is pressed)


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
g_Key = e.KeyCode.ToString
End Sub


ziggy(Posted 2007) [#8]
The problem is that the blitz3dSDK is flushing the event queue (I think on every flip, but I'm not sure of this), so even making a keypreview and holding this events throug native net code, some events are lost. To the point that having a textbox next to an active 3DCanvas, if the 3D canvas is being drawn (doing FLIPS), a lot of key events are simply never fired. As I wrote in a bug report, I was writing "Hello World" and what I got writted in the GUI was "Word"


Daz(Posted 2007) [#9]
Hi Ziggy,

I have just had a little play around with keyboard input. I agree with you about loosing the events.

However, if you try changing the flip command from flip(1) to flip(0) then keyboard input events don't seem to be lost.


Cheers.


ziggy(Posted 2007) [#10]
I supose that disabling the frame sync makes the flip command faster and it makes it more difficult to flush events, as they may be already handled by Visual Studio, but it is not 100% secure. Some of them may eventually be lost.I think there must be a enablepolledinput(boolean) function to enable and disable internal event handling. This will prevent a lot of problems like this one. I've already posted this on the bugs forum.


Barnabius(Posted 2007) [#11]
The key to proper event handling is the bbSetBlitz3DEventCallback ( callback ) function so it would be great if we can get some examples about using that function. I?ve found something in the docs but it's strictly BMax stuff and it also uses bbSystemEmitOSEvent hwnd,msg,wp,lp,Null function, which does not exist in SDK DLL. Since event handling is crucial to proper usage of SDK inside GUI environment it would be great if BRL can supply some more info about it.

Barney


ziggy(Posted 2007) [#12]
This could be great, but take in consideration that some environments use delegates to process events, and using a static function to process events may be traduced in a lose of functionality. It would be even better to let the host language process events (at last, make it possible). It would also reduce a lot of compatibility issues that may appear IMHO.


Daz(Posted 2007) [#13]
I agree with you, I would like an example (either in VB.NET or C#.NET) on how to properly use the SetBlitz3dEventCallback method.

I had a quick look intro trying to work out how this command works but didn't get very far. I had a look at the MSDN documentation on hooks, deligated and callbacks but it was like trying to read a foreign language!!!

Also, looking at the function prototype, public static extern int bbSetBlitz3DEventCallback(int callback), the parameter passed into the function is an int. Looking at the BlitzMax example from the documentation, it passes a function name!!! That threw me as well.

You may be interested to have a quick look at http://www.codeproject.com/csharp/GlobalSystemHook.asp

This link contains a .NET keyboard and mouse event hook DLL which can be set up with very few lines of code. It works by intercepting all mouse and keyboard events before the .NET application. I have only had a very brief chance to have a look at it and it does work, but you have to be careful otherwise things can get pretty erratic. FOr example, I think you would have to temporarily disable the hook when the form looses focus or the mouse pointer goes outside your application's form. Then, you'd have to switch them back on when the reverse happens.


Staton_Richardson(Posted 2007) [#14]
I also would like to see that.


dynaman(Posted 2007) [#15]
The trick I tried above, setting the form's keypreview and then using the form's Keydown and Keyhit events, seemed to be working with flip(1) or flip(0), I was typing fast as I could and characters were not getting lost. I'd agree that the best solution is to get the events working through the callback though.

(this only works when the renderwindow is parented to a control on a form)


ziggy(Posted 2007) [#16]
were you typing on a textbox OUTSIDE the 3dcanvas, while the 3dcanvas whas rendering stuff?


dynaman(Posted 2007) [#17]
> were you typing on a textbox OUTSIDE the 3dcanvas, while the 3dcanvas whas rendering stuff?

No, the control that actually has the focus is the one "above" the canvas with the blitz3d rendering window. It was a little tricky to setup the first time (half the time the wrong canvas ended up on top, or was obscuring the rendering) but once it is done it works.


dynaman(Posted 2007) [#18]
Looks like it was fooling me, focus was on the last item I had selected rather then the canvas. I'll be working with it some more (I found out when I hit space and it clicked a button)


ziggy(Posted 2007) [#19]
But my problem is not how to get events on the Blitz3D window (that's only one of my problems). The other problem is how to get key events on standard controls (sometimes are lost) when the 3d canvas is rendering stuff (at some point Blitz3DSDK seems to flush the eventqueue).


dynaman(Posted 2007) [#20]
> The other problem is how to get key events on standard controls

Weird, I've never had that problem. Perhaps because I handle all the rendering in a backgroundworker component? I've only done small tests so far though.


ziggy(Posted 2007) [#21]
that happens. I was typing hello world in a textbox and I get typed only word.


Staton_Richardson(Posted 2007) [#22]
Is anyone using SharpDevelop http://www.icsharpcode.net/OpenSource/SD/
I converted and ran the Daz's code above
but when I move the window the screen locks
if I let it run for a while it returns null from
a few of the bb calls and crashes.

any Ideas