pouët.net

code Q - SDL pset function not working

category: general [glöplog]
Fixed!!! It WAS because I was drawing outside of my bitmap! Here is the fixed pset function.

Code: void pset(SDL_Surface * img, int x, int y, Uint8 R, Uint8 G, Uint8 B){ Uint32 color = SDL_MapRGB(img->format, R, G, B); if(x <= defXres && y <= defYres && x >= 0 && y >= 0){ switch(img->format->BytesPerPixel) { case 1: //8bpp { Uint8 *bufp; bufp = (Uint8 *)img->pixels + y*img->pitch + x; *bufp = color; } break; case 2: //16bpp { Uint16 *bufp; bufp = ( Uint16 *)img->pixels + y*img->pitch/2+x; *bufp = color; } break; case 3: //24bpp (slow) { Uint8 *bufp; bufp = (Uint8 * )img->pixels + y*img->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 *)img->pixels + y*img->pitch/4 + x; *bufp = color; } break; } } }
added on the 2008-12-21 16:51:14 by b0ib0t b0ib0t
...you have a switch/case inside your setpixel function...? O_O
added on the 2008-12-21 16:57:00 by Gargaj Gargaj
Not anymore, it was just there to see if the problem was a bpp problem. ;)
added on the 2008-12-21 16:58:11 by b0ib0t b0ib0t
So now you're getting colors as wanted?
added on the 2008-12-21 17:37:28 by Adok Adok
Code: case 3: //24bpp (slow)

lol. that's definitely the bottleneck in your code :)
added on the 2008-12-21 18:11:57 by dodke dodke
Considering the block of cases, yes 24bpp would be a major bottleneck! =D

j/k ofc

Colors aren't working right, but at least its not crashing anymore. Looking into the color problems in a bit.
added on the 2008-12-21 18:25:16 by b0ib0t b0ib0t
Fixed the color problem. Just before I begin manipulating individual pixels, I do this...

Code: gbmp = SDL_CreateRGBSurface(0, 1024, 768, 32, 0, 0, 0, 0);


gbmp is the surface I am drawing to. Now it works perfect!
added on the 2008-12-21 18:34:43 by b0ib0t b0ib0t
To elaborate on Gargaj's point a bit: You've got a SETPIXEL FUNCTION?
added on the 2008-12-21 19:22:09 by kb_ kb_
YO DAWG I HEARD U LIKE SETPIXEL FUNCTION SO I PUT A SETPIXEL FUNCTION CALL INSIDE UR SETPIXEL FUNCTION CALL SO U CAN OVERFLOW UR STACK WHILE U PLOT!
added on the 2008-12-21 19:53:40 by kusma kusma
What is wrong with a set pixel function? What could I be doing better?
added on the 2008-12-21 19:57:42 by b0ib0t b0ib0t
Calling functions in an inner loop that renders lot's of pixel every frame is never a good idea :)
*lots
*pixels

I blame the beer.
b0b0t:

In essence you do the following:

Code: Uint32 *ptr = (Uint32*) img->pixels; const int pitch=img->pitch; [inner loop begins...] ptr[y*pitch+x]=color; [inner loop ends]


This warrants the overhead of a function call, accessing the img object properties and worst of all calling ANOTHER function (SDL_MapRGB) HOW exactly?

(I'll save getting rid of the multiplication for Part Two of this very informative tutorial)
added on the 2008-12-21 21:14:27 by kb_ kb_
(Part Three will be about why in 99.99% of cases you'll never have to set single pixels anyway, low-res dot effects notwithstanding)
added on the 2008-12-21 21:15:33 by kb_ kb_
he's using SDL, he's fucked already anyway :)
added on the 2008-12-21 21:17:41 by Gargaj Gargaj
part 4 shall be about typing 'pixeltoaster' in the google searchbar!
kb_: full ack.

Gargaj: ah, c'mon! SDL ain't so bad. Works quite well for Linux/Windows platform stuff in my experience. Nice API, many supported platforms and not much bloat because they keep the optional stuff in separate libraries.
(well..most of it..I personally don't use the 2D surface stuff very much..SDL also plays well with OpenGL)


added on the 2008-12-21 21:44:30 by xyz xyz
If SDL sucks, please, what is your recommendation?
added on the 2008-12-21 21:51:47 by b0ib0t b0ib0t
you failed part 4 of the tutorial, dear sir!
So instead of calling SDL_MapRGB() 300.000+ in one loop one could just

Code: color = r << 16; color = color | (g << 8); color = color | b;


assuming one just want a hex triplet and not some nearest matching indexed color lookup??
added on the 2008-12-21 23:46:51 by El Topo El Topo
El Topo: SDL_MapRGB is yet another "supposedly simple but yet contains a switch to pick a fucking value" kind of function from the lovely SDL. Seriously, use PixelToaster.
added on the 2008-12-22 18:47:17 by decipher decipher
But SDL is ported to everyone and his aunt and it seems pretty much standard on Linux which PixelToaster isn't atm. It sounds nice though.
added on the 2008-12-22 19:26:37 by El Topo El Topo
I've looked at PixelToaster, it's cool, but... why not use OpenGL or straight DirectX? I know that in OpenGL, it's also possible to set individual pixels, and as the Windows version of PixelToaster is based on DirectX, it is obviously possible there, too. OpenGL or DirectX will make your life very easy if you want to do 3D graphics.
added on the 2008-12-22 19:35:48 by Adok Adok

login