コード例 #1
0
        public void Render(IScreenBuffer screen, PlayerInfo player)
        {
            screen.Clear();

            // The screen has an origin in the top left.  Positive Y is DOWN
            // Maps have an origin in the bottom left.  Positive Y is UP

            var screenDimensionsV      = screen.Dimensions.ToVector2();
            var desiredMapScreenBounds = screenDimensionsV * DefaultMapToScreenRatio;

            var gameToScreenFactor = Math.Min(desiredMapScreenBounds.X / _map.Area.X, desiredMapScreenBounds.Y / _map.Area.Y);

            var screenAreaInMapCoords = screenDimensionsV / gameToScreenFactor;
            var mapCenteringOffset    = (screenAreaInMapCoords - _map.Area) / 2 - _map.BottomLeftCorner;

            // Transform all vertices
            for (int v = 0; v < _map.Vertices.Length; v++)
            {
                _verticesInScreenCoords[v] = ToScreenCoords(_map.Vertices[v]);
            }

            Point ToScreenCoords(Vector2 worldCoordinate)
            {
                var shiftedWorldCoordinate = worldCoordinate + mapCenteringOffset;

                // This fixes jittering
                var pixelOffset = new Vector2(0.5f, 0.5f);

                return(((shiftedWorldCoordinate * gameToScreenFactor) + pixelOffset).ToPoint().InvertY(screen.Height));
            }

            void DrawLineFromVertices(int v1, int v2, Color c) =>
            DrawLineFromScreenCoordinates(_verticesInScreenCoords[v1], _verticesInScreenCoords[v2], c);

            void DrawLineFromWorldCoordinates(Vector2 wc1, Vector2 wc2, Color c)
            {
                var sc1 = ToScreenCoords(wc1);
                var sc2 = ToScreenCoords(wc2);

                DrawLineFromScreenCoordinates(sc1, sc2, c);
            }

            void DrawLineFromScreenCoordinates(Point sc1, Point sc2, Color c)
            {
                var result = LineClipping.ClipToScreen(screen, sc1, sc2);

                if (result.shouldDraw)
                {
                    screen.PlotLineSmooth(result.p0, result.p1, c);
                }
            }

            foreach (var lineDef in _map.Map.LineDefs.Take((int)_linesToDraw))
            {
                ref Vector2 vertex1 = ref _map.Vertices[lineDef.V1];
                ref Vector2 vertex2 = ref _map.Vertices[lineDef.V2];
コード例 #2
0
        public void Render(IScreenBuffer screen, PlayerInfo player)
        {
            screen.Clear();
            Interpreter.Settings = player.CameraSettings;

            void DrawLineFromScreenCoordinates(Point sc1, Point sc2, Color c)
            {
                var result = LineClipping.ClipToScreen(screen, sc1, sc2);

                if (result.shouldDraw)
                {
                    _drawLine(screen, result.p0, result.p1, c);
                }
            }

            foreach (SectorInfo sector in _map.Sectors)
            {
                foreach (Line line in sector.Lines)
                {
                    Vector3 cameraPosition = new Vector3(player.Position, player.VerticalPosition + player.ViewHeight);

                    _camera.Center            = cameraPosition;
                    _camera.RotationInRadians = player.Angle - MathHelper.PiOver2;

                    Vector3 topLeftConverted     = _camera.WorldToPerspective(new Vector3(line.Vertex1.X, line.Vertex1.Y, sector.Info.HeightCeiling));
                    Vector3 topRightConverted    = _camera.WorldToPerspective(new Vector3(line.Vertex2.X, line.Vertex2.Y, sector.Info.HeightCeiling));
                    Vector3 bottomLeftConverted  = _camera.WorldToPerspective(new Vector3(line.Vertex1.X, line.Vertex1.Y, sector.Info.HeightFloor));
                    Vector3 bottomRightConverted = _camera.WorldToPerspective(new Vector3(line.Vertex2.X, line.Vertex2.Y, sector.Info.HeightFloor));

                    var topLineResult = Interpreter.ConvertWorldLineToScreenPoints(screen, topLeftConverted, topRightConverted);
                    if (topLineResult.shouldDraw)
                    {
                        DrawLineFromScreenCoordinates(topLineResult.p1, topLineResult.p2, Color.Red);
                    }

                    var bottomLineResult = Interpreter.ConvertWorldLineToScreenPoints(screen, bottomLeftConverted, bottomRightConverted);
                    if (bottomLineResult.shouldDraw)
                    {
                        DrawLineFromScreenCoordinates(bottomLineResult.p1, bottomLineResult.p2, Color.Red);
                    }

                    var leftLineResult = Interpreter.ConvertWorldLineToScreenPoints(screen, topLeftConverted, bottomLeftConverted);
                    if (leftLineResult.shouldDraw)
                    {
                        DrawLineFromScreenCoordinates(leftLineResult.p1, leftLineResult.p2, Color.Red);
                    }

                    var rightLineResult = Interpreter.ConvertWorldLineToScreenPoints(screen, topRightConverted, bottomRightConverted);
                    if (rightLineResult.shouldDraw)
                    {
                        DrawLineFromScreenCoordinates(rightLineResult.p1, rightLineResult.p2, Color.Red);
                    }
                }
            }
        }
コード例 #3
0
        public void SimpleClipping()
        {
            for (int i = 0; i < NumberOfPoints - 1; i++)
            {
                var sc1 = _points[i];
                var sc2 = _points[i + 1];

                if (LineClipping.CouldAppearOnScreen(_buffer, sc1, sc2))
                {
                    _buffer.PlotLineSmooth(sc1, sc2, Color.Red);
                }
            }
        }
コード例 #4
0
        public void CohenSutherlandClipping()
        {
            for (int i = 0; i < NumberOfPoints - 1; i++)
            {
                var sc1 = _points[i];
                var sc2 = _points[i + 1];

                var result = LineClipping.ClipToScreen(_buffer, sc1, sc2);
                if (result.shouldDraw)
                {
                    _buffer.PlotLineSmooth(result.p0, result.p1, Color.Red);
                }
            }
        }
コード例 #5
0
        public void Render(IScreenBuffer screen, PlayerInfo player)
        {
            screen.Clear();

            // The screen has an origin in the top left.  Positive Y is DOWN
            // Maps have an origin in the bottom left.  Positive Y is UP

            _camera.ScreenBounds      = screen.Dimensions;
            _camera.Center            = player.Position;
            _camera.RotationInRadians = _settings.RotateMode ? player.Angle - MathHelper.PiOver2 : 0;

            // Transform all vertices
            for (int v = 0; v < _map.Vertices.Length; v++)
            {
                _verticesInScreenCoords[v] = ToScreenCoords(_map.Vertices[v]);
            }


            Point ToScreenCoords(Vector2 worldCoordinate)
            {
                return(_camera.WorldToScreen(worldCoordinate).ToPoint());
            }

            void DrawLineFromVertices(int v1, int v2, Color c) =>
            DrawLineFromScreenCoordinates(_verticesInScreenCoords[v1], _verticesInScreenCoords[v2], c);

            void DrawLineFromWorldCoordinates(Vector2 wc1, Vector2 wc2, Color c)
            {
                var sc1 = ToScreenCoords(wc1);
                var sc2 = ToScreenCoords(wc2);

                DrawLineFromScreenCoordinates(sc1, sc2, c);
            }

            void DrawLineFromScreenCoordinates(Point sc1, Point sc2, Color c)
            {
                var result = LineClipping.ClipToScreen(screen, sc1, sc2);

                if (result.shouldDraw)
                {
                    _drawLine(screen, result.p0, result.p1, c);
                }
            }

            void DrawCircle(Vector2 center, float radiusInWorldCoordinates, Color c)
            {
                screen.PlotCircle(_camera.WorldToScreen(center).ToPoint(), (int)(radiusInWorldCoordinates * _camera.TotalZoom), c);
            }

            void DrawBox(Vector2 center, float halfWidth, Color c)
            {
                var topLeft     = center + new Vector2(-halfWidth, halfWidth);
                var topRight    = center + new Vector2(halfWidth, halfWidth);
                var bottomLeft  = center + new Vector2(-halfWidth, -halfWidth);
                var bottomRight = center + new Vector2(halfWidth, -halfWidth);

                DrawLineFromWorldCoordinates(topLeft, topRight, c);
                DrawLineFromWorldCoordinates(topRight, bottomRight, c);
                DrawLineFromWorldCoordinates(bottomRight, bottomLeft, c);
                DrawLineFromWorldCoordinates(bottomLeft, topLeft, c);
            }

            void DrawVertex(Vector2 wc, Color c) => DrawBox(wc, VertexHalfWidth, c);

            foreach (var lineDef in _map.Map.LineDefs)
            {
                ref Vector2 vertex1 = ref _map.Vertices[lineDef.V1];
                ref Vector2 vertex2 = ref _map.Vertices[lineDef.V2];