• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

22 replies to this topic

### #1MartinSmith160  Members

Posted 28 May 2013 - 04:39 AM

Hi Guys,

I was wandering if you could help me. I have a 2D game built using directx 9 and its all working fine. I would however like to reduce the load time for the game as there is a substantial amount of graphics to load. My sprite class uses LPDIRECT3DTEXTURE9 for the images and an image is loaded using D3DXGetImageInfoFromFile().

This approach works perfectly its just I have some animations that have a large number of large individual frames to load. This increases the load time and the size of the overall game by quite a bit. I was looking at a way to replace these animations with some form of flash animation sequence but i cant find a way to maintain the transparency of a flash animation in directx.

Any help of this would be greatly appreciated,

All the best,

Martin

### #2belfegor  Members

Posted 28 May 2013 - 08:34 AM

Pack all data into 1 simple binary file, then use D3DX...LoadFromMemory or fill your buffers manually (or whatever is your way).

I had ~3000 static objects, and each had its own file so it took ~40 seconds to load, after putting all into 1 file now it takes ~2sec.

### #3MartinSmith160  Members

Posted 28 May 2013 - 08:50 AM

Hi belfegor, thanks for the reply.

This sounds along the right lines. I dont really understand how you mean tho. could go explain a little further. I will save all the data of the graphics into one binary file and then use that to load into the directx textures.

### #4MartinSmith160  Members

Posted 28 May 2013 - 09:17 AM

Is this along the lines you you mentioned:

- Create a struct which contains a set of D3DXIMAGE_INFO items

- I would then open my binary data file and initialize all the D3DXIMAGE_INFO items in the struct

- I would then pass these items into my sprite objects load instead of a filename.

Therefore cutting out all the individual creations of the D3DXIMAGE_INFO objects, which is what happens now.

### #5belfegor  Members

Posted 28 May 2013 - 09:44 AM

Yes, put whatever info you need, there is no rule that say you must do it some specific way.

### #6MartinSmith160  Members

Posted 28 May 2013 - 11:15 AM

Ok so this is the load function. Could I just save out the ST_texture objects amd then load them back in. Im worried about the directx contexts not being right.

bool ST_Texture::Load(LPDIRECT3DDEVICE9 d3dDevice, std::string filename, D3DCOLOR transcolor)
{ //standard Windows return value
HRESULT result;

//get width and height from bitmap file
result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
if (result != D3D_OK)
{
st_engine->LogError("Failed to load graphics file: " + filename,false,"ENGINE_ERRORS.txt");
texture = NULL;
return 0;
}

D3DXCreateTextureFromFileEx(
d3dDevice, //Direct3D device object
filename.c_str(), //bitmap filename
info.Width, //bitmap image width
info.Height, //bitmap image height
1, //mip-map levels (1 for no chain)
D3DPOOL_DEFAULT, //the type of surface (standard)
D3DFMT_UNKNOWN, //surface format (default)
D3DPOOL_DEFAULT, //memory class for the texture
D3DX_DEFAULT, //image filter
D3DX_DEFAULT, //mip filter
transcolor, //color key for transparency
&info, //bitmap file info (from loaded file)
NULL, //color palette
&texture ); //destination texture

//make sure the bitmap textre was loaded correctly
if (result != D3D_OK)
{
st_engine->LogError("Failed to load graphics file: " + filename,false,"ENGINE_ERRORS.txt");
texture = NULL;
return 0;
}

return 1;
}

### #7MartinSmith160  Members

Posted 29 May 2013 - 03:25 AM

I dont suppose you could show me the code to load this kind of data in could you.

### #8belfegor  Members

Posted 29 May 2013 - 03:44 AM

You know how to read/write to binary files?

Here is simple example with reading from actual texture file then using D3DXCreateTextureFromFileInMemory or you could use Ex version (its the same):

:

std::ifstream is("texture.dds", std::ios::binary);
is.seekg (0, is.end);
auto length = is.tellg();
is.seekg (0, is.beg);
std::vector<char> vFile(length);
is.close();
hr = D3DXCreateTextureFromFileInMemory(d3d9device, (LPCVOID)&vFile[0], vFile.size(), &texture);
if(FAILED(hr))
{
MessageBox(0, TEXT("D3DXCreateTextureFromFileInMemory() failed!"), 0, 0);
}


If you were pack bunch of textures in 1 binary, you could write some info that you need (offset to specific texture, dimensions, names, lenght in bytes...), and then you could read for eample:

