Compiling Files into SDL Application

Quite inconsistent. And the name of sizeof() seems to mislead you.
sizeof()
tells you the size of the variable, which is either a simple type like
char, int, and similar or a pointer. If you use char[20] it tells the

Before some nitpicker complains, the size of structs and classes, too.

Additional question, do you use C or C++?On Thu, 04 Feb 2010 16:09:04 +0100, Christoph Nelles <@Christoph_Nelles> wrote:


Christoph Nelles

E-Mail : @Christoph_Nelles
Jabber : eazrael at evilazrael.net ICQ : 78819723

PGP-Key : ID 0x424FB55B on subkeys.pgp.net
or http://evilazrael.net/pgp.txt

I have here some GIMP C-source dump made from .png picture. It is a
structure and has some pixel
data size member (pixel_data[15 * 15 * 4 + 1]).

A whole bunch of files. Just put your files into somefiles.h/c. And size
matters, take the size the filesize of the original file on your disk. Not
the length of the string, the size of the variable, the size of the image
data at some color depth, only the size of the file as it would be stored
else. The PBM file had a size of 11, but for the C string representation i
needed to convert the newlines to \n, but this does not change the size.
There’s no way to determine the size from within C if you don’t use
something like uuencode, base64 or something like this.

linkedinfile.h:
#ifndef LINKEDINFILE_H_
#define LINKEDINFILE_H_

#include <SDL/SDL_rwops.h>

typedef struct LinkedInFile {
unsigned int size;
char *data;
} LinkedInFile;

SDL_RWops* GetRWops(LinkedInFile src);

#endif /* LINKEDINFILE_H_ */

linkedinfile.c
#include “linkedinfile.h”

SDL_RWops* GetRWops(LinkedInFile src) {
return SDL_RWFromConstMem(src.data, src.size);
}

somefiles.h
#ifndef SOMEFILES_H_
#define SOMEFILES_H_

#include “linkedinfile.h”

extern LinkedInFile myfirstfile;

#endif /* SOMEFILES_H_ */

somefiles.c
#include “linkedinfile.h”

/* Image in PBM(PNM) Format */
LinkedInFile myfirstfile = {11,“P1\n2 2\n1001”};

main.c
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include “somefiles.h”
#include “stdio.h”

int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(100,100,0,0);
SDL_RWops *rwops = GetRWops(myfirstfile);
SDL_Surface *image = IMG_Load_RW(rwops, 1);
printf(“image size: %dx%d\n”, image->w, image->h);
return 0;
}On Thu, 04 Feb 2010 03:52:48 -0800, “dekyco” wrote:


Christoph Nelles

E-Mail : @Christoph_Nelles
Jabber : eazrael at evilazrael.net ICQ : 78819723

PGP-Key : ID 0x424FB55B on subkeys.pgp.net
or http://evilazrael.net/pgp.txt

Thank you very much Christoph,this is working.

Sure, in the using source file you declared a pointer. Looks like you
haven’t understood C/C++ memory handling yet.

Endeed,I have to keep up with this :slight_smile:

Can this be done with BMP or PNG images?

Here is what I did with BMP,the data is something like this:

LinkedInFile myfirstfile = {954,"\216\0\264\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.\0u\24\244\251\343\255

\354"
"\345\311\207\333\337x$\246L\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0L\0\212"
"\24~-\252d\2002\253nj\4\234*\0\0\0\0s\22\243f\345\254\355\376\377\314\376"

"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0L\0\210\0\0\0\0\0\0"
"\0\0\0\0\0\32\10\0\0\0\0\0\0\0\0\0\0@\0\0\0\0\0\0\0\0\0\0\0\0\0",
};

954 is the size of original BMP file on disk.This compiles but nothing is blitted.

dekyco

Thank you very much Christoph,this is working.

Sure, in the using source file you declared a pointer. Looks like you
haven’t understood C/C++ memory handling yet.

Endeed,I have to keep up with this :slight_smile:

Can this be done with BMP or PNG images?

Here is what I did with BMP,the data is something like this:

LinkedInFile myfirstfile = {954,"\216\0\264\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.\0u\24\244\251\343\255

\354"
"\345\311\207\333\337x$\246L\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0L\0\212"
"\24~-\252d\2002\253nj\4\234*\0\0\0\0s\22\243f\345\254\355\376\377\314\376"

"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0L\0\210\0\0\0\0\0\0"
"\0\0\0\0\0\32\10\0\0\0\0\0\0\0\0\0\0@\0\0\0\0\0\0\0\0\0\0\0\0\0",
};

