Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag
Large Bitmap Movement Lag Large Bitmap Movement Lag
Large Bitmap Movement Lag
Go Back  Xtreme Visual Basic Talk > > > Large Bitmap Movement Lag

Thread Tools Display Modes
Old 02-23-2013, 03:10 AM
Cadrean Cadrean is offline
Join Date: Feb 2013
Posts: 2
Default Large Bitmap Movement Lag

So I'm making a game in my Programming class, basically a little tank bullet hell. I've got most of it figured out except one bit, which I had actually finished and gotten to work correctly, until I decided to change the image for a better quality one.

How I have it is that The map background is always moving downwards, making it look like the tank is driving, and the tank is able to move side to side. The map is 750 wide, and 1500 high, Form height is 750, form width is 1200. This allows the map to move down, reach a certain point, and return to it's original position, essentially making it look never ending.

For some reason without changing any code, sizes of images, or format of images, Visual basic enlarges the Image past the size it was originally, slows down the game a ridiculous amount, and makes the game basically unplayable.

here is my code for the ma's movement, really simple:

Dim Map As Bitmap
Dim MapY As Integer = -750

Private Sub Form1_Load

Map = New Bitmap("Map2.jpg")
GameTimer.Enabled = True

end sub

Private Sub Form1_Paint

If GameTimer.Enabled = True Then
e.Graphics.DrawImage(Map, 0, MapY)

End If

end sub

Private Sub GameTimer_tick

MapY += 2
If MapY = 0 Then
MapY = -750
End If

end sub

Took out all of the other unrelated code I had in there, I'm wondering if there's something I'm missing.

Reply With Quote
Old 02-23-2013, 06:31 AM
passel's Avatar
passelLarge Bitmap Movement Lag passel is offline
Sinecure Expert

Super Moderator
* Guru *
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,031

Probably the easiest first improvement would be to use a TextureBrush to draw the background. Refer to this post for an example.
In that post the user also wanted to zoom the background image which you don't need so you would comment out or delete the "srcBrush.ScaleTransform(20, 20)" line which "zoomed" the image to 20 times its original size, when drawing. Using a texture brush should draw a large image much faster than DrawImage.
I also presume you don't have anything in the "BackgroundImage" or "Image" property of the form, which would significantly slowdown paint updates.

The second most effective improvement, which would be done after (in addition to) using the TextureBrush, would be to not use the built-in Form double-buffering and Paint events, (which are ideal for the normal control based paradigm of WinForms, but not for trying to do an arcade type game), would be to handle the back buffering and rendering to the screen chores yourself by using the BufferedGraphics class.
There are a number of examples around, but they may be more complicated for what you're trying to do.
So, I'll create a quick new example more along the lines of what you're doing.
This example will have both things mentioned above, using a texture brush and using the BufferedGraphics class.
It will create the brush, twice the size of the maximized form area, and then scroll it 2 pixels per tick of the timer. The winForm timer usually (no guarantee) ticks at a maximum of 64hz, so using an interval less than 15 will usually not be any quicker than an interval of 15.
Imports System.Drawing

