ncazanav
05-02-2007 23:53:51
In order to clearly understand the way events are fired up with mogre, I tried to mix tutorials 4 & 6.
I use a windows form where i put a splitContainer:
* in panel 1, I put 2 buttons : one called 'render', another call 'stop rendering'
* in panel 2, I render the scene when the button 'render' is pressed.
The 2 buttons work fine (i can see the message box telling me if the scene is rendered or not). However the keys and mouse events programmed are never fired?? I can' understand why the camera doesn't move when keys are pressed or why the 'R' key doesn't show the message programmed????
Here is my code, if an expert can show me my mistake i will greatly appreciate,
Thx
and the program.cs reads
I use a windows form where i put a splitContainer:
* in panel 1, I put 2 buttons : one called 'render', another call 'stop rendering'
* in panel 2, I render the scene when the button 'render' is pressed.
The 2 buttons work fine (i can see the message box telling me if the scene is rendered or not). However the keys and mouse events programmed are never fired?? I can' understand why the camera doesn't move when keys are pressed or why the 'R' key doesn't show the message programmed????
Here is my code, if an expert can show me my mistake i will greatly appreciate,
Thx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mogre;
using MogreFramework;
namespace Tuto06
{
public partial class OgreForm : Form
{
Root mRoot;
RenderWindow mWindow;
Vector3 mTranslation = Vector3.ZERO;
Point mLastPosition;
Camera cam;
const float TRANSLATE = 200;
const float ROTATE = 0.2f;
bool mRotating = false;
bool mRender = true;
public OgreForm()
{
InitializeComponent();
this.Size = new Size(800, 600);
Disposed += new EventHandler(OgreForm_Disposed);
Resize += new EventHandler(OgreForm_Resize);
}
public void CreateInputHandlers()
{
mRoot.FrameStarted += new FrameListener.FrameStartedHandler(FrameStarted);
this.KeyDown += new KeyEventHandler(KeyDownHandler);
this.KeyUp += new KeyEventHandler(KeyUpHandler);
this.MouseDown +=new MouseEventHandler(MouseDownHandler);
this.MouseUp += new MouseEventHandler(MouseUpHandler);
this.MouseMove +=new MouseEventHandler(MouseMoveHandler);
}
public void Go()
{
Show();
MessageBox.Show("Ok, I'll render the scene!!");
while (mRender)
{
mRoot.RenderOneFrame();
Application.DoEvents();
}
MessageBox.Show("I've stop the rendering of the scene, happy??!!");
}
public void Init()
{
// Create root object
mRoot = new Root();
// Define Resources
ConfigFile cf = new ConfigFile();
cf.Load("resources.cfg", "\t:=", true);
ConfigFile.SectionIterator seci = cf.GetSectionIterator();
String secName, typeName, archName;
while (seci.MoveNext())
{
secName = seci.CurrentKey;
ConfigFile.SettingsMultiMap settings = seci.Current;
foreach (KeyValuePair<string, string> pair in settings)
{
typeName = pair.Key;
archName = pair.Value;
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
}
}
// Setup RenderSystem
RenderSystem rs = mRoot.GetRenderSystemByName("Direct3D9 Rendering Subsystem");
mRoot.RenderSystem = rs;
rs.SetConfigOption("Full Screen", "No");
rs.SetConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
// Create Render Window
mRoot.Initialise(false, "Main Ogre Window");
NameValuePairList misc = new NameValuePairList();
misc["externalWindowHandle"] = this.splitContainer1.Panel2.Handle.ToString();
mWindow = mRoot.CreateRenderWindow("Main RenderWindow", 800, 600, false, misc);
// Init resources
TextureManager.Singleton.DefaultNumMipmaps = 5;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
// Create a Simple Scene
SceneManager mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
cam = mgr.CreateCamera("Camera");
cam.AutoAspectRatio = true;
mWindow.AddViewport(cam);
Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");
mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
cam.Position = new Vector3(0, 200, -400);
cam.LookAt(ent.BoundingBox.Center);
}
void OgreForm_Disposed(object sender, EventArgs e)
{
mRoot.Dispose();
mRoot = null;
}
void OgreForm_Resize(object sender, EventArgs e)
{
mWindow.WindowMovedOrResized();
}
//////////////////////////////////////////////////////
// Events from Tutorial 04
bool FrameStarted(FrameEvent evt)
{
cam.Position += cam.Orientation * mTranslation * evt.timeSinceLastFrame;
return true;
}
void KeyDownHandler(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.R:
MessageBox.Show("Ok i've seen you");
break;
case Keys.Up:
case Keys.W:
mTranslation.z = -TRANSLATE;
break;
case Keys.Down:
case Keys.S:
mTranslation.z = TRANSLATE;
break;
case Keys.Left:
case Keys.A:
mTranslation.x = -TRANSLATE;
break;
case Keys.Right:
case Keys.D:
mTranslation.x = TRANSLATE;
break;
case Keys.PageUp:
case Keys.Q:
mTranslation.y = TRANSLATE;
break;
case Keys.PageDown:
case Keys.E:
mTranslation.y = -TRANSLATE;
break;
}
}
void KeyUpHandler(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.W:
case Keys.Down:
case Keys.S:
mTranslation.z = 0;
break;
case Keys.Left:
case Keys.A:
case Keys.Right:
case Keys.D:
mTranslation.x = 0;
break;
case Keys.PageUp:
case Keys.Q:
case Keys.PageDown:
case Keys.E:
mTranslation.y = 0;
break;
}
}
void MouseDownHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Cursor.Hide();
mRotating = true;
mLastPosition = Cursor.Position;
}
}
void MouseUpHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Cursor.Show();
mRotating = false;
}
}
void MouseMoveHandler(object sender, MouseEventArgs e)
{
if (mRotating)
{
float x = mLastPosition.X - Cursor.Position.X;
float y = mLastPosition.Y - Cursor.Position.Y;
cam.Yaw(new Degree(x * ROTATE));
cam.Pitch(new Degree(y * ROTATE));
mLastPosition = Cursor.Position;
}
}
// End Events from Tutorial 04
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// Buttons Events
private void button1_Click_1(object sender, EventArgs e)
{
mRender = true;
this.Go();
}
private void button2_Click(object sender, EventArgs e)
{
mRender = false;
}
// END : Buttons Events
//////////////////////////////////////////////////////
}
}
and the program.cs reads
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Tuto06
{
static class Program
{
//public static mQuit = false;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OgreForm form = new OgreForm();
form.Init();
form.CreateInputHandlers();
form.Show();
Application.Run();
}
}
}