Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: create a sf::Image from an sqlite blob  (Read 2263 times)

0 Members and 1 Guest are viewing this topic.

woody1987

  • Newbie
  • *
  • Posts: 3
    • View Profile
create a sf::Image from an sqlite blob
« 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();
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: create a sf::Image from an sqlite blob
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

woody1987

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: create a sf::Image from an sqlite blob
« Reply #2 on: October 27, 2012, 01:05:22 pm »
Thankyou, could you possibly provide an example?

woody1987

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: create a sf::Image from an sqlite blob
« Reply #3 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();
}
 

 

anything