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.

perf(w3ddevice): upload the radar shroud once a frame, not once a pixel

Olcay Seygan715e3bc1a898
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f969b6cec..a6d515505 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -999,6 +999,15 @@ conversion now. It is also one less way for two machines in an online match to d
other: the old route asked the processor what rounding mode it happened to be in, and that is not
something a game gets to control.
+**And the black over the parts of the map you cannot see.** The minimap paints that as a small
+picture, and the game keeps it honest as your units move — every time one of them sees a little
+further, the strip of map it just revealed is repainted. It was being repainted a single dot at a
+time, and each dot meant handing the picture to the graphics card, changing that one dot, and taking
+it back. An army on the move reveals hundreds of those a second, and the start of every match repaints
+the entire map that way, one dot after another. The picture is now kept in ordinary memory, where a
+dot costs nothing, and handed over once a frame — only the rectangle that actually changed, and
+nothing at all on a frame where nothing moved.
+
## Sound, video, and getting it to start at all
- **Audio is real**, through the game's own audio library. The sound SDK was stripped from the source
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/RadarShroudCache.h b/GeneralsMD/Code/GameEngine/Include/Common/RadarShroudCache.h
new file mode 100644
index 000000000..6006eb4d0
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/Common/RadarShroudCache.h
@@ -0,0 +1,102 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** 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/>.
+*/
+
+// FILE: RadarShroudCache.h ///////////////////////////////////////////////////////////////////////
+// Desc: The radar's shroud layer, kept in main memory and uploaded once a frame.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef _RADAR_SHROUD_CACHE_H_
+#define _RADAR_SHROUD_CACHE_H_
+
+#include "Lib/BaseType.h"
+
+/* The radar draws the shroud as a small texture - 128x128 - with black pixels whose alpha says how
+ hidden that part of the map is. The simulation pokes at it one map cell at a time: every time a
+ unit's line of sight crosses a shroud cell boundary, the cell tells the radar the new state of
+ that one cell, which is one to four pixels.
+
+ That used to go straight at the texture, and the road it took was expensive beyond belief. Each
+ call asked the texture for its surface (a Direct3D GetSurfaceLevel plus a wrapper object), then
+ called DrawPixel once per pixel - and DrawPixel is a GetDesc, a LockRect of a one-pixel
+ rectangle, one byte written, and an UnlockRect. An army on the move crosses hundreds of shroud
+ cells a frame, and the first thing a match does is walk the whole map through
+ refreshShroudForLocalPlayer, one cell at a time. EA left a note next to the radar's terrain
+ drawing saying they had tried locking once and it made "absolutely *no* performance difference";
+ that was about the terrain, which is drawn once per map, and it is not true of the shroud, which
+ is drawn all match long.
+
+ So the shroud layer lives here instead, one alpha byte per texture pixel, and the texture is
+ written at most once a frame, over the rectangle that actually changed. Nothing here touches
+ Direct3D, which is also what makes it testable. */
+
+class RadarShroudCache
+{
+public:
+
+ RadarShroudCache();
+ ~RadarShroudCache();
+
+ /** Give the cache the size of the texture it shadows. Everything starts clear (alpha 0) and
+ fully dirty, because the texture it shadows has just been created and holds nothing. */
+ void setSize( Int width, Int height );
+
+ /** Every pixel to one alpha, and the whole surface dirty. */
+ void clear( UnsignedByte alpha );
+
+ /** One pixel. Points outside the texture are dropped, which is what the caller's
+ legalRadarPoint test used to do; a write that changes nothing does not dirty anything. */
+ void setAlpha( Int x, Int y, UnsignedByte alpha );
+
+ UnsignedByte getAlpha( Int x, Int y ) const;
+
+ Bool isDirty( void ) const { return m_dirty; }
+
+ // the dirty rectangle, inclusive on both ends; only meaningful while isDirty()
+ Int getDirtyMinX( void ) const { return m_dirtyMinX; }
+ Int getDirtyMinY( void ) const { return m_dirtyMinY; }
+ Int getDirtyMaxX( void ) const { return m_dirtyMaxX; }
+ Int getDirtyMaxY( void ) const { return m_dirtyMaxY; }
+
+ /** Write the dirty rectangle into a locked surface and mark the cache clean. bytesPerPixel is
+ the surface's, and the colour written is black with our alpha - GameMakeColor( 0, 0, 0, a )
+ truncated to the pixel size, exactly what DrawPixel used to write. Does nothing, and
+ stays dirty, if there is no buffer to write into. */
+ void flushTo( void *bits, Int pitch, Int bytesPerPixel );
+
+private:
+
+ void markDirty( Int x, Int y );
+
+ UnsignedByte *m_alpha; ///< one byte per texture pixel, row major, no padding
+ Int m_width;
+ Int m_height;
+
+ Bool m_dirty;
+ Int m_dirtyMinX;
+ Int m_dirtyMinY;
+ Int m_dirtyMaxX;
+ Int m_dirtyMaxY;
+
+ // no copying: it owns a buffer and nothing needs to copy one
+ RadarShroudCache( const RadarShroudCache & );
+ RadarShroudCache &operator=( const RadarShroudCache & );
+};
+
+#endif // _RADAR_SHROUD_CACHE_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/RadarShroudCache.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/RadarShroudCache.cpp
new file mode 100644
index 000000000..40e39995c
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/RadarShroudCache.cpp
@@ -0,0 +1,166 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** 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/>.
+*/
+
+// FILE: RadarShroudCache.cpp /////////////////////////////////////////////////////////////////////
+// Desc: The radar's shroud layer, kept in main memory and uploaded once a frame.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#include "PreRTS.h"
+
+#include "Common/RadarShroudCache.h"
+
+// ------------------------------------------------------------------------------------------------
+RadarShroudCache::RadarShroudCache()
+{
+ m_alpha = NULL;
+ m_width = 0;
+ m_height = 0;
+ m_dirty = FALSE;
+ m_dirtyMinX = 0;
+ m_dirtyMinY = 0;
+ m_dirtyMaxX = -1;
+ m_dirtyMaxY = -1;
+}
+
+// ------------------------------------------------------------------------------------------------
+RadarShroudCache::~RadarShroudCache()
+{
+ delete [] m_alpha;
+ m_alpha = NULL;
+}
+
+// ------------------------------------------------------------------------------------------------
+void RadarShroudCache::setSize( Int width, Int height )
+{
+ delete [] m_alpha;
+ m_alpha = NULL;
+ m_width = 0;
+ m_height = 0;
+ m_dirty = FALSE;
+ m_dirtyMinX = 0;
+ m_dirtyMinY = 0;
+ m_dirtyMaxX = -1;
+ m_dirtyMaxY = -1;
+
+ if( width <= 0 || height <= 0 )
+ return;
+
+ m_width = width;
+ m_height = height;
+ m_alpha = NEW UnsignedByte[ width * height ];
+
+ // the texture we shadow was just created and holds nothing we know about, so everything owes
+ // it a write
+ clear( 0 );
+}
+
+// ------------------------------------------------------------------------------------------------
+void RadarShroudCache::clear( UnsignedByte alpha )
+{
+ if( m_alpha == NULL )
+ return;
+
+ memset( m_alpha, alpha, m_width * m_height );
+
+ m_dirty = TRUE;
+ m_dirtyMinX = 0;
+ m_dirtyMinY = 0;
+ m_dirtyMaxX = m_width - 1;
+ m_dirtyMaxY = m_height - 1;
+}
+
+// ------------------------------------------------------------------------------------------------
+void RadarShroudCache::setAlpha( Int x, Int y, UnsignedByte alpha )
+{
+ if( m_alpha == NULL || x < 0 || y < 0 || x >= m_width || y >= m_height )
+ return;
+
+ UnsignedByte *cell = m_alpha + y * m_width + x;
+ if( *cell == alpha )
+ return;
+
+ *cell = alpha;
+ markDirty( x, y );
+}
+
+// ------------------------------------------------------------------------------------------------
+UnsignedByte RadarShroudCache::getAlpha( Int x, Int y ) const
+{
+ if( m_alpha == NULL || x < 0 || y < 0 || x >= m_width || y >= m_height )
+ return 0;
+
+ return m_alpha[ y * m_width + x ];
+}
+
+// ------------------------------------------------------------------------------------------------
+void RadarShroudCache::markDirty( Int x, Int y )
+{
+ if( m_dirty == FALSE )
+ {
+ m_dirty = TRUE;
+ m_dirtyMinX = m_dirtyMaxX = x;
+ m_dirtyMinY = m_dirtyMaxY = y;
+ return;
+ }
+
+ if( x < m_dirtyMinX ) m_dirtyMinX = x;
+ if( x > m_dirtyMaxX ) m_dirtyMaxX = x;
+ if( y < m_dirtyMinY ) m_dirtyMinY = y;
+ if( y > m_dirtyMaxY ) m_dirtyMaxY = y;
+}
+
+// ------------------------------------------------------------------------------------------------
+void RadarShroudCache::flushTo( void *bits, Int pitch, Int bytesPerPixel )
+{
+ if( m_alpha == NULL || bits == NULL || m_dirty == FALSE )
+ return;
+
+ /* The colour is GameMakeColor( 0, 0, 0, alpha ) - black, and the alpha in the top byte - and
+ it is written the way the surface wants it. Two-byte surfaces get the 4-bit alpha in the
+ top nibble; the DrawPixel this replaces masked the colour with 0xFFFF instead, which threw
+ the alpha away and left the radar shroud invisible. No machine we know of picks that
+ format for the shroud (A8R8G8B8 comes first and every Direct3D device supports it), so
+ this is a latent fix, not a visible change. */
+ for( Int y = m_dirtyMinY; y <= m_dirtyMaxY; y++ )
+ {
+ const UnsignedByte *src = m_alpha + y * m_width + m_dirtyMinX;
+ UnsignedByte *row = (UnsignedByte *)bits + y * pitch + m_dirtyMinX * bytesPerPixel;
+
+ for( Int x = m_dirtyMinX; x <= m_dirtyMaxX; x++, src++, row += bytesPerPixel )
+ {
+ switch( bytesPerPixel )
+ {
+ case 1:
+ *row = *src;
+ break;
+ case 2:
+ *(UnsignedShort *)row = (UnsignedShort)((*src >> 4) << 12);
+ break;
+ case 4:
+ *(UnsignedInt *)row = ((UnsignedInt)*src) << 24;
+ break;
+ }
+ }
+ }
+
+ m_dirty = FALSE;
+ m_dirtyMinX = 0;
+ m_dirtyMinY = 0;
+ m_dirtyMaxX = -1;
+ m_dirtyMaxY = -1;
+}
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/Common/W3DRadar.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/Common/W3DRadar.h
index 153108935..1362701ae 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/Common/W3DRadar.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/Common/W3DRadar.h
@@ -36,6 +36,7 @@
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "Common/Radar.h"
+#include "Common/RadarShroudCache.h"
#include "WW3D2/WW3DFormat.h"
// FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
@@ -102,6 +103,7 @@ protected:
WW3DFormat m_shroudTextureFormat; ///< format to use for shroud texture
Image *m_shroudImage; ///< shroud image abstraction for drawing
TextureClass *m_shroudTexture; ///< shroud texture
+ RadarShroudCache m_shroudCache; ///< what the shroud texture should say, in main memory
Int m_textureWidth; ///< width for all radar textures
Int m_textureHeight; ///< height for all radar textures
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp
index 7ce9098bc..e0e8ac101 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp
@@ -897,6 +897,9 @@ void W3DRadar::init( void )
m_shroudTexture->Get_Filter().Set_Min_Filter( TextureFilterClass::FILTER_TYPE_DEFAULT );
m_shroudTexture->Get_Filter().Set_Mag_Filter( TextureFilterClass::FILTER_TYPE_DEFAULT );
+ // the shroud is written in main memory and handed to that texture once a frame
+ m_shroudCache.setSize( m_textureWidth, m_textureHeight );
+
//
// create images used for rendering and set them up with the textures
//
@@ -1260,15 +1263,8 @@ void W3DRadar::clearShroud()
return;
#endif
- SurfaceClass *surface = m_shroudTexture->Get_Surface_Level();
-
// fill to clear, shroud will make black. Don't want to make something black that logic can't clear
- unsigned int color = GameMakeColor( 0, 0, 0, 0 );
- for( Int y = 0; y < m_textureHeight; y++ )
- {
- surface->DrawHLine(y, 0, m_textureWidth-1, color);
- }
- REF_PTR_RELEASE(surface);
+ m_shroudCache.clear( 0 );
}
// ------------------------------------------------------------------------------------------------
@@ -1284,9 +1280,6 @@ void W3DRadar::setShroudLevel(Int shroudX, Int shroudY, CellShroudStatus setting
if (!shroud)
return;
- SurfaceClass* surface = m_shroudTexture->Get_Surface_Level();
- DEBUG_ASSERTCRASH( surface, ("W3DRadar: Can't get surface for Shroud texture\n") );
-
Int mapMinX = shroudX * shroud->getCellWidth();
Int mapMinY = shroudY * shroud->getCellHeight();
Int mapMaxX = (shroudX+1) * shroud->getCellWidth();
@@ -1326,15 +1319,12 @@ void W3DRadar::setShroudLevel(Int shroudX, Int shroudY, CellShroudStatus setting
else
alpha = 0;
+ // this used to lock the shroud texture once per pixel, through DrawPixel, and it is called
+ // every time any unit's line of sight crosses a shroud cell boundary. The cache takes the
+ // writes and draw() hands the changed rectangle to the texture in one lock.
for( Int y = radarMinY; y <= radarMaxY; y++ )
- {
for( Int x = radarMinX; x <= radarMaxX; x++ )
- {
- if( legalRadarPoint( x, y ) )
- surface->DrawPixel( x, y, GameMakeColor( 0, 0, 0, alpha ) );
- }
- }
- REF_PTR_RELEASE(surface);
+ m_shroudCache.setAlpha( x, y, (UnsignedByte)alpha );
}
//-------------------------------------------------------------------------------------------------
@@ -1418,6 +1408,19 @@ void W3DRadar::draw( Int pixelX, Int pixelY, Int width, Int height )
if (true)
#endif
{
+ // hand the frame's shroud changes to the texture, one lock for all of them
+ if( m_shroudCache.isDirty() )
+ {
+ SurfaceClass *surface = m_shroudTexture->Get_Surface_Level();
+ SurfaceClass::SurfaceDescription sd;
+ surface->Get_Description( sd );
+ Int pitch;
+ void *bits = surface->Lock( &pitch );
+ m_shroudCache.flushTo( bits, pitch, Get_Bytes_Per_Pixel( sd.Format ) );
+ surface->Unlock();
+ REF_PTR_RELEASE(surface);
+ }
+
TheDisplay->drawImage( m_shroudImage, ul.x, ul.y, lr.x, lr.y );
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 28a62c2d8..01086ad73 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -45,6 +45,7 @@
#include <float.h>
#include "Common/AudioRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
+#include "Common/RadarShroudCache.h"
#include "GameNetwork/NetworkUtil.h"
#include "GameNetwork/NetCommandList.h"
#include "GameNetwork/GameInfo.h"
@@ -4060,3 +4061,198 @@ TEST(the_simulation_math_fingerprint_is_the_machines_math_and_not_the_callers_fp
setFPMode();
CHECK_EQ( getFPMode(), expectedFPMode() );
}
+
+// ------------------------------------------------------------------------------------------------
+// The radar's shroud layer. It used to be poked one pixel at a time straight into a Direct3D
+// texture, with a lock and an unlock around each pixel; it is a main-memory buffer now, and the
+// texture hears about it once a frame, over the rectangle that changed.
+
+TEST(radar_shroud_cache_starts_owing_the_whole_texture_a_write)
+{
+ RadarShroudCache cache;
+ cache.setSize( 128, 128 );
+
+ CHECK( cache.isDirty() );
+ CHECK_EQ( cache.getDirtyMinX(), 0 );
+ CHECK_EQ( cache.getDirtyMinY(), 0 );
+ CHECK_EQ( cache.getDirtyMaxX(), 127 );
+ CHECK_EQ( cache.getDirtyMaxY(), 127 );
+ CHECK_EQ( (Int)cache.getAlpha( 0, 0 ), 0 );
+ CHECK_EQ( (Int)cache.getAlpha( 127, 127 ), 0 );
+}
+
+TEST(radar_shroud_cache_grows_its_dirty_rectangle_around_what_changed)
+{
+ RadarShroudCache cache;
+ cache.setSize( 128, 128 );
+
+ // pretend the frame it was created in has been drawn
+ UnsignedByte surface[ 128 * 128 * 4 ];
+ cache.flushTo( surface, 128 * 4, 4 );
+ CHECK( !cache.isDirty() );
+
+ cache.setAlpha( 10, 20, 255 );
+ CHECK( cache.isDirty() );
+ CHECK_EQ( cache.getDirtyMinX(), 10 );
+ CHECK_EQ( cache.getDirtyMaxX(), 10 );
+ CHECK_EQ( cache.getDirtyMinY(), 20 );
+ CHECK_EQ( cache.getDirtyMaxY(), 20 );
+
+ // a second, far away pixel: the rectangle is the union, not the last one written
+ cache.setAlpha( 3, 90, 127 );
+ CHECK_EQ( cache.getDirtyMinX(), 3 );
+ CHECK_EQ( cache.getDirtyMaxX(), 10 );
+ CHECK_EQ( cache.getDirtyMinY(), 20 );
+ CHECK_EQ( cache.getDirtyMaxY(), 90 );
+
+ // and one inside it changes nothing about the bounds
+ cache.setAlpha( 5, 50, 1 );
+ CHECK_EQ( cache.getDirtyMinX(), 3 );
+ CHECK_EQ( cache.getDirtyMaxX(), 10 );
+ CHECK_EQ( cache.getDirtyMinY(), 20 );
+ CHECK_EQ( cache.getDirtyMaxY(), 90 );
+
+ CHECK_EQ( (Int)cache.getAlpha( 10, 20 ), 255 );
+ CHECK_EQ( (Int)cache.getAlpha( 3, 90 ), 127 );
+ CHECK_EQ( (Int)cache.getAlpha( 5, 50 ), 1 );
+}
+
+TEST(radar_shroud_cache_ignores_writes_that_change_nothing)
+{
+ RadarShroudCache cache;
+ cache.setSize( 128, 128 );
+ UnsignedByte surface[ 128 * 128 * 4 ];
+ cache.flushTo( surface, 128 * 4, 4 );
+
+ // the shroud is written cell by cell every time a unit moves, and most of those writes say
+ // what the pixel already said
+ cache.setAlpha( 40, 40, 0 );
+ CHECK( !cache.isDirty() );
+
+ cache.setAlpha( 40, 40, 255 );
+ CHECK( cache.isDirty() );
+ cache.flushTo( surface, 128 * 4, 4 );
+ cache.setAlpha( 40, 40, 255 );
+ CHECK( !cache.isDirty() );
+}
+
+TEST(radar_shroud_cache_drops_points_off_the_texture)
+{
+ RadarShroudCache cache;
+ cache.setSize( 128, 128 );
+ UnsignedByte surface[ 128 * 128 * 4 ];
+ cache.flushTo( surface, 128 * 4, 4 );
+
+ // the caller walks a rectangle of map cells and some of them fall outside the radar
+ cache.setAlpha( -1, 40, 255 );
+ cache.setAlpha( 40, -1, 255 );
+ cache.setAlpha( 128, 40, 255 );
+ cache.setAlpha( 40, 128, 255 );
+ cache.setAlpha( 10000, 10000, 255 );
+ CHECK( !cache.isDirty() );
+ CHECK_EQ( (Int)cache.getAlpha( -1, 40 ), 0 );
+ CHECK_EQ( (Int)cache.getAlpha( 128, 128 ), 0 );
+
+ // the last legal pixel is legal
+ cache.setAlpha( 127, 127, 255 );
+ CHECK( cache.isDirty() );
+ CHECK_EQ( (Int)cache.getAlpha( 127, 127 ), 255 );
+}
+
+TEST(radar_shroud_cache_writes_only_the_dirty_rectangle_into_the_surface)
+{
+ enum { W = 16, H = 8, PITCH = 128 }; // a pitch wider than the rows, like a real lock
+ RadarShroudCache cache;
+ cache.setSize( W, H );
+
+ UnsignedByte surface[ PITCH * H ];
+ memset( surface, 0xCD, sizeof( surface ) );
+ cache.flushTo( surface, PITCH, 4 ); // the opening flush covers everything
+ CHECK( !cache.isDirty() );
+
+ // past the end of each row nothing was touched
+ for( Int y = 0; y < H; y++ )
+ for( Int x = W * 4; x < PITCH; x++ )
+ CHECK_EQ( (Int)surface[ y * PITCH + x ], 0xCD );
+
+ memset( surface, 0xCD, sizeof( surface ) );
+ cache.setAlpha( 2, 3, 255 );
+ cache.setAlpha( 4, 5, 127 );
+ cache.flushTo( surface, PITCH, 4 );
+
+ // rows 3..5, columns 2..4, and not one byte more
+ Int written = 0;
+ for( Int y = 0; y < H; y++ )
+ {
+ for( Int x = 0; x < W; x++ )
+ {
+ UnsignedInt pixel;
+ memcpy( &pixel, surface + y * PITCH + x * 4, sizeof( pixel ) );
+ const Bool inside = ( y >= 3 && y <= 5 && x >= 2 && x <= 4 );
+ if( inside )
+ {
+ ++written;
+ CHECK_EQ( pixel, ((UnsignedInt)cache.getAlpha( x, y )) << 24 );
+ }
+ else
+ {
+ CHECK_EQ( pixel, 0xCDCDCDCD );
+ }
+ }
+ }
+ CHECK_EQ( written, 9 );
+ CHECK( !cache.isDirty() );
+
+ // and a flush with nothing to say does not touch the surface at all
+ memset( surface, 0xCD, sizeof( surface ) );
+ cache.flushTo( surface, PITCH, 4 );
+ for( Int i = 0; i < (Int)sizeof( surface ); i++ )
+ CHECK_EQ( (Int)surface[ i ], 0xCD );
+}
+
+TEST(radar_shroud_cache_puts_the_alpha_where_the_pixel_format_wants_it)
+{
+ enum { W = 4, H = 2 };
+ RadarShroudCache cache;
+ cache.setSize( W, H );
+ cache.setAlpha( 1, 1, 255 );
+ cache.setAlpha( 2, 1, 127 );
+
+ // eight bits of alpha, in the top byte, black underneath: GameMakeColor( 0, 0, 0, alpha )
+ UnsignedInt wide[ W * H ];
+ memset( wide, 0, sizeof( wide ) );
+ cache.flushTo( wide, W * 4, 4 );
+ CHECK_EQ( wide[ 1 * W + 1 ], 0xFF000000 );
+ CHECK_EQ( wide[ 1 * W + 2 ], 0x7F000000 );
+ CHECK_EQ( wide[ 0 * W + 0 ], 0u );
+
+ // four bits of it, in the top nibble. The DrawPixel this replaces masked the colour with
+ // 0xFFFF here and threw the whole alpha away.
+ cache.clear( 0 );
+ cache.setAlpha( 1, 1, 255 );
+ cache.setAlpha( 2, 1, 127 );
+ UnsignedShort narrow[ W * H ];
+ memset( narrow, 0, sizeof( narrow ) );
+ cache.flushTo( narrow, W * 2, 2 );
+ CHECK_EQ( (Int)narrow[ 1 * W + 1 ], 0xF000 );
+ CHECK_EQ( (Int)narrow[ 1 * W + 2 ], 0x7000 );
+ CHECK_EQ( (Int)narrow[ 0 * W + 0 ], 0 );
+}
+
+TEST(radar_shroud_cache_clear_owes_the_whole_texture_a_write_again)
+{
+ RadarShroudCache cache;
+ cache.setSize( 128, 128 );
+ UnsignedByte surface[ 128 * 128 * 4 ];
+ cache.flushTo( surface, 128 * 4, 4 );
+ cache.setAlpha( 60, 60, 255 );
+ cache.flushTo( surface, 128 * 4, 4 );
+
+ cache.clear( 0 );
+ CHECK( cache.isDirty() );
+ CHECK_EQ( cache.getDirtyMinX(), 0 );
+ CHECK_EQ( cache.getDirtyMinY(), 0 );
+ CHECK_EQ( cache.getDirtyMaxX(), 127 );
+ CHECK_EQ( cache.getDirtyMaxY(), 127 );
+ CHECK_EQ( (Int)cache.getAlpha( 60, 60 ), 0 );
+}