std::ifstream is("packed_textures.bin", std::ios::binary);
... //find offset to specific texture
is.seekg (...); // put cursor to that postion
UINT_t len;
std::vector<char> vFile(len);
hr = D3DXCreateTextureFromFileInMemory(d3d9device, (LPCVOID)&vFile[0], vFile.size(), &texture);
...


Edited by belfegor, 29 May 2013 - 03:47 AM.

### #9MartinSmith160  Members

Posted 29 May 2013 - 06:09 AM

Hi Belfegor,

Thanks for the code, that really helps. I fully understand the first section of code. I just don't get a few things regarding the second. When there is more than one texture in the binary file, im not sure how to search through the file to pick out each individual texture information.

This might be related to the previous question, I'm not sure how you actually "Pack" the textures into one binary file. All my Graphics are .tga files. Would I have to write a program to convert .tga in the .dds file and then save each one into one full file. I have never used a .dds file before so im not really sure how to use them.

### #10belfegor  Members

Posted 29 May 2013 - 07:12 AM

All my Graphics are .tga files

No need to convert to dds, in link i gave you for D3DXCreateTextureFromFileInMemory/Ex documentation:

This function supports the following file formats: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga

You should make up your system for accessing texture data.
I'll make an simple example for writing/packing:
...
struct TextureInfo
{
std::string name; // file name/path (dds, tga...)
D3DXIMAGE_INFO ii;
};
vector<TextureInfo> vTextureNames; // fill with textures info to be packed, use D3DXGetImageInfoFromFile to get D3DXIMAGE_INFO
...
ofsream os("packed_textures.bin", ios::binary);
UINT texFilesCount = vTextureNames.size();
os.write( (const char*)&texFilesCount, sizeof(UINT) );

for(size_t i = 0; i < vTextureNames.size(); ++i) {
UINT nameLen = vTextureNames[i].name.size();
os.write( (const char*)&nameLen, sizeof(size_t) );  // to know how much to read it later
os.write( vTextureNames[i].name.c_str(), vTextureNames[i].name.size() );
os.write( (const char*)&vTextureNames[i].ii, sizeof(D3DXIMAGE_INFO) );

// opening texture file to copy it to our binary
ifstream is(vTextureNames[i].name, ios::binary);
is.seekg (0, is.end);
UINT length = is.tellg();
is.seekg (0, is.beg);
std::vector<char> vFile(length);
is.close();

os.write( (const char*)&length, sizeof(UINT) ); // to know how much to read it later
os.write( (const char*)&vFile[0], vFile.size() );
}
os.close()

Does this makes some sense?

### #11MartinSmith160  Members

Posted 29 May 2013 - 08:02 AM

ok this is making sense now. Thanks for being patient.

Edited by MartinSmith160, 29 May 2013 - 08:38 AM.

### #12MartinSmith160  Members

Posted 30 May 2013 - 02:18 AM

Hi Belfegor,

Just a quick question, each graphic file will have binary data for the D3DXIMAGE_INFO and the actual graphic data from the source graphic file. For a specific item, i will extract the binary data from the packed file and put that into a buffer. I was just wandering does the data have to be in a specific order. So first I add the D3DXIMAGE_INFO bytes in and then the graphic data in and then use that buffer in the D3DXCreateTextureFromFileInMemory function.

Thanks again for the help.

Martin

### #13belfegor  Members

Posted 30 May 2013 - 04:33 AM

It doesn't matter what kind of and in what order do you put things, what matters is that you read those in order that you put them and you need to know their types and size.

Sometimes you must first write the count of objects for array type of things like vectors and strings, before you write those, so you can know how much to read later on:

write

std::vector<int> vInt;
...
std::size_t len = vInt.size();
os.write( (const char*)&len, sizeof(std::size_t) );
os.write( (const char*)&vInt[0], vInt.size() * sizeof(int) );


std::size_t len;
is.read( (char*)&len, sizeof(std::size_t) ); // first thing in file written is length so we read that first, sizeof(std::size_t) bytes
std::vector<int> vInt(len); // create buffer with enough space to read into it
is.read( (char*)&vInt[0], len * sizeof(int) ); // second thing in file is buffer to read, len * sizeof(int) bytes


Edited by belfegor, 30 May 2013 - 04:37 AM.

### #14MartinSmith160  Members

