Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Use GDI32 functions in fixed scale forms
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Sion
Warrior

Denmark
138 Posts

Posted - Aug 09 2004 :  12:24:47 PM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
Okay, the title may be a bit cryptic but it's hard to explain my question in a sentence that short.

I have a form where ScaleMode is vbUser. The scale could for instance be 100 wide and 100 heigh, with left and top set to 0.
In this form I would like to use some of the GDI32 functions such as "Polygon" and "Polyline" to paint on the form. But all the GDI32 functions are operating in pixels and dosn't take the custom form scale into account.
I've tried to convert the custom scale values of the polygon into pixel values with the following code:

  
Private Sub DrawPoly(poly() As Coord)  
    Dim ix As Integer
    'Converts custom scale to pixels  
    For ix = LBound(poly) To UBound(poly)  
      poly(ix).X = Me.ScaleLeft + (poly(ix).X - 5) * (((Me.Width / Screen.TwipsPerPixelX) / (Me.ScaleWidth - Me.ScaleLeft)))  
      poly(ix).Y = Me.ScaleTop + poly(ix).Y * (((Me.Height / Screen.TwipsPerPixelY) / (Me.ScaleHeight - Me.ScaleTop)))  
    Next ix  
  
    'Polygon function creates unfilled polygon on screen.  
    Polygon Me.hDC, poly(1), UBound(poly)  
End Sub
  


But it dosn't work very well. It's not very precise, and when the form is resized it becomes less and less precise. Isn't there a way (or prehabs a API call) to make a stable conversion from a custom scale to a pixel scale?

Thanks in advance

Visit my personal blog at www.AndersNissen.com!

Eric Coleman
Gladiator

USA
811 Posts

Posted - Aug 09 2004 :  1:06:47 PM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
Use ScaleX and ScaleY.

poly(ix).X = Object.ScaleX(poly(ix).X, vbUser, vbPixels)
poly(ix).Y = Object.ScaleY(poly(ix).Y, vbUser, vbPixels)

Object can be the form, a picturebox, the printer, or a user control.

Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Aug 09 2004 :  1:36:16 PM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
And once again Eric Coleman comes to the rescue!™

So simple and elegant... Guess I couldn't see the forest for trees or however the saying goes.
However, when I first tried it it didn't work. I realized that it - for some reason - only works if the scale is reset each time the object is resized. But now it works! Yeah!
EDIT: I didn't think straight when I tried it out - the scale must always be reset in order for the custom scale to work. That's common pratice. I'd actually done that in the game I should use this for... Phew, must be the heat

Mr. Coleman is now officially my programming rolemodel.

Visit my personal blog at www.AndersNissen.com!

Edited by - Sion on Aug 09 2004 2:10:25 PM
Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Aug 09 2004 :  2:03:45 PM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
For completeness here is the same code Eric posted, but also taking Object.ScaleLeft and Object.ScaleTop into account.

  
  poly(ix).x = Object.ScaleX(poly(ix).x - Object.ScaleLeft, vbUser, vbPixels)  
  poly(ix).y = Object.ScaleY(poly(ix).y - Object.ScaleTop, vbUser, vbPixels)  
  

Visit my personal blog at www.AndersNissen.com!
Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Aug 18 2004 :  09:33:58 AM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
I ran a bit low on clock cycles in my game, and found a lot of wasted cycles in this rutine. Instead of using the code as presented in my last post directly, it should be rearranged to be more efficiant.
  
    Dim XScale As Single, YScale As Single
    XScale = Object.ScaleX(1, vbUser, vbPixels)  
    YScale = Object.ScaleY(1, vbUser, vbPixels)  
  
    LOOP
        poly(ix).x = (poly(ix).x - Object.ScaleLeft) * XScale  
        poly(ix).y = (poly(ix).y - Object.ScaleTop) * YScale  
    END LOOP
  


Using the above approach I managed to reduce the cycle usage by ~40%. I ran both versions 2000 times on a polygon with 60 sides. First approach took ~2700ms and second approach took ~1650ms. That's 44,444.44 calculations per second compared to 72,727.27

Visit my personal blog at www.AndersNissen.com!
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Aug 18 2004 :  12:22:15 PM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
That's a great tip! Thanks for sharing.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.12 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.