Solvedsf::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();
}