Solved

VB .Circle GDI Alternative

Posted on 2006-11-30
10
756 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
ID: 18048257
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
ID: 18048666
That looks like .NET? Sorry if I confused... but I need a way in VB6 :D
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18048673
Ah....    =)
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18048803
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
ID: 18048811
Silly me...

Instead of GetDC()/ReleaseDC(), you can just use "Me.hdc".
0
 
LVL 4

Author Comment

by:garreH
ID: 18049388
: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
ID: 18049438
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
ID: 18049608
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
ID: 18049609
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
ID: 18049632
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 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