SFML community forums

Help => Graphics => Topic started by: woody1987 on October 27, 2012, 12:57:14 pm

Title: create a sf::Image from an sqlite blob
Post by: woody1987 on October 27, 2012, 12:57:14 pm
Im trying to create an sf::Image from a png stored in a sqlite database. I have an Entity class which currently has three variables, type (int), name (string) and texture (sf::image). The code below works correctly for type and name but im at a loss as to load the texture. Im using SFML2.0 on Ubuntu Linux 12.10 64bit

void loadEntities()
{
    writeToLog("Loading entities");

    openDatabase();
    sqlite3_stmt *query;
    if(sqlite3_prepare_v2(getDatabase(), ("SELECT * FROM " + TABLE_ENTITY).c_str(), -1, &query, 0) == SQLITE_OK)
    {
        writeToLog("Successfully opened " + TABLE_ENTITY);

        int result = 0;
        while(true)
        {
            result = sqlite3_step(query);

            if(result == SQLITE_ROW)
            {
                Entity *e = new Entity();
                e->type = (int)sqlite3_column_int(query, 0);
                e->name = (char*)sqlite3_column_text(query, 1);
                e->texture = (???)sqlite3_column_blob(query, 2);

                newEntity(e, &dataEntities);
            }
            else
            {
                break;
            }
        }
    }
    else
        writeToLog("Error loading " + TABLE_ENTITY);

    closeDatabase();
}
 
Title: Re: create a sf::Image from an sqlite blob
Post by: eXpl0it3r on October 27, 2012, 01:02:05 pm
Are the casts really necessary for int & char?

You can load image data directly from memory with loadFromMemory(), just make sure the format match. ;)
Title: Re: create a sf::Image from an sqlite blob
Post by: woody1987 on October 27, 2012, 01:05:22 pm
Thankyou, could you possibly provide an example?
Title: Re: create a sf::Image from an sqlite blob
Post by: woody1987 on October 27, 2012, 03:16:16 pm
Solved

sf::Image getImageFromBlob(int rowId)
{
    sf::Image image;
    sqlite3_blob *blob;

    int handle = sqlite3_blob_open(getDatabase(), "main", TABLE_ENTITY.c_str(), COLUMN_ENTITY_TEXTURE.c_str(), rowId, 0, &blob);
    if(handle != SQLITE_OK)
        fprintf(stderr, "Couldn't get blob handle (%i): %s\n", handle, sqlite3_errmsg(getDatabase()));
    else
    {
        int blobSize = sqlite3_blob_bytes(blob);
        int offset = 0;
        char *buffer = (char *)malloc(blobSize);

        int ret = sqlite3_blob_read(blob, buffer, blobSize, offset);
        if(ret != SQLITE_OK)
            fprintf(stderr, "Error reading blob: %s\n", sqlite3_errmsg(getDatabase()));
        else
        {
            image.loadFromMemory(buffer, blobSize);
            sqlite3_blob_close(blob);
        }
    }
    return image;
}

void loadEntities()
{
    writeToLog("Loading entities");

    openDatabase();
    sqlite3_stmt *query;
    if(sqlite3_prepare_v2(getDatabase(), ("SELECT * FROM " + TABLE_ENTITY).c_str(), -1, &query, 0) == SQLITE_OK)
    {
        writeToLog("Successfully opened " + TABLE_ENTITY);

        int result = 0;
        while(true)
        {
            result = sqlite3_step(query);

            if(result == SQLITE_ROW)
            {
                Entity *e = new Entity();
                e->type = (int)sqlite3_column_int(query, 0);
                e->name = (char*)sqlite3_column_text(query, 1);
                e->texture = getImageFromBlob(e->type);

                newEntity(e, &dataEntities);
            }
            else
            {
                break;
            }
        }
    }
        else
        writeToLog("Error loading " + TABLE_ENTITY);

    closeDatabase();
}