- use a TArray to pass the screenshot buffer

This also removes a few other explicit buffer allocations.
This commit is contained in:
Christoph Oelckers 2018-12-21 09:21:40 +01:00
parent b3d6dfb55f
commit cf18dbdfa7
6 changed files with 18 additions and 28 deletions

View file

@ -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. // 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(); auto numsectors = level.sectors.Size();
int *sectorrecord = new int[numsectors]; TArray<int> sectorrecord(numsectors, true);
memset(sectorrecord, -1, numsectors * sizeof(int)); memset(sectorrecord.Data(), -1, numsectors * sizeof(int));
for (auto &line : level.lines) for (auto &line : level.lines)
{ {
if (line.special == Static_Init && line.args[1] == Init_EDSector) if (line.special == Static_Init && line.args[1] == Init_EDSector)
@ -773,7 +773,6 @@ void ProcessEDSectors()
ProcessEDSector(&level.sectors[i], sectorrecord[i]); ProcessEDSector(&level.sectors[i], sectorrecord[i]);
} }
} }
delete[] sectorrecord;
} }
void LoadMapinfoACSLump() void LoadMapinfoACSLump()

View file

@ -2829,16 +2829,15 @@ bool G_CheckDemoStatus (void)
// uncompressed size of the BODY. // uncompressed size of the BODY.
uLong len = uLong(demo_p - demobodyspot); uLong len = uLong(demo_p - demobodyspot);
uLong outlen = (len + len/100 + 12); uLong outlen = (len + len/100 + 12);
Byte *compressed = new Byte[outlen]; TArray<Byte> compressed(outlen, true);
int r = compress2 (compressed, &outlen, demobodyspot, len, 9); int r = compress2 (compressed.Data(), &outlen, demobodyspot, len, 9);
if (r == Z_OK && outlen < len) if (r == Z_OK && outlen < len)
{ {
formlen = democompspot; formlen = democompspot;
WriteLong (len, &democompspot); WriteLong (len, &democompspot);
memcpy (demobodyspot, compressed, outlen); memcpy (demobodyspot, compressed.Data(), outlen);
demo_p = demobodyspot + outlen; demo_p = demobodyspot + outlen;
} }
delete[] compressed;
} }
FinishChunk (&demo_p); FinishChunk (&demo_p);
formlen = demobuffer + 4; formlen = demobuffer + 4;

View file

