Page 1 of 1

Resource

Posted: Fri Nov 25, 2005 11:43 pm
by Droopy
Code updated For 5.20+

With this functions you can include files, and use it with purebasic functions like CatchImage / CatchSound or extract included file .

Create a file with RC extension and include it as RC_DATA Resource type
Path can be absolute or relative
Exemple :
REG RCData "C:\WINDOWS\REGEDIT.EXE"
SON RCData "son.wav"
IMAGE RCData "image.bmp"

Code: Select all

;/ SaveResourceAs(Name.s,File.s) Save the resource as a file
;/ GetResourcePointer(Name.s) is usefull with PureBasic CatchXXX functions (CatchImage / CatchSound ... )

ProcedureDLL SaveResourceAs(Name.s,File.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource)
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ; Get size of the resource
    Size= SizeofResource_(Handle,HandleResource)
    
    ; Save the file
    FileId=OpenFile(#PB_Any,File)
    If FileId
      WriteData(FileId,HandleGlobalMemoryBlock,Size)
      CloseFile(FileId)
    EndIf
    
    ; Test if the file is written
    If FileSize(File)=Size
      ProcedureReturn 1
    EndIf
  EndIf
  
EndProcedure

ProcedureDLL GetResourcePointer(Name.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource)
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ProcedureReturn HandleGlobalMemoryBlock
  EndIf
EndProcedure

ProcedureDLL GetResourceSize(Name.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource)
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ; Return the size of the resource
    Size= SizeofResource_(Handle,HandleResource)
    ProcedureReturn Size
  EndIf
  
EndProcedure

Posted: Sat Nov 26, 2005 4:24 pm
by srod
I've already made good use of this.

Thanks for sharing Droopy.

Posted: Mon Dec 05, 2005 9:31 am
by Seldon
For images, you can also use LoadBitmap() API.

Put this on your .RC file:

Code: Select all

MY_IMAGE IMAGE "myimage.bmp"
And then you can get the image handle in your PB source.

Code: Select all

H_instance=GetModuleHandle_(0)
IMG_Sfondo=LoadBitmap_(H_instance,"MY_IMAGE")
You can use IMG_Sfondo with usual gfx PB commands. Please notice that IMG_Sfondo is the handle, not the internal ID that PureBasic can assign (unless you use #PB_ANY). For example:

Code: Select all

IMG_Sfondo=CatchImage(#PB_ANY,?myimagelabel)

Posted: Mon Dec 05, 2005 11:42 am
by blueznl
could you do a little more explanation on this? what to use and an example? i'd like to see more of this...

Posted: Mon Dec 05, 2005 9:02 pm
by Droopy
Thanks Seldon for the info

Posted: Tue Dec 06, 2005 9:38 am
by Seldon
I've put together few "PB alike" functions.

Code: Select all

Procedure GetRCPointer(Name.s) 
 H_resource_ID=FindResource_(0,@Name,#RT_RCDATA) 
 If H_resource_ID
  H_global_memory_block=LoadResource_(0,H_resource_ID) 
  If H_global_memory_block
   H_resource_pointer=LockResource_(H_global_memory_block) 
   If H_resource_pointer
    ProcedureReturn(H_resource_pointer) 
   EndIf
  EndIf
 EndIf 
EndProcedure

Procedure GetRCSize(Name.s)
 H_resource_ID=FindResource_(0,@Name,#RT_RCDATA) 
 If H_resource_ID
  size=SizeofResource_(0,H_resource_ID) 
  ProcedureReturn(size)
 EndIf 
EndProcedure

Procedure CatchImageRC(ImageID,Name.s)
 resource_pointer=GetRCPointer(Name)
 If resource_pointer
  IMG_tmp=CatchImage(ImageID,resource_pointer) 
  ProcedureReturn(IMG_tmp)
 EndIf
EndProcedure

Procedure CatchSpriteRC(ImageID,Name.s,Mode)
; 'Mode' parameter can be '0'
 resource_pointer=GetRCPointer(Name)
 If resource_pointer
  IMG_tmp=CatchSprite(ImageID,resource_pointer,Mode) 
  ProcedureReturn(IMG_tmp)
 EndIf
EndProcedure

Procedure CatchSoundRC(SoundID,Name.s,Length)
; Specify 'Length' parameter if using OGG
 resource_pointer=GetRCPointer(Name)
 If resource_pointer
  SND_tmp=CatchSound(SoundID,resource_pointer,Length) 
  ProcedureReturn(SND_tmp)
 EndIf
EndProcedure

Posted: Tue Dec 06, 2005 9:41 am
by Seldon
Droopy: You're welcome. Thank you! :) BTW, I think LoadBitmap() API does more or less what we do manually (i.e. it calls FindResource(), then LoadResource(), etc... ) , though it's handy for 2d images especially for icons or images inside gadgets (it can't be used for sprites as they need to pass from CatchSprite() ).