code Q - SDL pset function not working
category: general [glöplog]
I have created a pset function that looks like this.
When I'm using this I run into a few problems. When I try to do anything color (example: pset(gbmp, x,y,0,100,0);) it does not work. I am only able to get black. The other problem I have is when I try putting a lot of pixels that have different colors, the entire program just crashes on me. Example:
The strange thing is , if I had not messed with the G element of color and left it at 0 it would not crash. Any ideas?
Code:
void pset(SDL_Surface * gbmp, int x, int y, Uint32 R, Uint32 G, Uint32 B){
Uint32 color = SDL_MapRGB(gbmp->format, R, G, B);
Uint32 *bufp;
bufp = (Uint32 *)gbmp->pixels + y*gbmp->pitch/4 + x;
*bufp = color;
}
When I'm using this I run into a few problems. When I try to do anything color (example: pset(gbmp, x,y,0,100,0);) it does not work. I am only able to get black. The other problem I have is when I try putting a lot of pixels that have different colors, the entire program just crashes on me. Example:
Code:
for(int i = 0; i < 1024; i += 50)
pset(gbmp, x,y,0,i,0);
The strange thing is , if I had not messed with the G element of color and left it at 0 it would not crash. Any ideas?
dunno much about sdl but perhaps you're assuming that the surface is 32-bit aligned while it can be 24?
(i.e. you're assuming RGBA while it's RGB - as said this is just a shot in the dark)
you're also assuming that pitch is a multiple of 4 :o
no offense man but learn how to use a debugger :)
routine looks at first glance so check pointers/return values etc.
line-trace your prog. if necessary.
routine looks at first glance so check pointers/return values etc.
line-trace your prog. if necessary.
No offense taken. I'm pretty good at using a debugger in C#, but I haven't tried with Code::Blocks/gcc yet. I did try it with Devc++ and it was F*CKING TERRIBLE.
^^ d#oh
why the heck would you need a putpixel function anywayz ?
at least inline it :D
why the heck would you need a putpixel function anywayz ?
at least inline it :D
are you retarded?
..50 more pounds and I technically am !1
Ah! I may be retarded, but it is all relative. haha!
stfu,rtfm,wtfbbq and fix your putpixel code, mate =)
show the rest of the code. mayebe there is something in x, y
b0ib0t: You run i from 0 to 1023. G gets the value of i, so it gets the values from 0 to 1023. I don't know what it's like with SDL, but usually R, G, B values can be only from 0 to 255 (as they occupy 1 byte). Maybe that's the reason for your crash.
And if only R = G = B = 0 leads to an output at all, then probably the line
And if only R = G = B = 0 leads to an output at all, then probably the line
Code:
does not work as it should. Try something else. Maybe Uint32 color = SDL_MapRGB(gbmp->format, R, G, B);
Code:
will work (but I don't use SDL, so it's just a guess).color = ((R << 8) + G) << 8 + B;
Code:
for(int i = 0; i < 1024; i += 50)
pset(gbmp, x,y,0,i,0);
So you are passing a value from 0 to 1024 (in steps of 50) to a byte (0-255)? well done
SDL_MapRGB should take care of 255+ values for colour channels, although
wouldn't hurt.
Finally, I think Gargaj is quite right, are you sure the surface is 32 bits per pixel?
Code:
i & 0xFF
wouldn't hurt.
Finally, I think Gargaj is quite right, are you sure the surface is 32 bits per pixel?
All problems I've had with surfaces and direct pixel manipulation in SDL have been format related. Make sure the surface you draw to is set up properly, use default mask settings to SDL_CreateRGBSurface(), then blit to a "screen" surface that you then SDL_Flip().
Just got out of bed, once I'm awake I'll start trying stuff. I'll let you know what I find.
I've just took a look of my codeblocks/sdl config:
screen=SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
It is 32 bpp.
screen=SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
It is 32 bpp.
Jcl: RGB values of SDL_MapRGB() are UINTs as stated in the SDL manual, so, it should be not a problem.
Quote:
Synopsis
#include "SDL.h"
Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);
Description
Maps the RGB color value to the specified pixel format and returns the pixel value as a 32-bit int.
If the format has a palette (8-bit) the index of the closest matching color in the palette will be returned.
If the specified pixel format has an alpha component it will be returned as all 1 bits (fully opaque).
Erm... "If the format has a palette (8-bit) the index of the closest matching color in the palette will be returned.". B0ib0t, are you sure to be using a 32bpp mode?
I know my bitmaps are all 32b. I'm not sure about how to set the pixel format though.
Here is my bitmap stuff.
The only pixel format code I've written is what is in my pset function.
Here is my bitmap stuff.
Code:
#define defXres 1024
#define defYres 768
#define defBits 32
gbmp = SDL_SetVideoMode(defXres, defYres, defBits , SDL_SWSURFACE|SDL_DOUBLEBUF);
The only pixel format code I've written is what is in my pset function.
I'm rtfm right now to see about changing the pixel format.
I'm not sure if that is the problem. This should work if bpp were the problem.
Code:
void pset(SDL_Surface * img, int x, int y, Uint32 R, Uint32 G, Uint32 B){
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch(screen->format->BytesPerPixel)
{
case 1: //8bpp
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: //16bpp
{
Uint16 *bufp;
bufp = ( Uint16 *)screen->pixels + y*screen->pitch/2+x;
*bufp = color;
}
break;
case 3: //24bpp (slow)
{
Uint8 *bufp;
bufp = (Uint8 * )screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: //32bpp
{
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
}
Heh, maybe I'm drawing outside of the bitmap...