Hey guys, I'm trying to make a function that saves a screenshot to a folder. I have it creating the .png's correcting but all of them are blank.
I am able to draw to the screen and I have tried to flipping the screen before and after screenshotting but neither does anything.
Here is what my screen looks like and what the screenshots look like.
http://imgur.com/a/6LzYu
Here is the code that produces my screenshots. The actual al_save_bitmap call is near the bottom but I thought it good measure to include it all.
]]>
Don't save from the backbuffer. It's contents are usually lost when you flip the display. And you won't be referring to the same bitmap as the one being displayed anyway.
Try using a buffer bitmap or just redraw to a buffer when you want to save it.
]]>This is a function I came up with to do just that and it works beautifully. Saves the screenshot with a date and time extension so if you save multiple screenshots they will all be named differently, just need to pass it the name of the game "My_Game" and that will be used with the date and time appended...
a5_screenshot.c
a5_screenshot.h
#ifndef _a5_screenshot_h_ #define _a5_screenshot_h_ #include "allegro5/allegro.h" #include <stdio.h> bool a5_screenshot(const char *gamename); #endif //_a5_screenshot_h_
]]>
With that code, just make sure to call it before an al_flip_display if you're trying to save from the backbuffer. If not, then it doesn't really matter.
]]>Even when I say al_get_target_buffer it comes up blank. With both target and back buffer and whether or not the screen flipping comes before, after, or not all makes no difference.
]]>Dumb question : Have you init'ed the image addon? al_init_image_addon
]]>Yes I have. The solution NickHackr presented is deprecated but even when updated it does not work. It creates blank png's all the same.
I have tried setting it to al_get_target_bitmap() and moving and deleting the flipping to no avail.
I suspect it has to do with drawing improperly or something. I only ever draw primitives. For instance:
I'd like to note that my "engine" class has only static members. Static screen and static functions. However the thing that draws to it- the game class -is not static.
]]>
I can load and save a png just fine here. Even off the backbuffer.
Try this test code with the attached test.png and see what results it gives you on your computer.
It works for me on Windows Vista with MinGW 4.8.1 using either DirectX or OpenGL driver.
]]>Edgar my man it works. I fiddled with it so much I don't quite know what the exact solution is.
Thanks a gorillian.
I've attached a working screenshot x)
]]>With that code, just make sure to call it before an al_flip_display if you're trying to save from the backbuffer. If not, then it doesn't really matter.
I used it all through my game. I added calls to it from my title screen, my menu screen, my main game screen etc... never had a problem.
But I will keep what you said in mind.
]]>I used it all through my game. I added calls to it from my title screen, my menu screen, my main game screen etc... never had a problem.
The main issue is that there is no guarantee that the backbuffer after a flip has anything in it that makes sense. It could be what you drew to it the frame before last, 5 frames ago, or garbage. (this is why they say it's undefined or implementation defined).
Often I think the "implementation" is just to keep N frames around for page flipping, and hand you back a previous frame on a flip. so for the most part it looks fine, but may not give you back the exact frame you expected if you called it after a flip, but before drawing. (not that it often matters, but it might if you're trying to use the screenshots for debugging)
]]>I may go over my code and make sure my screenshot code is after a draw, just to be safe. Perhaps detect the screenshot key, flag the next draw for a save, then add code in after the draw, but before the flip to respond on that. Hmmm...
]]>