Skip to content

feat(screenshot): Add threaded JPEG/PNG screenshots without game stalls#1785

Open
bobtista wants to merge 31 commits into
TheSuperHackers:mainfrom
bobtista:bobtista/compressed-screenshot-f11
Open

feat(screenshot): Add threaded JPEG/PNG screenshots without game stalls#1785
bobtista wants to merge 31 commits into
TheSuperHackers:mainfrom
bobtista:bobtista/compressed-screenshot-f11

Conversation

@bobtista

@bobtista bobtista commented Nov 3, 2025

Copy link
Copy Markdown

Summary

Replaces the old BMP screenshot with compressed JPEG screenshots that don't stall the game, and adds PNG support.

Closes #1555
Closes #106 ... sort of

Adds a new screenshot function using the stb_image_write library with background threading:

  • F12 - Compressed JPEG screenshot (no stalling, ~600KB files)
  • Ctrl+F12 - Lossless PNG screenshot

Notes

  • stb_image-write is public domain licensed, single-header, zero compilation needed
  • Captures frame buffer on main thread (~1-2ms)
  • Spawns detached thread for JPEG/PNG compression and disk I/O
  • JPEG quality configurable via Options.ini (default 80)
  • Works for both Generals and Zero Hour

Testing

  • Screenshots save correctly with F12 (jpg) and Ctrl+F12 (png)
  • No game stalling during screenshot
  • Files appear in correct directory with sequential numbering
    sshot003

@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch 4 times, most recently from 20a3df1 to 37bd840 Compare November 3, 2025 04:32
@Stubbjax

Stubbjax commented Nov 3, 2025

Copy link
Copy Markdown

Some initial thoughts:

  • Keep variants of the same function to a single hotkey; instead toggle image format via a new setting in Options.ini
  • Apply image compression / quality via a new setting in Options.ini
  • Can we consolidate this logic in core instead of repeating the implementation twice?

@xezon

xezon commented Nov 3, 2025

Copy link
Copy Markdown

Agree with Stubbjax.

JPG 90 is big file. Better make it default 80.

Replace BMP screenshot with PNG screenshot. PNG is lossless compressed and always better than BMP.

Make F12 take JPG 80 screenshot. Make CTRL+F12 take PNG screenshot. Make JPG Quality adjustable.

Remove the old BMP code(s) and only use the new code for screenshot.

@xezon

xezon commented Nov 3, 2025

Copy link
Copy Markdown

Regarding Github formatting:

When you write

Addresses #1555

Then it will not close this report when this is merged.

Please read up on it here:
https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue#closing-multiple-issues

@xezon xezon added Enhancement Is new feature or request Major Severity: Minor < Major < Critical < Blocker Gen Relates to Generals ZH Relates to Zero Hour labels Nov 3, 2025
@L3-M L3-M added this to the GenTool features replication milestone Nov 3, 2025
@L3-M L3-M added the Input label Nov 3, 2025
@bobtista

bobtista commented Nov 3, 2025

Copy link
Copy Markdown
Author

Some initial thoughts:

  • Keep variants of the same function to a single hotkey; instead toggle image format via a new setting in Options.ini
  • Apply image compression / quality via a new setting in Options.ini
  • Can we consolidate this logic in core instead of repeating the implementation twice?

