Help with GDI
Help with GDI
Help with GDI
Help with GDI
Help with GDI
Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI
Help with GDI Help with GDI
Help with GDI
Go Back  Xtreme Visual Basic Talk > > > Help with GDI


Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2014, 12:37 AM
invertedcancan invertedcancan is offline
Newcomer
 
Join Date: Mar 2014
Posts: 2
Exclamation Help with GDI


Overview

Download link below

90% of code is neat enough.
Everything starts in form1. The timer updates everything. There is
a nice loading bar in there. As you can see the loadingbar was a failed
grid attempt. Close but not cigar. gameobject is the root of the hiearchy,then status,then an object. So included is eezeescript and i was
wondering how would i make a scripted tile engine? lol thanks. or maybe
how to make a normal tile engine ?
Also need help with finding a path to an object(im guessing a grid would help)

Last edited by Flyguy; 03-31-2014 at 01:19 AM. Reason: Removed external download link
Reply With Quote
  #2  
Old 03-31-2014, 01:21 AM
Flyguy's Avatar
FlyguyHelp with GDI Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,162
Default

Hi and welcome to this forum.
Don't expect us to download complete projects from external websites.

Better describe the actual problem you have. What do you expect of your code and what does it actually do.
Provide essential code by placing them in the posting between code tags.
Reply With Quote
  #3  
Old 04-01-2014, 08:07 PM
invertedcancan invertedcancan is offline
Newcomer
 
Join Date: Mar 2014
Posts: 2
Cool More information..

A Tilemap explained. Needs help.


Here is the meat and potato's of it. A Grid made with a few variables
variables are initialized in a public sub. loaded in Form load method.

The variables and propertys are tied into a for statement so i can
retrieve the coordinates.All the rectangles are inserted into a list then to
a listbox.

There is a Public Sub Draw that passes in Graphics.
A For statement drawing the grid, with the properties
and variables for later manipulation.

By selecting the coordinates in the listbox the sprite will move to the
coordinates. You have to type the sprite name in a text box.Then click
the picturebox the sprite was loaded in for that to work.
Now to the help part..the coordinates are not quite coming
in right. Will only place in the first two squares.

The Code:
Code:
Imports System
Imports System.Collections.Generic
Public Class grid
    Dim xwidth As Integer
    Dim yheight As Integer
    Dim gRec As Rectangle
    Dim x As Integer
    Dim y As Integer
    Dim listofRec As List(Of Rectangle)
    Public Property xgrid() As Integer
        Get
            Return x
        End Get
        Set(ByVal value As Integer)
            x = value
        End Set
    End Property
    Public Property ygrid() As Integer
        Get
            Return y
        End Get
        Set(ByVal value As Integer)
            y = value
        End Set
    End Property
    Public Property gridRec() As Rectangle
        Get
            Return gRec
        End Get
        Set(ByVal value As Rectangle)
            gRec = value
        End Set
    End Property
    Public Property listRec() As List(Of Rectangle)
        Get
            Return listofRec
        End Get
        Set(ByVal value As List(Of Rectangle))
            listofRec = value
        End Set
    End Property
    Public Sub New()

    End Sub
    Public Sub initialize(ByVal width As Integer, ByVal height As Integer)
        xwidth = width
        yheight = height
        xgrid = 0
        ygrid = 0
        ''gRec = New Rectangle(0, 0, 32, 32)
        listofRec = New List(Of Rectangle)
    End Sub
    Public Sub Draw(ByVal gfx As Graphics)
        For Me.y = 0 To yheight
            For Me.x = 0 To xwidth
                gfx.DrawRectangle(Pens.DarkGreen, Me.x * 16, Me.y * 16, 32, 32)
                gRec = New Rectangle(Me.x, Me.y, 32, 32)
                listofRec.Add(gRec)
                x += 1
            Next
            y += 1
        Next
    End Sub
    
