Example #1
0
        /// <summary>
        /// Read from keyboard and mouse
        /// and pass actions and keys to handlers
        /// </summary>
        private void readInput(Camera cam)
        {
            // if keyboard is acquired
            if (KeyboardState == InputStateEnum.Acquire)
            {
                KeyboardState key_state;

                //poll
                try
                {
                    g_keyboard.Poll();
                }
                catch (Exception e)
                {
                    if (Result.GetResultFromException(e).Failure == true)
                        goto mouse_proccess;
                }

                // get keyboard state
                key_state = g_keyboard.GetCurrentState();

                /// combine the keys pressed to create
                /// a move vector and pass
                foreach (Key key in key_state.PressedKeys)
                {
                    if (key == Key.Escape)
                        releaseInput();

                    if (AppHandleKeys != null)
                    {
                        // if the key have not be handle from application send it to camera
                        if (!AppHandleKeys(key))
                        {
                            cam.handleKeys(key);
                        }
                    }
                    else
                    {
                        cam.handleKeys(key);
                    }
                }
            }

            mouse_proccess:

            // if the engine owns the mouse device
            if (MouseState == InputStateEnum.Acquire)
            {
                MouseState mouse_state;

                try
                {
                    g_mouse.Poll();
                }
                catch (Exception e)
                {
                    if (Result.GetResultFromException(e).Failure == true)
                        return;
                }

                // get state
                mouse_state = g_mouse.GetCurrentState();

                // get list of mouse data
                var bufferedData = g_mouse.GetBufferedData();

                //fail check
                if (bufferedData == null)
                    goto mouse_exit;

                //fail check
                if (bufferedData.Length == 0)
                    goto mouse_exit;

                //counters
                float cnt_x = 0;
                float cnt_y = 0;
                float cnt_z = 0;

                // combine the mouse buffered data to create a single mouse state
                foreach (var packet in bufferedData)
                {
                    //handle event
                    switch (packet.Offset)
                    {
                        case MouseOffset.X:
                            mouse_state.X += (int)(packet.Value);
                            cnt_x++;
                            break;
                        case MouseOffset.Y:
                            mouse_state.Y += (int)(packet.Value);
                            cnt_y++;
                            break;
                        case MouseOffset.Z:
                            mouse_state.Z += (int)(packet.Value);
                            cnt_z++;
                            break;
                    }
                }

                //normalize..
                if (cnt_x > 0) mouse_state.X = (int)(mouse_state.X / cnt_x); else mouse_state.X = 0;
                if (cnt_y > 0) mouse_state.Y = (int)(mouse_state.Y / cnt_y); else mouse_state.Y = 0;
                if (cnt_z > 0) mouse_state.Z = (int)(mouse_state.Z / cnt_z); else mouse_state.Z = 0;

                /// pass the mouse state to camera handler
                cam.handleMouse(mouse_state);

                return;
            }

            mouse_exit:
            if (cam != null)
                cam.handleMouse(new MouseState());
        }
Example #2
0
        public Viewport(int Width, int Height, IntPtr RenderControl,Format backFormat)
        {
            /// Bind the render control
            this.m_RenderControl = RenderControl;

            ///
            this.backFormat = backFormat;
            SetView(Width, Height);

            ///////////////////////////////////////////////////////////////////////// Set the Camera

            /// Set up the camera
            m_Camera = new Camera(Width, Height);
            m_Camera.SetViewParams(
                new Vector3(0.0f, 1.0f, -10.0f),
                new Vector3(0.0f, 1.0f, 0.0f));

            ///////////////////////////////////////////////////////////////////////// Set the Viewport

            /// Setup the camera viewport
            m_Viewport = new CamViewport();

            /// No offset
            m_Viewport.X = 0;
            m_Viewport.Y = 0;

            /// Size is equal to the size of the form
            m_Viewport.Width = Width;
            m_Viewport.Height = Height;
            m_Viewport.MinZ = 0.0f;
            m_Viewport.MaxZ = 1.0f;

            ///////////////////////////////////////////////////////////////////////// Set the Rasterizer state

            m_RasterizerStateDesc = new RasterizerStateDescription();
            m_RasterizerStateDesc.CullMode = Settings.CullMode;
            m_RasterizerStateDesc.FillMode = Settings.FillMode;

            m_RasterizerState = new RasterizerState(Engine.g_device, m_RasterizerStateDesc);
        }