Page 1 of 1

Move data between two drawingbuffers

Posted: Mon Feb 15, 2016 7:05 pm
by wilbert
Is there any way to work directly with two drawingbuffers ?
Read from one, write to the other (source and target image).
As far as I know you can only access one at a time. :?

Re: Move data between two drawingbuffers

Posted: Mon Feb 15, 2016 9:04 pm
by Fred
No, it's not possible, you will have to have a third buffer in between.

Re: Move data between two drawingbuffers

Posted: Tue Feb 16, 2016 2:35 am
by netmaestro
I must say it does get frustrating when beginners try to answer questions that are above their skill level... :lol: :lol: :lol:

Code: Select all

CompilerIf #PB_Compiler_Thread = 0
  MessageRequester( "OOPS!", "Please compile threadsafe And try again :)" )
CompilerEndIf

Structure ImageData
  *address
  size.i
EndStructure

Declare WorkerBee(*void)

CreateImage( 0, 256, 256, 24, RGB( 255, 0, 0 ) ) ; Image 0 is red
CreateImage( 1, 256, 256, 24, RGB( 0, 0, 255 ) ) ; Image 1 starts out blue

StartDrawing( ImageOutput(0) )
  *imagedata.ImageData = AllocateMemory( SizeOf( ImageData ) )
  With *imagedata
    \address = DrawingBuffer()
    \size    = DrawingBufferPitch() * ImageHeight( 0 )
  EndWith
  tid = CreateThread( @WorkerBee(), *imagedata )
  WaitThread( tid )
StopDrawing()

OpenWindow(0,0,0,256,256,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ImageGadget(0, 0, 0, 256, 256, ImageID(1) ) ; Image 1 should show red now
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure WorkerBee(*this.ImageData)
  StartDrawing( ImageOutput(1) )
    *that = DrawingBuffer()
    CopyMemory( *this\address, *that, *this\size )
  StopDrawing()
EndProcedure
I couldn't resist it, Fred, don't doofus me ok? :mrgreen:

Re: Move data between two drawingbuffers

Posted: Tue Feb 16, 2016 6:49 am
by wilbert
Wow, that's a clever solution netmaestro :shock: :D
I never thought of the idea to use a second thread.
Great idea and it seems to be working cross platform as well.

Re: Move data between two drawingbuffers

Posted: Tue Feb 16, 2016 8:08 am
by Fred
:oops:

You're indeed right, each thread has its own context, so it's perfectly legit code !

Re: Move data between two drawingbuffers

Posted: Tue Feb 16, 2016 10:58 am
by srod
hehe. Bloody amateurs! :lol:

Re: Move data between two drawingbuffers

Posted: Tue Feb 16, 2016 11:37 am
by Fred
stop laughing damit !! :lol:

Re: Move data between two drawingbuffers

Posted: Tue Feb 16, 2016 12:38 pm
by DontTalkToMe
It's even hinted in the docs :lol:
help wrote: If "Create thread-safe executable" is enabled in the compiler options then every thread has its own current drawing output, which means two threads can do drawing on separate outputs at the same time.

Re: Move data between two drawingbuffers

Posted: Wed Feb 17, 2016 9:05 am
by Joris
What are the benefits of being able to do this ?
(I don't understand netmaestro's sample quit well...)

Thanks.

Re: Move data between two drawingbuffers

Posted: Wed Feb 17, 2016 9:18 am
by wilbert
Joris wrote:What are the benefits of being able to do this ?
(I don't understand netmaestro's sample quit well...)
Speed and memory usage.
If you can copy something directly Src -> Dst, it's faster compared to Src -> Tmp -> Dst and you don't need the memory Tmp would occupy.

Re: Move data between two drawingbuffers

Posted: Wed Feb 17, 2016 9:39 am
by Joris
Ha okay. Thanks Wilbert.

I began thinking about access for two users at the same time to a drawings surface...