Search code examples
c++visual-studiosdlembedded-resourcesdl-image

Open embedded BMP images with SDL?


I am trying to embed an image in a C++ program, but I can't open it with SDL.

I already tried using xpm, xfc, tried to use SDL_LoadBMP_RW(), tried to convert the image into C code...nothing worked.

I tried adding the image to the "resource files" section and coded this:

#include <image.bmp>
(...)
const char* image_path = "image.bmp";
SDL_RWops* file = SDL_RWFromFile(image_path, "rb");
gHelloWorld = SDL_LoadBMP_RW(file, SDL_TRUE);

This is the output:

Build started...
1>------ Build started: Project: SDL Game, Configuration: Debug x64 ------
1>main.cpp
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,7): error C2018: unknown character '0x12'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,13): error C2146: syntax error: missing ';' before identifier 'Š'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,25): error C2018: unknown character '0x2'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,30): error C2018: unknown character '0x1'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,33): error C2018: unknown character '0x1'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,37): error C2018: unknown character '0x3'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,44): error C2018: unknown character '0x12'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,64): error C3872: '0x8f': this character is not allowed in an identifier
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,96): error C2018: unknown character '0x1e'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,97): error C2018: unknown character '0x15'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,98): error C2018: unknown character '0x1e'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,99): error C3873: '0x2026': this character is not allowed as a first character of an identifier
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,104): error C2018: unknown character '0x1'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,108): error C2018: unknown character '0x13'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(1,116): error C2018: unknown character '0x6'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,1): error C3873: '0xd7': this character is not allowed as a first character of an identifier
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,3): error C2018: unknown character '0x3'
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,5): error C2017: illegal escape sequence
1>C:\Users\Lab\source\repos\SDL Game\resource\image.bmp(2,6): fatal error C1060: compiler is out of heap space
1>Done building project "SDL Game.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I also tried the same but I just wrote this:

gHelloWorld = SDL_LoadBMP("image.bmp");

It does compile but then it writes this in the console:

Unable to load image image.bmp! SDL Error: Parameter 'src' is invalid
Failed to load media!

I am using 64-bit Windows with Visual Studio, I also have SDL2 and SDL2_image installed.


Solution

  • As drescherjm pointed out image.bmp isn't going to be valid C++ code.

    Use something like xxd to convert the bitmap data to a header, ex:

    Input file (test.txt):

    Hello world!
    

    Convert:

    $ xxd -include test.txt | tee test.h
    unsigned char test_txt[] = {
      0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
      0x0a
    };
    unsigned int test_txt_len = 13;
    

    Then you can use SDL_RWFromConstMem() with the array & length values from the header to get a SDL_RWops that SDL_LoadBMP_RW() can ingest.