Results 1 to 8 of 8

Thread: GDI+ - Scrolling Background

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,887

    GDI+ - Scrolling Background

    So you may have seen my XNA - Scrolling Background, well here is the GDI+ version.

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private bgImage1 As Bitmap
        Private bgImage2 As Bitmap
        Private xPosition As Integer
        Private scrolling As Boolean
        Private scrollingThread As Threading.Thread
    
        Private Sub ResetGlobals()
            xPosition = 0
            scrolling = False
            scrollingThread = New Threading.Thread(AddressOf ScrollLoop)
            Me.SplitBackground()
        End Sub
    
        Private Sub SplitBackground()
            'The background1 will be the left half of the image and background2 will be the right half
            bgImage1 = My.Resources.gamebackground
            bgImage2 = My.Resources.gamebackground
        End Sub
    
        Private Sub ScrollLoop()
            'Update -> Draw -> Loop
    
            Dim s As Stopwatch = New Stopwatch
            s.Start()
    
            'Update
            Me.UpdateBackground()
    
            'Draw
            Me.DrawBackground()
    
            'Loop
            While s.Elapsed <= TimeSpan.FromMilliseconds(16.6) '60 FPS
                Application.DoEvents()
            End While
    
            'Stop the watch
            s.Stop()
    
            'Display FPS
            Console.WriteLine("Frames Per Second: " & (1000 / s.ElapsedMilliseconds))
    
            'Continue if applicable
            If scrolling Then
                scrollingThread = New Threading.Thread(AddressOf ScrollLoop)
                scrollingThread.Start()
            End If
        End Sub
    
        Private Sub UpdateBackground()
            'Move the x position to the left by 1 until we've scrolled to the width of the background
            xPosition = If(xPosition > -Me.ClientSize.Width, xPosition - 1, 0)
        End Sub
    
        Private Sub DrawBackground()
            'Refresh the form if the handle is still available
            If scrolling Then
                Me.BeginInvoke(Sub() RefreshForm())
            End If
        End Sub
    
        Private Sub RefreshForm()
            'Refresh
            Me.Refresh()
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            'Be sure to stop scrolling if applicable
            scrolling = False
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            'If the space key is hit then start scrolling
            'If the escape key is hit the stop scrolling
            If e.KeyCode = Keys.Space AndAlso Not scrolling Then
                scrolling = True
                scrollingThread.Start()
            ElseIf e.KeyCode = Keys.Escape AndAlso scrolling Then
                scrolling = False
            End If
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            e.Graphics.DrawImage(bgImage1, New Rectangle(xPosition, 0, Me.ClientSize.Width, Me.ClientSize.Height))
            e.Graphics.DrawImage(bgImage2, New Rectangle(xPosition + Me.ClientSize.Width, 0, Me.ClientSize.Width, Me.ClientSize.Height))
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Reset the globals
            Me.ResetGlobals()
    
            'Set the form's properties
            With Me
                .DoubleBuffered = True
                .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
                .KeyPreview = True
                .ClientSize = New Size(bgImage1.Size.Width \ 2, bgImage1.Size.Height)
                .Text = "GDI+ Scrolling Background Example"
            End With
    
        End Sub
    
        
    End Class
    This is the image that I use which I've named gamebackground and is located in My.Resources:
    Name:  gamebackground.jpg
Views: 36149
Size:  13.7 KB
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    New Member
    Join Date
    May 2015
    Posts
    2

    Re: GDI+ - Scrolling Background

    Hi

    Where can I download My.Resources?

    Cheers

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,887

    Re: GDI+ - Scrolling Background

    You do not download My.Resources, instead you upload any resources(documents, images, etc.) into My.Resources and then you get the resource when needed. Take a look at this link on How To: Add or Remove Resources.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    New Member
    Join Date
    May 2015
    Posts
    2

    Re: GDI+ - Scrolling Background

    A my bad.

    I should've taken the time to ask where I can get a copy of the background you are using. My apologies

    Thanks

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: GDI+ - Scrolling Background

    His gamebackground is the picture in the first post. If you right click it and save as, you should then be able to add it to My.Resources in your project.

  6. #6
    Member
    Join Date
    May 2024
    Posts
    40

    Re: GDI+ - Scrolling Background

    Hi. Do you mind if i use your background and code on my project? I have a sprite I want to overlay, do you have any code to achieve that?.

  7. #7

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,887

    Re: GDI+ - Scrolling Background

    Quote Originally Posted by ArthurDent View Post
    Hi. Do you mind if i use your background and code on my project?
    No problem. I pulled that from a Bing search 10 years ago using commercial use with no restrictions filter.

    Quote Originally Posted by ArthurDent View Post
    I have a sprite I want to overlay, do you have any code to achieve that?.
    I do not have anything on hand, but the way it would work in practice is that you would draw yours sprite after you've updated the position of the background.

    So inside of the ScrollLoop method, after you have called this line:
    Code:
    Me.DrawBackground()
    ' draw the sprite here
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Member
    Join Date
    May 2024
    Posts
    40

    Re: GDI+ - Scrolling Background

    Quote Originally Posted by dday9 View Post
    No problem. I pulled that from a Bing search 10 years ago using commercial use with no restrictions filter.


    I do not have anything on hand, but the way it would work in practice is that you would draw yours sprite after you've updated the position of the background.

    So inside of the ScrollLoop method, after you have called this line:
    Code:
    Me.DrawBackground()
    ' draw the sprite here
    Thanks!

    https://www.sourcecodester.com/visua...appy-bird.html

    I'm basing it on this piece of code, if the scrolling is smooth I'll develop it, if not, then I'll try directx.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width