diff --git a/Core/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp b/Core/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp index b10117084cc..9f84cd2c61c 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp @@ -698,6 +698,17 @@ Render2DSentenceClass::Allocate_New_Surface (const WCHAR *text, bool justCalcExt // CurSurface = NEW_REF (SurfaceClass, (CurrTextureSize, CurrTextureSize, WW3D_FORMAT_A4R4G4B4)); WWASSERT (CurSurface != NULL); + + // + // Validate that the underlying D3D surface was successfully created + // + if (CurSurface != NULL && !CurSurface->Is_Valid()) { + // Surface creation failed - clean up and return + REF_PTR_RELEASE(CurSurface); + CurSurface = NULL; + return; + } + CurSurface->Add_Ref (); // @@ -931,7 +942,15 @@ void Render2DSentenceClass::Build_Sentence_Centered (const WCHAR *text, int *hkX // if (LockedPtr == NULL) { LockedPtr = (uint16 *)CurSurface->Lock (&LockedStride); - WWASSERT (LockedPtr != NULL); + // If Lock() failed (returned NULL), we cannot render text - abort gracefully + if (LockedPtr == NULL) { + // Stop processing - cannot continue without a valid surface + if(hkX) + *hkX = hotKeyPosX; + if(hkX) + *hkY = hotKeyPosY; + return; + } } // @@ -1121,7 +1140,21 @@ Vector2 Render2DSentenceClass::Build_Sentence_Not_Centered (const WCHAR *text, i { if (LockedPtr == NULL) { LockedPtr = (uint16 *)CurSurface->Lock (&LockedStride); - WWASSERT (LockedPtr != NULL); + // If Lock() failed (returned NULL), we cannot render text - abort gracefully + if (LockedPtr == NULL) { + // Return the extent we've calculated so far + Vector2 extent; + extent.X = maxX + Font->Get_Extra_Overlap(); + extent.Y = Cursor.Y + char_height; + Cursor = cursor; + TextureOffset = textureOffset; + TextureStartX = textureStartX; + if(hkX) + *hkX = hotKeyPosX; + if(hkX) + *hkY = hotKeyPosY; + return extent; + } } } diff --git a/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp b/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp index ae112b257bb..1efcbe8228c 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp +++ b/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp @@ -249,6 +249,14 @@ SurfaceClass::~SurfaceClass(void) void SurfaceClass::Get_Description(SurfaceDescription &surface_desc) { + // Check if the D3D surface is valid before attempting to get description + if (D3DSurface == NULL) { + surface_desc.Format = WW3D_FORMAT_UNKNOWN; + surface_desc.Height = 0; + surface_desc.Width = 0; + return; + } + D3DSURFACE_DESC d3d_desc; ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC)); DX8_ErrorCode(D3DSurface->GetDesc(&d3d_desc)); @@ -259,6 +267,12 @@ void SurfaceClass::Get_Description(SurfaceDescription &surface_desc) void * SurfaceClass::Lock(int * pitch) { + // Check if the D3D surface is valid before attempting to lock + if (D3DSurface == NULL) { + if (pitch) *pitch = 0; + return NULL; + } + D3DLOCKED_RECT lock_rect; ::ZeroMemory(&lock_rect, sizeof(D3DLOCKED_RECT)); DX8_ErrorCode(D3DSurface->LockRect(&lock_rect, 0, 0)); @@ -268,6 +282,10 @@ void * SurfaceClass::Lock(int * pitch) void SurfaceClass::Unlock(void) { + // Check if the D3D surface is valid before attempting to unlock + if (D3DSurface == NULL) { + return; + } DX8_ErrorCode(D3DSurface->UnlockRect()); } @@ -288,6 +306,11 @@ void SurfaceClass::Unlock(void) *=============================================================================================*/ void SurfaceClass::Clear() { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -327,6 +350,11 @@ void SurfaceClass::Clear() *=============================================================================================*/ void SurfaceClass::Copy(const unsigned char *other) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -366,6 +394,11 @@ void SurfaceClass::Copy(const unsigned char *other) *=============================================================================================*/ void SurfaceClass::Copy(Vector2i &min,Vector2i &max, const unsigned char *other) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -411,6 +444,14 @@ void SurfaceClass::Copy(Vector2i &min,Vector2i &max, const unsigned char *other) *=============================================================================================*/ unsigned char *SurfaceClass::CreateCopy(int *width,int *height,int*size,bool flip) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + *width = 0; + *height = 0; + *size = 0; + return NULL; + } + SurfaceDescription sd; Get_Description(sd); @@ -472,6 +513,11 @@ void SurfaceClass::Copy( WWASSERT(width); WWASSERT(height); + // Check if either D3D surface is invalid + if (D3DSurface == NULL || other->D3DSurface == NULL) { + return; + } + SurfaceDescription sd,osd; Get_Description(sd); const_cast (other)->Get_Description(osd); @@ -529,6 +575,11 @@ void SurfaceClass::Stretch_Copy( { WWASSERT(other); + // Check if either D3D surface is invalid + if (D3DSurface == NULL || other->D3DSurface == NULL) { + return; + } + SurfaceDescription sd,osd; Get_Description(sd); const_cast (other)->Get_Description(osd); @@ -565,6 +616,11 @@ void SurfaceClass::Stretch_Copy( *=============================================================================================*/ void SurfaceClass::FindBB(Vector2i *min,Vector2i*max) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -640,6 +696,11 @@ void SurfaceClass::FindBB(Vector2i *min,Vector2i*max) *=============================================================================================*/ bool SurfaceClass::Is_Transparent_Column(unsigned int column) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return true; + } + SurfaceDescription sd; Get_Description(sd); @@ -708,6 +769,12 @@ bool SurfaceClass::Is_Transparent_Column(unsigned int column) *=============================================================================================*/ void SurfaceClass::Get_Pixel(Vector3 &rgb, int x,int y) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + rgb.Set(0, 0, 0); + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -805,6 +872,11 @@ void SurfaceClass::Detach (void) *=============================================================================================*/ void SurfaceClass::DrawPixel(const unsigned int x,const unsigned int y, unsigned int color) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -859,6 +931,11 @@ void SurfaceClass::DrawPixel(const unsigned int x,const unsigned int y, unsigned *=============================================================================================*/ void SurfaceClass::DrawHLine(const unsigned int y,const unsigned int x1, const unsigned int x2, unsigned int color) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + SurfaceDescription sd; Get_Description(sd); @@ -918,6 +995,11 @@ void SurfaceClass::DrawHLine(const unsigned int y,const unsigned int x1, const u *=============================================================================================*/ bool SurfaceClass::Is_Monochrome(void) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return false; + } + unsigned int x,y; SurfaceDescription sd; Get_Description(sd); @@ -1008,6 +1090,11 @@ bool SurfaceClass::Is_Monochrome(void) *=============================================================================================*/ void SurfaceClass::Hue_Shift(const Vector3 &hsv_shift) { + // Check if the D3D surface is valid + if (D3DSurface == NULL) { + return; + } + unsigned int x,y; SurfaceDescription sd; Get_Description(sd); diff --git a/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.h b/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.h index de3b7000576..b6b4ca7b7cc 100644 --- a/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.h +++ b/Core/Libraries/Source/WWVegas/WW3D2/surfaceclass.h @@ -140,6 +140,9 @@ class SurfaceClass : public W3DMPO, public RefCountClass WW3DFormat Get_Surface_Format() const { return SurfaceFormat; } + // Check if the underlying D3D surface is valid + bool Is_Valid() const { return D3DSurface != NULL; } + private: // Direct3D surface object