Solved

VB .Circle GDI Alternative

Posted on 2006-11-30
10
739 Views
Last Modified: 2008-02-01
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
Comment
Question by:garreH
  • 7
  • 3
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 4

Author Comment

by:garreH
Comment Utility
That looks like .NET? Sorry if I confused... but I need a way in VB6 :D
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Ah....    =)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Silly me...

Instead of GetDC()/ReleaseDC(), you can just use "Me.hdc".
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Author Comment

by:garreH
Comment Utility
: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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
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
 
LVL 4

Author Comment

by:garreH
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now