mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-10-04 13:27:14 +00:00
[vfs, #2] Move around some files (cleanup only)
This commit is contained in:
parent
a90f57afb0
commit
1709503344
8 changed files with 523 additions and 660 deletions
|
@ -1,476 +0,0 @@
|
|||
#ifndef BYTEBUFFER_H
|
||||
#define BYTEBUFFER_H
|
||||
|
||||
#include <string.h> // for memcpy
|
||||
|
||||
|
||||
// ** compatibility stuff for BBGE .... **
|
||||
|
||||
#include "Base.h"
|
||||
|
||||
#define BYTEBUFFER_NO_EXCEPTIONS
|
||||
|
||||
#if (defined(BBGE_BUILD_SDL) && (SDL_BYTEORDER == SDL_BIG_ENDIAN))
|
||||
# define BB_IS_BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
// ****
|
||||
|
||||
|
||||
namespace ByteBufferTools
|
||||
{
|
||||
template<size_t T> inline void convert(char *val)
|
||||
{
|
||||
std::swap(*val, *(val + T - 1));
|
||||
convert<T - 2>(val + 1);
|
||||
}
|
||||
template<> inline void convert<0>(char *) {}
|
||||
template<> inline void convert<1>(char *) {}
|
||||
|
||||
template<typename T> inline void EndianConvert(T *val)
|
||||
{
|
||||
convert<sizeof(T)>((char *)(val));
|
||||
}
|
||||
|
||||
#if BB_IS_BIG_ENDIAN
|
||||
template<typename T> inline void ToLittleEndian(T& val) { EndianConvert<T>(&val); }
|
||||
template<typename T> inline void ToBigEndian(T&) { }
|
||||
#else
|
||||
template<typename T> inline void ToLittleEndian(T&) { }
|
||||
template<typename T> inline void ToBigEndian(T& val) { EndianConvert<T>(&val); }
|
||||
#endif
|
||||
|
||||
template<typename T> void ToLittleEndian(T*); // will generate link error
|
||||
template<typename T> void ToBigEndian(T*); // will generate link error
|
||||
|
||||
};
|
||||
|
||||
#define BB_MAKE_WRITE_OP(T) inline ByteBuffer& operator<<(T val) { append<T>(val); return *this; }
|
||||
#define BB_MAKE_READ_OP(T) inline ByteBuffer& operator>>(T &val) { val = read<T>(); return *this; }
|
||||
|
||||
class ByteBuffer
|
||||
{
|
||||
public:
|
||||
typedef void (*delete_func)(void*);
|
||||
typedef void *(*allocator_func)(size_t);
|
||||
|
||||
enum Mode // for creation with existing pointers
|
||||
{
|
||||
COPY, //- Make a copy of the buffer (default action).
|
||||
REUSE, //- Use the passed-in buffer as is. Requires the pointer
|
||||
// to remain valid over the life of this object.
|
||||
TAKE_OVER, //- Take over the passed-in buffer; it will be deleted on object destruction.
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef __int64 int64;
|
||||
typedef long int32;
|
||||
typedef short int16;
|
||||
typedef char int8;
|
||||
typedef unsigned __int64 uint64;
|
||||
typedef unsigned long uint32;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned char uint8;
|
||||
#else
|
||||
typedef long long int64;
|
||||
typedef int int32;
|
||||
typedef short int16;
|
||||
typedef char int8;
|
||||
typedef unsigned long long uint64;
|
||||
typedef unsigned int uint32;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned char uint8;
|
||||
#endif
|
||||
|
||||
class Exception
|
||||
{
|
||||
public:
|
||||
Exception(const ByteBuffer *bb, const char *act, uint32 sp = 0)
|
||||
{
|
||||
action = act;
|
||||
rpos = bb->rpos();
|
||||
wpos = bb->wpos();
|
||||
sizeparam = sp;
|
||||
cursize = bb->size();
|
||||
}
|
||||
uint32 rpos, wpos, sizeparam, cursize;
|
||||
const char *action;
|
||||
};
|
||||
|
||||
#ifdef BYTEBUFFER_NO_EXCEPTIONS
|
||||
#define BYTEBUFFER_EXCEPT(bb, desc, sz) { Exception __e(bb, desc, sz); char errbuf[256]; \
|
||||
sprintf(errbuf, "Exception in ByteBuffer: '%s', rpos: %u, wpos: %u, cursize: %u, sizeparam: %u", \
|
||||
__e.action, __e.rpos, __e.wpos, __e.cursize, __e.sizeparam); errorLog(errbuf); abort(); }
|
||||
#else
|
||||
#define BYTEBUFFER_EXCEPT(bb, desc, sz) throw Exception(bb, desc, sz)
|
||||
#endif
|
||||
|
||||
protected:
|
||||
|
||||
uint8 *_buf; // the ptr to the buffer that holds all the bytes
|
||||
uint32 _rpos, // read position, [0 ... _size]
|
||||
_wpos, // write position, [0 ... _size]
|
||||
_res, // reserved buffer size, [0 ... _size ... _res]
|
||||
_size; // used buffer size
|
||||
delete_func _delfunc;
|
||||
allocator_func _allocfunc;
|
||||
bool _mybuf; // if true, destructor deletes buffer
|
||||
bool _growable; // default true, if false, buffer will not re-allocate more space
|
||||
|
||||
public:
|
||||
|
||||
|
||||
ByteBuffer()
|
||||
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true), _res(0), _mybuf(false), _delfunc(NULL),
|
||||
_allocfunc(NULL)
|
||||
{
|
||||
}
|
||||
ByteBuffer(uint32 res)
|
||||
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true), _res(0), _mybuf(false), _delfunc(NULL),
|
||||
_allocfunc(NULL)
|
||||
{
|
||||
_allocate(res);
|
||||
}
|
||||
ByteBuffer(ByteBuffer &buf, Mode mode = COPY, uint32 extra = 0)
|
||||
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true), _res(0), _mybuf(false), _delfunc(NULL),
|
||||
_allocfunc(NULL)
|
||||
{
|
||||
init(buf, mode, extra);
|
||||
}
|
||||
// del param only used with TAKE_OVER, extra only used with COPY
|
||||
ByteBuffer(void *buf, uint32 size, Mode mode = COPY, delete_func del = NULL, uint32 extra = 0)
|
||||
: _rpos(0), _wpos(0), _size(size), _buf(NULL), _growable(true), _delfunc(del),
|
||||
_mybuf(false), _allocfunc(NULL) // for mode == REUSE
|
||||
{
|
||||
init(buf, size, mode, del, extra);
|
||||
}
|
||||
|
||||
void init(void *buf, uint32 size, Mode mode = COPY, delete_func del = NULL, uint32 extra = 0)
|
||||
{
|
||||
_mybuf = false;
|
||||
switch(mode)
|
||||
{
|
||||
case COPY:
|
||||
_allocate(size + extra);
|
||||
append(buf, size);
|
||||
break;
|
||||
|
||||
case TAKE_OVER:
|
||||
_mybuf = true; // fallthrough
|
||||
case REUSE:
|
||||
_buf = (uint8*)buf;
|
||||
_res = size;
|
||||
_size = size;
|
||||
}
|
||||
}
|
||||
|
||||
void init(ByteBuffer& bb, Mode mode = COPY, uint32 extra = 0)
|
||||
{
|
||||
_allocfunc = bb._allocfunc;
|
||||
|
||||
switch(mode)
|
||||
{
|
||||
case COPY:
|
||||
reserve(bb.size() + extra);
|
||||
append(bb);
|
||||
break;
|
||||
|
||||
case TAKE_OVER:
|
||||
case REUSE:
|
||||
_mybuf = bb._mybuf;
|
||||
_delfunc = bb._delfunc;
|
||||
_buf = bb._buf;
|
||||
_res = bb._res;
|
||||
_size = bb._size;
|
||||
_growable = bb._growable;
|
||||
break;
|
||||
}
|
||||
|
||||
if(mode == TAKE_OVER)
|
||||
{
|
||||
bb._buf = NULL;
|
||||
bb._size = 0;
|
||||
bb._res = 0;
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~ByteBuffer()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void clear(void)
|
||||
{
|
||||
_delete();
|
||||
reset();
|
||||
}
|
||||
|
||||
inline void reset(void)
|
||||
{
|
||||
_rpos = _wpos = _size = 0;
|
||||
}
|
||||
|
||||
void resize(uint32 newsize)
|
||||
{
|
||||
reserve(newsize);
|
||||
_rpos = 0;
|
||||
_wpos = newsize;
|
||||
_size = newsize;
|
||||
}
|
||||
|
||||
void reserve(uint32 newsize)
|
||||
{
|
||||
if(_res < newsize)
|
||||
_allocate(newsize);
|
||||
}
|
||||
|
||||
// ---------------------- Write methods -----------------------
|
||||
|
||||
BB_MAKE_WRITE_OP(char);
|
||||
BB_MAKE_WRITE_OP(uint8);
|
||||
BB_MAKE_WRITE_OP(uint16);
|
||||
BB_MAKE_WRITE_OP(uint32);
|
||||
BB_MAKE_WRITE_OP(uint64);
|
||||
BB_MAKE_WRITE_OP(float);
|
||||
BB_MAKE_WRITE_OP(double);
|
||||
BB_MAKE_WRITE_OP(int);
|
||||
|
||||
ByteBuffer &operator<<(bool value)
|
||||
{
|
||||
append<char>((char)value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(const char *str)
|
||||
{
|
||||
append((uint8 *)str, str ? strlen(str) : 0);
|
||||
append((uint8)0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer &operator<<(const std::string &value)
|
||||
{
|
||||
append((uint8 *)value.c_str(), value.length());
|
||||
append((uint8)0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// -------------------- Read methods --------------------
|
||||
|
||||
BB_MAKE_READ_OP(char);
|
||||
BB_MAKE_READ_OP(uint8);
|
||||
BB_MAKE_READ_OP(uint16);
|
||||
BB_MAKE_READ_OP(uint32);
|
||||
BB_MAKE_READ_OP(uint64);
|
||||
BB_MAKE_READ_OP(float);
|
||||
BB_MAKE_READ_OP(double);
|
||||
BB_MAKE_READ_OP(int);
|
||||
|
||||
ByteBuffer &operator>>(bool &value)
|
||||
{
|
||||
value = read<char>() > 0 ? true : false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint8 operator[](uint32 pos)
|
||||
{
|
||||
return read<uint8>(pos);
|
||||
}
|
||||
|
||||
ByteBuffer &operator>>(std::string& value)
|
||||
{
|
||||
value.clear();
|
||||
char c;
|
||||
while(readable() && (c = read<char>()))
|
||||
value += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
|
||||
uint32 rpos() const { return _rpos; }
|
||||
uint32 rpos(uint32 rpos)
|
||||
{
|
||||
_rpos = rpos < size() ? rpos : size();
|
||||
return _rpos;
|
||||
}
|
||||
|
||||
uint32 wpos() const { return _wpos; }
|
||||
uint32 wpos(uint32 wpos)
|
||||
{
|
||||
_wpos = wpos < size() ? wpos : size();
|
||||
return _wpos;
|
||||
}
|
||||
|
||||
template <typename T> T read()
|
||||
{
|
||||
T r = read<T>(_rpos);
|
||||
_rpos += sizeof(T);
|
||||
return r;
|
||||
}
|
||||
template <typename T> T read(uint32 pos) const
|
||||
{
|
||||
if(pos + sizeof(T) > size())
|
||||
BYTEBUFFER_EXCEPT(this, "read", sizeof(T));
|
||||
T val = *((T const*)(_buf + pos));
|
||||
ByteBufferTools::ToLittleEndian<T>(val);
|
||||
return val;
|
||||
}
|
||||
|
||||
void read(void *dest, uint32 len)
|
||||
{
|
||||
if (_rpos + len <= size())
|
||||
memcpy(dest, &_buf[_rpos], len);
|
||||
else
|
||||
BYTEBUFFER_EXCEPT(this, "read-into", len);
|
||||
_rpos += len;
|
||||
}
|
||||
|
||||
void skipRead(uint32 len)
|
||||
{
|
||||
_rpos += len;
|
||||
}
|
||||
|
||||
inline const uint8 *contents() const { return _buf; }
|
||||
inline uint8 *contents() { return _buf; }
|
||||
|
||||
inline const void *ptr() const { return _buf; }
|
||||
inline void *ptr() { return _buf; }
|
||||
|
||||
inline uint32 size() const { return _size; }
|
||||
|
||||
inline uint32 bytes() const { return size(); }
|
||||
inline uint32 bits() const { return bytes() * 8; }
|
||||
|
||||
inline uint32 capacity() const { return _res; }
|
||||
|
||||
inline uint32 readable(void) const { return size() - rpos(); }
|
||||
inline uint32 writable(void) const { return size() - wpos(); } // free space left before realloc will occur
|
||||
|
||||
template <typename T> void append(T value)
|
||||
{
|
||||
ByteBufferTools::ToLittleEndian<T>(value);
|
||||
_enlargeIfReq(_wpos + sizeof(T));
|
||||
*((T*)(_buf + _wpos)) = value;
|
||||
_wpos += sizeof(T);
|
||||
if(_size < _wpos)
|
||||
_size = _wpos;
|
||||
}
|
||||
|
||||
void append(const void *src, uint32 bytes)
|
||||
{
|
||||
if (!bytes) return;
|
||||
_enlargeIfReq(_wpos + bytes);
|
||||
memcpy(_buf + _wpos, src, bytes);
|
||||
_wpos += bytes;
|
||||
if(_size < _wpos)
|
||||
_size = _wpos;
|
||||
}
|
||||
void append(const ByteBuffer& buffer)
|
||||
{
|
||||
if(buffer.size())
|
||||
append(buffer.contents(), buffer.size());
|
||||
}
|
||||
|
||||
void put(uint32 pos, const void *src, uint32 bytes)
|
||||
{
|
||||
memcpy(_buf + pos, src, bytes);
|
||||
}
|
||||
|
||||
template <typename T> void put(uint32 pos, T value)
|
||||
{
|
||||
if(pos >= size())
|
||||
BYTEBUFFER_EXCEPT(this, "put", sizeof(T));
|
||||
|
||||
ByteBufferTools::ToLittleEndian<T>(value);
|
||||
*((T*)(_buf + pos)) = value;
|
||||
}
|
||||
|
||||
inline bool growable(void) { return _growable; }
|
||||
inline void growable(bool b) { _growable = b; }
|
||||
|
||||
// dangerous functions
|
||||
|
||||
void _setPtr(void *p)
|
||||
{
|
||||
_buf = (uint8*)p;
|
||||
}
|
||||
|
||||
void _setAllocFunc(allocator_func f)
|
||||
{
|
||||
_allocfunc = f;
|
||||
}
|
||||
|
||||
void _setDelFunc(delete_func f)
|
||||
{
|
||||
_delfunc = f;
|
||||
}
|
||||
|
||||
void _setSize(uint32 s)
|
||||
{
|
||||
_size = s;
|
||||
}
|
||||
|
||||
void _setReserved(uint32 s)
|
||||
{
|
||||
_res = s;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void _delete(void)
|
||||
{
|
||||
if(_mybuf)
|
||||
{
|
||||
if(_delfunc)
|
||||
_delfunc(_buf);
|
||||
else
|
||||
delete [] _buf;
|
||||
_buf = NULL;
|
||||
_res = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// allocate larger buffer and copy contents. if we own the current buffer, delete old, otherwise, leave it as it is.
|
||||
void _allocate(uint32 s)
|
||||
{
|
||||
if(!_growable && _buf) // only throw if we already have a buf
|
||||
BYTEBUFFER_EXCEPT(this, "_alloc+locked", s);
|
||||
|
||||
// dangerous: It's up to the user to be sure that _allocfunc and _delfunc are matching
|
||||
uint8 *newbuf = (uint8*)(_allocfunc ? _allocfunc(s) : new char[s]);
|
||||
if(_buf)
|
||||
{
|
||||
memcpy(newbuf, _buf, _size);
|
||||
_delete();
|
||||
}
|
||||
_buf = newbuf;
|
||||
_res = s;
|
||||
_mybuf = true;
|
||||
|
||||
if (!_allocfunc)
|
||||
_delfunc = NULL;
|
||||
}
|
||||
|
||||
void _enlargeIfReq(uint32 minSize)
|
||||
{
|
||||
if(_res < minSize)
|
||||
{
|
||||
uint32 a = _res * 2;
|
||||
if(a < minSize) // fallback if doubling the space was not enough
|
||||
a += minSize;
|
||||
_allocate(a);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#undef BB_MAKE_WRITE_OP
|
||||
#undef BB_MAKE_READ_OP
|
||||
#undef BB_IS_BIG_ENDIAN
|
||||
|
||||
|
||||
|
||||
#endif
|
|
@ -1,258 +0,0 @@
|
|||
#include "Base.h"
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#include "DeflateCompressor.h"
|
||||
|
||||
// for weird gcc/mingw hackfix below
|
||||
#include <string.h>
|
||||
|
||||
|
||||
DeflateCompressor::DeflateCompressor()
|
||||
: _windowBits(-MAX_WBITS), // negative, because we want a raw deflate stream, and not zlib-wrapped
|
||||
_forceCompress(false),
|
||||
_iscompressed(false),
|
||||
_real_size(0)
|
||||
{
|
||||
}
|
||||
|
||||
ZlibCompressor::ZlibCompressor()
|
||||
: DeflateCompressor()
|
||||
{
|
||||
_windowBits = MAX_WBITS; // positive, means we use a zlib-wrapped deflate stream
|
||||
}
|
||||
|
||||
GzipCompressor::GzipCompressor()
|
||||
: DeflateCompressor()
|
||||
{
|
||||
_windowBits = MAX_WBITS + 16; // this makes zlib wrap a minimal gzip header around the stream
|
||||
_forceCompress = true; // we want this for gzip
|
||||
}
|
||||
|
||||
void DeflateCompressor::compress(void* dst, uint32 *dst_size, const void* src, uint32 src_size,
|
||||
uint8 level, int wbits)
|
||||
{
|
||||
z_stream c_stream;
|
||||
|
||||
c_stream.zalloc = (alloc_func)Z_NULL;
|
||||
c_stream.zfree = (free_func)Z_NULL;
|
||||
c_stream.opaque = (voidpf)Z_NULL;
|
||||
|
||||
if (Z_OK != deflateInit2(&c_stream, level, Z_DEFLATED, wbits, 8, Z_DEFAULT_STRATEGY))
|
||||
{
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
c_stream.next_out = (Bytef*)dst;
|
||||
c_stream.avail_out = *dst_size;
|
||||
c_stream.next_in = (Bytef*)src;
|
||||
c_stream.avail_in = (uInt)src_size;
|
||||
|
||||
if (Z_OK != deflate(&c_stream, Z_NO_FLUSH))
|
||||
{
|
||||
errorLog("ZLIB: Can't compress (zlib: deflate)");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (c_stream.avail_in != 0)
|
||||
{
|
||||
errorLog("Can't compress (zlib: deflate not greedy)");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Z_STREAM_END != deflate(&c_stream, Z_FINISH))
|
||||
{
|
||||
errorLog("Can't compress (zlib: deflate, finish)");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Z_OK != deflateEnd(&c_stream))
|
||||
{
|
||||
errorLog("Can't compress (zlib: deflateEnd)");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
*dst_size = c_stream.total_out;
|
||||
}
|
||||
|
||||
void DeflateCompressor::decompress(void *dst, uint32 *origsize, const void *src, uint32 size, int wbits)
|
||||
{
|
||||
z_stream stream;
|
||||
int err;
|
||||
|
||||
stream.next_in = (Bytef*)src;
|
||||
stream.avail_in = (uInt)size;
|
||||
stream.next_out = (Bytef*)dst;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = Z_NULL;
|
||||
stream.avail_out = *origsize;
|
||||
stream.total_out = 0;
|
||||
|
||||
err = inflateInit2(&stream, wbits);
|
||||
if (err != Z_OK)
|
||||
{
|
||||
*origsize = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
err = inflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END)
|
||||
{
|
||||
inflateEnd(&stream);
|
||||
*origsize = 0;
|
||||
return;
|
||||
}
|
||||
*origsize = (uint32)stream.total_out;
|
||||
|
||||
err = inflateEnd(&stream);
|
||||
if(err != Z_OK)
|
||||
*origsize = 0;
|
||||
}
|
||||
|
||||
|
||||
void DeflateCompressor::Compress(uint8 level)
|
||||
{
|
||||
if(!_forceCompress && (!level || _iscompressed || (!size())))
|
||||
return;
|
||||
|
||||
char *buf;
|
||||
|
||||
|
||||
uint32 oldsize = size();
|
||||
uint32 newsize = compressBound(oldsize) + 30; // for optional gzip header
|
||||
|
||||
buf = new char[newsize];
|
||||
|
||||
compress((void*)buf, &newsize, (void*)contents(), oldsize, level, _windowBits);
|
||||
if(!newsize || (!_forceCompress && newsize > oldsize)) // only allow more data if compression is forced (which is the case for gzip)
|
||||
{
|
||||
delete [] buf;
|
||||
return;
|
||||
}
|
||||
|
||||
resize(newsize);
|
||||
rpos(0);
|
||||
wpos(0);
|
||||
append(buf,newsize);
|
||||
delete [] buf;
|
||||
|
||||
_iscompressed = true;
|
||||
|
||||
_real_size = oldsize;
|
||||
}
|
||||
|
||||
void DeflateCompressor::Decompress(void)
|
||||
{
|
||||
if( (!_iscompressed) || (!size()))
|
||||
return;
|
||||
|
||||
if(!_real_size)
|
||||
{
|
||||
if(decompressBlockwise() == Z_OK)
|
||||
_iscompressed = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32 rs = (uint32)_real_size;
|
||||
uint32 origsize = rs;
|
||||
uint8 *target = new uint8[rs];
|
||||
wpos(0);
|
||||
rpos(0);
|
||||
decompress((void*)target, &origsize, (const void*)contents(), size(), _windowBits);
|
||||
if(origsize != rs)
|
||||
{
|
||||
char errbuf[256];
|
||||
sprintf(errbuf, "DeflateCompressor: Inflate error! cursize=%u origsize=%u realsize=%u",size(),origsize,rs);
|
||||
errorLog(errbuf);
|
||||
delete [] target;
|
||||
return;
|
||||
}
|
||||
clear();
|
||||
append(target, origsize);
|
||||
delete [] target;
|
||||
_real_size = 0;
|
||||
_iscompressed = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define CHUNK 16384
|
||||
|
||||
int DeflateCompressor::decompressBlockwise()
|
||||
{
|
||||
int ret;
|
||||
unsigned have;
|
||||
z_stream strm;
|
||||
unsigned char out[CHUNK];
|
||||
|
||||
/* allocate inflate state */
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
strm.avail_in = 0;
|
||||
strm.next_in = Z_NULL;
|
||||
ret = inflateInit2(&strm, _windowBits);
|
||||
if (ret != Z_OK)
|
||||
return ret;
|
||||
|
||||
ByteBuffer bb;
|
||||
|
||||
strm.avail_in = size();
|
||||
strm.next_in = contents();
|
||||
|
||||
/* decompress until deflate stream ends or end of file */
|
||||
do {
|
||||
/* run inflate() on input until output buffer not full */
|
||||
do {
|
||||
strm.avail_out = CHUNK;
|
||||
strm.next_out = out;
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
switch (ret) {
|
||||
case Z_NEED_DICT:
|
||||
case Z_STREAM_ERROR:
|
||||
ret = Z_DATA_ERROR; /* and fall through */
|
||||
case Z_DATA_ERROR:
|
||||
case Z_MEM_ERROR:
|
||||
(void)inflateEnd(&strm);
|
||||
return ret;
|
||||
}
|
||||
have = CHUNK - strm.avail_out;
|
||||
bb.append(out, have);
|
||||
} while (strm.avail_out == 0);
|
||||
/* done when inflate() says it's done */
|
||||
} while (ret != Z_STREAM_END);
|
||||
|
||||
/* clean up and return */
|
||||
(void)inflateEnd(&strm);
|
||||
|
||||
if (ret != Z_STREAM_END)
|
||||
return Z_DATA_ERROR;
|
||||
|
||||
// exchange pointer
|
||||
clear();
|
||||
init(bb, TAKE_OVER);
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
void GzipCompressor::Decompress(void)
|
||||
{
|
||||
uint32 t = 0;
|
||||
rpos(size() - sizeof(uint32)); // according to RFC 1952, input size are the last 4 bytes at the end of the file, in little endian
|
||||
*this >> t;
|
||||
_real_size = t;
|
||||
|
||||
// !! NOTE: this fixes a gcc/mingw bug where _real_size would be set incorrectly
|
||||
#if __GNUC__
|
||||
char xx[20];
|
||||
sprintf(xx, "%u", t);
|
||||
#endif
|
||||
|
||||
DeflateCompressor::Decompress(); // will set rpos back anyway
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
#ifndef DEFLATE_COMPRESSOR_H
|
||||
#define DEFLATE_COMPRESSOR_H
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
|
||||
// implements a raw deflate stream, not zlib wrapped, and not checksummed.
|
||||
class DeflateCompressor : public ByteBuffer
|
||||
{
|
||||
public:
|
||||
DeflateCompressor();
|
||||
DeflateCompressor(void *buf, uint32 size, Mode mode = COPY, delete_func del = NULL, uint32 extra = 0);
|
||||
virtual ~DeflateCompressor() {}
|
||||
virtual void Compress(uint8 level = 1);
|
||||
virtual void Decompress(void);
|
||||
|
||||
bool Compressed(void) const { return _iscompressed; }
|
||||
void Compressed(bool b) { _iscompressed = b; }
|
||||
uint32 RealSize(void) const { return _iscompressed ? _real_size : size(); }
|
||||
void RealSize(uint32 realsize) { _real_size = realsize; }
|
||||
void clear(void) // not required to be strictly virtual; be careful not to mess up static types!
|
||||
{
|
||||
ByteBuffer::clear();
|
||||
_real_size = 0;
|
||||
_iscompressed = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
int _windowBits; // read zlib docs to know what this means
|
||||
unsigned int _real_size;
|
||||
bool _forceCompress;
|
||||
bool _iscompressed;
|
||||
|
||||
private:
|
||||
static void decompress(void *dst, uint32 *origsize, const void *src, uint32 size, int wbits);
|
||||
static void compress(void* dst, uint32 *dst_size, const void* src, uint32 src_size,
|
||||
uint8 level, int wbits);
|
||||
|
||||
int decompressBlockwise();
|
||||
};
|
||||
|
||||
// implements deflate stream, zlib wrapped
|
||||
class ZlibCompressor : public DeflateCompressor
|
||||
{
|
||||
public:
|
||||
ZlibCompressor();
|
||||
virtual ~ZlibCompressor() {}
|
||||
};
|
||||
|
||||
// the output produced by this stream contains a minimal gzip header,
|
||||
// and can be directly written to a .gz file.
|
||||
class GzipCompressor : public DeflateCompressor
|
||||
{
|
||||
public:
|
||||
GzipCompressor();
|
||||
virtual ~GzipCompressor() {}
|
||||
virtual void Decompress(void);
|
||||
};
|
||||
|
||||
#endif
|
319
BBGE/glfont2.cpp
319
BBGE/glfont2.cpp
|
@ -1,319 +0,0 @@
|
|||
//*******************************************************************
|
||||
//glfont2.cpp -- glFont Version 2.0 implementation
|
||||
//Copyright (c) 1998-2002 Brad Fish
|
||||
//See glfont.html for terms of use
|
||||
//May 14, 2002
|
||||
//*******************************************************************
|
||||
|
||||
//STL headers
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
//OpenGL headers
|
||||
/*
|
||||
#ifdef _WINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include <OpenGL/gl.h>
|
||||
*/
|
||||
#include "Base.h"
|
||||
|
||||
#include "SDL_endian.h"
|
||||
|
||||
//glFont header
|
||||
#include "glfont2.h"
|
||||
using namespace glfont;
|
||||
|
||||
static int read_int(ifstream &input)
|
||||
{
|
||||
int buffer;
|
||||
|
||||
input.read((char *)&buffer, 4);
|
||||
return SDL_SwapLE32(buffer);
|
||||
}
|
||||
|
||||
static float read_float(ifstream &input)
|
||||
{
|
||||
union
|
||||
{
|
||||
int i;
|
||||
float f;
|
||||
} buffer;
|
||||
|
||||
input.read((char *)&buffer.i, 4);
|
||||
buffer.i = SDL_SwapLE32(buffer.i);
|
||||
return buffer.f;
|
||||
}
|
||||
|
||||
//*******************************************************************
|
||||
//GLFont Class Implementation
|
||||
//*******************************************************************
|
||||
GLFont::GLFont ()
|
||||
{
|
||||
//Initialize header to safe state
|
||||
header.tex = -1;
|
||||
header.tex_width = 0;
|
||||
header.tex_height = 0;
|
||||
header.start_char = 0;
|
||||
header.end_char = 0;
|
||||
header.chars = NULL;
|
||||
}
|
||||
//*******************************************************************
|
||||
GLFont::~GLFont ()
|
||||
{
|
||||
//Destroy the font
|
||||
Destroy();
|
||||
}
|
||||
//*******************************************************************
|
||||
bool GLFont::Create (const char *file_name, int tex, bool loadTexture)
|
||||
{
|
||||
ifstream input;
|
||||
int num_chars, num_tex_bytes;
|
||||
char *tex_bytes;
|
||||
|
||||
//Destroy the old font if there was one, just to be safe
|
||||
Destroy();
|
||||
|
||||
//Open input file
|
||||
input.open(file_name, ios::in | ios::binary);
|
||||
if (!input)
|
||||
return false;
|
||||
|
||||
// Read the header from file
|
||||
header.tex = tex;
|
||||
input.seekg(4, ios::cur); // skip tex field
|
||||
header.tex_width = read_int(input);
|
||||
header.tex_height = read_int(input);
|
||||
header.start_char = read_int(input);
|
||||
header.end_char = read_int(input);
|
||||
input.seekg(4, ios::cur); // skip chars field
|
||||
|
||||
std::ostringstream os;
|
||||
os << "tex_width: " << header.tex_width << " tex_height: " << header.tex_height;
|
||||
debugLog(os.str());
|
||||
|
||||
//Allocate space for character array
|
||||
num_chars = header.end_char - header.start_char + 1;
|
||||
if ((header.chars = new GLFontChar[num_chars]) == NULL)
|
||||
return false;
|
||||
|
||||
//Read character array
|
||||
for (int i = 0; i < num_chars; i++)
|
||||
{
|
||||
header.chars[i].dx = read_float(input);
|
||||
header.chars[i].dy = read_float(input);
|
||||
header.chars[i].tx1 = read_float(input);
|
||||
header.chars[i].ty1 = read_float(input);
|
||||
header.chars[i].tx2 = read_float(input);
|
||||
header.chars[i].ty2 = read_float(input);
|
||||
}
|
||||
|
||||
//Read texture pixel data
|
||||
num_tex_bytes = header.tex_width * header.tex_height * 2;
|
||||
tex_bytes = new char[num_tex_bytes];
|
||||
input.read(tex_bytes, num_tex_bytes);
|
||||
|
||||
|
||||
//Build2DMipmaps(3, header.tex_width, header.tex_height, GL_UNSIGNED_BYTE, tex_bytes, 1);
|
||||
|
||||
if (loadTexture)
|
||||
{
|
||||
#ifdef BBGE_BUILD_OPENGL
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, 2, header.tex_width,
|
||||
header.tex_height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
|
||||
(void *)tex_bytes);
|
||||
//gluBuild2DMipmaps(GL_TEXTURE_2D, 2, header.tex_width, header.tex_height, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (void*)tex_bytes);
|
||||
//Build2DMipmaps(3, header.tex_width, header.tex_height, GL_LUMINANCE_ALPHA, tex_bytes, 1);
|
||||
//Create OpenGL texture
|
||||
/*
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
|
||||
//Free texture pixels memory
|
||||
delete[] tex_bytes;
|
||||
|
||||
//Close input file
|
||||
input.close();
|
||||
|
||||
//Return successfully
|
||||
return true;
|
||||
}
|
||||
//*******************************************************************
|
||||
bool GLFont::Create (const std::string &file_name, int tex, bool loadTexture)
|
||||
{
|
||||
return Create(file_name.c_str(), tex);
|
||||
}
|
||||
//*******************************************************************
|
||||
void GLFont::Destroy (void)
|
||||
{
|
||||
//Delete the character array if necessary
|
||||
if (header.chars)
|
||||
{
|
||||
delete[] header.chars;
|
||||
header.chars = NULL;
|
||||
}
|
||||
}
|
||||
//*******************************************************************
|
||||
void GLFont::GetTexSize (std::pair<int, int> *size)
|
||||
{
|
||||
//Retrieve texture size
|
||||
size->first = header.tex_width;
|
||||
size->second = header.tex_height;
|
||||
}
|
||||
//*******************************************************************
|
||||
int GLFont::GetTexWidth (void)
|
||||
{
|
||||
//Return texture width
|
||||
return header.tex_width;
|
||||
}
|
||||
//*******************************************************************
|
||||
int GLFont::GetTexHeight (void)
|
||||
{
|
||||
//Return texture height
|
||||
return header.tex_height;
|
||||
}
|
||||
//*******************************************************************
|
||||
void GLFont::GetCharInterval (std::pair<int, int> *interval)
|
||||
{
|
||||
//Retrieve character interval
|
||||
interval->first = header.start_char;
|
||||
interval->second = header.end_char;
|
||||
}
|
||||
//*******************************************************************
|
||||
int GLFont::GetStartChar (void)
|
||||
{
|
||||
//Return start character
|
||||
return header.start_char;
|
||||
}
|
||||
//*******************************************************************
|
||||
int GLFont::GetEndChar (void)
|
||||
{
|
||||
//Return end character
|
||||
return header.end_char;
|
||||
}
|
||||
//*******************************************************************
|
||||
void GLFont::GetCharSize (int c, std::pair<int, int> *size)
|
||||
{
|
||||
//Make sure character is in range
|
||||
if (c < header.start_char || c > header.end_char)
|
||||
{
|
||||
//Not a valid character, so it obviously has no size
|
||||
size->first = 0;
|
||||
size->second = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
GLFontChar *glfont_char;
|
||||
|
||||
//Retrieve character size
|
||||
glfont_char = &header.chars[c - header.start_char];
|
||||
size->first = (int)(glfont_char->dx * header.tex_width);
|
||||
size->second = (int)(glfont_char->dy *
|
||||
header.tex_height);
|
||||
}
|
||||
}
|
||||
//*******************************************************************
|
||||
int GLFont::GetCharWidth (int c)
|
||||
{
|
||||
//Make sure in range
|
||||
if (c < header.start_char || c > header.end_char)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
GLFontChar *glfont_char;
|
||||
|
||||
//Retrieve character width
|
||||
glfont_char = &header.chars[c - header.start_char];
|
||||
|
||||
// hack to fix empty spaces
|
||||
if (c == ' ' && glfont_char->dx <= 0)
|
||||
{
|
||||
GLFontChar *glfont_a = &header.chars['a' - header.start_char];
|
||||
glfont_char->dx = glfont_a->dx*0.75;
|
||||
glfont_char->dy = glfont_a->dy;
|
||||
}
|
||||
|
||||
return (int)(glfont_char->dx * header.tex_width);
|
||||
}
|
||||
}
|
||||
//*******************************************************************
|
||||
int GLFont::GetCharHeight (int c)
|
||||
{
|
||||
//Make sure in range
|
||||
if (c < header.start_char || c > header.end_char)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
GLFontChar *glfont_char;
|
||||
|
||||
//Retrieve character height
|
||||
glfont_char = &header.chars[c - header.start_char];
|
||||
return (int)(glfont_char->dy * header.tex_height);
|
||||
}
|
||||
}
|
||||
//*******************************************************************
|
||||
void GLFont::Begin (void)
|
||||
{
|
||||
#ifdef BBGE_BUILD_OPENGL
|
||||
//Bind to font texture
|
||||
glBindTexture(GL_TEXTURE_2D, header.tex);
|
||||
#endif
|
||||
}
|
||||
//*******************************************************************
|
||||
void GLFont::GetStringSize (const std::string &text, std::pair<int, int> *size)
|
||||
{
|
||||
unsigned int i;
|
||||
char c;
|
||||
GLFontChar *glfont_char;
|
||||
float width;
|
||||
|
||||
//debugLog("size->second");
|
||||
//Height is the same for now...might change in future
|
||||
size->second = (int)(header.chars[header.start_char].dy *
|
||||
header.tex_height);
|
||||
|
||||
//Calculate width of string
|
||||
width = 0.0F;
|
||||
for (i = 0; i < text.size(); i++)
|
||||
{
|
||||
//Make sure character is in range
|
||||
c = (char)text[i];
|
||||
|
||||
if (c < header.start_char || c > header.end_char)
|
||||
continue;
|
||||
|
||||
//Get pointer to glFont character
|
||||
glfont_char = &header.chars[c - header.start_char];
|
||||
|
||||
//Get width and height
|
||||
width += glfont_char->dx * header.tex_width;
|
||||
}
|
||||
|
||||
//Save width
|
||||
//debugLog("size first");
|
||||
size->first = (int)width;
|
||||
|
||||
//debugLog("done");
|
||||
}
|
||||
|
||||
//End of file
|
||||
|
||||
|
146
BBGE/glfont2.h
146
BBGE/glfont2.h
|
@ -1,146 +0,0 @@
|
|||
//*******************************************************************
|
||||
//glfont2.h -- Header for glfont2.cpp
|
||||
//Copyright (c) 1998-2002 Brad Fish
|
||||
//See glfont.html for terms of use
|
||||
//May 14, 2002
|
||||
//*******************************************************************
|
||||
|
||||
#ifndef GLFONT2_H
|
||||
#define GLFONT2_H
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
//*******************************************************************
|
||||
//GLFont Interface
|
||||
//*******************************************************************
|
||||
|
||||
//glFont namespace
|
||||
namespace glfont
|
||||
{
|
||||
class GLFont;
|
||||
}
|
||||
|
||||
//glFont class
|
||||
class glfont::GLFont
|
||||
{
|
||||
private:
|
||||
|
||||
//glFont character structure
|
||||
typedef struct
|
||||
{
|
||||
float dx, dy;
|
||||
float tx1, ty1;
|
||||
float tx2, ty2;
|
||||
} GLFontChar;
|
||||
|
||||
//glFont header structure
|
||||
struct
|
||||
{
|
||||
int tex;
|
||||
int tex_width, tex_height;
|
||||
int start_char, end_char;
|
||||
GLFontChar *chars;
|
||||
} header;
|
||||
|
||||
public:
|
||||
|
||||
//Constructor
|
||||
GLFont ();
|
||||
|
||||
//Destructor
|
||||
~GLFont ();
|
||||
|
||||
public:
|
||||
|
||||
//Creates the glFont
|
||||
bool Create (const char *file_name, int tex, bool loadTexture=true);
|
||||
bool Create (const std::string &file_name, int tex, bool loadTexture=true);
|
||||
|
||||
//Destroys the glFont
|
||||
void Destroy (void);
|
||||
|
||||
//Texture size retrieval methods
|
||||
void GetTexSize (std::pair<int, int> *size);
|
||||
int GetTexWidth (void);
|
||||
int GetTexHeight (void);
|
||||
|
||||
//Character interval retrieval methods
|
||||
void GetCharInterval (std::pair<int, int> *interval);
|
||||
int GetStartChar (void);
|
||||
int GetEndChar (void);
|
||||
|
||||
//Character size retrieval methods
|
||||
void GetCharSize (int c, std::pair<int, int> *size);
|
||||
int GetCharWidth (int c);
|
||||
int GetCharHeight (int c);
|
||||
|
||||
void GetStringSize (const std::string &text, std::pair<int, int> *size);
|
||||
|
||||
|
||||
//Begins text output with this font
|
||||
void Begin (void);
|
||||
|
||||
//Template function to output a scaled, colored std::basic_string
|
||||
template<class T> void DrawString (
|
||||
const std::basic_string<T> &text, float scalar, float x,
|
||||
float y, const float *top_color, const float *bottom_color, float alpha, float lastAlpha)
|
||||
{
|
||||
unsigned int i;
|
||||
T c;
|
||||
GLFontChar *glfont_char;
|
||||
float width, height;
|
||||
|
||||
//Begin rendering quads
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
int sz = text.size();
|
||||
|
||||
float a = 0;
|
||||
//Loop through characters
|
||||
for (i = 0; i < sz; i++)
|
||||
{
|
||||
//Make sure character is in range
|
||||
c = text[i];
|
||||
if (c < header.start_char || c > header.end_char)
|
||||
continue;
|
||||
|
||||
//Get pointer to glFont character
|
||||
glfont_char = &header.chars[c - header.start_char];
|
||||
|
||||
//Get width and height
|
||||
width = (glfont_char->dx * header.tex_width) * scalar;
|
||||
height = (glfont_char->dy * header.tex_height) * scalar;
|
||||
|
||||
if (i == (sz-1))
|
||||
a = alpha*lastAlpha;
|
||||
else
|
||||
a = alpha;
|
||||
|
||||
//Specify colors, vertices, and texture coordinates
|
||||
glColor4f(top_color[0], top_color[1], top_color[2], a);
|
||||
glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
||||
glVertex3f(x, y, 0.0F);
|
||||
glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
||||
glVertex3f(x + width, y, 0.0F);
|
||||
glColor4f(bottom_color[0], bottom_color[1], bottom_color[2], a);
|
||||
glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
||||
glVertex3f(x + width, y + height, 0.0F);
|
||||
glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
||||
glVertex3f(x, y + height, 0.0F);
|
||||
|
||||
//Move to next character
|
||||
x += width;
|
||||
}
|
||||
|
||||
//Stop rendering quads
|
||||
glEnd();
|
||||
}
|
||||
};
|
||||
|
||||
//*******************************************************************
|
||||
|
||||
#endif
|
||||
|
||||
//End of file
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue