I’m having a problem with OpenGL and SDL. I get a segmentation
fault when I try to access the pixels directly. I’ve used the SDL
example as a reference, and the code looks like this:
int main(int argc, char *argv[]) {
SDL_Surface *screen;
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
screen = SDL_SetVideoMode(800, 600, 32, NULL, SDL_OPENGL);
You can’t do this with OpenGL, since it doesn’t support direct frame
buffer access. (This is one major PITA for the glSDL backend…)
You have to use OpenGL calls to read and write pixels. You should
avoid those, though, since they’re generally slow, and may interfere
with accelerated rendering. (Must hard-sync the CPU with the
accelerator, so you can’t have the CPU and GPU working in parallel
==> potential major frame rate reduction.)
Uint32 *pixels = (Uint32 *)
screen->pixels;
You must read screen->pixels after locking the surface! The pointer
is can be NULL or invalid on unlocked h/w surfaces.
SDL_LockSurface(screen);
pixels[0] = SDL_MapRGB(screen->format, 0xff, 0xff,
0xff);
SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 0, 0, 1, 1);
sleep(2);
SDL_Quit();
}
This should place a white dot in the top left corner of the
screen, but I get a seg-fault as I mentioned.
Yep, because the SDL_OPENGL backend doesn’t support access of the
display surface, or even SDL rendering calls for that matter. Either
you use OpenGL or you don’t - as simple as that. (Well, almost.
If I remove
SDL_OPENGL from the videomode flags it works fine. So my question
is, how do I modify the Surface pixels through SDL (or any other
method for that matter) directly when using OpenGL?
Have a look at the glSDL/wrapper code:
http://olofson.net/mixed.html
It implements SDL_BlitSurface(), SDL_FillRect() etc on top of OpenGL.
IIRC, screen locking (pixel access) is in 0.6 as well, but I’m not
sure…
Anyway, what do you want to do, actually…?
//David Olofson - Programmer, Composer, Open Source Advocate
.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
— http://olofson.net — http://www.reologica.se —On Monday 17 May 2004 14.49, Simon Ejsing wrote: