Crystal Space
Welcome, Guest. Please login or register.
December 19, 2014, 06:40:07 pm

Login with username, password and session length
Search:     Advanced search
9032 Posts in 2046 Topics by 9292 Members
Latest Member: Agaqkopa
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Embed PNG
« previous next »
Pages: [1] Print
Author Topic: Embed PNG  (Read 1410 times)
Phorem
Newbie
*
Posts: 22


View Profile
« on: January 04, 2012, 08:27:47 am »

I have been trying to embed a png (with no luck) into my CS project. This is required and not an option on my part for portability. There will be only two images that are needed to be embedded - additional images will be loaded as local files.
I am on a recent install of CS/CEL 2.0 beta-1 compiled from source on 64bit Linux. Other than HawkNL, it compiled fine.

I have tried using ImageMagick's command 'convert' to get a header file (convert foo.png -o foo.h). I have tried using that header in my project but it produces a very large executable and i couldn't get fmemopen (stdio.h) to do anything with it (lack of knowledge on my part, i know).

So i tried using 'bin2c' and that also produces a static array but in a vector format which is needed for png's (bin2c foo.png foo.cpp). It produces a much smaller file than ImageMagick and it gets complied into the project fine.

Example of Output....
Code:
static char foo_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x07, 0xd0, 0x00, 0x00, 0x03, 0x4f,

My question is, how do use that as a texture? I know how to use a file locally but I can't find any clear examples of how to get a stream or use fmemopen to get a FILE * pointer and use that as a texture.
If anyone has an example of could point me in the right direction that would be great.

Thanks, Phorem.

   
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #1 on: January 04, 2012, 08:45:39 am »

There is a class csMemFile in CS, you can use that to create a file object which can be passed to other CS APIs.
The following constructor can be used to create a “CS file” that wraps your data: http://crystalspace3d.org/docs/online/api/classcsMemFile.html#a95e9a11142f9988d635ff4d722e98b52
Logged
Phorem
Newbie
*
Posts: 22


View Profile
« Reply #2 on: January 06, 2012, 09:30:34 am »

Thank you that worked. Using that approach is MUCH easier.
Thanks, Phorem.
Logged
Phorem
Newbie
*
Posts: 22


View Profile
« Reply #3 on: January 07, 2012, 04:31:51 am »

Actually, technically it does work. I can get the png into an iDataBuffer and use that, for example, as a texture when you throw out a sphere as in the Physics demo.
The problem i am having now is if i call csMemFile and load my textures to memory in OnApplication() I cannot use the loader below (as an example). I have to call these first and then my csMemFile.

Code:
  if (!loader->LoadTexture ("stone", "/data/metal_rusty.png"))
  ReportError ("Error loading %s texture!",CS::Quote::Single ("stone4"));


The application seg faults if i call this after i call csMemFile. It seems i can only use csMemFile after all the other textures are loaded from file??
Does this seem like the usual behavior?
I wanted to load all the textures to memory when loading and use them but that is seg faulting the app.
Thanks for any help.
Phorem.
Logged
Phorem
Newbie
*
Posts: 22


View Profile
« Reply #4 on: January 09, 2012, 04:18:35 am »

Oops!!

There is a big difference between size_t = 6776000 and size_t = 315715.

Works fine now.
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 6.432 seconds with 17 queries.