Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
_DISPLAYORDER Question
#2
Here are a couple of tutorial programs I have created so far. Please let me know if you notice something wrong in either of them.

Code: (Select All)
OPTION _EXPLICIT

' Example 1
' Hardware image persistance and display order

DIM SoftwareBox AS LONG ' software image
DIM HardwareBox AS LONG ' hardware image
DIM TempBox AS LONG '     temporary image to draw other images
DIM Count AS INTEGER '    generic counter
DIM x AS INTEGER '        location of images on screen
DIM dir AS INTEGER '      direction of images on screen

SCREEN _NEWIMAGE(640, 480, 32) '                  create the main screen (software surface)

'+-------------------------------------------------------------------------------------------------------------------------+
'| Create two 320x200 images, one a software image, the other a hardware image                                             |
'+-------------------------------------------------------------------------------------------------------------------------+

TempBox = _NEWIMAGE(320, 200, 32) '               create a temporary software image
_DEST TempBox '                                   draw on the temporary image
CLS , _RGB32(0, 0, 255) '                         clear it with a blue background
COLOR _RGB32(255, 255, 0), _RGB32(0, 0, 255) '    set text color to yellow on blue
LOCATE 2, 1 '                                     position cursor
FOR Count = 1 TO 40 '                             print 40 times on temporary image
    PRINT " Software "; '                         print the word
NEXT Count
LINE (0, 0)-(319, 199), _RGB32(255, 255, 0), B '  put a yellow border around the image
SoftwareBox = _COPYIMAGE(TempBox, 32) '           copy temporary software image to a software image (notice the 32)
LOCATE 2, 1 '                                     reposition cursor
FOR Count = 1 TO 40 '                             print 40 times on temporary image
    PRINT " Hardware "; '                         print the word
NEXT Count
LINE (0, 0)-(319, 199), _RGB32(255, 255, 0), B '  redraw the yellow border
HardwareBox = _COPYIMAGE(TempBox, 33) '           copy temporary software image to a hardware image (notice the 33)
_DEST 0 '                                         go back to drawing on the main screen
_FREEIMAGE TempBox '                              temporary image no longer needed

'+-------------------------------------------------------------------------------------------------------------------------+
'| The display order of software and hardware images can be set using the _DISPLAYORDER statement. The default display     |
'| order is to draw software images first and then hardware images second. This has the effect of drawing software images  |
'| in the background (UNDER) and hardware images in the foreground (OVER). Basically, the last surface identified using    |
'| _DISPLAYORDER will have its images on top of all other surfaces.                                                        |
'+-------------------------------------------------------------------------------------------------------------------------+

'+-------------------------------------------------------------------------------------------------------------------------+
'| Enable the following line for the default display order                                                                 |
'+-------------------------------------------------------------------------------------------------------------------------+

_DISPLAYORDER _SOFTWARE , _HARDWARE '             HARDWARE images will appear ON TOP of SOFTWARE images (this is default)

'+-------------------------------------------------------------------------------------------------------------------------+
'| Enable the following line to reverse the default display order (disable the line above)                                 |
'+-------------------------------------------------------------------------------------------------------------------------+

'_DISPLAYORDER _HARDWARE , _SOFTWARE '             SOFTWARE images will appear ON TOP of HARDWARE images

