PDA

View Full Version : files2pas data 'resource' creator



paul_nicholls
16-03-2007, 05:37 AM
Hi all,

I have made a program that someone may find useful.

As I am using freepascal, I was wanting to do similar things as the borland command line resource compiler program that processes resource script files (.rc) and creates windows resource files (.res) that can be compiled into your programs, but in a cross-platform way.


For example I can take a resource script file like so:

SpritesResource.rc


Background BITMAP "data\background.bmp"
mario BITMAP "data\sprites_mario.bmp"

this is in the format of:

resource_name resource_type resource_filename (absolute or relative to the resource script file as in this example)

Resource_name has to be a valid pascal identifier.
Resource_type can be anything but shouldn't contain spaces. May be a validator or similar in the future.
Resource_file only needs "" around it if it has spaces in the path or name.

Pass it into my files2pas.exe program as the first parameter like this:


files2pas c:\myprograms\SpritesResource.rc

It will then spit out a .pas file for every resource from the script file like so containing the file in hex digit format as a large Const declaration:



Unit mario_Resource;
// Created by files2pas.exe
// on 16-Mar-2007, 03:26:46p

{$IFDEF FPC}
{$Mode Delphi}
{$ENDIF}

Interface

Const
cmario_type = 'BITMAP';
cmario_data: Array[0..120054 - 1] of Byte =
(
$42,$4D,$F6,$D4,$01,$00,$00,$00,$00,$00,$36,$00,$0 0,$00,$28,$00,$00,$00,$C8,$00,$00,$00,$C8,$00,$00,
$00,$01,$00,$18,$00,$00,$00,$00,$00,$C0,$D4,$01,$0 0,$C4,$0E,$00,$00,$C4,$0E,$00,$00,$00,$00,$00,$00,
$00,$00,$00,$00,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,

<SNIP>
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$F F,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
$FF,$FF,$FF,$FF
&#41;;

Implementation

End.


It will also spit out a .pas file with the same name as the resource script file that allows you to get the pointer to each resource data + the size + type using a function:


Unit SpritesResource;
// Created by files2pas.exe
// on 16-Mar-2007, 03&#58;26&#58;43p

&#123;$IFDEF FPC&#125;
&#123;$Mode Delphi&#125;
&#123;$H+&#125;
&#123;$ENDIF&#125;

Interface

Function GetResourceByName&#40;AResourceName&#58; AnsiString;
Var AData&#58; Pointer;
Var ASize&#58; LongInt;
Var AType&#58; AnsiString&#41;&#58; Boolean;

Implementation

Uses
SysUtils,
Background_Resource,
mario_Resource;

Function GetResourceByName&#40;AResourceName&#58; AnsiString;
Var AData&#58; Pointer;
Var ASize&#58; LongInt;
Var AType&#58; AnsiString&#41;&#58; Boolean;

Begin
Result &#58;= False;
AResourceName &#58;= UpperCase&#40;AResourceName&#41;;
If AResourceName = 'BACKGROUND' Then
Begin
AData &#58;= @cBackground_data&#91;0&#93;;
ASize &#58;= SizeOf&#40;cBackground_data&#41;;
AType &#58;= cBackground_type;
Result &#58;= True;
End
Else
If AResourceName = 'MARIO' Then
Begin
AData &#58;= @cmario_data&#91;0&#93;;
ASize &#58;= SizeOf&#40;cmario_data&#41;;
AType &#58;= cmario_type;
Result &#58;= True;
End;
End;

End.


So all you have to do is include the .pas file that has the information in it for retrieving the resource data info (SpritesResource.pas in this case) and use it.

Viola! now all your nice data files can be compiled into the executable file and not 'float' around for everyone to see.

It also makes it easy as you only have to distribute 1 file :)

People can download the source + exe from here

http://fpc4gp2x.eonclash.com/downloads/files2pas.zip

cheers,
Paul.