Jump to content
  • Advertisement
Sign in to follow this  

SDL surface-pixels array problems

This topic is 4799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ey guys, I'm working on a low-res game using all 8bpp surfaces. No matter what, when I go to check a surface's color at a particular pixel, it always returns true. Here's a sample of what I'm doing.
Uint8 *raw;
raw = ((Uint8 *)(surface->pixels)) + (y * surface->pitch);
if (raw[x]==((Uint8)0x010101)) { //always returns true
//blah
}
The color 0x010101 is not even in the bmp I'm using. So I'm guessing this is some weird C++ variable thing. By the way I'm doing this to accomplished pixel perfect collision detection.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
First of all, you realise that 0x010101 is 3 bytes right? And you're casting it as a single byte data type... I'm not even sure how thats handled, but it probably equates to (raw[x] == 0xFF). Couldn't tell you why that returns true though.

Share this post


Link to post
Share on other sites
Ah okay. That slipped by me. I'm now using SDL_MapRGB to map these colors to 8bpp. It's still always returning true. I'm also having problems trying to debug the pixel data. I'm trying to output it to an ofstream and it's causing a Segmentation Fault. I have all surfaces locked whenever I'm playing around with the pixel array. Any other suggestions?

Here's the function that is causing the problems. It's a function that draw a line pixel-by-pixel from (x1,y1) to (x2,y2). If the check_collisions parameter is true is checks to see if there's a non-colorkey value on the "wall" surface before it draws each pixel. The debug line causes a segmentation fault. I know this code is extremely sloppy, but I want to be as quick as possible when working with pixels.. so that's why this function is sort of multi-tasking as a line-drawer and a collision-detector.


void drawline(SDL_Surface *s, int x1, int y1, int x2, int y2, int color, bool check_collisions, SDL_Surface *wall, int & cx, int & cy) {

SDL_LockSurface(s);
if (check_collisions) SDL_LockSurface(wall);

x1=max(min(x1,WIDTH), 0);
x2=max(min(x2,WIDTH), 0);
y1=max(min(y1,HEIGHT), 0);
y2=max(min(y2,HEIGHT), 0);
int d,x,y,ax,ay,sx,sy,dx,dy;

Uint8 *lineAddr;
Uint8 *wallAddr;
Sint32 yOffset;

dx = x2 - x1;
ax = abs(dx) << 1;
sx = sign(dx);

dy = y2 - y1;
ay = abs(dy) << 1;
sy = sign(dy);
yOffset = sy * s->pitch;

x = x1;
y = y1;

lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);

if (check_collisions) wallAddr = ((Uint8 *)(wall->pixels)) + (y * wall->pitch);

if (ax>ay) {
d = ay - (ax >> 1);
while (true) {
if (DEBUGGING) {
debug << (Uint8)wallAddr[x] << endl; //causes segmentation fault
}
if (check_collisions && wallAddr[x]!=SDL_MapRGB(wall->format, 255, 0, 255)) {
cx=x;
cy=y;
goto end_drawline;
}
else {
*(lineAddr + x) = (Uint8)color;
}
if (x == x2) goto end_drawline;
if (d>=0) {
y += sy;
lineAddr += yOffset;
d -= ax;
}
x += sx;
d += ay;
}
}
else {
d = ax - (ay >> 1);
while (true) {
if (DEBUGGING) {
debug << (Uint8)wallAddr[x] << endl; //causes segmentation fault
}
if (check_collisions && wallAddr[x]!=SDL_MapRGB(wall->format, 255, 0, 255)) {
cx=x;
cy=y;
goto end_drawline;
}
else {
*(lineAddr + x) = (Uint8)color;
}
if (y == y2) goto end_drawline;
if (d>=0) {
x += sx;
d -= ay;
}
y += sy;
lineAddr += yOffset;
d += ax;
}
}

end_drawline:
SDL_UnlockSurface(s);
if (check_collisions) SDL_UnlockSurface(wall);

}

Share this post


Link to post
Share on other sites
Couldn't resist.. goto .. you should never use that!

The code doesn't initialize the wallAddr variable when collision is set to false. In case you are debugging and not in collision mode, that debug information will indeed crash, as the pointer is referencing to some random place in memory.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hahah yeah I know. I make fun of people who use goto's and here I am.. using a goto. ANyway, I do have some progress on this method and I can access pixel data nicely. However, there is definitely some sort of memory leak in my program. I use the same code in another area and it randomly crashes the program. Furthermore, executing the program from the IDE ends up in an instant crash, and I have to run the executable in windows to test things out. I'm a mess.

Share this post


Link to post
Share on other sites
Woops, that last post was me. I have an entire 1x1-pixel-particle engine coded and I'm desperately trying to iron out these crashes. AAAh. (Did not sleep last night).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!