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

Author Topic: Most efficient way to draw a picture pixel by pixel?  (Read 44664 times)

0 Members and 2 Guests are viewing this topic.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Most efficient way to draw a picture pixel by pixel?
« Reply #15 on: November 13, 2010, 09:28:54 pm »
Quote from: "Spidyy"
Errors can lead to some nice and artistic effects

Yes. My first impression was: Wow, it looks like a complex effect. Then I realized it was an error.  :D

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Most efficient way to draw a picture pixel by pixel?
« Reply #16 on: November 14, 2010, 01:02:55 am »
Quote from: "sam"

Quote from: "Xorlium"
For example, if x = 2, y =17 and x=17, y =2, you'd be getting always the same colour, but you clearly don't want that.
No, it's right. I wanted to see if I can get everything to be painted white before I make the next big step (adding the array). That's why I wrote
Quote from: "sam"
(stupid nonsense example of what I tried follows)
But I even failed this simple test.  :?


No, I know you wanted everything white, what I'm saying is that x*y doesn't uniquely identify one pixel, so it can't be right. Do what I said and then Laurent repeated: 4(800*y+x) and then that plus 1, 2, and 3. Notice that uniquely identifies each pixel, in the sense that if you have a number n = 800*y+x, there are no two different x and y that give that n.

Xorlium

AlexM

  • Newbie
  • *
  • Posts: 18
    • View Profile
Most efficient way to draw a picture pixel by pixel?
« Reply #17 on: November 24, 2010, 07:05:45 am »
hey I've been trying to get this to work and I've been getting "std::bad_alloc at memory location" errors at runtime when populating the array.

I tried pasting sam's code that outputted the funky array since it at least compiles but I still get the same error on my end.

Here is the my original code (setting up the array in a constructor).


Code: [Select]

ScreenArray::ScreenArray( int width, int height )
{
int arrayLength = width * height;
colorArray = new sf::Uint8[arrayLength * 4];


for (int pos = 1; pos < arrayLength; pos++)
{
colorArray[(pos*4)-1] = 255;
colorArray[(pos*4)-2] = 255;
colorArray[(pos*4)-3] = 255;
colorArray[(pos*4)-4] = 255;

}

}}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Most efficient way to draw a picture pixel by pixel?
« Reply #18 on: November 24, 2010, 08:21:57 am »
You should use the debugger to see what's happening, and where.

Or at least you should print the value of arrayLength.

Quote
Code: [Select]
  for (int pos = 1; pos < arrayLength; pos++)
   {
         colorArray[(pos*4)-1] = 255;
         colorArray[(pos*4)-2] = 255;
         colorArray[(pos*4)-3] = 255;
         colorArray[(pos*4)-4] = 255;
         
   }

This should be:
Code: [Select]
  for (int pos = 0; pos < arrayLength; pos++)
   {
         colorArray[(pos*4)+0] = 255;
         colorArray[(pos*4)+1] = 255;
         colorArray[(pos*4)+2] = 255;
         colorArray[(pos*4)+3] = 255;
   }

Or simply
Code: [Select]
std::fill(colorArray, colorArray + arrayLength + 4, 255);
And use std::vector, not raw arrays ;)
Laurent Gomila - SFML developer

AlexM

  • Newbie
  • *
  • Posts: 18
    • View Profile
Most efficient way to draw a picture pixel by pixel?
« Reply #19 on: November 24, 2010, 05:41:07 pm »
I was using the debugger, I just wasn't using the debugger enough :D

Turns out the error was due to a method called right after populating the array, seems the array logic was fine.

One question regarding std::vector. You would recommend using it even when I know I won't be resizing the array?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Most efficient way to draw a picture pixel by pixel?
« Reply #20 on: November 24, 2010, 05:56:36 pm »
Quote
You would recommend using it even when I know I won't be resizing the array?

Yes, because:
- you won't have to store the size yourself
- you won't have to manage the memory manually

std::vector is more than a resizable array, it's a generic abstraction of dynamic arrays.
Laurent Gomila - SFML developer

AlexM

  • Newbie
  • *
  • Posts: 18
    • View Profile
Most efficient way to draw a picture pixel by pixel?
« Reply #21 on: November 24, 2010, 06:05:06 pm »
ok thanks, I do use them when I know I'll be resizing often but I wasn't sure if I should use them in situations like this.

Thanks for the info, I'll make sure I do that in the future :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Most efficient way to draw a picture pixel by pixel?
« Reply #22 on: November 25, 2010, 12:15:44 am »
And if you need static arrays (size known at compile time), you should still abandon raw arrays and use std::tr1::array or boost::array instead. It's worth all the comfortable features like copy semantics, debug assertions or the iterator interface -- with zero overhead in release mode ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

AlexM

  • Newbie
  • *
  • Posts: 18
    • View Profile
Most efficient way to draw a picture pixel by pixel?
« Reply #23 on: November 25, 2010, 02:08:20 am »
Really? awesome.

I've been meaning to check out boost. I'll take a look into it this weekend. From a quick look I see a lot of useful classes there :)

 

anything