EOSERV Forum > Client Editing > How to get the last bmp in a egf file
Page: << 1 >>
How to get the last bmp in a egf file
Author Message
Post #202122 How to get the last bmp in a egf file

I am able to load LoadLibrary(egf name) and display the bitmaps ect I made this function in theory it should work intell there is a missing bmb in the egf file.

int Video::CountResources(std::string resname,unsigned int maxid)
{
    BITMAP bm;
    int id = 0;
    for(int resid = 0;resid < maxid;++resid)
    {
        HMODULE module = LoadLibrary(resname.c_str());
        if(!module)
        {
            return resid;
        }

        HBITMAP bitmap = LoadBitmap( module, MAKEINTRESOURCE( 101+resid ) );
        if (!bitmap)
        {
            return resid-1;
        }

        if (!GetObject( bitmap, sizeof( bm ), reinterpret_cast<LPSTR>( &bm ) ) )
        {
            return resid-1;
        }
        DeleteObject( bitmap );
        FreeLibrary( module );
        ++id;
    }
    return id;
}


I am hopeing there is a simple function I havnt researched that can get me to the last resource in the file with out much codeing. FindLastResource()

7 years, 16 weeks ago
Post #202123 Re: How to get the last bmp in a egf file

Got some resource leaks in your code.

  1. In if(!bitmap) you need to call FreeLibrary before returning
  2. In if(!GetObject) you need to call DeleteObject and FreeLibrary before returning

Also, you should definitely only call LoadLibrary once outside of the loop.

Also, if you declare your for loop like so:

int resid;
for(; resid < maxid; ++resid)

You won't need to have two separate counter variables.

---
class EOSERV {
Programmer | Oldbie
Open source EO Client: https://github.com/ethanmoffat/EndlessClient
};
7 years, 16 weeks ago
Post #202127 Re: How to get the last bmp in a egf file
ethanmoffat posted: (12th Jan 2017, 04:11 am)

Got some resource leaks in your code.

  1. In if(!bitmap) you need to call FreeLibrary before returning
  2. In if(!GetObject) you need to call DeleteObject and FreeLibrary before returning

Also, you should definitely only call LoadLibrary once outside of the loop.

Also, if you declare your for loop like so:

int resid;
for(; resid < maxid; ++resid)

You won't need to have two separate counter variables.


Yeah very slappy code. Thanks for finding those leaks. So back to the problem at hand what I'm thinking about doing is returning the last successfully loaded bitmap id? Here is an edited version.

unsigned int Video::CountResources(std::string resname,unsigned int maxid)
{
    HMODULE module = LoadLibrary(resname.c_str());
    if(!module)return 0;

    BITMAP bm;
    HBITMAP bitmap = 0;
    unsigned int last_id = 0;

    for(unsigned int resid = 0;resid < maxid;++resid)
    {
        if (bitmap) DeleteObject(bitmap);

        bitmap = LoadBitmap( module, MAKEINTRESOURCE( 101+resid ) );
        if (!bitmap) continue;

        if (!GetObject( bitmap, sizeof( bm ), reinterpret_cast<LPSTR>( &bm ))) continue;

        last_id = resid;
    }
    if (bitmap) DeleteObject(bitmap);

    FreeLibrary(module);

    return last_id;
}
7 years, 16 weeks ago
Page: << 1 >>

EOSERV Forum > Client Editing > How to get the last bmp in a egf file