Posted 30 May 2013 - 04:39 AM

Thanks a lot, you have been a great help.

### #15MartinSmith160  Members

Posted 30 May 2013 - 08:23 AM

Hi Belfegor,

I have done what you said and its working. I have wrote the code to do the following:

• Pack data into one big binary file

• Extract the data in a usable form

• load a LPDIRECT3DTEXTURE9 using that data

My question is now, i think my extraction must be poor because loading now takes longer.

struct TextureInfo
{
std::string name; // file name/path (dds, tga...)
D3DXIMAGE_INFO ii;
};

struct Texture_Item
{
std::string name;
std::vector<char> info_buffer;
std::vector<char> buffer;
};

std::vector<std::string> texture_filenames;
std::vector<TextureInfo> vTextureNames;
std::vector<Texture_Item> vTextureItems;

void CreateBinaryFile();
void ExtractBinaryData();

ST_Texture test_bulk_textures[3000];
ST_Sprite *test_bulk_sprites[3000];


Here is my Pack data function:

void GameApp::CreateBinaryFile()
{
texture_filenames.clear();
vTextureNames.clear();

for(int i = 0; i < 3000; i++)
texture_filenames.push_back("Graphics/cloud-day-3.tga");

TextureInfo temp_texture_info;

for(int i = 0; i < texture_filenames.size(); i++)
{
temp_texture_info.name = texture_filenames[i];
D3DXGetImageInfoFromFile(temp_texture_info.name.c_str(), &temp_texture_info.ii);
vTextureNames.push_back(temp_texture_info);
}

std::ofstream os("packed_textures.bin", ios::binary);
UINT texFilesCount = vTextureNames.size();
os.write( (const char*)&texFilesCount, sizeof(UINT) );

for(size_t i = 0; i < vTextureNames.size(); ++i)
{
UINT nameLen = vTextureNames[i].name.size();
os.write( (const char*)&nameLen, sizeof(size_t) );  // to know how much to read it later
os.write( vTextureNames[i].name.c_str(), vTextureNames[i].name.size() );
os.write( (const char*)&vTextureNames[i].ii, sizeof(D3DXIMAGE_INFO) );

// opening texture file to copy it to our binary
ifstream is(vTextureNames[i].name, ios::binary);
is.seekg (0, is.end);
UINT length = is.tellg();
is.seekg (0, is.beg);
std::vector<char> vFile(length);
is.close();

os.write( (const char*)&length, sizeof(UINT) ); // to know how much to read it later
os.write( (const char*)&vFile[0], vFile.size() );
}

os.close();
}


This is my Unpack function:

void GameApp::ExtractBinaryData()
{
vTextureItems.clear();

Texture_Item temp_item;

UINT total_texture_count = 0;
size_t name_length = 0;
UINT texture_byte_length = 0;

ifstream is("packed_textures.bin", ios::binary);
is.seekg (0, is.end);
UINT length = is.tellg();
is.seekg (0, is.beg);

for(int i = 0; i < total_texture_count; i++)
{
temp_item.name = "";
temp_item.info_buffer.clear();
temp_item.buffer.clear();

temp_item.name.resize(name_length);
is.read( (char*) &temp_item.name[0], temp_item.name.size() * sizeof(char));

temp_item.info_buffer.resize(sizeof(D3DXIMAGE_INFO));

temp_item.buffer.resize(texture_byte_length);

vTextureItems.push_back(temp_item);

}

is.close();
}


and this is my new load function to use that data:

bool ST_Texture::Load(LPDIRECT3DDEVICE9 d3dDevice,std::string filename, std::vector<char> d3d_info_buffer, std::vector<char> graphic_buffer, D3DCOLOR transcolor)
{
//standard Windows return value
HRESULT result;

//get width and height from bitmap file
/*result = D3DXGetImageInfoFromFileInMemory((LPCVOID)&d3d_info_buffer[0], d3d_info_buffer.size(), &info);
if (result != D3D_OK)
{
st_engine->LogError("Failed to load graphics file: INFO BUFFER",false,"ENGINE_ERRORS.txt");
texture = NULL;
return 0;
}*/

//get width and height from bitmap file
result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
if (result != D3D_OK)
{
st_engine->LogError("Failed to load graphics file: " + filename,false,"ENGINE_ERRORS.txt");
texture = NULL;
return 0;
}

result = D3DXCreateTextureFromFileInMemoryEx(
d3dDevice,
(LPCVOID)&graphic_buffer[0],
graphic_buffer.size(),
info.Width,
info.Height,
1,                     //mip-map levels (1 for no chain)
D3DPOOL_DEFAULT,       //the type of surface (standard)
D3DFMT_UNKNOWN,        //surface format (default)
D3DPOOL_DEFAULT,       //memory class for the texture
D3DX_DEFAULT,          //image filter
D3DX_DEFAULT,          //mip filter
transcolor,            //color key for transparency
&info,                 //bitmap file info (from loaded file)
NULL,                  //color palette
&texture );            //destination texture

if (result != D3D_OK)
{
st_engine->LogError("Failed to load graphics file: FROM BUFFER",false,"ENGINE_ERRORS.txt");
texture = NULL;
return 0;
}

return 1;

}


