1
0
Fork 0

Codechange: Change ScreenshotCallback into a std::function, so there is no need for void* user data.

pull/14037/head
frosch 2025-04-18 20:37:35 +02:00 committed by frosch
parent c09e825e0b
commit 0eb6964311
5 changed files with 47 additions and 50 deletions

View File

@ -70,7 +70,7 @@ std::string_view GetCurrentScreenshotExtension()
* Callback of the screenshot generator that dumps the current video buffer. * Callback of the screenshot generator that dumps the current video buffer.
* @see ScreenshotCallback * @see ScreenshotCallback
*/ */
static void CurrentScreenCallback(void *, void *buf, uint y, uint pitch, uint n) static void CurrentScreenCallback(void *buf, uint y, uint pitch, uint n)
{ {
Blitter *blitter = BlitterFactory::GetCurrentBlitter(); Blitter *blitter = BlitterFactory::GetCurrentBlitter();
void *src = blitter->MoveTo(_screen.dst_ptr, 0, y); void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
@ -79,55 +79,50 @@ static void CurrentScreenCallback(void *, void *buf, uint y, uint pitch, uint n)
/** /**
* generate a large piece of the world * generate a large piece of the world
* @param userdata Viewport area to draw * @param vp Viewport area to draw
* @param buf Videobuffer with same bitdepth as current blitter * @param buf Videobuffer with same bitdepth as current blitter
* @param y First line to render * @param y First line to render
* @param pitch Pitch of the videobuffer * @param pitch Pitch of the videobuffer
* @param n Number of lines to render * @param n Number of lines to render
*/ */
static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n) static void LargeWorldCallback(Viewport &vp, void *buf, uint y, uint pitch, uint n)
{ {
Viewport *vp = (Viewport *)userdata; DrawPixelInfo dpi{
DrawPixelInfo dpi; .dst_ptr = buf,
int wx, left; .left = 0,
.top = static_cast<int>(y),
.width = vp.width,
.height = static_cast<int>(n),
.pitch = static_cast<int>(pitch),
.zoom = ZOOM_LVL_WORLD_SCREENSHOT
};
/* We are no longer rendering to the screen */ /* We are no longer rendering to the screen */
DrawPixelInfo old_screen = _screen; AutoRestoreBackup screen_backup(_screen, {
bool old_disable_anim = _screen_disable_anim; .dst_ptr = buf,
.left = 0,
_screen.dst_ptr = buf; .top = 0,
_screen.width = pitch; .width = static_cast<int>(pitch),
_screen.height = n; .height = static_cast<int>(n),
_screen.pitch = pitch; .pitch = static_cast<int>(pitch),
_screen_disable_anim = true; .zoom = ZOOM_LVL_MIN
});
AutoRestoreBackup disable_anim_backup(_screen_disable_anim, true);
AutoRestoreBackup dpi_backup(_cur_dpi, &dpi); AutoRestoreBackup dpi_backup(_cur_dpi, &dpi);
dpi.dst_ptr = buf;
dpi.height = n;
dpi.width = vp->width;
dpi.pitch = pitch;
dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
dpi.left = 0;
dpi.top = y;
/* Render viewport in blocks of 1600 pixels width */ /* Render viewport in blocks of 1600 pixels width */
left = 0; int left = 0;
while (vp->width - left != 0) { while (vp.width - left != 0) {
wx = std::min(vp->width - left, 1600); int wx = std::min(vp.width - left, 1600);
left += wx; left += wx;
ViewportDoDraw(*vp, ViewportDoDraw(vp,
ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left, ScaleByZoom(left - wx - vp.left, vp.zoom) + vp.virtual_left,
ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top, ScaleByZoom(y - vp.top, vp.zoom) + vp.virtual_top,
ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left, ScaleByZoom(left - vp.left, vp.zoom) + vp.virtual_left,
ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top ScaleByZoom((y + n) - vp.top, vp.zoom) + vp.virtual_top
); );
} }
/* Switch back to rendering to the screen */
_screen = old_screen;
_screen_disable_anim = old_disable_anim;
} }
/** /**
@ -180,7 +175,7 @@ static bool MakeSmallScreenshot(bool crashlog)
auto provider = GetScreenshotProvider(); auto provider = GetScreenshotProvider();
if (provider == nullptr) return false; if (provider == nullptr) return false;
return provider->MakeImage(MakeScreenshotName(SCREENSHOT_NAME, provider->GetName(), crashlog), CurrentScreenCallback, nullptr, _screen.width, _screen.height, return provider->MakeImage(MakeScreenshotName(SCREENSHOT_NAME, provider->GetName(), crashlog), CurrentScreenCallback, _screen.width, _screen.height,
BlitterFactory::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette); BlitterFactory::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
} }
@ -283,8 +278,10 @@ static bool MakeLargeWorldScreenshot(ScreenshotType t, uint32_t width = 0, uint3
Viewport vp = SetupScreenshotViewport(t, width, height); Viewport vp = SetupScreenshotViewport(t, width, height);
return provider->MakeImage(MakeScreenshotName(SCREENSHOT_NAME, provider->GetName()), LargeWorldCallback, &vp, vp.width, vp.height, return provider->MakeImage(MakeScreenshotName(SCREENSHOT_NAME, provider->GetName()),
BlitterFactory::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette); [&](void *buf, uint y, uint pitch, uint n) {
LargeWorldCallback(vp, buf, y, pitch, n);
}, vp.width, vp.height, BlitterFactory::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
} }
/** /**
@ -294,7 +291,7 @@ static bool MakeLargeWorldScreenshot(ScreenshotType t, uint32_t width = 0, uint3
* @param n Number of lines to write. * @param n Number of lines to write.
* @see ScreenshotCallback * @see ScreenshotCallback
*/ */
static void HeightmapCallback(void *, void *buffer, uint y, uint, uint n) static void HeightmapCallback(void *buffer, uint y, uint, uint n)
{ {
uint8_t *buf = (uint8_t *)buffer; uint8_t *buf = (uint8_t *)buffer;
while (n > 0) { while (n > 0) {
@ -333,7 +330,7 @@ bool MakeHeightmapScreenshot(const char *filename)
_heightmap_highest_peak = std::max(h, _heightmap_highest_peak); _heightmap_highest_peak = std::max(h, _heightmap_highest_peak);
} }
return provider->MakeImage(filename, HeightmapCallback, nullptr, Map::SizeX(), Map::SizeY(), 8, palette); return provider->MakeImage(filename, HeightmapCallback, Map::SizeX(), Map::SizeY(), 8, palette);
} }
static ScreenshotType _confirmed_screenshot_type; ///< Screenshot type the current query is about to confirm. static ScreenshotType _confirmed_screenshot_type; ///< Screenshot type the current query is about to confirm.
@ -473,7 +470,7 @@ bool MakeScreenshot(ScreenshotType t, const std::string &name, uint32_t width, u
} }
static void MinimapScreenCallback(void *, void *buf, uint y, uint pitch, uint n) static void MinimapScreenCallback(void *buf, uint y, uint pitch, uint n)
{ {
uint32_t *ubuf = (uint32_t *)buf; uint32_t *ubuf = (uint32_t *)buf;
uint num = (pitch * n); uint num = (pitch * n);
@ -502,5 +499,5 @@ bool MakeMinimapWorldScreenshot()
auto provider = GetScreenshotProvider(); auto provider = GetScreenshotProvider();
if (provider == nullptr) return false; if (provider == nullptr) return false;
return provider->MakeImage(MakeScreenshotName(SCREENSHOT_NAME, provider->GetName()), MinimapScreenCallback, nullptr, Map::SizeX(), Map::SizeY(), 32, _cur_palette.palette); return provider->MakeImage(MakeScreenshotName(SCREENSHOT_NAME, provider->GetName()), MinimapScreenCallback, Map::SizeX(), Map::SizeY(), 32, _cur_palette.palette);
} }

