Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(platform): generals icon and app id for the linux window

Andreas Berger2370c0b69c56
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 95ea7880c..d00dcf861 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@ found and fixed — EA's own, not port damage.**
- The game no longer crashes on some Linux PCs as its window opens. The game hands out memory its own way, and on Linux the graphics driver gets its memory from the game too. Some drivers, AMD's on Debian 12 among them, need every piece lined up on a 16-byte boundary, and the game's lined up on 4, so the start died or went through depending on where the pieces happened to fall. On Linux everything outside the game's own pools now comes from the system, which lines it up as the driver expects.
- `./build-linux.sh` stopped on Linux Mint and Arch after the centred Classic interface went in: the new code compared layout names with a Windows-only spelling that GCC on Linux has never heard of. It uses the name every platform knows now, and so do the two debug-build spots with the same habit. Nobody has rebuilt on Linux since, so whether anything else stands in the way is not known yet.
- Windows on Arm laptops, the Snapdragon ones, get a build made for them. Microsoft never shipped those machines the library the game loads its textures and shaders through, so the game brings its own, the texture code the Mac and Linux versions already run. Switched on in place of Microsoft's library on an ordinary Windows PC, it draws the same battlefields, with no tree, house or patch of ground going missing. Nobody has played it on a Snapdragon laptop yet.
+- The Linux game has Generals' icon. The window carries it in the taskbar and in the window switcher, and `add-to-menu.sh` in the folder `./build-linux.sh` makes puts the game in the desktop's app menu with it, which is where GNOME takes a running game's icon from. It had none.
- Thanks to ilyasakin, whose port this is.
## Mods
diff --git a/GeneralsMD/Code/BuildFingerprint.manifest b/GeneralsMD/Code/BuildFingerprint.manifest
index cb44b24b8..4d3ffefea 100644
--- a/GeneralsMD/Code/BuildFingerprint.manifest
+++ b/GeneralsMD/Code/BuildFingerprint.manifest
@@ -3838,11 +3838,13 @@ Tools/fingerprint/build_fingerprint.cpp
Tools/gametext.py
Tools/gdi-font-metrics-header.py
Tools/gdi-font-metrics.ps1
+Tools/generals-icon.py
Tools/gentrigtables.py
Tools/include_case_check.py
Tools/install-guard.sh
Tools/license-headers.py
Tools/linux-check.sh
+Tools/linux-menu-entry.sh
Tools/linux-packages-check.sh
Tools/linux-packages.sh
Tools/linux-packages/CMakeLists.txt
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txt
index 02ff9ab36..2da290e31 100644
--- a/GeneralsMD/Code/CMakeLists.txt
+++ b/GeneralsMD/Code/CMakeLists.txt
@@ -1837,6 +1837,21 @@ if(ZH_PLATFORM_POSIX)
if(APPLE)
target_link_libraries(sdldevice PRIVATE "-framework CoreGraphics") # SdlDisplays' panel size
endif()
+ # Linux has no icon in the executable: the window sets Generals.ico's pixels on itself (SdlGameEngine).
+ # The Mac app's icon is the bundle's.
+ if(ZH_PLATFORM_LINUX)
+ find_package(Python3 COMPONENTS Interpreter REQUIRED)
+ set(ZH_WINDOW_ICON_HEADER ${CMAKE_CURRENT_BINARY_DIR}/generated/zh_window_icon.h)
+ add_custom_command(OUTPUT ${ZH_WINDOW_ICON_HEADER}
+ COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/generated
+ COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Tools/generals-icon.py header
+ ${CMAKE_CURRENT_SOURCE_DIR}/Main/Generals.ico ${ZH_WINDOW_ICON_HEADER}
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Main/Generals.ico ${CMAKE_CURRENT_SOURCE_DIR}/Tools/generals-icon.py
+ COMMENT "Window icon from Main/Generals.ico")
+ target_sources(sdldevice PRIVATE ${ZH_WINDOW_ICON_HEADER})
+ target_include_directories(sdldevice PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated)
+ target_compile_definitions(sdldevice PRIVATE ZH_WINDOW_ICON)
+ endif()
add_executable(generals ${CMAKE_CURRENT_SOURCE_DIR}/Main/PosixMain.cpp)
target_include_directories(generals PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Main ${GENERATED})
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/SdlDevice/Common/SdlGameEngine.cpp b/GeneralsMD/Code/GameEngineDevice/Source/SdlDevice/Common/SdlGameEngine.cpp
index 32683349b..7297dd33d 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/SdlDevice/Common/SdlGameEngine.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/SdlDevice/Common/SdlGameEngine.cpp
@@ -45,6 +45,9 @@
#include "W3DDevice/GameLogic/W3DGameLogic.h"
#include <SDL3/SDL.h>
+#ifdef ZH_WINDOW_ICON
+#include "zh_window_icon.h" // Main/Generals.ico's pixels, generated by Tools/generals-icon.py
+#endif
#include <math.h>
#include <stdio.h>
@@ -222,6 +225,17 @@ void SdlGameEngine::createWindow( void )
RELEASE_CRASH( why );
return;
}
+#ifdef ZH_WINDOW_ICON
+ // a Linux executable carries no icon, so the window brings its own for the taskbar and alt-tab;
+ // GNOME on Wayland ignores it and takes zero-hour-reforged.desktop's, by the app id (PosixMain.cpp)
+ SDL_Surface *icon = SDL_CreateSurfaceFrom( ZH_WINDOW_ICON_SIZE, ZH_WINDOW_ICON_SIZE, SDL_PIXELFORMAT_RGBA32,
+ const_cast<unsigned char *>( ZH_WINDOW_ICON_RGBA ), ZH_WINDOW_ICON_SIZE * 4 );
+ if (icon != NULL)
+ {
+ SDL_SetWindowIcon( m_window, icon );
+ SDL_DestroySurface( icon );
+ }
+#endif
DEBUG_LOG(( "SdlGameEngine: window %dx%d, %s%s%s\n", width, height,
m_request.windowed ? "windowed" : "fullscreen", m_request.borderless ? ", borderless" : "",
m_request.hidden ? ", hidden" : "" ));
diff --git a/GeneralsMD/Code/Main/PosixMain.cpp b/GeneralsMD/Code/Main/PosixMain.cpp
index e24f797b5..2b6b97124 100644
--- a/GeneralsMD/Code/Main/PosixMain.cpp
+++ b/GeneralsMD/Code/Main/PosixMain.cpp
@@ -445,6 +445,15 @@ int main( int argc, char *argv[] )
// lists in the user's format. LC_NUMERIC would change how the INI parser reads decimals.
setlocale( LC_TIME, "" );
+#ifdef __linux__
+ // Before SDL first starts (the folder chooser may): Wayland and X11 name the window by this app id,
+ // and desktops match it to the .desktop file of that name for the icon and the dock entry. SDL's own
+ // default is the executable's name, generals, which no .desktop file has. The Flatpak's is its id.
+ const char *flatpakId = getenv( "FLATPAK_ID" );
+ SDL_SetAppMetadata( "Zero Hour Reforged", VERSION_STRING,
+ flatpakId != NULL && flatpakId[0] != 0 ? flatpakId : "zero-hour-reforged" );
+#endif
+
try {
TheUnicodeStringCriticalSection = &critSec2;
diff --git a/GeneralsMD/Code/Tools/build-posix.sh b/GeneralsMD/Code/Tools/build-posix.sh
index 0acee0802..d851c746e 100755
--- a/GeneralsMD/Code/Tools/build-posix.sh
+++ b/GeneralsMD/Code/Tools/build-posix.sh
@@ -181,6 +181,13 @@ else
package="$BUILD/ZeroHourReforged"
rm -rf "$package"
mkdir -p "$package/bin" "$package/share/zero-hour-reforged"
+ # Generals' icon and add-to-menu.sh, which puts the folder in the app menu with it
+ for size in 48 128; do
+ mkdir -p "$package/share/icons/hicolor/${size}x${size}/apps"
+ python3 "$SRC/Tools/generals-icon.py" png "$SRC/Main/Generals.ico" \
+ "$package/share/icons/hicolor/${size}x${size}/apps/zero-hour-reforged.png" $size || fail "cannot make the icon."
+ done
+ cp "$SRC/Tools/linux-menu-entry.sh" "$package/add-to-menu.sh"
cp "$BUILD/generals" "$package/bin/generals"
ln -s ../../../overlay "$package/share/zero-hour-reforged/overlay"
echo "[build] done: run $package/bin/generals"
diff --git a/GeneralsMD/Code/Tools/generals-icon.py b/GeneralsMD/Code/Tools/generals-icon.py
new file mode 100644
index 000000000..c70ec63d4
--- /dev/null
+++ b/GeneralsMD/Code/Tools/generals-icon.py
@@ -0,0 +1,99 @@
+#!/usr/bin/env python3
+"""Main/Generals.ico for the platforms that do not read .ico files.
+
+ generals-icon.py png <ico> <out.png> [size] a PNG, at the .ico's own size or scaled to size
+ generals-icon.py header <ico> <out.h> the pixels as a C array, for SDL_SetWindowIcon
+
+Takes the .ico's largest 24-bit image (48 px) with its AND mask as alpha. Scaling is bilinear over
+premultiplied alpha; AppStream, which the Flatpak's metadata goes through, wants 64 px or more.
+"""
+import struct
+import sys
+import zlib
+
+
+def read_ico(path):
+ d = open(path, "rb").read()
+ count = struct.unpack("<H", d[4:6])[0]
+ best = None
+ for i in range(count):
+ w, h, c, r, planes, bpp, size, off = struct.unpack("<BBBBHHII", d[6 + 16 * i:22 + 16 * i])
+ w, h = w or 256, h or 256
+ if d[off:off + 4] == b"\x89PNG":
+ continue
+ header = struct.unpack("<IiiHHIIiiII", d[off:off + 40])
+ if header[4] == 24 and (best is None or w > best[0]):
+ best = (w, h, off + header[0])
+ w, h, pixels = best
+ row = (w * 3 + 3) & ~3
+ mask_row = ((w + 31) // 32) * 4
+ mask = pixels + row * h
+ rgba = [] # rows of (r, g, b, a), top first
+ for y in range(h):
+ src = pixels + row * (h - 1 - y)
+ msrc = mask + mask_row * (h - 1 - y)
+ line = []
+ for x in range(w):
+ b, g, r = d[src + 3 * x:src + 3 * x + 3]
+ transparent = (d[msrc + x // 8] >> (7 - x % 8)) & 1
+ line.append((r, g, b, 0 if transparent else 255))
+ rgba.append(line)
+ return rgba
+
+
+def scaled(image, size):
+ n = len(image)
+
+ def at(x, y):
+ r, g, b, a = image[min(max(y, 0), n - 1)][min(max(x, 0), n - 1)]
+ return (r * a / 255.0, g * a / 255.0, b * a / 255.0, float(a))
+
+ out = []
+ for y in range(size):
+ fy = (y + 0.5) * n / size - 0.5; y0 = int(fy // 1); ty = fy - y0
+ line = []
+ for x in range(size):
+ fx = (x + 0.5) * n / size - 0.5; x0 = int(fx // 1); tx = fx - x0
+ p = [at(x0, y0)[i] * (1 - tx) * (1 - ty) + at(x0 + 1, y0)[i] * tx * (1 - ty)
+ + at(x0, y0 + 1)[i] * (1 - tx) * ty + at(x0 + 1, y0 + 1)[i] * tx * ty for i in range(4)]
+ a = p[3]
+ line.append(tuple(int(round(min(255, c * 255.0 / a))) if a > 0 else 0 for c in p[:3]) + (int(round(a)),))
+ out.append(line)
+ return out
+
+
+def write_png(path, image):
+ def chunk(kind, data):
+ return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xffffffff)
+ size = len(image)
+ raw = b"".join(bytes([0]) + bytes(v for p in line for v in p) for line in image)
+ open(path, "wb").write(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0))
+ + chunk(b"IDAT", zlib.compress(raw, 9)) + chunk(b"IEND", b""))
+
+
+def write_header(path, image):
+ size = len(image)
+ data = [v for line in image for p in line for v in p]
+ lines = [", ".join(str(v) for v in data[i:i + 24]) for i in range(0, len(data), 24)]
+ open(path, "w").write(
+ "// Generated from Main/Generals.ico by Tools/generals-icon.py; not edited by hand.\n"
+ f"static const int ZH_WINDOW_ICON_SIZE = {size};\n"
+ "static const unsigned char ZH_WINDOW_ICON_RGBA[] = {\n\t" + ",\n\t".join(lines) + "\n};\n")
+
+
+def main():
+ if len(sys.argv) < 4 or sys.argv[1] not in ("png", "header"):
+ print(__doc__, file=sys.stderr)
+ return 2
+ image = read_ico(sys.argv[2])
+ if sys.argv[1] == "png":
+ if len(sys.argv) > 4:
+ image = scaled(image, int(sys.argv[4]))
+ write_png(sys.argv[3], image)
+ else:
+ write_header(sys.argv[3], image)
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/GeneralsMD/Code/Tools/linux-menu-entry.sh b/GeneralsMD/Code/Tools/linux-menu-entry.sh
new file mode 100755
index 000000000..c503ee1ca
--- /dev/null
+++ b/GeneralsMD/Code/Tools/linux-menu-entry.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+# Additional terms under GNU GPL section 7 apply: see LICENSE.md.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# build-posix.sh stages this as ZeroHourReforged/add-to-menu.sh. It puts the folder it sits in into the
+# desktop's app menu, with Generals' icon:
+#
+# ./add-to-menu.sh ~/.local/share/applications/zero-hour-reforged.desktop and the icons
+# ./add-to-menu.sh --remove takes them out again
+#
+# The file is named after the window's app id (PosixMain.cpp), which is how GNOME on Wayland finds the
+# icon for a running window. Run it again after moving the folder.
+
+set -euo pipefail
+
+data=${XDG_DATA_HOME:-$HOME/.local/share}
+desktop="$data/applications/zero-hour-reforged.desktop"
+here=$(cd "$(dirname "$0")" && pwd)
+
+refresh() {
+ if command -v update-desktop-database >/dev/null 2>&1; then update-desktop-database -q "$data/applications" || true; fi
+ if command -v gtk-update-icon-cache >/dev/null 2>&1; then gtk-update-icon-cache -q -t "$data/icons/hicolor" || true; fi
+}
+
+if [ "${1:-}" = "--remove" ]; then
+ rm -f "$desktop" "$data/icons/hicolor/48x48/apps/zero-hour-reforged.png" "$data/icons/hicolor/128x128/apps/zero-hour-reforged.png"
+ refresh
+ echo "[menu] removed Zero Hour Reforged from the app menu"
+ exit 0
+fi
+
+[ -x "$here/bin/generals" ] || { echo "[menu] ERROR: no bin/generals beside $0" >&2; exit 1; }
+
+for size in 48x48 128x128; do
+ mkdir -p "$data/icons/hicolor/$size/apps"
+ cp "$here/share/icons/hicolor/$size/apps/zero-hour-reforged.png" "$data/icons/hicolor/$size/apps/"
+done
+
+# Exec's quoting (the Desktop Entry spec): inside double quotes \ " ` $ are escaped, and % is doubled
+exec_path=$(printf '%s' "$here/bin/generals" | sed -e 's/[\\"`$]/\\\\&/g' -e 's/%/%%/g')
+mkdir -p "$data/applications"
+cat > "$desktop" <<DESKTOP
+[Desktop Entry]
+Type=Application
+Name=Zero Hour Reforged
+Comment=Command & Conquer Generals Zero Hour, reforged
+Exec="$exec_path"
+Icon=zero-hour-reforged
+StartupWMClass=zero-hour-reforged
+Categories=Game;StrategyGame;
+Terminal=false
+DESKTOP
+refresh
+echo "[menu] added Zero Hour Reforged to the app menu, starting $here/bin/generals"
diff --git a/GeneralsMD/Code/Tools/linux-portable.sh b/GeneralsMD/Code/Tools/linux-portable.sh
index db77a7f0e..24ea9a33e 100755
--- a/GeneralsMD/Code/Tools/linux-portable.sh
+++ b/GeneralsMD/Code/Tools/linux-portable.sh
@@ -174,63 +174,9 @@ exec "$here/bin/generals" "$@"
LAUNCHER
chmod +x "$OUT/zero-hour-reforged.sh"
-python3 - "$CODE/Main/Generals.ico" "$OUT/share/icons/hicolor/48x48/apps/zero-hour-reforged.png" \
- "$OUT/share/icons/hicolor/128x128/apps/zero-hour-reforged.png" <<'ICON_EOF' || fail "cannot make the icon"
-# the .ico's largest 24-bit image (48 px), its AND mask as alpha, written as a PNG; and the same scaled to 128 px
-# (bilinear, over premultiplied alpha), because AppStream, which the Flatpak's metadata goes through, wants 64 or more
-import struct, sys, zlib
-d = open(sys.argv[1], "rb").read()
-count = struct.unpack("<H", d[4:6])[0]
-best = None
-for i in range(count):
- w, h, c, r, planes, bpp, size, off = struct.unpack("<BBBBHHII", d[6 + 16 * i:22 + 16 * i])
- w, h = w or 256, h or 256
- if d[off:off + 4] == b"\x89PNG":
- continue
- header = struct.unpack("<IiiHHIIiiII", d[off:off + 40])
- if header[4] == 24 and (best is None or w > best[0]):
- best = (w, h, off + header[0])
-w, h, pixels = best
-row = (w * 3 + 3) & ~3
-mask_row = ((w + 31) // 32) * 4
-mask = pixels + row * h
-rgba = [] # rows of (r, g, b, a), top first
-for y in range(h):
- src = pixels + row * (h - 1 - y)
- msrc = mask + mask_row * (h - 1 - y)
- line = []
- for x in range(w):
- b, g, r = d[src + 3 * x:src + 3 * x + 3]
- transparent = (d[msrc + x // 8] >> (7 - x % 8)) & 1
- line.append((r, g, b, 0 if transparent else 255))
- rgba.append(line)
-def chunk(kind, data):
- return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xffffffff)
-def write_png(path, image):
- size = len(image)
- raw = b"".join(bytes([0]) + bytes(v for p in line for v in p) for line in image)
- open(path, "wb").write(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0))
- + chunk(b"IDAT", zlib.compress(raw, 9)) + chunk(b"IEND", b""))
-def scaled(image, size):
- n = len(image)
- def at(x, y):
- r, g, b, a = image[min(max(y, 0), n - 1)][min(max(x, 0), n - 1)]
- return (r * a / 255.0, g * a / 255.0, b * a / 255.0, float(a))
- out = []
- for y in range(size):
- fy = (y + 0.5) * n / size - 0.5; y0 = int(fy // 1); ty = fy - y0
- line = []
- for x in range(size):
- fx = (x + 0.5) * n / size - 0.5; x0 = int(fx // 1); tx = fx - x0
- p = [at(x0, y0)[i] * (1 - tx) * (1 - ty) + at(x0 + 1, y0)[i] * tx * (1 - ty)
- + at(x0, y0 + 1)[i] * (1 - tx) * ty + at(x0 + 1, y0 + 1)[i] * tx * ty for i in range(4)]
- a = p[3]
- line.append(tuple(int(round(min(255, c * 255.0 / a))) if a > 0 else 0 for c in p[:3]) + (int(round(a)),))
- out.append(line)
- return out
-write_png(sys.argv[2], rgba)
-write_png(sys.argv[3], scaled(rgba, 128))
-ICON_EOF
+python3 "$CODE/Tools/generals-icon.py" png "$CODE/Main/Generals.ico" "$OUT/share/icons/hicolor/48x48/apps/zero-hour-reforged.png" \
+ && python3 "$CODE/Tools/generals-icon.py" png "$CODE/Main/Generals.ico" "$OUT/share/icons/hicolor/128x128/apps/zero-hour-reforged.png" 128 \
+ || fail "cannot make the icon"
cat > "$OUT/share/applications/zero-hour-reforged.desktop" <<'DESKTOP'
[Desktop Entry]
diff --git a/build-linux.sh b/build-linux.sh
index 5c796b353..3c0258e39 100755
--- a/build-linux.sh
+++ b/build-linux.sh
@@ -25,6 +25,7 @@
#
# Needs a C++ compiler, CMake, Ninja and SDL3's X11, Wayland and Vulkan headers; it prints the apt, dnf
# or pacman command when something is missing. The result is build-linux/ZeroHourReforged/bin/generals.
+# build-linux/ZeroHourReforged/add-to-menu.sh puts it in the desktop's app menu, with Generals' icon.
# GeneralsMD/Code/Tools/build-posix.sh does the work, shared with build-macos.sh. The release packages
# (AppImage, .deb, .rpm, Arch, Flatpak) are GeneralsMD/Code/Tools/linux-packages.sh's.