Image2Source

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Image2Source

Post by sudi »

I felt like posting this pice of code. It conversts a image into a cpp so u can ship stuff like splash screens inside your source code without the need to put it into your media folder. That way noone can change that splashscreen.

Its pretty easy to use. Just compile this code drop your image onto the executeable and enter a name for it. The programm will generate a cpp with the specified name. Then include that cpp into your project. To load the texture just do:

Code: Select all

irr::video::ITexture* myCppTexture = LoadTexture_MyTexture(Device, "#myCppTexture");
The MyTexture after LoadTexture_ is the name u specified while converting the image. and the "#myCppTexture" is the name of the texture. It can be anything you want and doesn't have to be related to the original texturename at all. After loading once you can simply get the Texture like any other with:

Code: Select all

Device->getVideoDriver()->getTexture("#myCppTexture");

Code: Select all

#include <irrlicht.h>

int main(int argumentcount, char* arguments[])
{

    if (argumentcount < 2)
    {
        printf("No file specified\n");
        return 0;
    }
    irr::IrrlichtDevice* Device = irr::createDevice(irr::video::EDT_NULL);

    irr::video::IImage* texture = Device->getVideoDriver()->createImageFromFile(arguments[1]);

    if (!texture)
    {
        printf("Couldn't open file\n");
        return 0;
    }

    char buffer[255];
    printf("Enter Name for the header/source: ");
    int scanf_return = scanf("%s", buffer);
    if (scanf_return < 0)
    {
        printf("Couldn't parse Filename\n");
        texture->drop();
        Device->closeDevice();
        Device->drop();
        return 0;
    }
    irr::core::stringc name = buffer;
    irr::core::stringc filename = buffer;
    filename.append(".cpp");
    FILE* datei = fopen(filename.c_str(), "w+");
    if (datei)
    {
        fprintf(datei, "#include <irrlicht.h>\n");
        ///write data
        fprintf(datei, "unsigned char %s[] =\n{\n\t", name.c_str());
        int counter = 0;
        unsigned char* data = (unsigned char*)texture->lock();
        for (unsigned int i=0;i<texture->getDimension().Width*texture->getDimension().Height;++i)
        {
            if (i!=0)
                fprintf(datei, ", 0x%x", data[i]);
            else

                fprintf(datei, "0x%x", data[i]);
            counter++;
            if (counter >= 6)
            {
                counter = 0;
                fprintf(datei, "\n\t");
            }
        }
        texture->unlock();
        fprintf(datei, "}\n");

        ///write loading function
        fprintf(datei, "irr::video::ITexture* LoadTexture_%s(irr::IrrlichtDevice* MyDevice, const irr::c8* name)\n", name.c_str());
        fprintf(datei, "{\n");
        fprintf(datei, "\tirr::video::IImage* img = MyDevice->getVideoDriver()->createImageFromData(irr::video::ECF_R8G8B8, irr::core::dimension2d<irr::u32>(%i,%i), &%s[0], true, false);\n", texture->getDimension().Width, texture->getDimension().Height, name.c_str());
        fprintf(datei, "\tirr::video::ITexture* tex = MyDevice->getVideoDriver()->addTexture(name, img);\n");
        fprintf(datei, "\timg->drop();\n");
        fprintf(datei, "\treturn tex;\n");
        fprintf(datei, "}\n");
        fclose(datei);

        printf("Finished writing File!\n");
    }
    else
    {
        printf("Can't open file for writing");
    }

    texture->drop();
    Device->closeDevice();
    Device->drop();

    return 0;
}
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

Looks useful. I`ll try it. Thanks. :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
ceyron
Posts: 63
Joined: Tue Mar 03, 2009 5:10 pm
Location: Bucuresti, România

Post by ceyron »

thanks, it works, but only if you change line 43 to

Code: Select all

for (unsigned int i=0;i<texture->getDimension().Width*texture->getDimension().Height*texture->getBytesPerPixel();++i) 
Image
Post Reply