| Topic: No input when window is not focussed
|
|
 | No input when window is not focussed | |  |
| Poster | : thkoe002 | | Posts | : 17 | | Country | : Germany | | City | : Düsseldorf |
| | | | Posted by thkoe002 on 04/08/2006 at 04:03:34
| | Hi there. I have a small problem with DirectInput, that is, the application doesn't care if the form window is focussed or not. (I currently use the mouse as input source, and whenever I drag a file or scroll through a folder in the windows explorer, the application is spinning/zooming accordingly).
Is there a way to change this behaviour?
Also, I'm still interested in http://www.riemers.net/Forum/index.php?var=166&var2=0 :)
Thanks, cheers
Thomas | |
|
| | | | | | Poster | : thkoe002 | | Posts | : 17 | | Country | : Germany | | City | : Düsseldorf |
| | | | Posted by thkoe002 on 24/08/2006 at 23:31:44
| | Hi, me again.
In case that anyone is still interested, I just wanted to mention how to solve this problem: You can add
"if (Form1.ActiveForm == this) " infront of the ReadKeyboard() call in the OnPaint() method.
This will have the disadvantage that when using the mouse as input as well, there will be a mistaken input when you click into the unfocussed form in order to focus it again. To prevent this, I'd recommend to use a boolean that tells the program if it just has been "reactivated".
Cheers, Thomas
PS. Not utterly important, but I'd still like a solution to http://www.riemers.net/Forum/index.php?var=166&var2=0 ... | |
|
| | | | | | Poster | : riemer | | Posts | : 1388 | | Country | : Belgium | | City | : Antwerp |
| | | | Posted by riemer on 25/08/2006 at 03:06:50
| | Thanks for the post! It's always nice when someone post a solution.
BTW I have written a short tut on how you can solve the resizing problem, you can find it here:
http://www.riemers.net/ShortTuts/lost_device.php
You can find the short tuts at the upper left of every page. | |
|
| | | | | | Poster | : mwagnborg | | Posts | : 5 | | Country | : USA | | City | : Carrboro |
| | | | Posted by mwagnborg on 07/07/2007 at 09:31:11
| | good afternoon,
I have a problem integrating the
if (Form1.ActiveForm == this) solution. I cannot access Form1 or our_directx_form in the onPaint method.
Sorry for eating up a lot of space, but bellow is my complete code. One feature of it is that if you have a large enough heightmap, you can change the numberOfMeshes variable to any square-rootable number, and you can draw multiple meshes, so that you are not restricted to a 128x128 surface. (ex, if you want to draw 4 meshes, you need a heightmap of 256x256 or larger - just put the origianl in photoshop and resample it to 256x256).
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.IO;
using Microsoft.DirectX.DirectInput;
namespace DirectX_Tutorial
{
public class WinForm : System.Windows.Forms.Form
{
private int WIDTH = 128;
private int HEIGHT = 128;
private int imageWidth = 1024;
private int imageHeight = 1024;
private int numberOfMeshes = 1;
private Microsoft.DirectX.Direct3D.Device device;
private System.ComponentModel.Container components = null;
private float angle = 0f;
private float angle2 = 0f;
private int translateX = -128;
private int translateY = -128;
private int zoomValue = 0;
private CustomVertex.PositionColored[] vertices;
private int[,] heightData;
private int[,] imageLocation;
private short[] indices;
private Microsoft.DirectX.DirectInput.Device keyb;
private Mesh tempMesh;
private Mesh[] meshArray;
public WinForm()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
#region Initialization and up-keep
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(1000, 1000);
this.Text = "3D Viewer";
}
public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.DeviceReset += new EventHandler(HandleResetEvent);
}
public void InitializeKeyboard()
{
keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyb.Acquire();
}
private void CameraPositioning()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)(this.Width / this.Height), 0.3f, 5000f);
device.Transform.View = Matrix.LookAtLH(new Vector3(200, 0, 200), new Vector3(-20, 0, 0), new Vector3(0, 0, 1));
device.RenderState.CullMode = Cull.None;
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(-0.5f, 0, -1f);
device.Lights[0].Enabled = true;
}
private void IndicesDeclaration()
{
indices = new short[(WIDTH - 1) * (HEIGHT - 1) * 6];
for (int x = 0; x < WIDTH - 1; x++)
{
for (int y = 0; y < HEIGHT - 1; y++)
{
indices[(x + y * (WIDTH - 1)) * 6] = (short)(x + y * WIDTH);
indices[(x + y * (WIDTH - 1)) * 6 + 1] = (short)((x + 1) + y * WIDTH);
indices[(x + y * (WIDTH - 1)) * 6 + 2] = (short)((x + 1) + (y + 1) * WIDTH);
indices[(x + y * (WIDTH - 1)) * 6 + 3] = (short)(x + (y + 1) * WIDTH);
indices[(x + y * (WIDTH - 1)) * 6 + 4] = (short)(x + y * WIDTH);
indices[(x + y * (WIDTH - 1)) * 6 + 5] = (short)((x + 1) + (y + 1) * WIDTH);
}
}
}
private void InitializeImageLocation()
{
imageLocation = new int[numberOfMeshes, 4];
int x = 0;
while (x < numberOfMeshes)
{
for (int i = 0; i < (int)Math.Sqrt(Convert.ToDouble(numberOfMeshes)); i++)
{
for (int j = 0; j < (int)Math.Sqrt(Convert.ToDouble(numberOfMeshes)); j++)
{
imageLocation[x, 0] = i * 128; // start x
imageLocation[x, 1] = j * 128; // start y
imageLocation[x, 2] = (i + 1) * 128; // stop x
imageLocation[x, 3] = (j + 1) * 128; // stop y
x++;
}
}
}
}
private void ReadKeyboard()
{
KeyboardState keys = keyb.GetCurrentKeyboardState();
// ROTATION
if (keys[Key.RightArrow])
{
angle += 0.1f;
}
if (keys[Key.LeftArrow])
{
angle -= 0.1f;
}
//PITCH
if (keys[Key.UpArrow])
{
angle2 += 0.1f;
}
if (keys[Key.DownArrow])
{
angle2 -= 0.1f;
}
// ZOOM
if (keys[Key.PageUp])
{
zoomValue -= 25;
}
if (keys[Key.PageDown])
{
zoomValue += 25;
}
// PAN
if (keys[Key.W])
{
translateX += 20;
}
if (keys[Key.S])
{
translateX -= 20;
}
if (keys[Key.A])
{
translateY -= 20;
}
if (keys[Key.D])
{
translateY += 20;
}
// RESET
if (keys[Key.R])
{
angle = 0f;
angle2 = 0f;
zoomValue = 0;
translateX = -512;
translateY = -512;
}
}
private void HandleResetEvent(object caller, EventArgs args)
{
device.RenderState.FillMode = FillMode.WireFrame;
device.RenderState.CullMode = Cull.None;
CameraPositioning();
IndicesDeclaration();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion
private void LoadHeightData()
{
int offset;
byte dummy;
FileStream fs = new FileStream("heightmap.bmp", FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
for (int i = 0; i < 10; i++)
{
dummy = r.ReadByte();
}
offset = r.ReadByte();
offset += r.ReadByte() * 256;
offset += r.ReadByte() * 256 * 256;
offset += r.ReadByte() * 256 * 256 * 256;
for (int i = 0; i < 4; i++)
{
dummy = r.ReadByte();
}
imageWidth = r.ReadByte();
imageWidth += r.ReadByte() * 256;
imageWidth += r.ReadByte() * 256 * 256;
imageWidth += r.ReadByte() * 256 * 256 * 256;
imageHeight = r.ReadByte();
imageHeight += r.ReadByte() * 256;
imageHeight += r.ReadByte() * 256 * 256;
imageHeight += r.ReadByte() * 256 * 256 * 256;
heightData = new int[imageWidth, imageHeight];
for (int i = 0; i < (offset - 26); i++)
{
dummy = r.ReadByte();
}
for (int i = 0; i < imageHeight; i++)
{
for (int y = 0; y < imageWidth; y++)
{
int height = (int)(r.ReadByte());
height += (int)(r.ReadByte());
height += (int)(r.ReadByte());
height /= 8;
heightData[imageWidth - 1 - y, imageHeight - 1 - i] = height;
}
}
}
private void CreateMesh()
{
//Bitmap picture = (Bitmap)Bitmap.FromFile("original.jpg");
Color pixelColor;
vertices = new CustomVertex.PositionColored[WIDTH * HEIGHT];
meshArray = new Mesh[numberOfMeshes];
for (int i = 0; i < numberOfMeshes; i++)
{
// first we give the verticies a position and color
for (int x = imageLocation[i,0]; x < imageLocation[i,2]; x++)
{
for (int y = imageLocation[i,1]; y < imageLocation[i,3]; y++)
{
// assign position
vertices[(x - imageLocation[i, 0]) + (y - imageLocation[i, 1]) * WIDTH].Position = new Vector3((x - imageLocation[i, 0]), (y - imageLocation[i, 1]), heightData[x, y]);
// assign color
pixelColor = Color.Green; //picture.GetPixel(x, y);
vertices[(x-imageLocation[i,0]) + (y-imageLocation[i,1]) * WIDTH].Color = pixelColor.ToArgb();
}
}
// then we put the vertices in a mesh and optimize the mesh
tempMesh = new Mesh((WIDTH -1) * (HEIGHT -1) * 2, WIDTH * HEIGHT, MeshFlags.Managed, CustomVertex.PositionColored.Format, device);
tempMesh.SetVertexBufferData(vertices, LockFlags.None);
tempMesh.SetIndexBufferData(indices, LockFlags.None);
int[] adjac = new int[tempMesh.NumberFaces * 3];
tempMesh.GenerateAdjacency(0.5f, adjac);
tempMesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adjac);
tempMesh = tempMesh.Clone(tempMesh.Options.Value, CustomVertex.PositionNormalColored.Format, device);
tempMesh.ComputeNormals();
// then we put the mesh in a meshArray
meshArray[i] = tempMesh;
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
device.BeginScene();
device.Transform.View = Matrix.LookAtLH(new Vector3(500 + zoomValue, 0, 500 + zoomValue), new Vector3(-20, 0, 0), new Vector3(0, 0, 1));
for (int x = 0; x < numberOfMeshes; x++)
{
int numSubSets = meshArray[x].GetAttributeTable().Length;
for (int i = 0; i < numSubSets; ++i)
{
device.Transform.World = Matrix.Translation(translateX + imageLocation[x,0], translateY + imageLocation[x,1], 0) * Matrix.RotationZ(angle) * Matrix.RotationY(angle2);
meshArray[x].DrawSubset(i);
}
}
device.EndScene();
device.Present();
this.Invalidate();
ReadKeyboard();
}
static void Main()
{
using (WinForm our_directx_form = new WinForm())
{
our_directx_form.InitializeDevice();
our_directx_form.InitializeKeyboard();
our_directx_form.CameraPositioning();
our_directx_form.IndicesDeclaration();
our_directx_form.InitializeImageLocation();
our_directx_form.LoadHeightData();
our_directx_form.CreateMesh();
our_directx_form.Show();
Application.Run(our_directx_form);
}
}
}
}
|
there is another problem too. When you draw multiple meshes, you'll notice a small black line between the meshes. For some reason, it won't paint the 128th, 256th, etc pixel. Please let me know if you can get rid of that, so that it is one continuous surface.
The other problem is of course that the map will pan, zoom, spin around whenever I press a key on the keyboard and that form is not active, thats why the post is here, and I have not been able to solve that problem.
Thank you for your time,
Marcus | |
|
| | | | | | Poster | : flavio | | Posts | : 35 | | Country | : | | City | : treviso |
| | | | Posted by flavio on 08/07/2007 at 15:21:14
| | You should be able to get access all methods/properties using the 'this' statement, as in
this.Draw(); | |
|
| | | | | | Poster | : limacat | | Posts | : 4 | | Country | : Italy | | City | : San Francesco al Campo |
| | | | Posted by limacat on 27/11/2007 at 04:19:17
| | @mwagnborg
Sorry if I am late. Your problem is that your class is actually called "WinForm" and not "Form1". You could replace Form1 in that code with WinForm, or better, you can access the ActiveForm property, since it is static and shared by all the instances of Form and its childrens, by referencing it as the Form.ActiveForm property.
if (Form.ActiveForm == this) {
// call the methods for reading user input
ReadKeyboard();
}
|
| |
|
|
 | | |  |
|
|
|
If you appreciate the amount of time I spend creating and updating these pages, feel free to donate -- any amount is welcome !
|
- Website design & DirectX code : Riemer Grootjans - ©2006 Riemer Grootjans
|
|