End Class
Code:
Imports jeosgdi
Imports System.Threading
Public Class Form1
    Protected gfx As Graphics
    Dim grid As grid
    Dim pbLoaded As Boolean = False
    Dim destrec, srcrec As Rectangle
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        Timer1.Interval = 1000
        Timer1.Start()
        grid = New grid()
        grid.initialize(50, 50)
        destrec = New Rectangle(MousePosition.X, MousePosition.Y, 32, 32)
        srcrec = New Rectangle(0, 66, 32, 32)

    End Sub

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        gfx = e.Graphics
        gfx.Clear(Color.Black)

        grid.Draw(gfx)
        If pbLoaded = True Then
            loadImg(gfx)
        End If
       

        placeImg(gfx, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, 0, 0, 0))
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub ListBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Invalidate()
        If ListBox1.Items.Count <= 1 Then
            boxret()
        Else
          
        End If
    End Sub
    Public Function boxret()
        For x = 0 To 13520
            Try
                ListBox1.Items.Add(grid.listRec.ElementAt(x))
                x += 1
                Thread.Sleep(1)
            Catch ex As ArgumentOutOfRangeException
                MsgBox(ex.Message)
            End Try
        Next
        Return 0
    End Function
    Private Sub loadImg(ByVal gfx As Graphics)
        If PictureBox1.Image IsNot Nothing Then
            If pbLoaded = True Then
                
                gfx.DrawImage(PictureBox1.Image, MousePosition.X, MousePosition.Y, 32, 32)
            End If
        ElseIf PictureBox1.Image Is Nothing Then
            Return
        End If
        
    End Sub
    Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
            If TextBox2.Text = "wmage" Then
                If pbLoaded = False Then
                    PictureBox1.Image = New Bitmap("img/whitemage.png")
                End If
            ElseIf TextBox2.Text = "skeleton" Then
                PictureBox1.Image = New Bitmap("img/skl1_fr1.gif")
            ElseIf TextBox2.Text = "listcount" Then
                TextBox1.Text = grid.listRec.Count
            End If
        End If
    End Sub

    Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        pbLoaded = True
    End Sub

    Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
        pbLoaded = False
    End Sub
    Private Sub placeImg(ByVal gfx As Graphics, ByVal args As MouseEventArgs)
        If pbLoaded = True Then
            Dim SelectedRectangle As Rectangle

            SelectedRectangle = ListBox1.SelectedItem()

            gfx.DrawImage(PictureBox1.Image, SelectedRectangle.X, SelectedRectangle.Y, 32, 32)

        End If
    End Sub
    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        
    End Sub
End Class

Last edited by Flyguy; 04-02-2014 at 01:35 AM.
Reply With Quote
  #4  
Old 04-02-2014, 11:42 AM
passel's Avatar
passelHelp with GDI passel is offline
Sinecure Expert

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

A lot of problems in that code.
It seems it would be easier to click on the square to use, rather than create a list of thousands of coordinates in a listbox to choose from.

But a few things you shouldn't be doing. I realize you probably added the sleep for debug purposes.
You don't increment the interator (i.e. x) in a For loop. The For loop will automatically increment the value each time through the loop.
Code:
    Public Function boxret()
        For x = 0 To 13520    <======= use Count of items in the list, not an arbitrary constant
            Try
                ListBox1.Items.Add(grid.listRec.ElementAt(x)) <=== Perhaps grid coord,instead of rect position
            '    x += 1              <====== Should not increment X in the loop.
            '    Thread.Sleep(1)   <===== Remove the sleep, slows things down
            Catch ex As ArgumentOutOfRangeException
                MsgBox(ex.Message)
            End Try
        Next
        Return 0
    End Function
