Skip to content

Commit

Permalink
Migrate screenshot commands to the C++ command API
Browse files Browse the repository at this point in the history
  • Loading branch information
slipher committed Jan 1, 2019
1 parent ec700e1 commit c912be5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/engine/renderer/tr_image_jpg.cpp
Expand Up @@ -453,7 +453,7 @@ int SaveJPGToBuffer( byte *buffer, size_t bufSize, int quality, int image_width,
return outcount;
}

void SaveJPG( char *filename, int quality, int image_width, int image_height, byte *image_buffer )
void SaveJPG( const char *filename, int quality, int image_width, int image_height, byte *image_buffer )
{
byte *out;
size_t bufSize;
Expand Down
149 changes: 65 additions & 84 deletions src/engine/renderer/tr_init.cpp
Expand Up @@ -514,21 +514,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
}
}

/*
==============================================================================
SCREEN SHOTS
screenshots get written in fs_homepath + fs_gamedir
.. base/screenshots\*.*
three commands: "screenshot", "screenshotJPEG" and "screenshotPNG"
the format is etxreal-YYYY_MM_DD-HH_MM_SS-MS.tga/jpeg/png
==============================================================================
*/

/*
==================
RB_ReadPixels
Expand Down Expand Up @@ -568,10 +553,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

/*
==================
R_TakeScreenshot
RB_TakeScreenshot
==================
*/
static void RB_TakeScreenshot( int x, int y, int width, int height, char *fileName )
static void RB_TakeScreenshot( int x, int y, int width, int height, const char *fileName )
{
byte *buffer;
int dataSize;
Expand Down Expand Up @@ -610,7 +595,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
RB_TakeScreenshotJPEG
==================
*/
static void RB_TakeScreenshotJPEG( int x, int y, int width, int height, char *fileName )
static void RB_TakeScreenshotJPEG( int x, int y, int width, int height, const char *fileName )
{
byte *buffer = RB_ReadPixels( x, y, width, height, 0 );

Expand All @@ -623,7 +608,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
RB_TakeScreenshotPNG
==================
*/
static void RB_TakeScreenshotPNG( int x, int y, int width, int height, char *fileName )
static void RB_TakeScreenshotPNG( int x, int y, int width, int height, const char *fileName )
{
byte *buffer = RB_ReadPixels( x, y, width, height, 0 );

Expand Down Expand Up @@ -661,82 +646,84 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
R_TakeScreenshot
==================
*/
void R_TakeScreenshot( const char *name, ssFormat_t format )
static bool R_TakeScreenshot( Str::StringRef path, ssFormat_t format )
{
static char fileName[ MAX_OSPATH ]; // bad things may happen if two screenshots per frame are taken.
ScreenshotCommand *cmd;
int lastNumber;

cmd = R_GetRenderCommand<ScreenshotCommand>();
ScreenshotCommand *cmd = R_GetRenderCommand<ScreenshotCommand>();

if ( !cmd )
{
return;
return false;
}

if ( ri.Cmd_Argc() == 2 )
{
Com_sprintf( fileName, sizeof( fileName ), "screenshots/" PRODUCT_NAME_LOWER "-%s.%s", ri.Cmd_Argv( 1 ), name );
}
else
{
qtime_t t;
cmd->x = 0;
cmd->y = 0;
cmd->width = glConfig.vidWidth;
cmd->height = glConfig.vidHeight;
Q_strncpyz(cmd->fileName, path.c_str(), sizeof(cmd->fileName));
cmd->format = format;

ri.RealTime( &t );
return true;
}

// scan for a free filename
for ( lastNumber = 0; lastNumber <= 999; lastNumber++ )
namespace {
class ScreenshotCmd : public Cmd::StaticCmd {
const ssFormat_t format;
const std::string fileExtension;
public:
ScreenshotCmd(std::string cmdName, ssFormat_t format, std::string ext)
: StaticCmd(cmdName, Cmd::RENDERER, Str::Format("take a screenshot in %s format", ext)),
format(format), fileExtension(ext) {}

void Run(const Cmd::Args& args) const override {
if (!tr.registered) {
Print("ScreenshotCmd: renderer not initialized");
return;
}
if (args.Argc() > 2) {
PrintUsage(args, "[name]");
return;
}

std::string fileName;
if ( args.Argc() == 2 )
{
fileName = Str::Format( "screenshots/" PRODUCT_NAME_LOWER "-%s.%s", args.Argv(1), fileExtension );
}
else
{
Com_sprintf( fileName, sizeof( fileName ), "screenshots/" PRODUCT_NAME_LOWER "_%04d-%02d-%02d_%02d%02d%02d_%03d.%s",
1900 + t.tm_year, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, lastNumber, name );
qtime_t t;
ri.RealTime( &t );

if ( !ri.FS_FileExists( fileName ) )
// scan for a free filename
int lastNumber;
for ( lastNumber = 0; lastNumber <= 999; lastNumber++ )
{
break; // file doesn't exist
fileName = Str::Format( "screenshots/" PRODUCT_NAME_LOWER "_%04d-%02d-%02d_%02d%02d%02d_%03d.%s",
1900 + t.tm_year, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, lastNumber, fileExtension );

if ( !ri.FS_FileExists( fileName.c_str() ) )
{
break; // file doesn't exist
}
}

if ( lastNumber == 1000 )
{
Print("ScreenshotCmd: Couldn't create a file" );
return;
}
}

if ( lastNumber == 1000 )
if (R_TakeScreenshot(fileName, format))
{
Log::Notice("ScreenShot: Couldn't create a file" );
return;
Print("Wrote %s", fileName);
}

lastNumber++;
}

Log::Notice("Wrote %s", fileName );

cmd->x = 0;
cmd->y = 0;
cmd->width = glConfig.vidWidth;
cmd->height = glConfig.vidHeight;
cmd->fileName = fileName;
cmd->format = format;
}

/*
==================
R_ScreenShot_f
screenshot
screenshot [filename]
==================
*/
static void R_ScreenShot_f()
{
R_TakeScreenshot( "tga", ssFormat_t::SSF_TGA );
}

static void R_ScreenShotJPEG_f()
{
R_TakeScreenshot( "jpg", ssFormat_t::SSF_JPEG );
}

static void R_ScreenShotPNG_f()
{
R_TakeScreenshot( "png", ssFormat_t::SSF_PNG );
}
};
ScreenshotCmd screenshotTGARegistration("screenshot", ssFormat_t::SSF_TGA, "tga");
ScreenshotCmd screenshotJPEGRegistration("screenshotJPEG", ssFormat_t::SSF_JPEG, "jpg");
ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "png");
} // namespace

//============================================================================

Expand Down Expand Up @@ -1328,9 +1315,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
ri.Cmd_AddCommand( "animationlist", R_AnimationList_f );
ri.Cmd_AddCommand( "fbolist", R_FBOList_f );
ri.Cmd_AddCommand( "vbolist", R_VBOList_f );
ri.Cmd_AddCommand( "screenshot", R_ScreenShot_f );
ri.Cmd_AddCommand( "screenshotJPEG", R_ScreenShotJPEG_f );
ri.Cmd_AddCommand( "screenshotPNG", R_ScreenShotPNG_f );
ri.Cmd_AddCommand( "gfxinfo", GfxInfo_f );
ri.Cmd_AddCommand( "buildcubemaps", R_BuildCubeMaps );

Expand Down Expand Up @@ -1463,9 +1447,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Log::Debug("RE_Shutdown( destroyWindow = %i )", destroyWindow );

ri.Cmd_RemoveCommand( "modellist" );
ri.Cmd_RemoveCommand( "screenshotPNG" );
ri.Cmd_RemoveCommand( "screenshotJPEG" );
ri.Cmd_RemoveCommand( "screenshot" );
ri.Cmd_RemoveCommand( "imagelist" );
ri.Cmd_RemoveCommand( "shaderlist" );
ri.Cmd_RemoveCommand( "shaderexp" );
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_local.h
Expand Up @@ -3769,7 +3769,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
int y;
int width;
int height;
char *fileName;
char fileName[MAX_OSPATH];
ssFormat_t format;
};
struct VideoFrameCommand : public RenderCommand {
Expand Down Expand Up @@ -3885,7 +3885,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
void LoadTGA( const char *name, byte **pic, int *width, int *height, int *numLayers, int *numMips, int *bits, byte alphaByte );

void LoadJPG( const char *filename, unsigned char **pic, int *width, int *height, int *numLayers, int *numMips, int *bits, byte alphaByte );
void SaveJPG( char *filename, int quality, int image_width, int image_height, unsigned char *image_buffer );
void SaveJPG( const char *filename, int quality, int image_width, int image_height, unsigned char *image_buffer );
int SaveJPGToBuffer( byte *buffer, size_t bufferSize, int quality, int image_width, int image_height, byte *image_buffer );

void LoadPNG( const char *name, byte **pic, int *width, int *height, int *numLayers, int *numMips, int *bits, byte alphaByte );
Expand Down

0 comments on commit c912be5

Please sign in to comment.