pouët.net

code Q - SDL pset function not working

category: general [glöplog]
I have created a pset function that looks like this.

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?
added on the 2008-12-21 04:03:25 by b0ib0t b0ib0t
dunno much about sdl but perhaps you're assuming that the surface is 32-bit aligned while it can be 24?
added on the 2008-12-21 04:16:30 by Gargaj Gargaj
(i.e. you're assuming RGBA while it's RGB - as said this is just a shot in the dark)
added on the 2008-12-21 04:16:53 by Gargaj Gargaj
you're also assuming that pitch is a multiple of 4 :o
added on the 2008-12-21 04:17:52 by Gargaj Gargaj
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.
added on the 2008-12-21 04:41:28 by xyz xyz
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.
added on the 2008-12-21 04:49:47 by b0ib0t b0ib0t
Maybe this helps:
Quote:
Code:void putpixel(int x, int y, unsigned int color) { Uint32 *ptr = static_cast<Uint32 *>(sc2->screen->pixels); // eliminates a divide and a scratch variable *(ptr + (sc2->screen->pitch * y) + x*sizeof(Uint32) )=color; }
added on the 2008-12-21 04:57:14 by bdk bdk
^^ d#oh

why the heck would you need a putpixel function anywayz ?
at least inline it :D
added on the 2008-12-21 05:09:37 by xyz xyz
are you retarded?
added on the 2008-12-21 05:11:18 by shiva shiva
..50 more pounds and I technically am !1

BB Image
added on the 2008-12-21 05:23:03 by xyz xyz
Ah! I may be retarded, but it is all relative. haha!
added on the 2008-12-21 05:37:48 by b0ib0t b0ib0t
stfu,rtfm,wtfbbq and fix your putpixel code, mate =)
added on the 2008-12-21 05:44:26 by xyz xyz
show the rest of the code. mayebe there is something in x, y
added on the 2008-12-21 05:47:21 by texel texel
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
Code:Uint32 color = SDL_MapRGB(gbmp->format, R, G, B);
does not work as it should. Try something else. Maybe
Code:color = ((R << 8) + G) << 8 + B;
will work (but I don't use SDL, so it's just a guess).
added on the 2008-12-21 09:37:01 by Adok Adok
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

added on the 2008-12-21 10:19:36 by Jcl Jcl
SDL_MapRGB should take care of 255+ values for colour channels, although
Code: i & 0xFF

wouldn't hurt.

Finally, I think Gargaj is quite right, are you sure the surface is 32 bits per pixel?
added on the 2008-12-21 11:11:57 by decipher decipher
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().
added on the 2008-12-21 14:39:58 by El Topo El Topo
Just got out of bed, once I'm awake I'll start trying stuff. I'll let you know what I find.
added on the 2008-12-21 15:46:08 by b0ib0t b0ib0t
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.
added on the 2008-12-21 16:08:06 by texel texel
Jcl: RGB values of SDL_MapRGB() are UINTs as stated in the SDL manual, so, it should be not a problem.
added on the 2008-12-21 16:09:44 by texel texel
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?
added on the 2008-12-21 16:13:20 by texel texel
I know my bitmaps are all 32b. I'm not sure about how to set the pixel format though.

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.
added on the 2008-12-21 16:22:50 by b0ib0t b0ib0t
I'm rtfm right now to see about changing the pixel format.
added on the 2008-12-21 16:25:28 by b0ib0t b0ib0t
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; } }
added on the 2008-12-21 16:37:48 by b0ib0t b0ib0t
Heh, maybe I'm drawing outside of the bitmap...
added on the 2008-12-21 16:46:56 by b0ib0t b0ib0t

login