i get an base64 encoded image from the cpprestsdk and try to show it in an sdl window.
I cant create a surface from the base64 string, but it works well from a file on disk.
I have tried multiple conversions of the string to char arrays and vectors but the SDL_Surface is always null at the end.
These two posts guided me in the direction of SDL_RWops:
SDL_Surface* base64ToSurface(std::string *image)
{
SDL_RWops *rw = SDL_RWFromConstMem(image, sizeof(image));
SDL_Surface *img = SDL_LoadBMP_RW(rw, 1);
if (img == nullptr)
{
logSDLError(std::cout, "base64ToSurface");
}
return img;
}
void convertBase64ToTexture()
{
//base64 image string trimmed for a better readability
std::string aImage = "R0lGODlhPQB...";
SDL_Window *aWindow = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *aRenderer = SDL_CreateRenderer(aWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Surface *aSurface = base64ToSurface(&aImage);
SDL_Texture *texture = nullptr;
texture = SDL_CreateTextureFromSurface(aRenderer, aSurface);
//Make sure converting went ok too
if (texture == nullptr)
{
logSDLError(std::cout, "CreateTextureFromSurface");
}
}