Resource

Share your advanced PureBasic knowledge/code with the community.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Resource

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I've already made good use of this.

Thanks for sharing Droopy.
I may look like a mule, but I'm not a complete ass.
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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)
Last edited by Seldon on Mon Dec 05, 2005 11:52 am, edited 1 time in total.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks Seldon for the info
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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() ).
Post Reply