Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » just another getpixel question

This thread is locked; no one can reply to it. rss feed Print
just another getpixel question
Albin Engström
Member #8,110
December 2006
avatar

I have a little getpixel problem:

the following condition crashes..

  if(_getpixel32((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat, 
        game::enemies::enemies[enyNR].Xpos - game::projectiles::projectiles[proNR].Xpos,
        game::enemies::enemies[enyNR].Ypos - game::projectiles::projectiles[proNR].Ypos)
        != makecol(255, 0, 255))

BUT if i replace != with == it "works"...
to bad i don't need == in this case..

and it doesn't respond like i want it to
as:

  if(getpixel((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat, 
        game::enemies::enemies[enyNR].Xpos - game::projectiles::projectiles[proNR].Xpos,
        game::enemies::enemies[enyNR].Ypos - game::projectiles::projectiles[proNR].Ypos)
        == makecol(255, 0, 255))
{dostuff()}
else
{}

do stuff doesn't execute even if the pixel I'm trying to check is (255, 0, 255). getpixel looks up bitmaps with all color depths right?

i can't figure out what's wrong! :(
help appreciated! (duh)

Matthew Leverton
Supreme Loser
January 1999
avatar

_getpixel32 does not do any bounds checking, so it can crash if you use it improperly, but...

Quote:

BUT if i replace != with == it "works"...
to bad i don't need == in this case..

If that's true, then the crash is in your dostuff() call.

Albin Engström
Member #8,110
December 2006
avatar

oops,

"(_getpixel32((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat,"
is "old", i now use getpixel.

strange... it no longer crashes. - -'..

but the more important problem still exists.

Matthew Leverton
Supreme Loser
January 1999
avatar

Are you sure the color depth of the bitmap matches the screen's?

And of course, are you sure the pixel is 255,0,255?

Albin Engström
Member #8,110
December 2006
avatar

Matthew Leverton said:

Are you sure the color depth of the bitmap matches the screen's?

i thought bitmaps where converted into the screens color depth?
using paint i can only save bitmaps into the 24-bit format..
(lost?)

Matthew Leverton said:

And of course, are you sure the pixel is 255,0,255?

quite sure about that, when drawing them to the buffer the pixels doesn't get drawn and in 32 and 24 color depth isn't that an automatic event trigged by a pixel with maximum red and blue value resp minimum green when using draw_sprite()?

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

i thought bitmaps where converted into the screens color depth?

They are, if you load them after you set the graphics mode.

Albin Engström
Member #8,110
December 2006
avatar

Matthew Leverton said:

They are, if you load them after you set the graphics mode.

And that's what i do. :/

X-G
Member #856
December 2000
avatar

Note that in 32-bit mode the fourth byte - the alpha value - is relevant too. (255, 0, 255, 255) and (255, 0, 255, 0) are very different colors according to the system.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Richard Phipps
Member #1,632
November 2001
avatar

Are the positions you passing within the image correct?
Try finding out what the read values are..

Albin Engström
Member #8,110
December 2006
avatar

Z-G said:

Note that in 32-bit mode the fourth byte - the alpha value - is relevant too. (255, 0, 255, 255) and (255, 0, 255, 0) are very different colors according to the system.

Noted. But am i not checking only for the other values?
even if the alpha channel exist it doesn't matter if i don't check for it. right/wrong?

Richard Phipps said:

Are the positions you passing within the image correct?

They probably are: i have a condition which checks if the point I'm trying to read is inside the bitmap (in this case it's an enemy's bitmap and the action point of a projectile) if it is inside it should be destroyed which it is (not including the second condition), the second condition is the getpixel dependent one:

 if(getpixel((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat, 
        game::enemies::enemies[enyNR].Xpos -  
game::projectiles::projectiles[proNR].Xpos,
        game::enemies::enemies[enyNR].Ypos - 
game::projectiles::projectiles[proNR].Ypos)
        != makecol(255, 0, 255))

and it always executes (if the first condition is true).
the first condition look like this:

//sorry for the unreadable stuff..
      if(game::projectiles::projectiles[proNR].Xpos > game::enemies::enemies[enyNR].Xpos 
      && game::projectiles::projectiles[proNR].Xpos < game::enemies::enemies[enyNR].Xpos 
      + ((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat)->w
      && game::projectiles::projectiles[proNR].Ypos > game::enemies::enemies[enyNR].Ypos 
      && game::projectiles::projectiles[proNR].Ypos < game::enemies::enemies[enyNR].Ypos 
      + ((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat)->h
      )

Richard Phipps said:

Try finding out what the read values are..

Good idea, i will.

Thanks for your help!

X-G
Member #856
December 2000
avatar

Quote:

Noted. But am i not checking only for the other values?
even if the alpha channel exist it doesn't matter if i don't check for it. right/wrong?

You're comparing a 32-bit integer to another 32-bit integer. Of course the entire thing is being taken into account.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Albin Engström
Member #8,110
December 2006
avatar

Quote:

You're comparing a 32-bit integer to another 32-bit integer. Of course the entire thing is being taken into account.

I guess that makes sense in a way.. but how do i check the alpha value? _makecol32 still only accepts 3 values. :/

X-G
Member #856
December 2000
avatar

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Albin Engström
Member #8,110
December 2006
avatar

X-G said:

makeacol32

... ><. thanks :).

Is alpha 255 solid or is it 0?.
no matter which one i use the problem is the same as before. :/.

I'm going to check what values i get.

Tobias Dammers
Member #2,604
August 2002
avatar

I think what you want to compare is the non-alpha part of the pixel value. Since you use magic pink, the alpha channel isn't interesting at all (magic pink pixels are masked out regardless of alpha: in allegro, the alpha channel is only used for the alpha blender).
To be sure, you could bitwise-and the getpixel result with makeacol(255, 255, 255, 0) to mask out the alpha channel in the comparison. Example:

if (getpixel(bmp, x, y) & makeacol32(255, 255, 255, 0) == makeacol32(255, 0, 255, 0))
  do_stuff();

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Kris Asick
Member #1,424
July 2001

Note that getpixel() is based on the pixel format of the bitmap you are getting the pixel from, while makecol() is based on the current screen format.

If you want your routine to be universal, you need to check the bitmap depth before comparing and the use the appropriate makecol#() command. For instance:

source_colour = getpixel(my_bitmap);
switch (bitmap_color_depth(my_bitmap))
{
  case 8: test_colour = makecol8(255,0,255); break;
  case 15: test_colour = makecol15(255,0,255); break;
  case 16: test_colour = makecol16(255,0,255); break;
  case 24: test_colour = makecol24(255,0,255); break;
  case 32: {
    source_colour = makecol24(getr32(source_colour),getg32(source_colour),getb32(source_colour));
    test_colour = makecol24(255,0,255);
  break; }
}
if (source_colour == test_colour) // Do Stuff

Also, makecol() takes a little CPU time. (Not much, a lot more if it's doing 8-bit conversions.) What you should do if you're using constants in makecol() is to get the values you're going to compare immediately after entering a graphics mode and store them into variables. That way, you can skip calling makecol() every time you want to make the comparison.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Albin Engström
Member #8,110
December 2006
avatar

I got it to work after a while, the problem was simple as i didn't check for the right pixels :/.

if(getpixel((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat, 
        (game::enemies::enemies[enyNR].Xpos + ((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat)->w)
         - game::projectiles::projectiles[proNR].Xpos,
        (game::enemies::enemies[enyNR].Ypos + ((BITMAP*)datafiles::sprites_enemies[game::enemies::enemies[enyNR].visual].dat)->h)
         - game::projectiles::projectiles[proNR].Ypos)
        != makecol(255, 0, 255))

Thanks for all your help!! :-*

Go to: