Jump to content

SFML 2D VB.Net SpotLight Tutorial


jcsnider

Recommended Posts

This has been requested wayyyy to many times so I am finally caving in. Here is a really simple tutorial for making basic spotlights in C# and VB.Net with SFML. If you are using another rendering library this may still help.

VB.Net Guide

Step 1

  Reveal hidden contents
Link to comment
Share on other sites

Sub DrawNight()
        'Create a RenderTexture for our Dark/Night overlay
        NightGfx.Clear(New SFML.Graphics.Color(0, 0, 0, 200))

        For X = TileView.left To TileView.right
            For Y = TileView.top To TileView.bottom

                If IsValidMapPoint(X, Y) Then
                    If Map.Tile(X, Y).Type = TILE_TYPE_LIGHT Then
                        X = ConvertMapX(X * PIC_X) - 135
                        Y = ConvertMapY(Y * PIC_Y) - 20
                        'Create the light texture to multiply over the dark texture.
                        Dim lightSprite As New Sprite(lightGfx)
                        lightSprite.Position = New SFML.Window.Vector2f(X, Y)
                        NightGfx.Draw(lightSprite, New RenderStates(BlendMode.Multiply))
                    End If
                End If

            Next
        Next

        GameWindow.Draw(NightSprite)
    End Sub

this draws on a tile_type_light, but only 1 at a time, how can i make it so it draws more???

Link to comment
Share on other sites

  On 10/4/2015 at 12:39 PM, Damian666 said:

nice tut JC, this is what i got :P

-snip-

any way to reduce it to increase FPS?

    [*]Make sure you only create the NightTex at start. Do not create it everyframe, just call clear and redraw it.

    [*]Only update the night tex if something has changed/Player has moved

    [*]This is complex. Use an off screen texture to store all the map lights when the map loads then every frame render that texture to an empty texture, only draw the player light, and render that. (Less multiplicative drawing.)

    [*]Accept that lighting is extremely resource hungry and that you should target other areas to optimize.

      Quote

    this draws on a tile_type_light, but only 1 at a time, how can i make it so it draws more???

    
    
    Sub DrawNight()
    '- snip -
            NightGfx.Display()
            GameWindow.Draw(NightSprite)
        End Sub

    All you need to do (from what I can tell) is call the display function on the Night Texture after you draw all the lights.

Link to comment
Share on other sites

Sub DrawNight()
        'Create a RenderTexture for our Dark/Night overlay
        NightGfx.Clear(New SFML.Graphics.Color(0, 0, 0, 200))

        For X = TileView.left To TileView.right
            For Y = TileView.top To TileView.bottom

                If IsValidMapPoint(X, Y) Then
                    If Map.Tile(X, Y).Type = TILE_TYPE_LIGHT Then
                        X = ConvertMapX(X * PIC_X) - 135
                        Y = ConvertMapY(Y * PIC_Y) - 20

                        'Create the light texture to multiply over the dark texture.
                        Dim lightSprite As New Sprite(lightGfx)
                        lightSprite.Position = New SFML.Window.Vector2f(X, Y)
                        NightGfx.Draw(lightSprite, New RenderStates(BlendMode.Multiply))
                    End If
                End If

            Next
        Next
        NightGfx.Display()
        GameWindow.Draw(NightSprite)
    End Sub

still nothing :(

Link to comment
Share on other sites

We figured it out.  The problem was that he was using the X and Y variables for his loops and reusing them to calculate coordinates.  Here is the fixed code...

Sub DrawNight()
        'Create a RenderTexture for our Dark/Night overlay
        NightGfx.Clear(New SFML.Graphics.Color(0, 0, 0, 200))

        For X = TileView.left To TileView.right
            For Y = TileView.top To TileView.bottom

                If IsValidMapPoint(X, Y) Then
                    If Map.Tile(X, Y).Type = TILE_TYPE_LIGHT Then
                        dim X1 = ConvertMapX(X * PIC_X) - 135
                        dim Y1 = ConvertMapY(Y * PIC_Y) - 20

                        'Create the light texture to multiply over the dark texture.
                        Dim lightSprite As New Sprite(lightGfx)
                        lightSprite.Position = New SFML.Window.Vector2f(X1, Y1)
                        NightGfx.Draw(lightSprite, New RenderStates(BlendMode.Multiply))
                    End If
                End If

            Next
        Next
        NightGfx.Display()
        GameWindow.Draw(NightSprite)
    End Sub

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Γ—
Γ—
  • Create New...