Jones
2006.05.18, 04:06 PM
I was googling to find a method to get pixel colors so I could test for colorkeys in my collision system.
The command I found was getpixel(). I quickly slapped it into version3 of my code:
#include <SDL/sdl.h>
/* A structure type for holding 2D pixel addresses. */
typedef struct {
int pixelX;
int pixelY;
} col_pixelCoord;
int r, g, b;
/* Collision function; makes and tests the pixel charts. */
int testPixelCollision(SDL_Surface *col_Surface_1, SDL_Surface *col_Surface_2, int col_red, int col_green, int col_blue) {
// Is there a collision? (1 = yes, 0 = no).
int col_isThereCollision = 0;
// Arrays for holding each surface's pixel data.
col_pixelCoord col_pixelData_surface1[col_Surface_1->w * col_Surface_1->h];
col_pixelCoord col_pixelData_surface2[col_Surface_2->w * col_Surface_2->h];
// SDL_rect objects for each surface.
SDL_Rect col_rect_surf1;
SDL_GetClipRect(col_Surface_1, &col_rect_surf1);
SDL_Rect col_rect_surf2;
SDL_GetClipRect(col_Surface_2, &col_rect_surf2);
/* Calculate surface 1's map. */
// Starting values for the collumn and row holders.
int col_i = 0;
int col_u = 0;
// Pixel count, starts at one so as not to fill slot 0 of the array.
int col_pixelsCounted_surf1 = 0;
// Take a collumn.
for (; col_i < col_Surface_1->w;) {
// Take the item from the row of that collumn.
for (; col_u < col_Surface_1->h;) {
Uint32 pixel = getpixel(col_Surface_1, col_i, col_u);
SDL_GetRGB(pixel, col_Surface_1->format, &r, &b, &g);
if (r != col_red && g != col_green && b != col_blue) {
/* Important bit: Storing the pixel coordinates. */
col_pixelsCounted_surf1++;
col_pixelData_surface1[col_pixelsCounted_surf1].pixelX = col_i + col_rect_surf1.x;
col_pixelData_surface1[col_pixelsCounted_surf1].pixelY = col_u + col_rect_surf1.y;
// If we're at the last item of that row of that collumn...
if (col_u == col_Surface_1->h) {
col_i++; // Next collumn.
col_u = 0; // First item of that row of the next collumn.
}
else {
col_u++;
}
}
}
}
/* Calculate surface 2's map. */
// Starting values for the collumn and row holders.
int col_o = 0;
int col_p = 0;
// Pixel count, starts at one so as not to fill slot 0 of the array.
int col_pixelsCounted_surf2 = 0;
// Take a collumn.
for (; col_o < col_Surface_2->w;) {
Uint32 pixel = getpixel(col_Surface_2, col_i, col_u);
SDL_GetRGB(pixel, col_Surface_2->format, &r, &b, &g);
// Take the item from the row of that collumn.
for (; col_p < col_Surface_2->h;) {
if (r != col_red && g != col_green && b != col_blue) {
/* Important bit: Storing the pixel coordinates. */
col_pixelsCounted_surf2++;
col_pixelData_surface2[col_pixelsCounted_surf2].pixelX = col_o + col_rect_surf2.x;
col_pixelData_surface2[col_pixelsCounted_surf2].pixelY = col_p + col_rect_surf2.y;
// If we're at the last item of that row of that collumn...
if (col_o == col_Surface_2->h) {
col_o++; // Next collumn.
col_p = 1; // First item of that row of the next collumn.
}
else {
col_p++;
}
}
}
}
/* Ok, the big important bit; Cross testing the pixel charts. */
// Reuse some counters.
col_i = 1;
col_o = 1;
while (col_i <= col_pixelsCounted_surf1) {
while (col_o <= col_pixelsCounted_surf2) {
if (col_pixelData_surface1[col_i].pixelX == col_pixelData_surface2[col_i].pixelX && col_pixelData_surface1[col_o].pixelY == col_pixelData_surface2[col_o].pixelY) {
col_isThereCollision = 1;
goto col_finishCollisionTest;
}
col_o++;
}
col_i++;
}
col_finishCollisionTest:
return col_isThereCollision;
}
But the compiler returns:
Undefined symbol _getpixel!
Does that mean that getpixel is a user made function that I do not have installed? Or perhaps it is just some silly mistake in my code.
Thanks for the help!
The command I found was getpixel(). I quickly slapped it into version3 of my code:
#include <SDL/sdl.h>
/* A structure type for holding 2D pixel addresses. */
typedef struct {
int pixelX;
int pixelY;
} col_pixelCoord;
int r, g, b;
/* Collision function; makes and tests the pixel charts. */
int testPixelCollision(SDL_Surface *col_Surface_1, SDL_Surface *col_Surface_2, int col_red, int col_green, int col_blue) {
// Is there a collision? (1 = yes, 0 = no).
int col_isThereCollision = 0;
// Arrays for holding each surface's pixel data.
col_pixelCoord col_pixelData_surface1[col_Surface_1->w * col_Surface_1->h];
col_pixelCoord col_pixelData_surface2[col_Surface_2->w * col_Surface_2->h];
// SDL_rect objects for each surface.
SDL_Rect col_rect_surf1;
SDL_GetClipRect(col_Surface_1, &col_rect_surf1);
SDL_Rect col_rect_surf2;
SDL_GetClipRect(col_Surface_2, &col_rect_surf2);
/* Calculate surface 1's map. */
// Starting values for the collumn and row holders.
int col_i = 0;
int col_u = 0;
// Pixel count, starts at one so as not to fill slot 0 of the array.
int col_pixelsCounted_surf1 = 0;
// Take a collumn.
for (; col_i < col_Surface_1->w;) {
// Take the item from the row of that collumn.
for (; col_u < col_Surface_1->h;) {
Uint32 pixel = getpixel(col_Surface_1, col_i, col_u);
SDL_GetRGB(pixel, col_Surface_1->format, &r, &b, &g);
if (r != col_red && g != col_green && b != col_blue) {
/* Important bit: Storing the pixel coordinates. */
col_pixelsCounted_surf1++;
col_pixelData_surface1[col_pixelsCounted_surf1].pixelX = col_i + col_rect_surf1.x;
col_pixelData_surface1[col_pixelsCounted_surf1].pixelY = col_u + col_rect_surf1.y;
// If we're at the last item of that row of that collumn...
if (col_u == col_Surface_1->h) {
col_i++; // Next collumn.
col_u = 0; // First item of that row of the next collumn.
}
else {
col_u++;
}
}
}
}
/* Calculate surface 2's map. */
// Starting values for the collumn and row holders.
int col_o = 0;
int col_p = 0;
// Pixel count, starts at one so as not to fill slot 0 of the array.
int col_pixelsCounted_surf2 = 0;
// Take a collumn.
for (; col_o < col_Surface_2->w;) {
Uint32 pixel = getpixel(col_Surface_2, col_i, col_u);
SDL_GetRGB(pixel, col_Surface_2->format, &r, &b, &g);
// Take the item from the row of that collumn.
for (; col_p < col_Surface_2->h;) {
if (r != col_red && g != col_green && b != col_blue) {
/* Important bit: Storing the pixel coordinates. */
col_pixelsCounted_surf2++;
col_pixelData_surface2[col_pixelsCounted_surf2].pixelX = col_o + col_rect_surf2.x;
col_pixelData_surface2[col_pixelsCounted_surf2].pixelY = col_p + col_rect_surf2.y;
// If we're at the last item of that row of that collumn...
if (col_o == col_Surface_2->h) {
col_o++; // Next collumn.
col_p = 1; // First item of that row of the next collumn.
}
else {
col_p++;
}
}
}
}
/* Ok, the big important bit; Cross testing the pixel charts. */
// Reuse some counters.
col_i = 1;
col_o = 1;
while (col_i <= col_pixelsCounted_surf1) {
while (col_o <= col_pixelsCounted_surf2) {
if (col_pixelData_surface1[col_i].pixelX == col_pixelData_surface2[col_i].pixelX && col_pixelData_surface1[col_o].pixelY == col_pixelData_surface2[col_o].pixelY) {
col_isThereCollision = 1;
goto col_finishCollisionTest;
}
col_o++;
}
col_i++;
}
col_finishCollisionTest:
return col_isThereCollision;
}
But the compiler returns:
Undefined symbol _getpixel!
Does that mean that getpixel is a user made function that I do not have installed? Or perhaps it is just some silly mistake in my code.
Thanks for the help!