x = 0 '                                           set x location of software image
dir = 1 '                                         set movement direction of software image
DO

    _LIMIT 30
    '+---------------------------------------------------------------------------------------------------------------------+
    '| You'll notice that the software image is leaving a trail of yellow behind it (artifacts) while the hardware is not. |
    '| The hardware surface will automatically move a hardware image from one location to another without leaving any sign |
    '| where the image previously was. A "self-cleaning" method so to speak because the GPU discards the image immediatly. |
    '| The software surface does not have this "self-cleaning" ability and therefore must be maintained by the programmer  |
    '| to keep artifacting from happening. A simple CLS statement will accomplish this.                                    |
    '+---------------------------------------------------------------------------------------------------------------------+

    '    +-----------------------------------------------------------------------------------------------------------------+
    'CLS '| Enable this line of code to keep the software image from artifacting                                            |
    '    +-----------------------------------------------------------------------------------------------------------------+

    '+---------------------------------------------------------------------------------------------------------------------+
    '| Notice that when you activated the CLS statement above the software image now begins to flicker too. The _DISPLAY   |
    '| statement explained below will clear this up as well.                                                               |
    '+---------------------------------------------------------------------------------------------------------------------+

    x = x + dir '                                 a few lines of code to keep the images moving back and forth
    IF x = 320 OR x = 0 THEN dir = -dir

    _PUTIMAGE (x, 100), SoftwareBox '             the software surface is persistant
    _PUTIMAGE (320 - x, 179), HardwareBox '       the hardware surface is NOT persistant

    '+---------------------------------------------------------------------------------------------------------------------+
    '| You'll notice the hardware image is flickering. This is because the hardware surface will automatically clear       |
    '| itself. _DISPLAY must be used in order to retain hardware images until the next screen update.                      |
    '| Remove the remark from the line below to see that the flickering has disappeared.                                   |
    '+---------------------------------------------------------------------------------------------------------------------+

    '          +-----------------------------------------------------------------------------------------------------------+
    '_DISPLAY ' | Enable this line to continuing showing hardware images between screen updates                             |
    '          +-----------------------------------------------------------------------------------------------------------+

    '+---------------------------------------------------------------------------------------------------------------------+
    '| When the _DISPLAY statement above is active all drawing events to the screen are held in a queue. When the code     |
    '| reaches the _DISPLAY statement all drawing events are performed at once. This keeps multiple hardware images from   |
    '| disappearing on the hardware surface during drawing. See HWLesson2.BAS to see an example.                           |
    '+---------------------------------------------------------------------------------------------------------------------+

LOOP UNTIL _KEYDOWN(27) '                         exit when the ESC key is pressed
Code: (Select All)
OPTION _EXPLICIT

' Example 2
' Software surface vs hardware surface

DIM HardwareBox AS LONG ' hardware image
DIM SoftwareBox AS LONG ' software image
DIM TempBox AS LONG '     temporary image to draw other images
DIM Count AS INTEGER '    generic counter

SCREEN _NEWIMAGE(640, 480, 32) '                  create the main screen (software surface)

'+-------------------------------------------------------------------------------------------------------------------------+
'| Create two 320x200 images, one a software image, the other a hardware image                                             |
'+-------------------------------------------------------------------------------------------------------------------------+

TempBox = _NEWIMAGE(320, 200, 32) '               create a temporary software image
_DEST TempBox '                                   draw on the temporary image
CLS , _RGB32(0, 0, 255) '                         clear it with a blue background
COLOR _RGB32(255, 255, 0), _RGB32(0, 0, 255) '    set text color to yellow on blue
LOCATE 2, 1 '                                     position cursor
FOR Count = 1 TO 40 '                             print 40 times on temporary image
    PRINT " Software "; '                         print the word
NEXT Count
LINE (0, 0)-(319, 199), _RGB32(255, 255, 0), B '  put a yellow border around the image
SoftwareBox = _COPYIMAGE(TempBox, 32) '           copy temporary software image to a software image (notice the 32)
LOCATE 2, 1 '                                     reposition cursor
FOR Count = 1 TO 40 '                             print 40 times on temporary image
    PRINT " Hardware "; '                         print the word
NEXT Count
LINE (0, 0)-(319, 199), _RGB32(255, 255, 0), B '  redraw the yellow border
HardwareBox = _COPYIMAGE(TempBox, 33) '           copy temporary software image to a hardware image (notice the 33)
_DEST 0 '                                         go back to drawing on the main screen
_FREEIMAGE TempBox '                              temporary image no longer needed

'+------------------------------------------------------------+
'| Draw three software images to the software surface outside |
'| of a loop. Remember that the SCREEN is the software        |
'| surface. The software surface is maintained by the CPU and |
'| QB64 itself through underlying code.                       |
'+------------------------------------------------------------+

