Skip to content

Commit

Permalink
feat: Initial work on screenshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
carstene1ns committed Sep 26, 2024
1 parent ba01eba commit 08cedfa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/io/gfx/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
#include "setup.h"
#include "util.h"
#include "io/log.h"
#include "io/file.h"

#include <string.h>
#include <miniz.h>


/**
Expand Down Expand Up @@ -623,6 +625,10 @@ void Video::update (SDL_Event *event) {

}

// If F12 has been pressed, save a screenshot
if (event->key.keysym.sym == SDLK_F12)
saveScreenShot();

break;
#endif

Expand Down Expand Up @@ -834,3 +840,51 @@ void setLogicalPalette (SDL_Surface* surface, SDL_Color *palette, int start, int
#endif

}

/**
* Save a screenshot to numbered file
*/
void Video::saveScreenShot () {

#if OJ_SDL2
char check[] = "OpenJazz_000.png";
bool ok = false;

for (int i = 1; i <= 100; i++) {
snprintf(check, sizeof(check), "OpenJazz_%03d.png", i);

if (!fileExists(check, PATH_TYPE_TEMP))
break;
}

SDL_Surface *rgbShot = SDL_CreateRGBSurfaceWithFormat(0,
canvasW, canvasH, 32, SDL_PIXELFORMAT_RGB24);
if (rgbShot) {
SDL_Rect src = { 0, 0, canvasW, canvasH };
if (SDL_BlitSurface(screen, &src, rgbShot, nullptr) == 0) {
size_t length = 0;
char *pngImage = (char *)tdefl_write_image_to_png_file_in_memory(
rgbShot->pixels, rgbShot->w, rgbShot->h, 3, &length);
if (pngImage != nullptr) {
try {
FilePtr pngFile = std::make_unique<File>(check, PATH_TYPE_TEMP, true);
pngFile->storeData(pngImage, length);
ok = true;
} catch (int e) {
//
}
mz_free(pngImage);
}
}

SDL_FreeSurface(rgbShot);
}

if (!ok) {
LOG_WARN("Could not save screenshot %s.", check);
}
#else
LOG_WARN("Sorry, screenshot support is SDL2-only.");
#endif

}
1 change: 1 addition & 0 deletions src/io/gfx/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Video {
void flip (int mspf, PaletteEffect* paletteEffects = NULL, bool effectsStopped = false);

void clearScreen (int index);
void saveScreenShot ();

};

Expand Down

0 comments on commit 08cedfa

Please sign in to comment.