Graphics with QB64 - QB64 Wiki

Graphics with QB64

From QB64 Wiki

Jump to: navigation, search

In QB 4.5 for DOS you would simply code:


SCREEN 13 PSET (100, 100), 15


...to display a primitive kind of graphics (a dot).


SCREEN 13 is 320 * 200 with 256 color values.


This still works in QB64, however as QB64 is made for modern systems it also has modern graphics.


Instead of using SCREEN 13 we would rather use some other screen mode with a higher resolution, say 800*600, and instead of using 256 colors we would rather use millions of colors (32-bit for example).


So we would like to define our own screen mode, instead of 13.


Take a glimpse at this code:

newscreen = _NEWIMAGE(800, 600, 32) SCREEN newscreen PSET (100, 100), _RGB(255, 255, 255)

_NEWIMAGE creates a rectangular graphic that can be used as the screen. So instead of SCREEN 13, we have SCREEN newscreen that we can define to be exactly what we want (newscreen can of course be any name you want).


In this case we defined it to be 800*600 with 32-bit colors (millions of colors!)


With 32-bit modes we can use _RGB as the color value, this way we can define the color we want!

RGB stands for Red, Green, Blue, and you can define the amount of Red, Green and Blue you want mixed to produce the color.

255 is the highest value of Red, Green and Blue, so having all set to 255 will produce bright white.


See how much more freedom you get with defining your own screen mode instead of having to use predefined ones as in QB 4.5?

Well, this is not the end of your freedom, a lot more can be done with QB64 by simple means!


Let's create something interesting!


newscreen = _NEWIMAGE(800, 600, 32) SCREEN newscreen FOR x = 0 TO 799 FOR y = 0 TO 599 PSET (x, y), _RGB(x MOD 255, y MOD 255, x + y MOD 255) NEXT NEXT


MOD is used so that the RGB values never go over 255 but starts at 1 again instead. This is a use of MOD that is not always described, but now you know!

If you run the example you can easily see how the Red, Green and Blue colors are mixed depending on the x and y position!


The appearent boxes being made are the result of MOD being used so that it reverts back to 1 after it goes beyond 255.



Here's a code example of loading and displaying image files submitted by Unseenmachine! Download Earth.png to use this code:

SCREEN _NEWIMAGE(600, 400, 32) 'Loading and displaying image files. The basics. 'Unlike QB4.5(and all the others!), QB64 can load various image formats without the need 'for complicated code. 'Example of loading an image into memory. ImageFile& = _LOADIMAGE("Earth.png") 'http://dl.dropbox.com/u/8822351/earth.PNG 'Assuming you have a picture called Earth.png in the current folder, it will be loaded to the 'handle ImageFile&. Image file handles are always of type LONG. 'Placing the image on screen is just as easy as loading it. _PUTIMAGE (0, 0), ImageFile& 'This will place the image at the top left corner of the screen (0,0). 'The image will be drawn fullsize. 'To get the width of the image you can use the _WIDTH command. ImageWidth% = _WIDTH(ImageFile&) 'The height of an image can be obtained in a similar manner with the _HEIGHT command. ImageHeight% = _HEIGHT(ImageFile&) 'Using these commands it is easy to scale an image. Here's how to draw a 10% size version. _PUTIMAGE (0, 0)-(ImageWidth% / 10, ImageHeight% / 10), ImageFile& 'You could use the _WIDTH and _HEIGHT command in the _PUTIMAGE code to acheive the same effect, '_PUTIMAGE (0, 0)-(_WIDTH(ImageFile&)/10,_HEIGHT(ImageFile&)/10), ImageFile& 'it's not recommended though. (Do it matter really CLIPPY? i dunno, is one way faster?) 'For sprites, or images where you wish to make one color transparent, you can use the ' _CLEARCOLOR command. _CLEARCOLOR _RGB(0, 0, 0), ImageFile& 'Then when you place the image, all pixels in the image that are black will be invisible.

Code by: unseenmachine




Navigation:

QB64 Tutorials

Go to Keyword Reference - Alphabetical

Go to Keyword Reference - By usage