RE moving logic to core, I moved what I could to core - but there are a lot more files that need to be moved to core before this can be moved there eg WWVegas/WW3D2/*

@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch 3 times, most recently from 3535e1e to efc773f Compare November 3, 2025 17:45
Comment thread Generals/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DDisplay.h Outdated
Comment thread Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch 2 times, most recently from 977a6dc to f8162f3 Compare November 3, 2025 23:07
Comment thread cmake/stb.cmake Outdated
@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch from d7e8a8d to d197bdd Compare November 5, 2025 18:04
@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch from d197bdd to 9669966 Compare November 11, 2025 21:27
@xezon

xezon commented Nov 22, 2025

Copy link
Copy Markdown

Needs rebase.

@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch from 9669966 to 4897b0b Compare November 22, 2025 18:10
@bobtista

Copy link
Copy Markdown
Author

Needs rebase.

Done

Comment thread Core/GameEngineDevice/Include/W3DDevice/GameClient/W3DScreenshot.h Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp
Comment thread Generals/Code/GameEngine/Include/Common/MessageStream.h Outdated
Comment thread Generals/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp Outdated
Comment thread Generals/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp Outdated
Comment thread GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h Outdated
Comment thread Generals/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp Outdated
Skyaero42
Skyaero42 previously approved these changes Nov 22, 2025

@Skyaero42 Skyaero42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch from 9c99306 to de35e57 Compare December 3, 2025 16:48
@bobtista bobtista force-pushed the bobtista/compressed-screenshot-f11 branch from ec48835 to 7b46fad Compare July 8, 2026 20:52
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
strlcpy(threadData->pathname, pathname, ARRAY_SIZE(threadData->pathname));

DWORD threadId;
HANDLE hThread = CreateThread(nullptr, 0, screenshotThreadFunc, threadData, 0, &threadId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spawning new thread for every image is quite an expensive approach. It would be better to have a screenshot processing thread sleeping and wake up when new work is ready. Unfortunately with c++98 we do not have good sync primitives for that so maybe the thread spawn is ok for now.

Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp
Comment thread GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h Outdated
Comment thread GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h Outdated
Comment thread scripts/cpp/unify_move_files.py Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Generals/Code/GameEngine/Include/Common/MessageStream.h Outdated
Comment thread Core/GameEngine/Source/Common/OptionPreferences.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngine/Source/Common/OptionPreferences.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
// TheSuperHackers @feature bobtista 08/07/2026 Cap the quality at 95, because JPEG quality
// above that increases the file size significantly with no visible benefit.
Int quality = atoi(it->second.str());
return clamp(1, quality, 95);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still offers quality 1. What is the use case for this low quality level?


for (y = 0; y < height; y++)
{
memcpy(pixels + y * width * bytesPerPixel, (const unsigned char*)lrect.pBits + y * lrect.Pitch, width * bytesPerPixel);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appeared to work and is a bit simpler:

diff --git a/Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp b/Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp
index 7d3e84791..d4dbe7c07 100644
--- a/Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp
+++ b/Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp
@@ -171,7 +171,6 @@ void W3D_TakeCompressedScreenshot(ScreenshotFormat format, Int jpegQuality)
 		return;
 	}
 
-	unsigned int y;
 	unsigned int width = surfaceDesc.Width;
 	unsigned int height = surfaceDesc.Height;
 
@@ -179,11 +178,7 @@ void W3D_TakeCompressedScreenshot(ScreenshotFormat format, Int jpegQuality)
 	// operations are done on the screenshot thread to keep the main thread cheap.
 	const unsigned int bytesPerPixel = is32Bit ? 4 : 2;
 	unsigned char* pixels = new unsigned char[bytesPerPixel * width * height];
-
-	for (y = 0; y < height; y++)
-	{
-		memcpy(pixels + y * width * bytesPerPixel, (const unsigned char*)lrect.pBits + y * lrect.Pitch, width * bytesPerPixel);
-	}
+	memcpy(pixels, lrect.pBits, bytesPerPixel * width * height);
 
 	surfaceCopy->Unlock();
 	surfaceCopy->Release_Ref();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work correctly in regards to lrect.Pitch ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so because lrect.Pitch * height equals bytesPerPixel * width * height, we're copying all the channels

Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DScreenshot.cpp Outdated
Comment thread cmake/stb.cmake
endif()

add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${Stb_INCLUDE_DIR})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Missing package include path

This wrapper target is now always created, but the found-package path still does not reliably populate its include directory. Stb_INCLUDE_DIR is only assigned in the FetchContent branch above, so when find_package(Stb CONFIG QUIET) succeeds with a package that exposes an imported target or different include variable, this line can give the local stb target no include path. Downstream targets still include <stb_image_write.h>, so that build can configure successfully and then fail at compile time with the header missing. Please populate the wrapper from the found package's exported include directories, or link the package target directly when it exists.

Prompt To Fix With AI
This is a comment left during a code review.
Path: cmake/stb.cmake
Line: 19

Comment:
**Missing package include path**

This wrapper target is now always created, but the found-package path still does not reliably populate its include directory. `Stb_INCLUDE_DIR` is only assigned in the FetchContent branch above, so when `find_package(Stb CONFIG QUIET)` succeeds with a package that exposes an imported target or different include variable, this line can give the local `stb` target no include path. Downstream targets still include `<stb_image_write.h>`, so that build can configure successfully and then fail at compile time with the header missing. Please populate the wrapper from the found package's exported include directories, or link the package target directly when it exists.

How can I resolve this? If you propose a fix, please make it concise.

Comment thread cmake/stb.cmake
endif()

add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${Stb_INCLUDE_DIR})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Found package loses includes
When find_package(Stb CONFIG QUIET) succeeds, Stb_INCLUDE_DIR is never assigned because it is only set in the FetchContent branch. The local stb target is still created, but line 19 gives it no usable include path, so downstream files that include <stb_image_write.h> can fail to compile with a vcpkg-provided package. Please populate the wrapper target from the found package target or include directories on that path.

Prompt To Fix With AI
This is a comment left during a code review.
Path: cmake/stb.cmake
Line: 19

Comment:
**Found package loses includes**
When `find_package(Stb CONFIG QUIET)` succeeds, `Stb_INCLUDE_DIR` is never assigned because it is only set in the FetchContent branch. The local `stb` target is still created, but line 19 gives it no usable include path, so downstream files that include `<stb_image_write.h>` can fail to compile with a vcpkg-provided package. Please populate the wrapper target from the found package target or include directories on that path.

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Is new feature or request Gen Relates to Generals Input Major Severity: Minor < Major < Critical < Blocker ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add JPG/PNG Screenshot with no stalls BMP Screenshot taken with F12 can stall game significantly

9 participants