PRINT "              3 Software images drawn 1/10th of a second apart"
_PUTIMAGE (0, 20), SoftwareBox '        draw software images to the software surface (the SCREEN)
_DELAY .1
_PUTIMAGE (159, 139), SoftwareBox
_DELAY .1
_PUTIMAGE (319, 259), SoftwareBox
_DELAY .1
LOCATE 24, 2: PRINT "Once a software image is drawn"
LOCATE 25, 2: PRINT "it persitently stays on the screen"
LOCATE 26, 2: PRINT "with no updating or redrawing needed."
LOCATE 28, 2: PRINT "Hardware images next, press a key..."
SLEEP
CLS

'+------------------------------------------------------------+
'| Draw three hardware images to the hardware surface outside |
'| of a loop. Remember that hardware images have their own    |
'| hardware surface completely independent from the software  |
'| surface. The hardware surface is maintained by the GPU.    |
'+------------------------------------------------------------+

PRINT "              3 Hardware images drawn 1/10th of a second apart"
_PUTIMAGE (0, 20), HardwareBox '        draw hardware images to the hardware surface in the GPU
_DELAY .1
_PUTIMAGE (159, 139), HardwareBox
_DELAY .1
_PUTIMAGE (319, 259), HardwareBox
_DELAY 1.1

LOCATE 24, 2: PRINT "Where are they?"
SLEEP 2
LOCATE 25, 2: PRINT "Hardware images are maintained by the"
LOCATE 26, 2: PRINT "GPU. They are displayed and then discarded."
LOCATE 28, 2: PRINT "Press a key...."
SLEEP

'+------------------------------------------------------------+
'| Draw three hardware images to the hardware surface inside  |
'| of a loop. The _DISPLAY statement needs to be used to      |
'| queue the images. Without the _DISPLAY statement in the    |
'| loop below the hardware images will flicker because the    |
'| GPU will remove them immediatly after they are drawn.      |
'+------------------------------------------------------------+

CLS
PRINT "                   3 Hardware images drawn using a queue"
LOCATE 24, 2: PRINT "You need to queue them up using"
LOCATE 25, 2: PRINT "_DISPLAY inside of a loop after"
LOCATE 26, 2: PRINT "all the images have been drawn."
LOCATE 28, 2: PRINT "Press ESC to exit...."


DO
    _LIMIT 15 '                         limit CPU to keep from overheating
    _PUTIMAGE (0, 20), HardwareBox '    draw hardware images to the _DISPLAY queue
    _PUTIMAGE (159, 139), HardwareBox
    _PUTIMAGE (319, 259), HardwareBox

    '+--------------------------------------------------------+
    '| Disable the _DISPLAY statement below to see the        |
    '| flickering mentioned above.                            |
    '+--------------------------------------------------------+

    _DISPLAY '                          dump hardware images to the GPU, software images to the SCREEN
LOOP UNTIL _KEYDOWN(27)

'+------------------------------------------------------------+
'| When we leave the loop there is nothing refreshing the     |
'| hardware surface so the GPU instantly clears the images.   |
'+------------------------------------------------------------+

_AUTODISPLAY
LOCATE 16, 8: PRINT "Notice how the hardware images disappeared when we left the loop."
LOCATE 18, 5: PRINT "Remember that hardware images are not persistent and will be discarded."
SLEEP 2
END
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply


Messages In This Thread
_DISPLAYORDER Question - by TerryRitchie - 09-14-2023, 09:26 PM
RE: _DISPLAYORDER Question - by TerryRitchie - 09-14-2023, 10:58 PM
RE: _DISPLAYORDER Question - by SMcNeill - 09-14-2023, 11:26 PM
RE: _DISPLAYORDER Question - by GareBear - 09-15-2023, 12:43 AM
RE: _DISPLAYORDER Question - by TerryRitchie - 09-15-2023, 03:53 AM
RE: _DISPLAYORDER Question - by mnrvovrfc - 09-15-2023, 01:18 AM
RE: _DISPLAYORDER Question - by SMcNeill - 09-15-2023, 04:11 AM
RE: _DISPLAYORDER Question - by TerryRitchie - 09-15-2023, 04:17 AM



Users browsing this thread: 2 Guest(s)