-
Apr 21st, 2014, 03:35 PM
#1
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:
-
May 12th, 2015, 05:14 AM
#2
New Member
Re: GDI+ - Scrolling Background
Hi
Where can I download My.Resources?
Cheers
-
May 12th, 2015, 08:39 AM
#3
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.
-
May 13th, 2015, 12:10 AM
#4
New Member
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
-
May 13th, 2015, 07:19 AM
#5
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.
-
Jun 3rd, 2024, 07:24 AM
#6
Member
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?.
-
Jun 3rd, 2024, 08:35 AM
#7
Re: GDI+ - Scrolling Background
Originally Posted by ArthurDent
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.
Originally Posted by ArthurDent
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
-
Jun 3rd, 2024, 10:03 AM
#8
Member
Re: GDI+ - Scrolling Background
Originally Posted by dday9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|