From 98713caf4ba10647df482b1a5bcccc7f3d372501 Mon Sep 17 00:00:00 2001 From: rogerman Date: Sat, 15 Feb 2014 06:40:55 +0000 Subject: [PATCH] Cocoa Port: - Fix potential crashing bug when changing the video filter from None to a different video filter with 1x scaling. --- desmume/src/cocoa/OGLDisplayOutput.cpp | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/desmume/src/cocoa/OGLDisplayOutput.cpp b/desmume/src/cocoa/OGLDisplayOutput.cpp index adfbe110a..162ded95e 100644 --- a/desmume/src/cocoa/OGLDisplayOutput.cpp +++ b/desmume/src/cocoa/OGLDisplayOutput.cpp @@ -370,34 +370,30 @@ void OGLVideoOutput::SetVideoFilterOGL(const VideoFilterTypeID videoFilterTypeID const GLsizei vfDstWidth = this->_vf->GetDstWidth(); const GLsizei vfDstHeight = (this->_displayMode == DS_DISPLAY_TYPE_DUAL) ? this->_vf->GetDstHeight() : this->_vf->GetDstHeight() * 2; - - size_t colorDepth = sizeof(uint32_t); - this->_glTexPixelFormat = GL_UNSIGNED_INT_8_8_8_8_REV; - - if (videoFilterTypeID == VideoFilterTypeID_None) - { - colorDepth = sizeof(uint16_t); - this->_glTexPixelFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV; - } + const GLenum newPixFormat = (videoFilterTypeID == VideoFilterTypeID_None) ? GL_UNSIGNED_SHORT_1_5_5_5_REV : GL_UNSIGNED_INT_8_8_8_8_REV; // Convert textures to Power-of-Two to support older GPUs // Example: Radeon X1600M on the 2006 MacBook Pro const GLsizei potW = GetNearestPositivePOT((uint32_t)vfDstWidth); const GLsizei potH = GetNearestPositivePOT((uint32_t)vfDstHeight); + const size_t colorDepth = (newPixFormat == GL_UNSIGNED_SHORT_1_5_5_5_REV) ? sizeof(uint16_t) : sizeof(uint32_t); + const size_t texBackSize = potW * potH * colorDepth; - if (this->_glTexBackWidth != potW || this->_glTexBackHeight != potH) + if (this->_glTexBackWidth != potW || this->_glTexBackHeight != potH || this->_glTexPixelFormat != newPixFormat) { this->_glTexBackWidth = potW; this->_glTexBackHeight = potH; + this->_glTexPixelFormat = newPixFormat; - free(this->_glTexBack); - this->_glTexBack = (GLvoid *)calloc((size_t)potW * (size_t)potH, colorDepth); + this->_glTexBack = (GLvoid *)realloc(this->_glTexBack, texBackSize); if (this->_glTexBack == NULL) { return; } } + memset(this->_glTexBack, 0, texBackSize); + const GLfloat s = (GLfloat)vfDstWidth / (GLfloat)potW; const GLfloat t = (GLfloat)vfDstHeight / (GLfloat)potH; this->UpdateTexCoords(s, t);