Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to clear a buffer quickly?
#1
I'm trying to implement tinyptc to speed up my screen saver. Problem is, it works faster using pset and screencopy at the moment.

One thing that would help the speed of the tinyptc code would be clearing the buffer in one fell swoop (setting it to all zeros) instead of using a for next loop to set all 480,000 entries one at a time.

I know this is probably a stupid newb type question but I couldn't find the answer using the forum search.


Also, I'm using a screensaver example that I found posted on this board I think. It uses the following function:

Code:
FUNCTION keypressed()
    DIM AS INTEGER temp,tx,ty
    FOR temp = 0 TO 127
        IF MULTIKEY(1) THEN RETURN 10
    NEXT
    GETMOUSE tx,ty,,temp
    IF tx <> 320 OR ty <> 240 OR temp <> 0 THEN RETURN -1
    RETURN 0
END FUNCTION

That no longer works when I am using tinyptc. The program will exit as soon as it starts even though no key has been pressed and no mouse movement has occured. Any ideas why?
Reply
#2
first off, please, don't use so many colors, people use different backgrounds for the forum, so just stick with the default color, thanks.

second, i'd imagine that you might have to find some mouse / keyb manipulation routines in tinyptc (any reason that you're not using fb gfx???)

3rd, show some code with your "buffer" and i'll show you the two lines you need to clear it, ok?
Reply
#3
Ok, sorry about the colors, didn't take different background colors into account. :oops:

TinyPTC is a small simple library that does double buffering and nothing else. I'm trying it out because I have read that you should 'NEVER' use pset because it is slower. I was using gfx when I still had pset, screenset and screencopy etc. in the code. (That's right, isn't it? Built in graphic functions are part of gfx?)

The code I'm using for the buffer is pretty much just straight out of the tinyptc example files.

Code:
' Allocates the buffer
redim shared buffer( 0 to 479999 ) as integer

' Place a color at a location in the buffer
buffer( localy * 800 + localx ) = RGB(R,G,B)

' Blit buffer to screen
ptc_update varptr( buffer(0) )

I need to clear the buffer between frames or the new design is plotted while the previous one remains.

Right now I am using this code to accomplish that.

Code:
for i = 0 to 479999
    buffer(i) = 0
next i
Reply
#4
Well nevermind, I just redimensioned the array and that seemed to do it. Now the program is right on par with the gfx version, except for the issue with the keypressed function not working.

Edit: And the keypressed function isn't working because it uses gfx functions and the gfx library isn't loaded because I removed the screenset statement so that I could use tinyptc. Right?
Reply
#5
TinyPTC has no input handling functions, and I think gfxlib's handlers only work when gfxlib is active (through SCREEN or SCREENRES). For an application like this, you'd be okay with the GetAsyncKeyState API call. Not quite sure how you'd handle the mouse though.
\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Reply
#6
Probably the fastest way to set your entire array to zero would be to have a second array that is entirely blank and then just copying it over using a memory dump routine. This is assuming you don't mind using a bit more RAM.

Code:
#include "crt.bi"
redim shared buffer( 0 to 479999 ) as integer
redim shared emptybuffer( 0 to 479999 ) as integer

buffersize = (ubound(buffer) - lbound(buffer) + 1)  * 4
memcpy(@buffer, @emptybuffer, buffersize)

Also, since you are using redim I think simply typing redim buffer(0 To 479999) will reset everything to zero, or am I wrong?
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#7
btw walalce, youre just wasting mem doing that.. c has memset ;]

Code:
#include "crt.bi"

'for i = 0 to 479999
'    buffer(i) = 0
'next i

memset( Varptr( buffer( 0 ) ), 0, 480000 )


maybe using varptr will make the qb freaks happy Sad
Reply
#8
FB has a wrapper for memset since its early days... CLEAR!!!
declare sub CLEAR(byref dest as any, byval value as integer = 0, byval bytes as integer)
Antoni
Reply
#9
I tried using clear but I obviously had it wrong because it wouldn't compile and before I spent much time figuring out why I thought about redimming which did reset the array to zero values.

Copying an array into another one might help me reset another array in my code however. I need it's elements to be reset to 800 many times while the program is running. Would it be worth doing that as far as a time saving measure as opposed to using a loop or would there not be a noticable difference?


btw, thanks for the input everybody. I think I can learn much at this place.
Reply
#10
ReDimming an array just to reset the values to 0 is overkill. A simple CLEAR or memset should do the job cleaner and faster. Assuming your buffer is called "my_buffer" and you have 640*480*4 bytes to clear, this should do the job:

Code:
clear byval my_buffer, 0, 648 * 480 * 4
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)