Page 1 of 2

Please make all example code DPI aware

Posted: Sun Sep 13, 2020 9:13 am
by Little John
Graphics example codes in the Help of PB 5.72 are not DPI aware.

For instance, this is the example code for LineXY():

Code: Select all

If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   If CreateImage(0, 200, 200) And StartDrawing(ImageOutput(0))
      Box(0, 0, 200, 200, RGB(255, 255, 255))
      For Angle = 0 To 360 Step 3
         LineXY(100, 100, 100+Cos(Radian(Angle))*90, 100+Sin(Radian(Angle))*90, RGB(Random(255), Random(255), Random(255)))
      Next Angle
      StopDrawing() 
      ImageGadget(0, 0, 0, 200, 200, ImageID(0))
   EndIf
   
   Repeat
      Event = WaitWindowEvent()
   Until Event = #PB_Event_CloseWindow
EndIf
When compiled with the compiler option
[ ] Enable DPI aware executable (Windows)
switched OFF, the result is fine:
Image


But with that compiler option switched ON,
the result doesn't look as expected (DPI 125 %):
Image

In order to make it look properly, we have to scale images and graphics functions by our own code.
Only windows and gadgets are scaled automatically by PureBasic.

So the code should be like this:

Code: Select all

If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   If CreateImage(0, DesktopScaledX(200), DesktopScaledY(200)) And StartDrawing(ImageOutput(0))
      Box(0, 0, DesktopScaledX(200), DesktopScaledY(200), RGB(255, 255, 255))
      For Angle = 0 To 360 Step 3
         LineXY(DesktopScaledX(100), DesktopScaledY(100), DesktopScaledX(100+Cos(Radian(Angle))*90), 
                DesktopScaledY(100+Sin(Radian(Angle))*90), RGB(Random(255), Random(255), Random(255)))
      Next Angle
      StopDrawing() 
      ImageGadget(0, 0, 0, 200, 200, ImageID(0))
   EndIf
   
   Repeat
      Event = WaitWindowEvent()
   Until Event = #PB_Event_CloseWindow
EndIf
Using this code, the result always looks like the first picture above, regardless whether the compiler option
[ ] Enable DPI aware executable (Windows)
is switched OFF or ON (PB 5.72 on Windows).

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 11:39 am
by NicTheQuick
I don't think you want to scale the 2D drawing functions itself. They are independent of the GUI. What you want is scaling the content of the ImageGadget when it is shown, not the underlying image.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 11:43 am
by Little John
NicTheQuick wrote:I don't think you want to scale the 2D drawing functions itself. They are independent of the GUI. What you want is scaling the content of the ImageGadget when it is shown, not the underlying image.
Sorry, I don't understand what you mean exactly. Can you please provide code that shows how that example for LineXY() should be written properly in your opinion?

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 12:01 pm
by NicTheQuick
Oh, sorry, I was not aware I am in the forum for bugs in the documentation. I thought you want to make 2D drawing functions DPI aware in general.
Of course in this case your solution is well suited. A more simple solution would be to resize the image just before setting it to the ImageGadget. Or changing the behaviour of the ImageGadget to resize itself to the DPI aware image version, so that the resizing happens in the background without changing the code at all. But the latter would mean that the behaviour of the ImageGadget has to be changed.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 3:23 pm
by Paul
NicTheQuick wrote:I don't think you want to scale the 2D drawing functions itself.
By rescaling the 2D drawing functions as you use them, you end up with a crisp clear image. If you simply resize the final image before displaying it, you end up with a blurry image from stretching pixels. (which is what your apps look like if you make them non DPI aware and let Windows auto scale them for you)

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 3:34 pm
by NicTheQuick
I know, I know. This statement of mine was before I realized we are in the documentation's bug section. I only wanted to say that you don't want automatic DPI awareness within the 2D drawing library.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 3:56 pm
by Saki
The pixel based drawing functions have nothing to do with the DPI thing.

So it is not a mistake of the manual.

It is just difficult to understand and to handle.

The manual should rather go into more detail about these things.

Correctly, the line thickness must also be taken into account.
In so far the Vector Lib is here recommended..

In this respect, enlarging is in principle the right approach :wink:

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 4:41 pm
by Paul
NicTheQuick wrote:I know, I know. This statement ...
LOL, sorry Nic :lol:

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 5:26 pm
by Little John
Saki wrote:The pixel based drawing functions have nothing to do with the DPI thing.

So it is not a mistake of the manual.
These statements are wrong. Everyone can see this easily by looking at my first post in this thread.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 6:00 pm
by Saki
Nevertheless they are two different things.
Your example above adapts one to the other, but still incomplete.
When scaling over 100%, the line thickness must also change.
How do you want to display this in the existing demo codes.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 6:45 pm
by Little John
I just wrote that and how some examples in the Help could and should be improved. That's all.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 7:13 pm
by Saki
Yes, but it should be treated in a separate part,
because the pixel-based commands do exactly what they are supposed to do.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 7:23 pm
by Little John
I cannot explain it better than I already did. EOD.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 7:30 pm
by Saki
It's OK, but as an OS specific thing it should also be treated separately,
as it is not universally valid on PB.

Re: Please make all example code DPI aware

Posted: Sun Sep 13, 2020 7:52 pm
by Little John
Saki wrote:It's OK, but as an OS specific thing it should also be treated separately,
as it is not universally valid on PB.
Sigh.
The PB code that I suggested (2nd code in the first post) runs fine on all supported platforms.