@ -425,7 +425,7 @@ void OpenGLFrameBuffer::BeginFrame()
// //
//=========================================================================== //===========================================================================
void OpenGLFrameBuffer::GetScreenshotBuffer(const uint8_t *&buffer, int &pitch, ESSType &color_type, float &gamma) TArray<uint8_t> OpenGLFrameBuffer::GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma)
{ {
const auto &viewport = mOutputLetterbox; const auto &viewport = mOutputLetterbox;
@ -441,7 +441,7 @@ void OpenGLFrameBuffer::GetScreenshotBuffer(const uint8_t *&buffer, int &pitch,
int w = SCREENWIDTH; int w = SCREENWIDTH;
int h = SCREENHEIGHT; int h = SCREENHEIGHT;
auto ScreenshotBuffer = new uint8_t[w * h * 3]; TArray<uint8_t> ScreenshotBuffer(w * h * 3, true);
float rcpWidth = 1.0f / w; float rcpWidth = 1.0f / w;
float rcpHeight = 1.0f / h; float rcpHeight = 1.0f / h;
@ -463,10 +463,10 @@ void OpenGLFrameBuffer::GetScreenshotBuffer(const uint8_t *&buffer, int &pitch,
pitch = w * 3; pitch = w * 3;
color_type = SS_RGB; color_type = SS_RGB;
buffer = ScreenshotBuffer;
// Screenshot should not use gamma correction if it was already applied to rendered image // 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; gamma = 1 == vid_hwgamma || (2 == vid_hwgamma && !fullscreen) ? 1.0f : Gamma;
return ScreenshotBuffer;
} }
//=========================================================================== //===========================================================================

View file

@ -48,7 +48,7 @@ public:
// Retrieves a buffer containing image data for a screenshot. // Retrieves a buffer containing image data for a screenshot.
// Hint: Pitch can be negative for upside-down images, in which case buffer // 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. // 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<uint8_t> GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma) override;
void Swap(); void Swap();
bool IsHWGammaActive() const { return HWGammaActive; } bool IsHWGammaActive() const { return HWGammaActive; }

View file

@ -96,7 +96,7 @@ void M_FindResponseFile (void)
else else
{ {
char **argv; char **argv;
char *file = NULL; TArray<uint8_t> file;
int argc = 0; int argc = 0;
int size; int size;
long argsize = 0; long argsize = 0;
@ -116,10 +116,9 @@ void M_FindResponseFile (void)
{ {
Printf ("Found response file %s!\n", Args->GetArg(i) + 1); Printf ("Found response file %s!\n", Args->GetArg(i) + 1);
size = (int)fr.GetLength(); size = (int)fr.GetLength();
file = new char[size+1]; file = fr.Read (size);
fr.Read (file, size);
file[size] = 0; file[size] = 0;
argsize = ParseCommandLine (file, &argc, NULL); argsize = ParseCommandLine ((char*)file.Data(), &argc, NULL);
} }
} }
else else
@ -131,7 +130,7 @@ void M_FindResponseFile (void)
{ {
argv = (char **)M_Malloc (argc*sizeof(char *) + argsize); argv = (char **)M_Malloc (argc*sizeof(char *) + argsize);
argv[0] = (char *)argv + argc*sizeof(char *); argv[0] = (char *)argv + argc*sizeof(char *);
ParseCommandLine (file, NULL, argv); ParseCommandLine ((char*)file.Data(), NULL, argv);
// Create a new argument vector // Create a new argument vector
FArgs *newargs = new FArgs; FArgs *newargs = new FArgs;
@ -161,10 +160,6 @@ void M_FindResponseFile (void)
// Remove the response file from the Args object // Remove the response file from the Args object
Args->RemoveArg(i); Args->RemoveArg(i);
} }
if (file != NULL)
{
delete[] file;
}
} }
} }
if (added_stuff > 0) if (added_stuff > 0)
@ -609,13 +604,12 @@ void M_ScreenShot (const char *filename)
} }
// save the screenshot // save the screenshot
const uint8_t *buffer;
int pitch; int pitch;
ESSType color_type; ESSType color_type;
float gamma; float gamma;
screen->GetScreenshotBuffer(buffer, pitch, color_type, gamma); auto buffer = screen->GetScreenshotBuffer(pitch, color_type, gamma);
if (buffer != NULL) if (buffer.Size() > 0)
{ {
PalEntry palette[256]; PalEntry palette[256];
@ -627,21 +621,19 @@ void M_ScreenShot (const char *filename)
if (file == NULL) if (file == NULL)
{ {
Printf ("Could not open %s\n", autoname.GetChars()); Printf ("Could not open %s\n", autoname.GetChars());
delete[] buffer;
return; return;
} }
if (writepcx) if (writepcx)
{ {
WritePCXfile(file, buffer, palette, color_type, WritePCXfile(file, buffer.Data(), palette, color_type,
screen->GetWidth(), screen->GetHeight(), pitch); screen->GetWidth(), screen->GetHeight(), pitch);
} }
else else
{ {
WritePNGfile(file, buffer, palette, color_type, WritePNGfile(file, buffer.Data(), palette, color_type,
screen->GetWidth(), screen->GetHeight(), pitch, gamma); screen->GetWidth(), screen->GetHeight(), pitch, gamma);
} }
delete file; delete file;
delete[] buffer;
if (!screenshot_quiet) if (!screenshot_quiet)
{ {

View file

@ -553,7 +553,7 @@ public:
// Retrieves a buffer containing image data for a screenshot. // Retrieves a buffer containing image data for a screenshot.
// Hint: Pitch can be negative for upside-down images, in which case buffer // 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. // 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<uint8_t> GetScreenshotBuffer(int &pitch, ESSType &color_type, float &gamma) {}
static float GetZNear() { return 5.f; } static float GetZNear() { return 5.f; }
static float GetZFar() { return 65536.f; } static float GetZFar() { return 65536.f; }