Sign in to follow this  

SDL drawing on 8 bit surface skips 4 pixels.

This topic is 2006 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

Hello

This might have already been asked, but I couldn't find anything anywhere. What happens is, I'm creating a simple top down shooter, I started with the basics of pixel manipulation from Sol's tutorial and made a single pixel move on a 640x480x32 surface, the screen. Now the problem is I thought why would I need a 32-bit surface for an 8-bit game, so I brought it down to 16, just to see whats the difference, and saw something strange which was pixel skipping, the lines I drew by moving the pixel skipped a pixel. But since I had this function:
[source lang="cpp"]void putpixel(int x, int y, int color)
{
unsigned int *ptr = (unsigned int*)screen->pixels;
int lineoffset = y * (screen->pitch / 4);
ptr[lineoffset + x] = color;
}[/source]
I thought it could have been a pitch problem because it's different for 8, 16 and 32, then I went to 8-bit, and that's when it began skipping even more, 4 pixels. So, according to the pitch definition being the width in pixels, 640 pixels*4bytes/pixel is 2560 and 640pixels*1byte/pixel for 8-bit is 640 so dividing it by 4 won't give me the width. I did a test by drawing to the whole screen, making every pixel white, but I got an error at about (500,100), out of bounds, which I also got while moving the pixel. I multiplied that and it was way below the 307 200 array of pixels, and then I multiplied it by 4 and got a little over 307 200, there was the error. I don't understand where that 4 comes from, I read something on these forums about DirectX pitching which was the distance in bytes from one pixel row to the other, and someone said that if it's 8-bit surface it usually gets promoted to 4 byte distance. That's not what I read on Sol's tutorial, saying that it was the additional width added by the video card for convenience that was not displayed. My question is, why does that happen, and how can I make it draw continous lines on 8 and 16-bit surfaces? Also, what in the world is the pitch? I saw various definitions and none of them explain it very well, the pitch (according to Sol) should be the added width in pixels (he gave example with the surface being 640x480 and the virtual being 1024x480). Or is it the width in bytes? That seems more logical to me, since with an 8-bit surface and 511x480, it shows 512 (the width in bytes), that also means even numbers (every 4th) which translates into adding one more byte(what is it used for, is it just a pixel that is never used).
Here goes the relevant code, the rest is event handling and surface locking, nothing that affects the pixel part.
[source lang="java"]SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 8, SDL_HWSURFACE);

// Draw to screen
for (i=0; i < 480; i++)
{

for (j=0; j < 640; j++)
{
((unsigned int*)screen->pixels)[i*j] = 255;
}[/source]
And Sol's tutorial: [url="http://sol.gfxile.net/gp/ch02.html"]http://sol.gfxile.net/gp/ch02.html[/url]
Thank you very much.

Share this post


Link to post
Share on other sites
You're treating your image (pixels) as an array of integers, for 16 bit graphics you need to treat it as a array of shorts and for 8 bit graphics you need to treat it as an array of bytes. Just cast screen->pixels to the right type. Edited by Ravyne

Share this post


Link to post
Share on other sites

This topic is 2006 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this