two layer buffering
two layer buffering
two layer buffering
two layer buffering
two layer buffering
two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering
two layer buffering two layer buffering
two layer buffering
Go Back  Xtreme Visual Basic Talk > > > two layer buffering


Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2011, 12:47 AM
Jneiswin Jneiswin is offline
Newcomer
 
Join Date: Dec 2011
Posts: 2
Unhappy two layer buffering


I am working on an over-head game kind of like zelda. I have two back buffers, one for the ground, one for objects. If I am only drawing the ground it seems to work, but when I try to draw the other layer on top of it nothing happens.

Code:
Imports System.Drawing
Imports System.IO

Public Class Form1

    Dim GroundBuffer As Bitmap
    Dim ObjectBuffer As Bitmap
    Dim GroundSource As Bitmap
    Dim ObjectSource As Bitmap
    Dim GroundDrawer As Graphics
    Dim ObjectDrawer As Graphics
    Dim sRect As Rectangle
    Dim dRect As Rectangle
    Dim Seasons As Integer
    Dim GroundFile As String
    Dim ObjectFile As String
    Dim GroundLines() As String
    Dim ObjectLines() As String
    Dim GroundX As Integer
    Dim GroundY As Integer
    Dim ObjectX As Integer
    Dim ObjectY As Integer
    Dim GroundChar As Char
    Dim ObjectChar As Char

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        GroundBuffer = New Bitmap(499, 499)
        ObjectBuffer = New Bitmap(499, 499)
        GroundDrawer = Me.CreateGraphics
        ObjectDrawer = Me.CreateGraphics
        GroundSource = picGround.Image
        ObjectSource = picObjects.Image

    End Sub

    Private Sub SeasonClock_Tick(sender As System.Object, e As System.EventArgs) Handles SeasonClock.Tick

        Seasons = Seasons + 300
        If Seasons > 900 Then
            Seasons = 0
        End If

        ReadMapFiles()

    End Sub

    Private Sub ReadMapFiles()

        Dim gReader As New StreamReader("C:/00L1.txt")
        Dim oReader As New StreamReader("C:/00L2.txt")

        GroundFile = gReader.ReadToEnd
        ObjectFile = oReader.ReadToEnd

        GroundLines = GroundFile.Split(New String() {ControlChars.NewLine.ToCharArray}, StringSplitOptions.None)
        ObjectLines = ObjectFile.Split(New String() {ControlChars.NewLine.ToCharArray}, StringSplitOptions.None)

        GroundX = GroundLines(0).Length
        GroundY = GroundLines.Count
        ObjectX = ObjectLines(0).Length
        ObjectY = ObjectLines.Count

        For i = 0 To GroundX - 1
            For j = 0 To GroundY - 1
                GroundChar = GroundLines(i).Chars(j)
                Select Case GroundChar
                    Case "G"
                        sRect = New Rectangle(0 + Seasons, 0, 50, 50)
                        dRect = New Rectangle(i * 50, j * 50, 50, 50)
                End Select
                GroundDrawer.DrawImage(GroundSource, dRect, sRect, GraphicsUnit.Pixel)
                GroundDrawer = Me.CreateGraphics
                GroundDrawer = (Graphics.FromImage(GroundBuffer))
            Next
        Next

        For i = 0 To ObjectX - 1
            For j = 0 To ObjectY - 1
                ObjectChar = ObjectLines(i).Chars(j)
                Select Case ObjectChar
                    Case "T"
                        sRect = New Rectangle(0, 0, 50, 50)
                        dRect = New Rectangle(i * 50, j * 50, 50, 50)
                End Select
                ObjectDrawer.DrawImage(ObjectSource, dRect, sRect, GraphicsUnit.Pixel)
                ObjectDrawer = Me.CreateGraphics
                ObjectDrawer = (Graphics.FromImage(ObjectBuffer))
            Next
        Next

    End Sub

    Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        GroundBuffer.MakeTransparent(Color.Fuchsia)
        ObjectBuffer.MakeTransparent(Color.Fuchsia)
        GroundDrawer.DrawImage(GroundBuffer, 0, 0, 499, 499)
        ObjectDrawer.DrawImage(ObjectBuffer, 0, 0, 499, 499)
        ReadMapFiles()

    End Sub
End Class
I'm sure I'm being a bit redonent with some of the graphic objects and bitmaps, but I was trying to separate them to make sure that wasn't the issue. I am a bit confused.
Reply With Quote
  #2  
Old 12-23-2011, 04:02 PM
passel's Avatar
passeltwo layer buffering passel is offline
Sinecure Expert

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

I think what you're doing is very wrong.
Calling Me.Create graphics repeatedly is wrong for more reasons than I care to mention.
I haven't tested this with your code, but what I think can be done along the lines that you are attempting, is to :
create the two bitmaps (done).
GroundBuffer = New Bitmap(499, 499)
ObjectBuffer = New Bitmap(499, 499)

Get a Graphics context to each of the bitmaps(FromImage), not to the form (Me.CreateGraphics)
GroundDrawer = Graphics.FromImage(GroundBuffer)
ObjectDrawer = Graphics.FromImage(ObjectBuffer)

Update your bitmaps using the Graphics objects created to access them.
Use the Form's current Graphics object, which is passed to you in the paint event, to draw the bitmaps to the form.

As for when to draw, you have the ReadMapFiles located in several places, and I'm not sure about the need for that. I don't think ReadMapFiles should be at the end of your paint event.
When you update your bitmaps, as you are currently doing in the ReadMapFiles routine, when finished updating them, you would then issue the Me.Invalidate() call, which will trigger the Paint event and update the form with the contents of your bitmaps.
If you have Me.Invalidate at the end of ReadMapFiles, you definitely don't want ReadMapFiles at the end of the Paint event, or you go into a continuous loop between those two routines.

