[fpc-pascal] Placing binary data (resources) in object files?

Anthony Walter sysrpl at gmail.com
Tue Jun 4 20:20:31 CEST 2013


I came with a nice solution to all this and thought I'd share...

I wrote a tool named makeres. In Lazarus edit the default project
configuration adding Compilation | Execute Before | makeres $(CompPath).
Then when ever you build/run a project the following happens:

* Two directories are created if they don't already exist, one named raw
the other resources.
* Any files in raw get compressed and converted to base64 encoded res files
placed in the resources folder
* A unit is automatically maintained for your project named
ProjectName.Resources
* The ProjectName.Resources unit contains the following (example resources
from raw)

unit ProjectName.Resources;

{$mode delphi}

interface

{doc off}
{ To strip unused resources turn off debug symbols and compile with -CX -XX
}

const
  ResLargeFont = {$i resources/large.font.base64};
  ResShaderFrag = {$i resources/shader.frag.base64};
  ResShaderVert = {$i resources/shader.vert.base64};
  ResSkyboxJpeg = {$i resources/skybox.jpg.base64};
{doc on}

implementation

end.

* Then to decompressed and read a resources in your app you call call any
of these functions (built into my library)

{ Convert a resource into a memory location }
function ResMemory(constref Resource: string): Pointer;
{ Convert a resource into a stream }
function ResStream(constref Resource: string): TStream;
{ Convert a resource into a string }
function ResString(constref Resource: string): string;

So for example you can say:

Font.LoadFromStream(ResStream(ResLargeFont));
or
Shader.Compile(ResString(ResShaderFrag), shaderFragment);

So in summary, you just drop files into your raw folder, and use the above
two lines of code. That's all you have to do.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130604/fc8f6d86/attachment.html>


More information about the fpc-pascal mailing list