CopyContent image>canvas>image - canvas>image>canvas

Share your advanced PureBasic knowledge/code with the community.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

CopyContent image>canvas>image - canvas>image>canvas

Post by walbus »

This little module can simple directly copy the content from image to image, canvas to canvas, image to canvas, canvas to image

It's is a new function inside BucketFill advanced
http://www.purebasic.fr/english/viewtop ... 12&t=66927

It's very usefull
On PB it's a little fiddly for copy the content from a image to a canvas or other
So i have also maked this little separate module from the function

As sample, you want animated drawing on a canvas
So you can simple pre secure the background how ever you want and also simple write back :wink:

More is not needed !

Code: Select all

CopyContent(image_ID, image_ID) 
CopyContent(image_ID, canvas_ID)
CopyContent(canvas_ID, image_ID)
CopyContent(canvas_ID, canvas_ID)

Code: Select all

DeclareModule CopyContent
  
  Declare CopyContent(source_ID, destination_ID)
  
EndDeclareModule

Module CopyContent
  ; This module can directly copy the content from image to image, canvas to canvas, image to canvas, canvas to image
  ; This module is a part from BucketFill advanced - Author W. Albus - www.nachtoptik.de 
  EnableExplicit
  
  Procedure CopyContent(source_ID, destination_ID)
    Protected source_image, destination_image
    Protected source_canvas, destination_canvas
    Protected *drawing_buffer_adress_source, *drawing_buffer_adress_destination
    Protected drawing_buffer_length_source, drawing_buffer_length_destination
    
    If IsImage(source_ID)
      If StartDrawing(ImageOutput((source_ID)))
        *drawing_buffer_adress_source=DrawingBuffer()
        drawing_buffer_length_source=DrawingBufferPitch()*ImageHeight(source_ID)
        StopDrawing()
        source_image=1
      Else
        ProcedureReturn -1
      EndIf
    EndIf
    
    If IsImage(destination_ID)
      If StartDrawing(ImageOutput((destination_ID)))
        *drawing_buffer_adress_destination=DrawingBuffer()
        drawing_buffer_length_destination=DrawingBufferPitch()*ImageHeight(destination_ID)
        StopDrawing()
        destination_image=1
      Else
        ProcedureReturn -1
      EndIf
    EndIf
    
    If IsGadget(source_ID)
      If StartDrawing(CanvasOutput((source_ID)))
        *drawing_buffer_adress_source=DrawingBuffer()
        drawing_buffer_length_source=DrawingBufferPitch()*GadgetHeight(source_ID)
        StopDrawing()
        source_canvas=1
      Else
        ProcedureReturn -1
      EndIf
    EndIf
    
    If IsGadget(destination_ID)
      If StartDrawing(CanvasOutput((destination_ID)))
        *drawing_buffer_adress_destination=DrawingBuffer()
        drawing_buffer_length_destination=DrawingBufferPitch()*GadgetHeight(destination_ID)
        StopDrawing()
        destination_canvas=1
      Else
        ProcedureReturn -1
      EndIf
    EndIf
    
    If (source_image Or source_canvas) And (destination_image Or destination_canvas) And drawing_buffer_length_source=drawing_buffer_length_destination
      CopyMemory(*drawing_buffer_adress_source, *drawing_buffer_adress_destination, drawing_buffer_length_source) 
      ProcedureReturn 1
    Else
      ProcedureReturn -1
    EndIf  
  EndProcedure
  
EndModule
UseModule CopyContent

CompilerIf #PB_Compiler_IsMainFile
  OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 800, 600, "Test", #PB_Window_SystemMenu )
  canvas_ID=CanvasGadget(#PB_Any, 50, 50, 700, 500, #PB_Canvas_Container)
  ButtonGadget(1, 250, 100, 200, 100, "1234567890xxTestxx1234567890")
  
  image_ID=CreateImage(#PB_Any, 700, 500, 24, $FF)
  
  CopyContent(image_ID, canvas_ID) ; Now the canvas is red
  
  Repeat
  Until WaitWindowEvent()=#PB_Event_CloseWindow
CompilerEndIf
Last edited by walbus on Wed Feb 22, 2017 4:56 pm, edited 1 time in total.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: CopyContent image>canvas>image - canvas>image>canvas

Post by #NULL »

i think if you use static IDs instead of #PB_ANY it might not work correctly. for example you can have an image with ID 0 and a canvas gadget with ID 0 at the same time, so IsImage() or IsGadget() would both succeed, or whichever is called first.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: CopyContent image>canvas>image - canvas>image>canvas

Post by walbus »

Yep, this fails, but it is absolutely not a problem
Static ID change not automatic, so, you see on development immediately it works or it works not
The function give back 1 or 0 and you should ever checking the result
Works it not, you become not a visible positive result, this you can not overlooking
Changing the static ID is then more as simple

Primary you use this function only with temporary images for buffering backgrounds

With PB 560> canvas has on PB a new and important state

Animations without hardware sprites,
directly on canvas or images with a textured background it's more a part for advanced users with a exactely plan
So i self see only a important simplification for better code handling and debugging

As a sample, you can see and test it with codes you found in the BucketFill advanced download packet
You found two samples, one named ...deflickering_RAW.pb, one ...named deflickering_simple.pb
The simple code use this new function, compare the RAW and the simple code, then you see what i mean

This are sample codes for a directly multiple sprite output on canvas, without flickering

The RAW code animation part looking for other eyes as mine complicated, i think, the simple code very very simple
Post Reply