Your code below only modified here on the forum, not tested in Visual Studio, so could have errors:
Code:
Imports System.Drawing
Imports System.IO

Public Class Form1

    Dim GroundBuffer As Bitmap
    Dim ObjectBuffer As Bitmap
    Dim GroundSource As Bitmap
    Dim ObjectSource As Bitmap
    Dim GroundDrawer As Graphics
    Dim ObjectDrawer As Graphics
    Dim sRect As Rectangle
    Dim dRect As Rectangle
    Dim Seasons As Integer
    Dim GroundFile As String
    Dim ObjectFile As String
    Dim GroundLines() As String
    Dim ObjectLines() As String
    Dim GroundX As Integer
    Dim GroundY As Integer
    Dim ObjectX As Integer
    Dim ObjectY As Integer
    Dim GroundChar As Char
    Dim ObjectChar As Char

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        GroundBuffer = New Bitmap(499, 499)
        ObjectBuffer = New Bitmap(499, 499)
        GroundDrawer = Graphics.FromImage(GroundBuffer)
        ObjectDrawer = Graphics.FromImage(ObjectBuffer)
        GroundSource = picGround.Image
        ObjectSource = picObjects.Image
    End Sub

    Private Sub SeasonClock_Tick(sender As System.Object, e As System.EventArgs) Handles SeasonClock.Tick
'... unchanged
    End Sub

    Private Sub ReadMapFiles()
        Dim gReader As New StreamReader("C:/00L1.txt")
        Dim oReader As New StreamReader("C:/00L2.txt")

        GroundFile = gReader.ReadToEnd
        ObjectFile = oReader.ReadToEnd

        GroundLines = GroundFile.Split(New String() {ControlChars.NewLine.ToCharArray}, StringSplitOptions.None)
        ObjectLines = ObjectFile.Split(New String() {ControlChars.NewLine.ToCharArray}, StringSplitOptions.None)

        GroundX = GroundLines(0).Length
        GroundY = GroundLines.Count
        ObjectX = ObjectLines(0).Length
        ObjectY = ObjectLines.Count

        For i = 0 To GroundX - 1
            For j = 0 To GroundY - 1
                GroundChar = GroundLines(i).Chars(j)
                Select Case GroundChar
                    Case "G"
                        sRect = New Rectangle(0 + Seasons, 0, 50, 50)
                        dRect = New Rectangle(i * 50, j * 50, 50, 50)
                End Select
                GroundDrawer.DrawImage(GroundSource, dRect, sRect, GraphicsUnit.Pixel)
' remove these                GroundDrawer = Me.CreateGraphics
' remove these               GroundDrawer = (Graphics.FromImage(GroundBuffer))
            Next
        Next

        For i = 0 To ObjectX - 1
            For j = 0 To ObjectY - 1
                ObjectChar = ObjectLines(i).Chars(j)
                Select Case ObjectChar
                    Case "T"
                        sRect = New Rectangle(0, 0, 50, 50)
                        dRect = New Rectangle(i * 50, j * 50, 50, 50)
                End Select
                ObjectDrawer.DrawImage(ObjectSource, dRect, sRect, GraphicsUnit.Pixel)
'remove                ObjectDrawer = Me.CreateGraphics
'remove                ObjectDrawer = (Graphics.FromImage(ObjectBuffer))
            Next
        Next

    End Sub

    Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        GroundBuffer.MakeTransparent(Color.Fuchsia)
        ObjectBuffer.MakeTransparent(Color.Fuchsia)

        'You want to draw your bitmaps on the form, so use the Form's current graphics object which is passed to you
        e.Graphics.DrawImage(GroundBuffer, 0, 0, 499, 499)
        e.Graphics.DrawImage(ObjectBuffer, 0, 0, 499, 499)

'===        GroundDrawer.DrawImage(GroundBuffer, 0, 0, 499, 499)
'===        ObjectDrawer.DrawImage(ObjectBuffer, 0, 0, 499, 499)
        ReadMapFiles()

    End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 12-23-2011, 05:45 PM
snarfblam's Avatar
snarfblamtwo layer buffering snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

Just passing through, and saw this like of code that irked me:
Code:
        GroundBuffer = New Bitmap(499, 499)
        ObjectBuffer = New Bitmap(499, 499)
I'm pretty sure you mean to create a bitmap that is five-hundred pixels wide and tall, but you're actually create a bitmap that is four-hundred ninety-nine pixels wide and tall.

This probably wouldn't cause your program to come grinding to a halt (you would probably only be missing the bottom row and right-most column if pixels), but if you do this in other places (for example, a buffer to hold your tile graphics), the artifacts from clipping could be disastrous.
__________________
C# _VB.NET _
Reply With Quote
  #4  
Old 12-26-2011, 05:06 PM
Jneiswin Jneiswin is offline
Newcomer
 
Join Date: Dec 2011
Posts: 2
Default

Thank you both for your help. And yes, I was trying to make a 500pixel bitmap, but I thought 0 counted. I will edit it when I get home and let you both know how it turns out.
Reply With Quote
  #5  
Old 12-26-2011, 09:34 PM
passel's Avatar
passeltwo layer buffering passel is offline
Sinecure Expert

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

0 does count, but you calling a function to create a bitmap which expects width and height, not the upper bounds of the pixel coordinates.
A bitmap that is 500 x 500 has a width of 500 and height of 500, whose pixels coordinates range 0 to 499.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply

Tags
back buffering


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

Advertisement:





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.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
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..
subscribe
two layer buffering
two layer buffering
two layer buffering two layer buffering
two layer buffering
two layer buffering
two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering two layer buffering
two layer buffering
two layer buffering
 
two layer buffering
two layer buffering
 
-->