Solved

VB .Circle GDI Alternative

Posted on 2006-11-30
10
773 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 86

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 86

Expert Comment

by:Mike Tomlinson
ID: 18048673
Ah....    =)
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

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 86

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 86

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 86

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 86

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 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