Beispiel #1
0
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            // Update positions
            Parallel.ForEach(sprites, delegate(Sprite s)
            {
                s.Position += new Vector2((float)(e.Time * s.Scale.X * Math.Cos(s.Rotation)), (float)(e.Time * s.Scale.Y * Math.Sin(s.Rotation)));
            });

            KeyboardState keyboardState = OpenTK.Input.Keyboard.GetState();

            // Quit if requested
            if (keyboardState[Key.Escape])
            {
                Exit();
            }

            // Move view based on key input
            float moveSpeed = 50.0f * ((keyboardState[Key.ShiftLeft] || keyboardState[Key.ShiftRight]) ? 3.0f : 1.0f); // Hold shift to move 3 times faster!

            // Up-down movement
            if (keyboardState[Key.Up])
            {
                CurrentView.Y += moveSpeed * (float)e.Time;
            }
            else if (keyboardState[Key.Down])
            {
                CurrentView.Y -= moveSpeed * (float)e.Time;
            }

            // Left-right movement
            if (keyboardState[Key.Left])
            {
                CurrentView.X -= moveSpeed * (float)e.Time;
            }
            else if (keyboardState[Key.Right])
            {
                CurrentView.X += moveSpeed * (float)e.Time;
            }

            // Add sprites
            if (keyboardState[Key.Plus])
            {
                addSprite();
            }

            // Update graphics
            List <Vector2> verts     = new List <Vector2>();
            List <Vector2> texcoords = new List <Vector2>();
            List <int>     inds      = new List <int>();

            int vertcount = 0;
            int viscount  = 0;

            // Get data for visible sprites
            foreach (Sprite s in sprites)
            {
                if (s.IsVisible)
                {
                    verts.AddRange(Sprite.GetVertices());
                    texcoords.AddRange(s.GetTexCoords());
                    inds.AddRange(s.GetIndices(vertcount));
                    vertcount += 4;
                    viscount++;

                    s.CalculateModelMatrix();
                    s.ModelViewProjectionMatrix = s.ModelMatrix * ortho;
                }
            }

            // Buffer vertex coordinates
            GL.BindBuffer(BufferTarget.ArrayBuffer, shaders[currentShader].GetBuffer("v_coord"));
            GL.BufferData <Vector2>(BufferTarget.ArrayBuffer, (IntPtr)(verts.Count * Vector2.SizeInBytes), verts.ToArray(), BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(shaders[currentShader].GetAttribute("v_coord"), 2, VertexAttribPointerType.Float, false, 0, 0);

            // Buffer texture coords
            GL.BindBuffer(BufferTarget.ArrayBuffer, shaders[currentShader].GetBuffer("v_texcoord"));
            GL.BufferData <Vector2>(BufferTarget.ArrayBuffer, (IntPtr)(texcoords.Count * Vector2.SizeInBytes), texcoords.ToArray(), BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(shaders[currentShader].GetAttribute("v_texcoord"), 2, VertexAttribPointerType.Float, true, 0, 0);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            // Buffer indices
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(inds.Count * sizeof(int)), inds.ToArray(), BufferUsageHint.StaticDraw);

            updated = true;

            // Display average FPS and sprite statistics in title bar
            avgfps = (avgfps + (1.0f / (float)e.Time)) / 2.0f;
            Title  = String.Format("OpenTK Sprite Demo ({0} sprites, {1} drawn, FPS:{2:0.00})", sprites.Count, viscount, avgfps);
        }
