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

Author Topic: How to work with std::vector when using pixel array.  (Read 4904 times)

0 Members and 1 Guest are viewing this topic.

Artfloriani

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to work with std::vector when using pixel array.
« on: June 13, 2013, 03:54:26 am »
Hello,

I'm currently using a pixel array on my project. I want to use std::vector but werdly I can't.

For now I'm using:
sf::Uint8* pixels =  new sf::Uint8[sizeX * sizeY * 4];

sf::Image image;
image.create(sizeX, sizeY, pixels);
 

But if I try to change it to:
std::vector<sf::Uint8> pixels; //or even with sf::Uint8*
sf::Image image;
image.create(sizeX, sizeY, pixels);

My IDE gives me an error (before compiling) on image.create saying "no instance of overloaded function "sf::Image::create matches the argument list".

How do I properly create an image with a std::vector of pixels?

Thanks

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How to work with std::vector when using pixel array.
« Reply #1 on: June 13, 2013, 05:58:23 am »
The function you are calling:

Code: [Select]
void Image::create (unsigned int width, unsigned int height, const Uint8 *pixels);
only accepts a pointer to the data, which is why your code doesn't compile. If you want to use std::vector, you'll either have to:
(a) copy all the data into an array, or
(b) try the following code

Code: [Select]
std::vector<sf::Uint8> pixels;
sf::Image image;
image.create(sizeX, sizeY, pixels.data());

Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,

Code: [Select]
std::vector<sf::Uint8> pixels;
// fill pixels...

sf::Uint8* dat = new sf::Uint8[pixels.size()];
std::copy(pixels.begin(), pixels.end(), dat); // copy them somehow..
sf::Image image;
image.create(sizeX, sizeY, dat);
delete[]dat;

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: How to work with std::vector when using pixel array.
« Reply #2 on: June 13, 2013, 08:33:30 am »
Try
std::vector<sf::Uint8> pixels; //or even with sf::Uint8*
sf::Image image;
image.create(sizeX, sizeY, &pixels[0]);
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to work with std::vector when using pixel array.
« Reply #3 on: June 13, 2013, 08:37:32 am »
Quote
Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,
Wow, this is so wrong and unnecessary (see the other answer(s)) :P
Laurent Gomila - SFML developer

Artfloriani

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to work with std::vector when using pixel array.
« Reply #4 on: June 13, 2013, 02:21:51 pm »
Thank you all  :)

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How to work with std::vector when using pixel array.
« Reply #5 on: June 14, 2013, 01:06:53 am »
Quote
Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,
Wow, this is so wrong and unnecessary (see the other answer(s)) :P

I disagree, you shouldn't be afraid to copy your data between structures. If the OP had a std::list for example, this method is the only option, so it's good to know. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to work with std::vector when using pixel array.
« Reply #6 on: June 14, 2013, 08:09:58 am »
Quote
I disagree, you shouldn't be afraid to copy your data between structures. If the OP had a std::list for example, this method is the only option, so it's good to know.
But it's not a std::list. So it is wrong and misleading to give such an answer (and to add "most compilers don't support it" ???).
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to work with std::vector when using pixel array.
« Reply #7 on: June 14, 2013, 08:54:20 am »
Also, it's completely unnecessary to work with new[] and delete[], because std::vector would do the job much better.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: How to work with std::vector when using pixel array.
« Reply #8 on: June 14, 2013, 12:11:54 pm »
Option (b) will only work with the very latest compilers, so I'd recommend option (a). i.e.,
That's just flat-out wrong. Based on vector's definition in the standard, it's safe and has always been safe (in any sane implementation of std::vector) to use &pixels[0] in place of data().
I use the latest build of SFML2

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How to work with std::vector when using pixel array.
« Reply #9 on: June 16, 2013, 05:00:49 am »
Quote
I disagree, you shouldn't be afraid to copy your data between structures. If the OP had a std::list for example, this method is the only option, so it's good to know.
But it's not a std::list. So it is wrong and misleading to give such an answer (and to add "most compilers don't support it" ???).

It's just a more general answer, and it works. That doesn't fall into my definition of wrong.

As for support, it was a guess. I should have said more clearly that data() is a c++11 feature.

Knowledge that vector guarantees that things are contiguous requires a higher-level understanding, especially if you are learning about STL and the way it generically handles containers.