In this example we will show you how to save window content to the graphic file and how to implement this functionality in your Cinder application. This could be useful to save output of a graphics algorithm.
We will add a window content saving function to your application:
#include "cinder/ImageIo.h" #include "cinder/Utilities.h"
bool mMakeScreenshot;
setup
method:mMakeScreenshot = false;
keyDown
method as follows:void MainApp::keyDown(KeyEvent event) { if(event.getChar() == 's') { mMakeScreenshot = true; } }
draw
method:if(mMakeScreenshot) { mMakeScreenshot = false; writeImage( getDocumentsDirectory() / fs::path("MainApp_screenshot.png"), copyWindowSurface() ); }
Every time you set mMakeScreenshot
to true
the screenshot of your application will be selected and saved. In this case the application waits for the S key to be pressed and then sets the flag mMakeScreenshot
to true
. The current application window screenshot will be saved inside your documents directory under the name MainApp_screenshot.png
.
This is just the basic example of common usage of the writeImage
function. There are many other practical applications.
Let's say you want to record a equence of images Perform the following steps to do so:
if(mMakeScreenshot || mRecordFrames) { mMakeScreenshot = false; writeImage( getDocumentsDirectory() / fs::path("MainApp_screenshot_" + toString(mFramesCounter) + ".png"), copyWindowSurface() ); mFramesCounter++; }
mRecordFrames
and mFrameCounter
as properties of your main application class:bool mRecordFrames; int mFramesCounter;
setup
method:mRecordFrames = false; mFramesCounter = 1;
We assume that you are using TrackRef
from the audio
namespace to play your sound Perform the following steps:
update
method:if(mRecordFrames) { mTrack->setTime(mFramesCounter / 30.f); }
We are calculating the desired audio track position based on the number of frames that passed. We are doing that to synchronize animation with the music track. In this case we want to produce 30
fps animation so we are dividing mFramesCounter
by 30
.
18.225.255.86