From cf18dbdfa7f976470f474989f72ae9ae6ed8786f Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 21 Dec 2018 09:21:40 +0100 Subject: [PATCH] - use a TArray to pass the screenshot buffer This also removes a few other explicit buffer allocations. --- src/edata.cpp | 5 ++--- src/g_game.cpp | 7 +++---- src/gl/system/gl_framebuffer.cpp | 6 +++--- src/gl/system/gl_framebuffer.h | 2 +- src/m_misc.cpp | 24 ++++++++---------------- src/v_video.h | 2 +- 6 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/edata.cpp b/src/edata.cpp index 41aa61a2f..363a65d8c 100644 --- a/src/edata.cpp +++ b/src/edata.cpp @@ -756,8 +756,8 @@ void ProcessEDSectors() // collect all Extradata sector records up front so we do not need to search the complete line array for each sector separately. auto numsectors = level.sectors.Size(); - int *sectorrecord = new int[numsectors]; - memset(sectorrecord, -1, numsectors * sizeof(int)); + TArray sectorrecord(numsectors, true); + memset(sectorrecord.Data(), -1, numsectors * sizeof(int)); for (auto &line : level.lines) { if (line.special == Static_Init && line.args[1] == Init_EDSector) @@ -773,7 +773,6 @@ void ProcessEDSectors() ProcessEDSector(&level.sectors[i], sectorrecord[i]); } } - delete[] sectorrecord; } void LoadMapinfoACSLump() diff --git a/src/g_game.cpp b/src/g_game.cpp index 56ccd46b2..a1c6f8304 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -2829,16 +2829,15 @@ bool G_CheckDemoStatus (void) // uncompressed size of the BODY. uLong len = uLong(demo_p - demobodyspot); uLong outlen = (len + len/100 + 12); - Byte *compressed = new Byte[outlen]; - int r = compress2 (compressed, &outlen, demobodyspot, len, 9); + TArray compressed(outlen, true); + int r = compress2 (compressed.Data(), &outlen, demobodyspot, len, 9); if (r == Z_OK && outlen < len) { formlen = democompspot; WriteLong (len, &democompspot); - memcpy (demobodyspot, compressed, outlen); + memcpy (demobodyspot, compressed.Data(), outlen); demo_p = demobodyspot + outlen; } - delete[] compressed; } FinishChunk (&demo_p); formlen = demobuffer + 4; diff --git a/src/gl/system/gl_framebuffer.cpp b/src/gl/system/gl_framebuffer.cpp index 08a9cc030..3c6e9e25b 100644 --- a/src/gl/system/gl_framebuffer.cpp +++ b/src/gl/system/gl_framebuffer.cpp @@ -425,7 +425,7 @@ void OpenGLFrameBuffer::BeginFrame() // //=========================================================================== -void OpenGLFrameBuffer::GetScreenshotBuffer(const uint8_t *&buffer, int &pitch, ESSType &color_type, float &gamma) +TArray OpenGLFrameBuffer::GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma) { const auto &viewport = mOutputLetterbox; @@ -441,7 +441,7 @@ void OpenGLFrameBuffer::GetScreenshotBuffer(const uint8_t *&buffer, int &pitch, int w = SCREENWIDTH; int h = SCREENHEIGHT; - auto ScreenshotBuffer = new uint8_t[w * h * 3]; + TArray ScreenshotBuffer(w * h * 3, true); float rcpWidth = 1.0f / w; float rcpHeight = 1.0f / h; @@ -463,10 +463,10 @@ void OpenGLFrameBuffer::GetScreenshotBuffer(const uint8_t *&buffer, int &pitch, pitch = w * 3; color_type = SS_RGB; - buffer = ScreenshotBuffer; // Screenshot should not use gamma correction if it was already applied to rendered image gamma = 1 == vid_hwgamma || (2 == vid_hwgamma && !fullscreen) ? 1.0f : Gamma; + return ScreenshotBuffer; } //=========================================================================== diff --git a/src/gl/system/gl_framebuffer.h b/src/gl/system/gl_framebuffer.h index c77b3af67..94467118b 100644 --- a/src/gl/system/gl_framebuffer.h +++ b/src/gl/system/gl_framebuffer.h @@ -48,7 +48,7 @@ public: // Retrieves a buffer containing image data for a screenshot. // Hint: Pitch can be negative for upside-down images, in which case buffer // points to the last row in the buffer, which will be the first row output. - virtual void GetScreenshotBuffer(const uint8_t *&buffer, int &pitch, ESSType &color_type, float &gamma) override; + virtual TArray GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma) override; void Swap(); bool IsHWGammaActive() const { return HWGammaActive; } diff --git a/src/m_misc.cpp b/src/m_misc.cpp index 3aa2df4d4..392ba670d 100644 --- a/src/m_misc.cpp +++ b/src/m_misc.cpp @@ -96,7 +96,7 @@ void M_FindResponseFile (void) else { char **argv; - char *file = NULL; + TArray file; int argc = 0; int size; long argsize = 0; @@ -116,10 +116,9 @@ void M_FindResponseFile (void) { Printf ("Found response file %s!\n", Args->GetArg(i) + 1); size = (int)fr.GetLength(); - file = new char[size+1]; - fr.Read (file, size); + file = fr.Read (size); file[size] = 0; - argsize = ParseCommandLine (file, &argc, NULL); + argsize = ParseCommandLine ((char*)file.Data(), &argc, NULL); } } else @@ -131,7 +130,7 @@ void M_FindResponseFile (void) { argv = (char **)M_Malloc (argc*sizeof(char *) + argsize); argv[0] = (char *)argv + argc*sizeof(char *); - ParseCommandLine (file, NULL, argv); + ParseCommandLine ((char*)file.Data(), NULL, argv); // Create a new argument vector FArgs *newargs = new FArgs; @@ -161,10 +160,6 @@ void M_FindResponseFile (void) // Remove the response file from the Args object Args->RemoveArg(i); } - if (file != NULL) - { - delete[] file; - } } } if (added_stuff > 0) @@ -609,13 +604,12 @@ void M_ScreenShot (const char *filename) } // save the screenshot - const uint8_t *buffer; int pitch; ESSType color_type; float gamma; - screen->GetScreenshotBuffer(buffer, pitch, color_type, gamma); - if (buffer != NULL) + auto buffer = screen->GetScreenshotBuffer(pitch, color_type, gamma); + if (buffer.Size() > 0) { PalEntry palette[256]; @@ -627,21 +621,19 @@ void M_ScreenShot (const char *filename) if (file == NULL) { Printf ("Could not open %s\n", autoname.GetChars()); - delete[] buffer; return; } if (writepcx) { - WritePCXfile(file, buffer, palette, color_type, + WritePCXfile(file, buffer.Data(), palette, color_type, screen->GetWidth(), screen->GetHeight(), pitch); } else { - WritePNGfile(file, buffer, palette, color_type, + WritePNGfile(file, buffer.Data(), palette, color_type, screen->GetWidth(), screen->GetHeight(), pitch, gamma); } delete file; - delete[] buffer; if (!screenshot_quiet) { diff --git a/src/v_video.h b/src/v_video.h index 216fa2d31..47e4ba919 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -553,7 +553,7 @@ public: // Retrieves a buffer containing image data for a screenshot. // Hint: Pitch can be negative for upside-down images, in which case buffer // points to the last row in the buffer, which will be the first row output. - virtual void GetScreenshotBuffer(const uint8_t *&buffer, int &pitch, ESSType &color_type, float &gamma) {} + virtual TArray GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma) {} static float GetZNear() { return 5.f; } static float GetZFar() { return 65536.f; }