View Full Version : Taking screenshots of an SDL window.
Mar 7, 2009, 04:38 AM
I've written a project using SDL, and for the testing section of the evaluation I need many screenshots of it. I've been using the built-in mac screencapture (cmd-shift-4), but the default mode (click & drag to select area) means that I need to crop each image to the window borders. I'd far rather use the alternative mode (spacebar to select the whole window), but strangely when I try that I just get a blank window, attached below.
My question is whether there exists any easy way to get the SDL surfaces to display in the screenshot properly. I've found a couple of ways to output the current screen buffer using SDL, but most of them seem to depend on SDL libraries not easily available for the mac. Also, some of the actions that I need to demonstrate testing (such as resizing the window) wouldn't come across so easily this way.
(note: yes this is for a school project, but as this doesn't affect the function of the project my tutor confirms it's fine to request help)
Mar 7, 2009, 08:00 AM
Every time you take a screenshot it comes out blank? Even when taking the entire screen? I'm guessing SDL draws via OpenGL, but screenshots should work with OpenGL. I wonder if SDL is using a weird mode that isn't supported by Apple's screenshot tool. You could try the screencapture command-line tool but I'm guessing that is what Apple uses when taking screenshots via the keyboard.
Mar 7, 2009, 08:45 AM
When I take the whole screen, or a selection that includes the SDL window, it all shows up fine. Unfortunately, as I need roughly 60 or so screenshots that means I'd need to crop each of those to include just the window, so I was trying to save time by taking advantage of the feature in screencapture that allows you to capture just the window that the mouse is over, and it's the pictures that come from this that are turning out blank (or rather the edges of the window and the shadow are there, as above, it's just the SDL content that is missing)
Mar 7, 2009, 09:08 AM
I'm guessing the keyboard commands do the same thing, but have you tried getting a window with grab?
Mar 7, 2009, 09:22 AM
Just tested with all three: terminal commands, keyboard shortcuts and grab. Any mode which includes more than one window works fine; I guess that it takes the info from the screen's display buffer. However, in all three, trying to capture just the window I need comes up blank.
Mar 7, 2009, 10:46 AM
Well as a kind of workaround you can capture the entire screen for now and keep the window you want centered. Then afterwords use Automator to do the cropping for you. I've done this before and it worked nicely. That way you won't have to manually crop them yourself. Just make sure to keep the window being captured in the same spot.
Mar 7, 2009, 11:18 AM
You might be better off putting the capture directly into your app and activating it through a key combination or something like that. If you're using OpenGL then it's simply a matter of reading pixels directly, glReadPixels(), from your front buffer and then saving them out to a file. If you're not using OpenGL then you can read the screen directly. There is a sample project on the Apple dev site which shows you how to implement a screen capture utility. Sorry I can't remember the name of the project, but one of the files is called OpenGLScreenReader.m/.h. Ah, and yes, you use OpenGL to do the reading!
b e n