GetPixel is returning -1?! [SOLVED]

Started by Scavenger, Sun 03/10/2010 03:52:31

Previous topic - Next topic

Scavenger

So I'm working on functions for my game (which I'm programming from the bottom up, as the last version had unworkable code.), and I need to judiciously use GetPixel since I don't want to use plugins this time. Once again, working in 8-bit. I am using 3.1.2 SP1, but I did compile it on 3.2, with no improvement.

As I was working on a colourisation function, I noticed I was getting a lot of strange crashes about "Array Out of Bounds: Range 0-255, not -1". So I tested it a bit and found out it wasn't my shoddy coding producing this result, but the GetPixel function itself.

I'm not sure quite what's causing it, and I'm pretty sure GetPixel isn't supposed to give values like that.

EDIT: The problem has been found and a workaround given. Quite a surprise!

I changed palette slot 254 to Magenta, which messed up the background colours. I have since reimported the background, but maybe that's what's going wrong.

Thanks, guys :) I am pretty hopeless without y'all.

GarageGothic

I'm guessing that GetPixel returns COLOR_TRANSPARENT (which is very likely defined as -1) for RGB(255,0,255) colored pixels no matter their position in the palette, so check whether the color value is COLOR_TRANSPARENT before using it as an array index.

Code: ags

int pal_index = Surface.GetPixel (i, j);
if (pal_index != COLOR_TRANSPARENT) {
  Surface.DrawingColor = _C8PaletteMap [pal_index];
  Surface.DrawPixel (i, j);
  }

Scavenger

It was returning COLOR_TRANSPARENT! Very odd, especially in an 8-Bit game - since there's already a transparent colour
  • . But, I put in the check you suggested and now it runs absolutely fine, and quite speedily, too. :)

    Thankyou!

SMF spam blocked by CleanTalk