[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 806
  • Last Modified:

VB .Circle GDI Alternative

Hey

For my game I am using VB6's .Circle and .Line - but I wondered if these were slow as I have made a basic "particle engine" - and it appears to be quite slow when dealing with 300+ particles (even though fps is reported as 114). I lack expertise in directx and opengl, and due to the time span I really don't want to go down that road of learning either of them.

I read that .NET GetPixel and SetPixel is about 12x faster than .Point - So maybe the same for .Circle and .Line? - Also could you please supply code that will generate a circle (ellipse as I think it is known in GDI)

thanks a lot :D

garreh
0
garreH
Asked:
garreH
  • 7
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
There are several overloads that allow you to call them in different ways.

Here is one way:

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawEllipse(Pens.Black, New Rectangle(50, 50, 25, 25))
        e.Graphics.DrawLine(Pens.Red, New Point(0, 0), New Point(PictureBox1.Width, PictureBox1.Height))
    End Sub
0
 
garreHAuthor Commented:
That looks like .NET? Sorry if I confused... but I need a way in VB6 :D
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ah....    =)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here ya go...

Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long

Private Sub Command1_Click()
    Dim DC As Long
    DC = GetDC(Me.hWnd)
   
    MoveToEx DC, 25, 25, ByVal 0&
    LineTo DC, 50, 50
    Ellipse DC, 25, 25, 50, 50
   
    ReleaseDC Me.hWnd, DC
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Silly me...

Instead of GetDC()/ReleaseDC(), you can just use "Me.hdc".
0
 
garreHAuthor Commented:
:O it works good Idle_Mind! But one more question.. how would I go about changing the colour of the circle or line? And maybe even like its thickness.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You have to create a new Pen.  Then you set the current Pen, keeping a reference to the old pen so that you can put it back when you are done.

Have to pick up the kids from school...will post an example when I get back.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Option Explicit

Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Const PS_SOLID = 0

Private Sub Command1_Click()
    Dim myBrush As Long
    Dim myPen As Long
    Dim prevBrush As Long
    Dim prevPen As Long
   
    myBrush = CreateSolidBrush(RGB(255, 0, 0)) ' Red Brush
    myPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 255)) ' Blue Pen
   
    ' Select the new Pen/Brush KEEPING references to the previous ones
    prevBrush = SelectObject(Me.hdc, myBrush)
    prevPen = SelectObject(Me.hdc, myPen)
       
    Ellipse Me.hdc, 25, 25, 50, 50
    MoveToEx Me.hdc, 25, 25, ByVal 0&
    LineTo Me.hdc, 50, 50
   
    ' Put the previous Pen/Brush back
    SelectObject Me.hdc, prevBrush
    SelectObject Me.hdc, prevPen
   
    ' Clean-up our Pen/Brush
    DeleteObject myBrush
    DeleteObject myPen
End Sub

0
 
garreHAuthor Commented:
hehe okay sure... thanx for ur help so far btw, really appreciate it :)

so far i have found

hPen = CreatePen(PS_SOLID, 0, RGB(0, 255, 0))
hOldPen = SelectObject(picBuffer.hdc, hPen)

and it is working fine.. strange that when i change 0 value to being higher it gets much much slower in drawing... but anyway

thinking about it i was hoping for a more elegant approach as i'm going to be changing the colour every frame (its fading to black) and creating a new pen everytime might be slow?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry, I don't do much low level GDI drawing.  I don't know if there is a faster or better method.  =\
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now