Skip to main content
GameDev.net gamedev.net
🔒 Locked

Drawing Pixels in SDL(SOLVED)

Started by Ectara May 24, 2009 at 11:30 AM 3 replies 850+ views
Original Post
Ectara
Ectara
I am heavily underway in remaking my engine, and I am adding the option of drawing 2D in SDL instead of OpenGL; some might get a performance boost, especially if software rendering on a software surface. But everytime the function is called, it segfaults when dereferencing the pointer. //drawing to SDL void SDL2D::draw(SDL_Surface *screen, si32 x, si32 y, si8 r, si8 g, si8 b) { ui32 color = SDL_MapRGB(screen->format, r, g, b); switch (screen->format->BitsPerPixel) { case 8: { ui8 * p; p = (ui8 *)screen->pixels + y*screen->pitch + x; *p = color; } break; case 15: case 16: { ui16 *p; p = (ui16 *)screen->pixels + y*screen->pitch + x*2; *p = color; } break; case 24: { ui8 * p; p = (ui8 *)screen->pixels + y*screen->pitch + x * 3; if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { p[0] = color; p[1] = color >> 8; p[2] = color >> 16; } else { p[2] = color; p[1] = color >> 8; p[0] = color >> 16; } } break; case 32: { ui32 * p; p = (ui32*) screen->pixels + y*screen->pitch + x*4; *p = color; } break; } } The typedefs are analogous to more common ones (i.e. ui8 = unsigned char = Uint8 = GLubyte) I am using a 32bpp surface/display. I have checked, and p comes out null, unless I change the line to p = (ui32*) &screen->pixels + y*screen->pitch + x*4; In which case it will wait a bit, do nothing, then segfault. I don't know what is wrong, and I have tried several other versions of the same function. Can anyone lend a hand? [Edited by - Ectara on May 24, 2009 12:33:46 PM]
Ectara
Ectara
I discovered that it works as long as SDL_OPENGL is disabled. Using SDL_OPENGLBLIT does not segfault, but it does not draw SDL either. Any way to get around this?
Ectara
Ectara
Solved.

Using SDL_OPENGL|SDL_OPENGLBLIT,

the calling SDL_UpdateRect(screen,x,y,1,1);, after drawing the pixel at x,y on screen.

I think that is the shortest lived thread I've made.


LEARN!
rip-off
rip-off
SDL_OPENGLBLIT is deprecated. Drawing individual pixels is generally not a good idea on modern graphics cards. Can you give us a higher level overview of what you want to achieve?
Ectara
Ectara
In one of the engines I wrote, I have various functions for drawing 2D primitives, and in the configuration class, there is a flag for drawing 2D with SDL or drawing 2D with OpenGL. My basic goal, which I achieved with a deprecated method, was simply to blit over an OpenGL context.

[Edited by - Ectara on May 24, 2009 10:35:07 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.