I didn't see any Allegro 5 screenshot-ing code in the wiki, so I wrote some:
EDIT: Code was updated. Original is in spoiler at bottom of post.
It's supposed to be copy-paste-able, so other people can just drop it into their games and live the joys of screenshots.
It's used like this:
al_wait_for_event(event_queue, &ev); ale_screenshot_process_event(ev, "screenshots/", "IonWave"); ... the rest of your game logic ...
or
if(User clicks the screenshot button, or does a funny dance) ale_screenshot("screenshots/", "IonWave");
Works great for me! Should I put it on the wiki? Comments? Additions? Complaints?
Note: I tried to have the key be ALLEGRO_KEY_PRINTSCREEN, but that event never fired on my system. Of course, you're free to use whatever key you like.
Old Versions
My main complaint/comment about that piece of code is that there doesn't seem to be a good reason for the function to take an ALLEGRO_EVENT. In fact, it's probably better if it doesn't, because as it is you can't "drop it in" as is if you want different ways to safe a screenshot. Cleaner, in my opinion, is to catch the relevant event in the main loop and then call the relevant function.
If the path is NULL, you could default to the ALLEGRO_USER_DOCUMENTS_PATH folder. If gamename is NULL, you could default to al_get_app_name().
You could use al_filename_exists().
Thank you for the suggestions and comments!
I updated the code in the original post. Compiles and runs in my game (Allegro 5.0.0, MinGW).
I couldn't figure out a simple way to take adventage of al_get_app_name, since it can return the EXE's full filename (test.exe), and that would mess up the later call to al_set_path_extension(path, ".png"); (It would overwrite the timecode things)
I'll work on that a little bit more and post another update later.
My main complaint/comment about that piece of code is that there doesn't seem to be a good reason for the function to take an ALLEGRO_EVENT.
Fixed that too. Now there's two functions al_screenshot which just takes a path and name, and immediately takes the screenshot; and al_screenshot_process_event which is the convenience function that calls al_screenshot when F11 is pressed.
EDIT: Oh, also, it returns success codes now, so you can check that and have your game display "Screenshot taken" or something.
nitpick: a lot of people expect F11 to go into fullscreen mode. Maybe you should use F12 for screenshot.
I couldn't figure out a simple way to take adventage of al_get_app_name, since it can return the EXE's full filename
It would be on the programmer to set the app name first.
But since al_get_app_name() returns a const char*, it wouldn't really be any extra effort for the programmer to pass that if he wanted.
Please don't use the al_ prefix.
Code updated again.
nitpick: a lot of people expect F11 to go into fullscreen mode. Maybe you should use F12 for screenshot.
Fixed.
It would be on the programmer to set the app name first.
Well, anyway, I fixed the code to handle '.' in the gamename.
Please don't use the al_ prefix.
Sorry I've now changed it to ale_ simply because it's related to Allegro's functions and couldn't come up with a more appropriate prefix (feel free to suggest).
EDIT: So, should this go on the Wiki, or is it complete garbage? I haven't tried to edit the wiki before ...
It isn't garbge, put it on the wiki right away!
I agree with amcerasoli. The more content the better!
Cool
Alright I wrote a page on the wiki Allegro 5 Screenshot
I hope I did it right. So ... how do I link it from one of the categories and which category would it go under?
EDIT: Welp, I figured out how to put it in a category. It's just under Allegro for now...
Do you not think it's confusing that the allegro wiki mixes A4 and A5 to the point where you are guessing which one you are reading (e.g. click on the 'examples' wiki entry).
Perhaps it's time to actively promote A5 and legacyise (a made-up word perhaps) A4; the best way being to make A4 wiki less prominent and make the wiki A5 focused? Just like in the allegro.cc home page where the entry point for 'Files' is allegro 5.
Better yet would be to make a separate category for Allegro 4 and Allegro 5. There's no reason to make Allegro 4 articles 'deprecated' or anything like that.
I kind of like the way World of Warcraft names their screenshots.
WoWScrnShot_010411_015003
You have whatever name you wish, followed by the MMDDYY_HHMMSS (although I prefer YYMMDD_HHMMSS myself, the files will then be listed in the right order in a directory). This gets rid of any limits on how many you can do in a day because they will never be over written and it makes it easy to know when you took the screenshot at a glance.
You could also simply do a "screenshot%d" or "screenshot%03d" and add on a number you increment for each shot.
The function prefix could also be "a5_screenshot" if you're looking for something that is easily recognizable other than "al_".
Edit: I would also prefer it to be automated so that nothing needs to be passed to the function, just have it get the executable filename, crop the ".exe" off of it, then save the screenshot into the same folder that the executable resides in, perhaps creating a "screenshots" folder if one doesn't exist and saving all screenshots in there.
I would also prefer it to be automated so that nothing needs to be passed to the function, just have it get the executable filename, crop the ".exe" off of it, then save the screenshot into the same folder that the executable resides in, perhaps creating a "screenshots" folder if one doesn't exist and saving all screenshots in there.
Horrible idea in general. That's not the sort of behaviour you want to hard-code inside the function, because it's not portable.
Guess my screenshot function will be horrible and windows only then.
It's perfectly okay to write another wrapper function that does those things and then calls the underlying ale_screenshot function (everything except the different time-format).
I personally like the idea of having the screenshots folder being created automatically, but past some point I didn't want to bloat out my code. I'm hoping for it to be part-useful tool/part-useful learning guide. Too much complexity and it becomes hard to understand.
So yeah, if you do write a wrapper, put it on the Wiki. It can be another sub-section of the article I put up, or a separate article linked from the original.
And thanks for commenting on it regardless!
Ah yes, for a learning tool, you definitely need to stick to just the basics. You can then perhaps gives tips for how the reader could expand it.
Guess my screenshot function will be horrible and windows only then.
In Windows you can't be sure that you can write to the executable directory either. Allegro has a function for querying standard path locations.
I'm curious, under which circumstances would an allegro game not be able to write to it's own directory?
I'm curious, under which circumstances would an allegro game not be able to write to it's own directory?
Installed in Program Files, and you're not admin.
Under GNU/Linux, the executable may be installed to '/usr/bin', with its data stored at '/usr/share/mygame', then user files and configuration go to '~/.mygame'. The user will not have permissions to write to the executable or data directory.
Well that explains why I don't install my games in program files and why I don't use Linux.
In my games, I make the default installation directory %userprofile%/GameName