Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
10-09-2023, 04:35 PM
(This post was last modified: 10-09-2023, 04:36 PM by TerryRitchie.)
Quite often I find the need to clear an image other than the current screen or _DISPLAY and have to do this:
oDest& = _DEST
_DEST MyImage&
CLS
_DEST oDest&
Perhaps CLS could be modified:
CLS [method%][, bgColor&][, ImageHandle&]
so this could be done:
CLS , , MyImage&
Just a thought.
If you have a better way of handing a situation like this than I'm doing let me know.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 2,686
Threads: 326
Joined: Apr 2022
Reputation:
215
Dim Shared ClearScreen As Long
ClearScreen = _NEWIMAGE(_Width,_Height,32)
_DontBlend ClearScreen
....stuff
_PutImage , ClearScreen, Dest
^^ From what I recall, the above is faster than CLS usually.
Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
(10-09-2023, 04:51 PM)SMcNeill Wrote: Dim Shared ClearScreen As Long
ClearScreen = _NEWIMAGE(_Width,_Height,32)
_DontBlend ClearScreen
....stuff
_PutImage , ClearScreen, Dest
^^ From what I recall, the above is faster than CLS usually. As usual a simple solution I've overlooked. Thanks for the tip Steve.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 660
Threads: 142
Joined: Apr 2022
Reputation:
58
10-09-2023, 05:36 PM
(This post was last modified: 10-09-2023, 05:37 PM by James D Jarvis.)
don't forget the cursor position isn't going to be reset (which is good or bad depending on what you are doing). In a really crude test CLS was still about twice as fast but the _putimage method is certainly a great idea for clearing the image at a handle.
Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
(10-09-2023, 05:36 PM)James D Jarvis Wrote: don't forget the cursor position isn't going to be reset (which is good or bad depending on what you are doing). In a really crude test CLS was still about twice as fast but the _putimage method is certainly a great idea for clearing the image at a handle.
That's a good point about the cursor position. Like Steve, I thought _PUTIMAGE would be faster too. Thanks for testing.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 2,686
Threads: 326
Joined: Apr 2022
Reputation:
215
Another idea that might work:
SCREEN _NEWIMAGE(1280, 720, 32)
PCOPY 0,1 'copy blank screen
... stuff
PCOPY 1, 0 'restore blank screen
Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
(10-09-2023, 06:36 PM)SMcNeill Wrote: Another idea that might work:
SCREEN _NEWIMAGE(1280, 720, 32)
PCOPY 0,1 'copy blank screen
... stuff
PCOPY 1, 0 'restore blank screen Doh! I completely forgot about the PCOPY command. I haven't used that since my Tandy 1000 days.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 1,587
Threads: 59
Joined: Jul 2022
Reputation:
52
I was going to propose something with `_COPYIMAGE` but it has been iffy in one of my programs. Allocate `LONG` variable, create graphics screen and right afterward, use `_COPYIMAGE` to replicate it. Then use `_PUTIMAGE` to recall that handle.
`PCOPY` is a more elegant solution, though. Usually no more than two screens are needed, which on a 32-bit system would have gobbled a lot of RAM.
Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
I tested all the various methods pointed out in this thread and by far LINE is faster than anything else. I created a subroutine called CLSI (CLS Image) that can be used for my intended purpose. The code is below with a speed tester to show the slight improvement over CLS.
Code: (Select All) '
' CLS Image
'
' There is a very slight increase in performance using CLSI over CLS.
' The reason I wrote CLS Image was to avoid having to change the
' destination to the image and then back again to the original image.
' The subroutine now handles that cleanly.
'
' Note that CLSI does not support CLS' methods (0 through 2).
'
CONST RED~& = _RGB32(255, 0, 0) ' define a few colors
CONST CYAN~& = _RGB32(0, 255, 255)
DIM Image AS LONG ' test image
DIM c AS LONG ' counter
DIM t1 AS DOUBLE ' time start
DIM t2 AS DOUBLE ' time end
DIM CLSTime AS DOUBLE ' CLS total time
DIM CLSITime AS DOUBLE ' CLSI total time
Image = _NEWIMAGE(320, 200, 32) ' create test image
SCREEN _NEWIMAGE(640, 480, 32) ' create graphics screen
'+----------------------------------------+
'| CLS time test ~2 seconds on 6th gen i7 |
'+----------------------------------------+
t1 = TIMER(.001) ' start time
c = 0 ' reset counter
DO ' begin counted loop
CLS , RED ' clear main screen red
_DEST Image ' change write image to Image
CLS , CYAN ' clear image cyan
_DEST 0 ' change write image to SCREEN
c = c + 1 ' increment counter
LOOP UNTIL c = 32768 ' leave after 32K times
t2 = TIMER(.001) ' end time
CLSTime = t2 - t1 ' calculate total time
'+-----------------------------------------+
'| CLSI time test ~2 seconds on 6th gen i7 |
'+-----------------------------------------+
t1 = TIMER(.001) ' start time
c = 0 ' reset counter
DO ' begin counted loop
CLSI RED, _DEST ' clear main screen red
CLSI CYAN, Image ' clear image cyan
c = c + 1 ' increment counter
LOOP UNTIL c = 32768 ' leave after 32K times
t2 = TIMER(.001) ' end time
CLSITime = t2 - t1 ' calculate total time
'+-----------------+
'| Display results |
'+-----------------+
_PUTIMAGE (159, 139), Image ' place test image onto graphics screen
PRINT "CLS :"; USING "##.#############"; CLSTime ' total time for CLS
PRINT "CLSI :"; USING "##.#############"; CLSITime ' total time for CLSI
PRINT "Difference:"; USING "##.#############"; CLSTime - CLSITime ' difference
SLEEP ' wait for key press
SYSTEM ' return to OS
' _____________________________________________________________________________
'/ \
SUB CLSI (bgColor AS _UNSIGNED LONG, ImageHandle AS LONG) ' CLSI |
' _________________________________________________________________________|____
'/ \
'| CLS Image |
'| Note: does not support CLS methods |
'| |
'| bgColor : color used to clear the image |
'| ImageHandle: image to clear |
'\______________________________________________________________________________/
DIM oDest AS LONG ' calling destination
oDest = _DEST ' save calling destination
_DEST ImageHandle ' change write image
LINE (0, 0)-(_WIDTH - 1, _HEIGHT - 1), bgColor, BF ' draw a box filled line
_DEST oDest ' return to calling destination
END SUB
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 422
Threads: 27
Joined: Apr 2022
Reputation:
26
10-10-2023, 05:30 PM
(This post was last modified: 10-10-2023, 05:40 PM by Jack.)
on my PC running Windows 11 the difference is 0.001
the time varies too much between runs to get a good estimate
|