As you can see from the load function, i couldnt get the loading of the D3DXIMAGE_INFO from memory working. im not sure why. so I tried it with the standard way and it worked. so atleast i know the graphic binary data load is working.

My question is, am i doing something obviously wrong because my load time is huge now. The ExtractBinaryData() takes ages and so does the actual load.

it worked great for one file, but then i tried your example of using 3000 items. now its takes ages.

Sorry i know that was a lot.

### #16belfegor  Members

Posted 30 May 2013 - 09:17 AM

3000 textures OMG

I see you load whole file in memory, that is not good idea as you will run out of memory fast.

You are doing it all wrong, "graphic_buffer" holds "file in memory" not "d3d_info_buffer" so:

result = D3DXGetImageInfoFromFileInMemory((LPCVOID)&graphic_buffer[0], graphic_buffer.size(), &info);


but you dont even need that since you read that info from file already. Code is a big mess now for quick solution.

//temp_item.info_buffer.resize(sizeof(D3DXIMAGE_INFO)); // NO!
//is.read( (char*) &temp_item.info_buffer[0], sizeof(D3DXIMAGE_INFO) );// NO!
...
D3DXIMAGE_INFO info;

result = D3DXCreateTextureFromFileInMemoryEx(
...,...,,....,
&info,
NULL, //color palette
&texture );



Let me setup project and i will write whole code sample for you.

Edited by belfegor, 30 May 2013 - 09:30 AM.

### #17belfegor  Members

Posted 30 May 2013 - 11:23 AM

EDIT:

OK here is the test in attachment. I think that OS is doing some tricks behind back with file caching (or whatever) since i create 3000 textures from 1 file (as you do),

and reading from 1 binary is just 100ms faster then old "from file" method, although it took just ~1 second to load them all (Release mode), and i don't have SSD hdd.

I think it should be tested with different texture files so it doesn't have a chance to do funny bisnis and results to be valid for comparison.

#### Attached Files

Edited by belfegor, 30 May 2013 - 11:30 AM.

### #18MartinSmith160  Members

Posted 30 May 2013 - 01:27 PM

Ok cheers for making that code. I ran it and I used one of my graphics. After some testing i think .tga is better than .png. They seem to load faster. Anyway, I made some changes to my code to reflect you feedback and now files are loading faster. I ran the 3000 test in your program and then I ran it it in mine and they were pretty much the same. Around 4 seconds to load 3000 textures which seems great.

but...

I added the time measuring between start and end of loading to my application and then tested the time it would take to load 3000 textures via the old method. Using the D3DXCreateTextureFromFileEx function and they wern't very far apart at all.

I will however start using this loading technique with my game and see if i start to see faster results.

Thanks again for the help, your a true legend.

Ill let you know how I get on.

### #19MartinSmith160  Members

Posted 31 May 2013 - 02:46 AM

I wrote a test program that created 500 textures. all the textures were mixed, so different file types and directories. I used the standard load from file method and then used the load from single binary file and measured the durations.

its still faster to use the load from file method. The binary file takes longer. Not really sure what im going to do now. might have to look into getting flash video into the game instead of using all the big animations. I started off tying to get flash in but i could get the transparency of a video to show through which meant it was hard to mix the videos in with the other game graphics.

I will keep trying tho.

Cheers mate.

### #20Tom KQT  Members

Posted 31 May 2013 - 04:23 AM

Do I understand it right that you have each animation frame in a separate image file? Maybe you could try to put more frames to one image, into some kind of matrix. And then just change which part of the image is the sprite using, instead of changing which image is the sprite using. That could be overall faster.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.