Public Class Form1
  Private tb As TextureBrush  'A texture brush used to fill the background
  Private Manager As BufferedGraphicsContext = BufferedGraphicsManager.Current 'BufferedGraphics requires a manager, use the existing one
  Private gfx As BufferedGraphics          'Use to draw on a memory buffer and render to the screen
  Private WithEvents tmr As New Timer      'Cycle the example using a winForm timer (so timer event is in the GUI thread)
  Private rnd As New Random                'A random number generator
  Private MapY As Integer                  'Offset the vertical position by this amount to implement vertical scrolling
  Private fw, fh As Integer                'Keep track of the maximized form width and height
  Private Quit As Boolean                  'Allow us to stop the timer and clean up when program is exited

  Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Not Quit Then     'If this is the first time we've been asked to close
      e.Cancel = True    '  Cancel the close
      Quit = True        '  Set the Quit flag so the timer will clean up and stop before the example exits.
    Else                 'Else the Timer should have stop and clean up other resource
      tmr.Dispose()      '  Dispose of the timer itself (and don't prevent the closing)
    End If
  End Sub

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.WindowState = FormWindowState.Maximized      'Maximize the form so we setup for the full size of the default screen
    Me.Show()                                       'Show the form (might not be necessary) to ensure it reports the maximum client area
    fw = Me.ClientSize.Width                        'Save the Form's client area width
    fh = Me.ClientSize.Height                       'Save the Form's client area height
    Dim tmpBmp As New Bitmap(fw, fh * 2)            'Create a bitmap twice the height for simple seamless scrolling of a background image
    Dim g As Graphics = Graphics.FromImage(tmpBmp)  'Get a graphics object for the bitmap so we can draw on the bitmap
    g.Clear(Color.ForestGreen)                      'Clear it to a background color to get rid of all the transparent pixels
    Dim sb As New SolidBrush(rndColor)              'Create a solid brush to fill the ellipse and set it to a random color
    Dim er As Rectangle                             'Create a rectangle to use when drawing the ellipses
    For i As Integer = 0 To 50                      'Let's draw 51 ellipses on the bitmap
      er.X = rnd.Next(0, fw)                        'Pick a random X value for the position of the left side of the ellipse
      er.Y = rnd.Next(0, fh)                        'Pick a random Y value (in the upper half of the bitmap) for the top of the ellipse
      er.Y -= fh                                    'adjust the Y down one form height to make seamless vertical wrapping progressive
      er.Width = rnd.Next(20, 200)                  'Pick a random width between 20 to 200 pixels for the ellipse
      er.Height = rnd.Next(20, 200)                 'Pick a random height (20 to 200) for the ellipse

      'draw the ellipse 3 times for seamless vertical wrapping
      g.FillEllipse(sb, er)    'draw above the window (part might be on window)
      er.Y += fh               'adjust y down one frame (so in the top half of window)
      g.FillEllipse(sb, er)
      er.Y += fh               'adjust y down one frame (so in bottom half of window), might be partially off bottom,
      '                         but should "mesh" with first one drawn at the "top" in that case, making it a "seamless" transition
      g.FillEllipse(sb, er)
      sb.Color = rndColor()    'Chose a new random color for the next series of ellipses

    tb = New TextureBrush(tmpBmp)      'create a texture brush from the bitmap we've just drawn all these ellipses on 
    gfx = Manager.Allocate(Me.CreateGraphics, Me.ClientRectangle) 'Initialize the BackgroundGraphics object to backbuffer our form area

    sb.Dispose()       'Dispose the solid brush as it is no longer needed
    g.Dispose()        'Dispose of the graphics object used to draw on the bitmap
    tmpBmp.Dispose()   'Dispose of the bitmap itself (we now have a texture brush we will use instead)

    AddHandler tmr.Tick, AddressOf TimerTick  'Add an event handler for the winForm timer we'll use to cycle the example
    tmr.Interval = 15                         'Set the interval to 15 ms, since any less probably won't make a difference
    tmr.Start()                               'Start the timer
  End Sub

  Private Function rndColor() As Color
    Return Color.FromArgb(rnd.Next(20, 255), rnd.Next(20, 255), rnd.Next(20, 255))
  End Function

  Private Sub TimerTick(sender As System.Object, e As System.EventArgs)
    If Quit Then                                                 'If we're quitting Then
      tmr.Enabled = False                                         '  Disable the timer
      gfx.Dispose()                                               '  Dispose of the disposable items we've created
      Me.Close()                                                  '  Close the form
    Else                                                          'Else
      MapY += 2                                                   '  increment our map position
      If MapY > fh Then                                           '  If the position is outside the scroll area
        MapY -= fh                                                '    wrap it back in
      End If
      tb.TranslateTransform(0, MapY)                              '  Offset the texture brush to match the scroll position
      gfx.Graphics.FillRectangle(tb, gfx.Graphics.ClipBounds)     '  Fill the backbuffer with the brush pattern
      tb.ResetTransform()                                         '  Reset the brush offset so it is at 0 the next time we use it
      gfx.Render()                                                '  Render the backbuffer to the screen
    End If
  End Sub

End Class
A third option would be to not use winForm's at all, but use tools more suitable to game programming, but that is an advanced subject.
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 02-23-2013 at 02:51 PM.
Reply With Quote
Old 02-28-2013, 08:13 PM
Cadrean Cadrean is offline
Join Date: Feb 2013
Posts: 2

Hey, thanks for the help. I've got the map problem solved, I'll be completely honest, I really have no idea how. I did try some of the options you've suggested, but unfortunately the map I have in the background was a little too detailed for the texturebrush, the map always turned out really blurry.

Now onto the Collision detection and I'll be close to finished.

Thanks again,

Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag
Large Bitmap Movement Lag