View File

@ -43,7 +43,7 @@ class ScreenshotProvider_Bmp : public ScreenshotProvider {
public: public:
ScreenshotProvider_Bmp() : ScreenshotProvider("bmp", "BMP", 10) {} ScreenshotProvider_Bmp() : ScreenshotProvider("bmp", "BMP", 10) {}
bool MakeImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) override bool MakeImage(const char *name, const ScreenshotCallback &callb, uint w, uint h, int pixelformat, const Colour *palette) override
{ {
uint bpp; // bytes per pixel uint bpp; // bytes per pixel
switch (pixelformat) { switch (pixelformat) {
@ -117,7 +117,7 @@ public:
h -= n; h -= n;
/* Render the pixels */ /* Render the pixels */
callb(userdata, buff.data(), h, w, n); callb(buff.data(), h, w, n);
/* Write each line */ /* Write each line */
while (n-- != 0) { while (n-- != 0) {

View File

@ -41,7 +41,7 @@ class ScreenshotProvider_Pcx : public ScreenshotProvider {
public: public:
ScreenshotProvider_Pcx() : ScreenshotProvider("pcx", "PCX", 20) {} ScreenshotProvider_Pcx() : ScreenshotProvider("pcx", "PCX", 20) {}
bool MakeImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) override bool MakeImage(const char *name, const ScreenshotCallback &callb, uint w, uint h, int pixelformat, const Colour *palette) override
{ {
uint maxlines; uint maxlines;
uint y; uint y;
@ -93,7 +93,7 @@ public:
uint i; uint i;
/* render the pixels into the buffer */ /* render the pixels into the buffer */
callb(userdata, buff.data(), y, w, n); callb(buff.data(), y, w, n);
y += n; y += n;
/* write them to pcx */ /* write them to pcx */

View File

@ -30,7 +30,7 @@ class ScreenshotProvider_Png : public ScreenshotProvider {
public: public:
ScreenshotProvider_Png() : ScreenshotProvider("png", "PNG", 0) {} ScreenshotProvider_Png() : ScreenshotProvider("png", "PNG", 0) {}
bool MakeImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) override bool MakeImage(const char *name, const ScreenshotCallback &callb, uint w, uint h, int pixelformat, const Colour *palette) override
{ {
png_color rq[256]; png_color rq[256];
uint i, y, n; uint i, y, n;
@ -151,7 +151,7 @@ public:
n = std::min(h - y, maxlines); n = std::min(h - y, maxlines);
/* render the pixels into the buffer */ /* render the pixels into the buffer */
callb(userdata, buff.data(), y, w, n); callb(buff.data(), y, w, n);
y += n; y += n;
/* write them to png */ /* write them to png */

View File

@ -21,7 +21,7 @@
* @param pitch Number of pixels to write (1 byte for 8bpp, 4 bytes for 32bpp). @see Colour * @param pitch Number of pixels to write (1 byte for 8bpp, 4 bytes for 32bpp). @see Colour
* @param n Number of lines to write. * @param n Number of lines to write.
*/ */
using ScreenshotCallback = void(void *userdata, void *buf, uint y, uint pitch, uint n); using ScreenshotCallback = std::function<void (void *buf, uint y, uint pitch, uint n)>;
/** Base interface for a SoundLoader implementation. */ /** Base interface for a SoundLoader implementation. */
class ScreenshotProvider : public PriorityBaseProvider<ScreenshotProvider> { class ScreenshotProvider : public PriorityBaseProvider<ScreenshotProvider> {
@ -36,7 +36,7 @@ public:
ProviderManager<ScreenshotProvider>::Unregister(*this); ProviderManager<ScreenshotProvider>::Unregister(*this);
} }
virtual bool MakeImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) = 0; virtual bool MakeImage(const char *name, const ScreenshotCallback &callb, uint w, uint h, int pixelformat, const Colour *palette) = 0;
}; };
#endif /* SCREENSHOT_TYPE_H */ #endif /* SCREENSHOT_TYPE_H */