Jump to content

Embedding data in CPP file?


Josh
 Share

Recommended Posts

I am embedding the BFN texture into the engine to make it so the engine doesn't need any media files to run. The code below does this (with most of the data edited out so it won't be five megabytes).

The data is stored in six unsigned char arrays (the BFN texture is a cube map).

This works fine, but my question is whether it's a good idea to be storing data in this manner and whether there is something safer I should do in the code?

#include "Leadwerks.h"

namespace Leadwerks
{
	shared_ptr<Texture> CreateBFN()
	{
		auto bfn = Texture::CubeMap(256, 256);
		auto bank = CreateBank(0);
		
		const unsigned char bfn_data0[182789] = {245,45,5,45,4,54,5,4,54,54...};
		const unsigned char bfn_data1[182789] = {105,45,5,45,4,54,5,4,54,54...};
		const unsigned char bfn_data2[182789] = {245,45,5,45,4,54,5,4,54,54...};
		const unsigned char bfn_data3[182789] = {45,45,5,45,4,54,5,4,54,54...};
		const unsigned char bfn_data4[182789] = {35,45,5,45,4,54,5,4,54,54...};
		const unsigned char bfn_data5[182789] = {55,45,5,45,4,54,5,4,54,54...};
		
		bank->Resize(sizeof(bfn_data0));
		bank->PokeMem(0,(const char*)bfn_data0,sizeof(bfn_data0));
		bank = bank->Decompress();
		bfn->SetPixels(bank->buf,0,0,0);
		
		bank->Resize(sizeof(bfn_data1));
		bank->PokeMem(0,(const char*)bfn_data1,sizeof(bfn_data1));
		bank = bank->Decompress();
		bfn->SetPixels(bank->buf,0,0,1);
		
		bank->Resize(sizeof(bfn_data2));
		bank->PokeMem(0,(const char*)bfn_data2,sizeof(bfn_data2));
		bank = bank->Decompress();
		bfn->SetPixels(bank->buf,0,0,2);
		
		bank->Resize(sizeof(bfn_data3));
		bank->PokeMem(0,(const char*)bfn_data3,sizeof(bfn_data3));
		bank = bank->Decompress();
		bfn->SetPixels(bank->buf,0,0,3);
		
		bank->Resize(sizeof(bfn_data4));
		bank->PokeMem(0,(const char*)bfn_data4,sizeof(bfn_data4));
		bank = bank->Decompress();
		bfn->SetPixels(bank->buf,0,0,4);
		
		bank->Resize(sizeof(bfn_data5));
		bank->PokeMem(0,(const char*)bfn_data5,sizeof(bfn_data5));
		bank = bank->Decompress();
		bfn->SetPixels(bank->buf,0,0,5);
		
		bfn->SetClampMode(true, true, true);
		return bfn;
	}
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Here's what I did:

  • Store one face of the BFN texture as an int array, but first I compressed the data with zlib. I had to add three bytes for the last int since it was not aligned to 4 bytes.
  • Load the array, cut off three bytes, and decompress the data.
  • Then copy the face five times and flip / swap channels to make the other faces. I'm not 100% sure it works yet, but I think it will.
  • It would be a little more efficient if I stored an int64 array, but I will try this.

It's still a 400 kb cpp file that takes a while to compile, but it's manageable.

I would prefer to generate the BFN dynamically if it's not too slow but the SubH's original BFN generator code seems to have vanished.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...