Better, not good, but better. Note we add a string, that divides the rectangle position by 32 to convert it to "grid" coordinates, which is more meaningful than 1376, 544, 32, 32
Code:
  Public Function boxret()
    Dim lr As List(Of Rectangle) = grid.listRec
    ListBox1.BeginUpdate()
    For x = 0 To lr.Count - 1
      Try
       ' ListBox1.Items.Add(lr.ElementAt(x))
        ListBox1.Items.Add(String.Format("{0,3},{1,3}", lr(x).X \ 32, lr(x).Y \ 32))

      Catch ex As ArgumentOutOfRangeException
        MsgBox(ex.Message)
      End Try
    Next
    ListBox1.EndUpdate()
    Return 0
  End Function
Because you're incrementing x and y in the loop, they are stepping by 2, which is why you're multiplying by 16 to get rectangles spaced at 32 pixel intervals. That is messed up.
Also, while you are multiplying x and y in the drawing, you don't multiply x and y in your storing of the rectangle, which is why only the upper left corner appears to work.
You're adding to the list everytime you draw, so the list just increases by 2500+ items each second. You should probably generate the rectangle list in the initialize routine, since it only needs to be done once, and then use that list in the draw routine, so I guess I'll show the code modified to do that.
Code:
    Public Sub Draw(ByVal gfx As Graphics)
    '0 to yheight creates yheight + 1 rectangles. You probably want yheight rectangles
    'so should be 0 to yheight - 1.  Same for xwidth
        For Me.y = 0 To yheight    <==== Normally wouldn't use a class variable (Me.x) as an iterator
            For Me.x = 0 To xwidth  <====
                gfx.DrawRectangle(Pens.DarkGreen, Me.x * 16, Me.y * 16, 32, 32)
                gRec = New Rectangle(Me.x, Me.y, 32, 32)   <===== Here is your "problem"
                listofRec.Add(gRec)  <======= This adds more to the list everytime you draw
                x += 1  <======= Don't increment
            Next
            y += 1  <====== Don't increment
        Next
    End Sub
Code:
  Public Sub initialize(ByVal width As Integer, ByVal height As Integer)
    xwidth = width
    yheight = height
    xgrid = 0
    ygrid = 0

    listofRec = New List(Of Rectangle)
    gRec = New Rectangle(0, 0, 32, 32)  'initialize the list with grid of rectangles

    For y As Integer = 0 To yheight - 1
      For x As Integer = 0 To xwidth - 1
        gRec.X = x * 32
        gRec.Y = y * 32
        listofRec.Add(gRec)
      Next
    Next
  End Sub

  Public Sub Draw(ByVal gfx As Graphics)
    For Each r As Rectangle In listofRec
      gfx.DrawRectangle(Pens.DarkGreen, r)
    Next
  End Sub
p.s. As a quick example of one way of determing the rectangle clicked in, I added a little bit of code, you might want to try.
'In the grid class
Code:
'In the declarations area added a class variable after Dim listOfRec
  Dim SelectedRec As Integer = -1

'Added a new method
  Public Function FindRect(xIn As Integer, yIn As Integer) As Integer
    For i = 0 To listofRec.Count - 1
      If listofRec(i).Contains(xIn, yIn) Then
        SelectedRec = i
        Return i 'return found index
      End If
    Next
    Return -1  'not found
  End Function

'Modified Draw to draw the selected rectangle in a different color
  Public Sub Draw(ByVal gfx As Graphics)
    For Each r As Rectangle In listofRec
      gfx.DrawRectangle(Pens.DarkGreen, r)
    Next

    If SelectedRec >= 0 Then
      gfx.DrawRectangle(Pens.Yellow, listofRec(SelectedRec))
    End If
  End Sub
In the form's mousedown event handler
Code:
  Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    grid.FindRect(e.X, e.Y)
    Invalidate() 'update the grid image now
  End Sub
__________________
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; 04-02-2014 at 11:55 AM.
Reply With Quote
Reply

Tags
gdi, rpg skelleton, targetsystem


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
Help with GDI
Help with GDI
Help with GDI Help with GDI
Help with GDI
Help with GDI
Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI Help with GDI
Help with GDI
Help with GDI
 
Help with GDI
Help with GDI
 
-->