Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Doubt using Image::LoadFromMemory  (Read 3543 times)

0 Members and 1 Guest are viewing this topic.

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Doubt using Image::LoadFromMemory
« on: November 20, 2009, 05:50:54 am »
Good (and late) night! (sigh)

I'm using sfml to remake an old game. A feature that I liked from sfml is the ability to load images from pixels in memory.

The thing is that I have coded a tool that can convert the old game images (paletized, 8-bit, with some compression codes) to a raw tga file.

The problem is that I can't figure how to do that in memory! As I can't use write statements to output things to RAM don't know how to "convert" this algorithm of many cases and a bit of complexity so that it operates without the need of creating a temporary output file.

I know that I just could create that temporary file and use LoadFromFile but it would be much neater if I could do that operations on memmory.

A small portion of the algorithm, as an example:

Code: [Select]
fread( &n, sizeof(n), 1, fichero );
switch( n )
{
case 0: // End of line, fill the rest of widht as translucid
if( (pixelesEscritos % encabezadoTGA.width) != 0 )
{
pixelesRestantes = encabezadoTGA.width - pixelesEscritos % encabezadoTGA.width;
for( int i = 0; i < pixelesRestantes; i++ )
fwrite( &transparente, sizeof(transparente), 1, output );
pixelesEscritos += pixelesRestantes;
}
break;
case 128: // End of image, fill as translucid the widht*height
if( pixelesEscritos < encabezadoTGA.width * encabezadoTGA.height )
{
for( int i = 0; i < (pixelesEscritos - (encabezadoTGA.width * encabezadoTGA.height)); i++ )
{
fwrite( &transparente, sizeof(transparente), 1, output );
pixelesEscritos++;
}
}
seguir = false;
break;



There are other two special cases(n=192, n=193) and three default cases (n>0, n>128, n>193).

The thing is how to make all that without the needing of an output file, if possible. I know this is more a technique/pro. language related question than sfml topic, but I think exploiting this sfml feature will come very handy.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Doubt using Image::LoadFromMemory
« Reply #1 on: November 20, 2009, 08:07:12 am »
If you were outputing to a std::ostream& instead of using those C functions, you could easily switch between a std::ofstream (file) and a std::ostringstream (memory).
fwrite can easily be replaced with stream.write.
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Doubt using Image::LoadFromMemory
« Reply #2 on: November 20, 2009, 01:19:21 pm »
It works! Didn't know about ostringstream. And now I get rid of some deprecated code, too.

Lots of thanks as always Laurent! :D Waiting impatiently for next sfml version!

Cheers!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Doubt using Image::LoadFromMemory
« Reply #3 on: November 20, 2009, 01:34:17 pm »
Quote from: "Balder"
It works! Didn't know about ostringstream. And now I get rid of some deprecated code, too.
Hm, maybe it wouldn't be bad to revise some basic C++ knowledge. ;)

There are many powerful features in C++ which help you to structure code and which simplify maintenance and debugging. Even though you are able to use C in C++, the latter makes a lot of work more comfortable. The time invested in C++ theory is almost always worthwhile. :)
Especially spend some time on OOP and the STL (Standard Template Library).

Quote from: "Balder"
Lots of thanks as always Laurent! :D Waiting impatiently for next sfml version!
You can already now download the latest SVN version of SFML to be up to date. I would even recommend that approach because like this, you have the possibility to use new SFML features and you are going to have to port less code when switching from SFML 1.x to SFML 2.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Doubt using Image::LoadFromMemory
« Reply #4 on: November 20, 2009, 05:12:39 pm »
Thanks for the advice Nexus! I'm actually reading a book about it so I become a more object oriented person :)  The problem is that my C\C++ skills are a bit rusty after some time programming just Pascal at university (hate it).

Thanks also for pointing me the svn thing, I thought it wouldn't be a good idea to use it for development yet but in that case I'll get updated.

'til next time! :D

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Doubt using Image::LoadFromMemory
« Reply #5 on: November 20, 2009, 11:55:25 pm »
Quote from: "Balder"
Thanks for the advice Nexus! I'm actually reading a book about it so I become a more object oriented person :)  The problem is that my C\C++ skills are a bit rusty after some time programming just Pascal at university (hate it).

Thanks also for pointing me the svn thing, I thought it wouldn't be a good idea to use it for development yet but in that case I'll get updated.

'til next time! :D


For a nice C++ tutorial, take a look here: http://hhttp://www.cplusplus.com/doc/tutorial/introduction/. I learnt a lot about C++ since last year with this tutorial and private experiments :D
For a nice C++ reference, just go here: http://cplusplus.com/reference/. I always use it to know how STL works. You can check lots of examples and so.

The best website in my opinion!

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Doubt using Image::LoadFromMemory
« Reply #6 on: November 23, 2009, 05:13:45 am »
Hey! I use that page as reference always while programming but didn't know about that tutorial. Many thanks fella :wink: