QB64 Phoenix Edition
Keyword of the Day 45: _PIXELSIZE - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16)
+--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13)
+---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49)
+---- Thread: Keyword of the Day 45: _PIXELSIZE (/showthread.php?tid=1362)



Keyword of the Day 45: _PIXELSIZE - SMcNeill - 01-03-2023

_PIXELSIZE...  A truly useful word for the people who might want to make various library routines, but not so much so, for everyone else.

What is it?: _Pixelsize is a simple little function which tells us how many bytes a pixel takes up in memory.

How do we use it?: We simply just call it like any other function.  Something as simple as PS = _PixelSize will work just fine.

An example for people:


Code: (Select All)
Screen 0
Print _PixelSize 'should print 0
Sleep

Screen 12
Print _PixelSize 'should print 1
Sleep

Screen _NewImage(640, 480, 32)
Print _PixelSize 'should print 4
Sleep
System


Run the code above and give it a try.  You'll see that we start out in a text only screen, so _PixelSize reports 0 back to us -- there's no pixels in a text only screen!  There's only columns and rows!

Hit any key and then we swap over to a SCREEN 12 screen.  At this point, _PixelSize will report a value of 1 back to us.  Each pixel on the screen takes up one byte of memory.

Hit another key and we swap over to a 32-bit screen.  Now _PixelSize reports a value of 4 back to us.  This is what everyone should expect as 32-bit screens are, by definition, 32-bits -- or 4-bytes -- in size.  Each pixel has a single byte dedicated for Alpha, Red, Green, Blue color values, in a 32-bit screen.

... Which is all well and good, but not really all that useful in a single program.  If you create your screens, you'll probably know what they are to begin with, and there's no need to use _PixelSize to get that value for you...

... Which is why I mentioned this is a gem of a command for anyone who works on making library style code.  You never know when, who, or where your library routine might end up being called, and as such, you'll want to write it to work across various screen modes.  (Or to at least trouble shoot and error check against certain screen modes.)

For example, let's say I write a PerfectBox routine which draws a lovely graphical box, puts a texture on the background for it, and a label, and it interacts with the mouse, and plays music when it's on the screen and highlighted, and does your taxes for you, and convinces your significant other to look the other way while you make out with the sexy neighbor down the hall or across the road...

If I wrote such an amazing piece of code, I'd probably want to write it up as a library function and save it for use in all my programs.  Right?  Heck, I'd probably even want to share it with all you guys so I could brag to the world (well, the world of QB64PE anyway) about what I'd accomplished, and let all of you bask in its glory as well!

And then old @Pete comes along and starts griping... "None of my programs work now, thanks to your stupid PerfectLibrary button!!"

So how do I fix that??

Code: (Select All)
Sub PerfectButton (Parameters As HiddenByTrademark)
  If _PixelSize = 0 Then
     Print "Pete, this doesn't work in Screen 0!  It's a graphical button!"
     DeletePetesHardDrive
     LaughAtPete
     System
   End If
.... 
    'Rest of my PerfectButton Sub goes below here.
End Sub

Now, as you can see, we've error trapped for anyone using Screen 0 with our perfect button.  Pete can now insert our PerfectButton routine into all his code, and not get generic error messages about invalid syntax and such when he attempts to run his code...

... Let's just hope nobody except Pete happens to try and use our PerfectButton routine on a text Screen...  Our error handler may be a little more intense than most people approve of, in the example above...


RE: Keyword of the Day 45: _PIXELSIZE - SpriggsySpriggs - 01-03-2023

"My girlfriend says my Pixelsize is adequate but I don't know"

-Pete, probably