* Add util functions or zlib inflation/deflation.

This commit is contained in:
iProgramInCpp
2023-08-12 13:53:35 +03:00
parent 1918bf2a7e
commit 944eb3ace4
4 changed files with 86 additions and 7 deletions

View File

@@ -11,7 +11,7 @@ PLT_DIR=platforms
TARGET=minecraftcpp
# Compilation flags for C++ source files
CXXFLAGS=-Isource -I. -Ithirdparty/raknet -DUSE_SDL -DUSE_OPENAL -DUSE_MATH_DEFINES -DHANDLE_CHARS_SEPARATELY -O3 -MMD
CXXFLAGS=-Isource -I. -Ithirdparty/raknet -Ithirdparty/zlib -DUSE_SDL -DUSE_OPENAL -DUSE_MATH_DEFINES -DHANDLE_CHARS_SEPARATELY -O3 -MMD
# Compilation flags for zlib source files
ZLIBFLAGS=-O3 -I. -MMD

View File

@@ -33,6 +33,9 @@
#include "compat/GL.hpp"
// include zlib stuff
#include "zlib.h"
int g_TimeSecondsOnInit = 0;
#ifndef USE_SDL
@@ -330,3 +333,75 @@ float Max(float a, float b)
a = b;
return a;
}
// zlib stuff
uint8_t* ZlibInflateToMemory(uint8_t* pInput, size_t compressedSize, size_t decompressedSize)
{
int ret;
z_stream strm;
memset(&strm, 0, sizeof strm);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// initialize the inflation state machine
ret = inflateInit(&strm);
if (ret != Z_OK)
return nullptr;
uint8_t* pDestBuff = new uint8_t[decompressedSize + 1]; //room for extra null at the end;
pDestBuff[decompressedSize] = 0; //add the extra null, if we decompressed a text file this can be useful
strm.avail_in = compressedSize;
strm.next_in = pInput;
strm.avail_out = decompressedSize;
strm.next_out = pDestBuff;
ret = inflate(&strm, Z_NO_FLUSH);
if (!(ret == Z_OK || ret == Z_STREAM_END))
{
SAFE_DELETE_ARRAY(pDestBuff);
return nullptr;
}
(void)inflateEnd(&strm);
return pDestBuff;
}
uint8_t* ZlibDeflateToMemory(uint8_t* pInput, size_t sizeBytes, size_t* compressedSizeOut)
{
z_stream strm;
memset(&strm, 0, sizeof strm);
int ret;
// initialize deflate state
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
// initialize deflation state machine
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK)
return nullptr;
// padding bytes in case our compressed output is larger than the raw input for some reason
const int ZLIB_PADDING_BYTES = (1024 * 5);
uint8_t* pOut = new uint8_t[sizeBytes + ZLIB_PADDING_BYTES];
strm.avail_in = sizeBytes;
strm.next_in = pInput;
strm.avail_in = sizeBytes + ZLIB_PADDING_BYTES;
strm.next_out = pOut;
ret = deflate(&strm, Z_FINISH);
assert(ret != Z_STREAM_ERROR);
deflateEnd(&strm);
*compressedSizeOut = strm.total_out;
return pOut;
}

View File

@@ -564,6 +564,10 @@ constexpr float Lerp(float a, float b, float progress)
bool createFolderIfNotExists(const char* pDir);
bool DeleteDirectory(const std::string& name, bool unused);
// compress and decompress stuff with zlib: ( you must SAFE_DELETE_ARRAY what it returns )
uint8_t* ZlibInflateToMemory(uint8_t* pInput, size_t compressedSize, size_t decompressedSize);
uint8_t* ZlibDeflateToMemory(uint8_t* pInput, size_t sizeBytes, size_t *compressedSizeOut);
// things that we added:
#ifndef ORIGINAL_CODE

View File

@@ -802,32 +802,32 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/zlib;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Configuration)\</OutDir>
<IntDir>obj\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugAsan|Win32'">
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/zlib;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Configuration)\</OutDir>
<IntDir>obj\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/zlib;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Configuration)\</OutDir>
<IntDir>obj\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/zlib;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugAsan|x64'">
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/zlib;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<IncludePath>$(ProjectDir)/../thirdparty;$(ProjectDir)/../platforms/windows;$(ProjectDir)/../thirdparty/zlib;$(ProjectDir)/../thirdparty/raknet;$(ProjectDir);$(ProjectDir)/..;$(ProjectDir)/../source;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>