Beispiel #2
0
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            Ticker += e.Time;
            if (Ticker > 1.0)
            {
                //   myText.UpdateText(Ticker.ToString());
                Ticker -= 1.0;
            }

            KeyboardState keyboardState = OpenTK.Input.Keyboard.GetState();

            player.Update((float)e.Time);
            bool moverequest = false;



            // Move view based on key input
            //float moveSpeed = 2f;// 50.0f * ((keyboardState[Key.ShiftLeft] || keyboardState[Key.ShiftRight]) ? 3.0f : 1.0f); // Hold shift to move 3 times faster!

            // Up-down movement
            if (InputMgr.IsDown(Key.Up, Key.W))
            {
                //player.MoveY((moveSpeed));// * (float)e.Time));
            }
            else if (InputMgr.IsDown(Key.Down, Key.S))
            {
                //MainCamera.Y -= moveSpeed;
            }


            // Left-right movement
            if (InputMgr.IsDown(Key.Left, Key.A))
            {
                player.speedX -= player.accX;
                moverequest    = true;
            }
            if (InputMgr.IsDown(Key.Right, Key.D))
            {
                player.speedX += player.accX;
                moverequest    = true;
            }

            if (InputMgr.IsDown(Key.W) && !player.jumping && !player.jumpKeyDown)
            {
                player.jumping     = true;
                player.jumpKeyDown = true;
                player.speedY     += player.jumpStartSpeedY;
            }
            else
            {
                player.jumpKeyDown = false;
            }

            if (player.speedX > player.maxSpeedX)
            {
                player.speedX = player.maxSpeedX;
            }
            if (player.speedX < -player.maxSpeedX)
            {
                player.speedX = -player.maxSpeedX;
            }
            if (player.speedY < -player.maxSpeedY)
            {
                player.speedY = -player.maxSpeedY; //Terminal Velocity
            }
            player.speedY -= player.accY;          //Apply force of gravity

            if (!moverequest)
            {
                if (player.speedX < 0)
                {
                    player.speedX += player.decX;
                }
                if (player.speedX > 0)
                {
                    player.speedX -= player.decX;
                }

                // Deceleration may produce a speed that is greater than zero but
                // smaller than the smallest unit of deceleration. These lines ensure
                // that the player does not keep travelling at slow speed forever after
                // decelerating.
                if (player.speedX > 0 && player.speedX < player.decX)
                {
                    player.speedX = 0;
                }
                if (player.speedX < 0 && player.speedX > -player.decX)
                {
                    player.speedX = 0;
                }
            }



            //if(player.Position != player.LastPosition)
            MainCamera.CenterOnTarget(player.Position);//



            // Quit if requested
            if (InputMgr.IsDown(Key.Escape))
            {
                Exit();
            }
            if (InputMgr.IsDown(Key.Q))
            {
                int leftOf   = ((int)player.Left / Properties.Settings.Default.TileSize);
                int heightOf = ((int)player.Top / Properties.Settings.Default.TileSize) - 1;
                world.blocks[leftOf, heightOf].TileID     = 5;
                world.blocks[leftOf + 1, heightOf].TileID = 5;
            }
            if (InputMgr.IsDown(Key.Plus, Key.KeypadPlus))
            {
                Console.WriteLine(String.Format("player x: {0}, y: {1}. w: {2}", player.position.X, player.position.Y, (world.Width * Properties.Settings.Default.TileSize) - MainCamera.Width));
                Console.WriteLine(String.Format("sprite: x: {0}, y: {1}. w: {2}", player.sprite.Position.X, player.sprite.Position.Y, (world.Width * Properties.Settings.Default.TileSize) - MainCamera.Width));
            }



            // Update graphics
            List <Vector2> verts     = new List <Vector2>();
            List <Vector2> texcoords = new List <Vector2>();
            List <int>     inds      = new List <int>();

            int vertcount = 0;
            int viscount  = 0;

            // Get data for visible sprites
            //foreach (Sprite s in sprites)
            if (DRAW_BLOCKS)
            {
                for (int y = 0; y < world.blocks.GetLength(1); y++)
                {
                    for (int x = 0; x < world.blocks.GetLength(0); x++)
                    {
                        if (world.blocks[x, y].IsVisible)
                        {
                            //verts.AddRange(Sprite.GetVertices());
                            verts.AddRange(Sprite.GetVertices());
                            //texcoords.AddRange(t.GetTexCoords());
                            texcoords.AddRange(TextureMgr.GetTexCoords(world.blocks[x, y].TileID));
                            inds.AddRange(Sprite.GetIndices(vertcount));
                            vertcount += 4;
                            viscount++;

                            world.blocks[x, y].CalculateModelMatrix();
                            world.blocks[x, y].ModelViewProjectionMatrix = world.blocks[x, y].ModelMatrix * ortho3d;
                        }
                    }
                }
            }

            if (DRAW_PLAYER && player.sprite.IsVisible)
            {
                /*Vector2mousePos = new Vector2(e.X, e.Y);
                 * mousePos.X += MainCamera.X;
                 * mousePos.Y = ClientSize.Height - mousePos.Y + MainCamera.Y;
                 * if (mousePos.X < player.Left + (player.Width / 2)) player.facingRight = false;
                 */
                //else player.facingRight = true;
                //if (player.facingRight)
                //    player.sprite.Size = new Size(-32, 48);
                //else player.sprite.Size = new Size(32, 48);

                verts.AddRange(Sprite.GetVertices());
                texcoords.AddRange(player.sprite.GetTexCoords());
                inds.AddRange(Sprite.GetIndices(vertcount));
                vertcount += 4;
                viscount++;

                player.sprite.CalculateModelMatrix();
                player.sprite.ModelViewProjectionMatrix = player.sprite.ModelMatrix * ortho3d;

                verts.AddRange(Sprite.GetVertices());
                texcoords.AddRange(item.sprite.GetTexCoords());
                inds.AddRange(Sprite.GetIndices(vertcount));
                vertcount += 4;
                viscount++;

                item.sprite.CalculateModelMatrix();
                item.sprite.ModelViewProjectionMatrix = item.sprite.ModelMatrix * ortho3d;
            }
            foreach (GLText glt in UIMgr.Elements)
            {
                if (DRAW_GUI)//(myChar.IsVisible)
                {
                    verts.AddRange(Sprite.GetVertices());
                    texcoords.AddRange(DBG_GL.GLHelper.GetCommonTexCoords());
                    inds.AddRange(Sprite.GetIndices(vertcount));
                    vertcount += 4;
                    viscount++;

                    glt.CalculateModelMatrix();
                    glt.ModelViewProjectionMatrix = glt.ModelMatrix * ortho3d;
                }
            }
            // Buffer vertex coordinates
            GL.BindBuffer(BufferTarget.ArrayBuffer, ShaderMgr.CurrentShader.GetBuffer("v_coord"));
            GL.BufferData <Vector2>(BufferTarget.ArrayBuffer, (IntPtr)(verts.Count * Vector2.SizeInBytes), verts.ToArray(), BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(ShaderMgr.CurrentShader.GetAttribute("v_coord"), 2, VertexAttribPointerType.Float, false, 0, 0);

            // Buffer texture coords
            GL.BindBuffer(BufferTarget.ArrayBuffer, ShaderMgr.CurrentShader.GetBuffer("v_texcoord"));
            GL.BufferData <Vector2>(BufferTarget.ArrayBuffer, (IntPtr)(texcoords.Count * Vector2.SizeInBytes), texcoords.ToArray(), BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(ShaderMgr.CurrentShader.GetAttribute("v_texcoord"), 2, VertexAttribPointerType.Float, true, 0, 0);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            // Buffer indices
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(inds.Count * sizeof(int)), inds.ToArray(), BufferUsageHint.StaticDraw);

            updated = true;

            // Display average FPS and sprite statistics in title bar
            avgfps = (avgfps + (1.0f / (float)e.Time)) / 2.0f;
            Title  = String.Format("OpenTK Sprite Demo ({0} sprites, {1} drawn, FPS:{2:0.00})", 0 /*sprites.Count*/, viscount, avgfps);
        }