954 is the size of original BMP file on disk.This compiles but nothing is blitted.

dekyco

How do you convert your files? A BMP starts (usually) with the magic
number “BM”, so this can’t be a BMP.

http://pastebin.com/f18833d11
Try this (C++) it takes two parameters, variable name and file name and
converts this to a LinkedInFile variable.
convert myvariable someimage.png > myvariable.c for example. You can
even pipe this directly into the compiler with
convert myvariable someimage.png |gcc -c -x c -o myvariable.o -
and link it in :wink: With a little bit C++ you can even automagically
register these compiled in files without touching any other file.

(and yes, this program is Q&D)Am 04.02.2010 21:33, schrieb dekyco:

Here is what I did with BMP,the data is something like this:

LinkedInFile myfirstfile =
{954,"\216\0\264\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.\0u\24\244\251\343\255


Christoph Nelles

E-Mail : @Christoph_Nelles
Jabber : eazrael at evilazrael.net ICQ : 78819723

PGP-Key : ID 0x424FB55B on subkeys.pgp.net
or http://evilazrael.net/pgp.txt

You can manually set the pixels of an SDL_Surface.

Code:

SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32 /* 24 if no alpha */, 0, 0, 0, 0)
SDL_LockSurface(surface);
memcpy(surface->pixels, myfirstfile.data, myfirstfile.size);
SDL_UnlockSurface(surface);

Or something like that. Been awhile since I toyed with it.

Christoph thank you for this converter-I am using GIMP and its utility to dump BMP or PNG file to C-source.You are wright concerning BM header,really don’t know why it is not showing up in C source file being made.

Here is what happens when I convert PNG file with this utility and put it in the source:
file.cpp|23|error: stray \ in program|
during compile-time.I is fixed by removing one extra " character from linkedinfile content.

Next,again during compile time:
file.cpp|46|warning: deprecated conversion from string constant to char*|
||=== Build finished: 0 errors, 1 warnings ===|
but this compiles.

Next,during run-time (with PNG file converted): libpng error: invalid literal/lengths set

It is working with BMP (image shows up),but it is blurred and some colors are missing.

dekyco

Why does a BMP header not show up in a C source code file generated by GIMP?

Because GIMP does not “dump BMP or PNG file to C-source” as you think.
GIMP supports C source as an output fileformat for image data – when
you are editing an image in the GIMP or most other raster image
editors, you are not editing a BMP or a GIF or a JPEG – you are just
editing image data, and that data only takes the form of BMP or GIF or
JPEG data when you hit Save.

C source code is not a Windows Bitmap, ergo, no Windows Bitmap header :)On Fri, Feb 5, 2010 at 7:19 AM, dekyco wrote:

Christoph thank you for this converter-I am using GIMP and its utility to
dump BMP or PNG file to C-source.You are wright concerning BM header,really
don’t know why it is not showing up in C source file being made.


http://codebad.com/

Ah ok, didn’t know that GIMP can do this. I thought he used GIMP’s own
XCF format. GIMP provides even RLE for the dumps, that’s cool. Ok, then
dekyco’s earlier dump makes sense and copying the data into an
appropiate SDL_Surface is indeed sufficient.

But real compressed images may be smaller (just for my rescue ;))Am 05.02.2010 14:49, schrieb Donny Viszneki:

On Fri, Feb 5, 2010 at 7:19 AM, dekyco wrote:

Christoph thank you for this converter-I am using GIMP and its utility to
dump BMP or PNG file to C-source.You are wright concerning BM header,really
don’t know why it is not showing up in C source file being made.

Why does a BMP header not show up in a C source code file generated by GIMP?

Because GIMP does not “dump BMP or PNG file to C-source” as you think.
GIMP supports C source as an output fileformat for image data – when
you are editing an image in the GIMP or most other raster image
editors, you are not editing a BMP or a GIF or a JPEG – you are just
editing image data, and that data only takes the form of BMP or GIF or
JPEG data when you hit Save.


Christoph Nelles

E-Mail : @Christoph_Nelles
Jabber : eazrael at evilazrael.net ICQ : 78819723

PGP-Key : ID 0x424FB55B on subkeys.pgp.net
or http://evilazrael.net/pgp.txt