From b789e3879d07dd791d982ba1494e32d6abd9bd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 30 Oct 2013 12:46:18 +0100 Subject: [PATCH] Rewritten PackStream in C++, using Qt serialization --- Makefile | 2 +- src/basics/ColorRGB.cpp | 5 + src/basics/ColorRGB.h | 10 + src/basics/basics.pro | 8 +- src/definition/BaseDefinition.cpp | 6 + src/definition/BaseDefinition.h | 35 ++++ src/definition/definition.pro | 18 +- src/definition/definition_global.h | 4 + src/editing/baseform.h | 2 +- src/editing/basepreview.h | 2 +- src/editing/common/mainwindow.h | 2 +- src/editing/formwater.cpp | 2 +- src/editing/lighting/DialogMaterialEditor.cpp | 2 +- src/editing/terrain/paintingbrush.cpp | 3 +- src/editing/terrain/widgetheightmap.cpp | 8 +- src/paysages.pro | 11 +- src/rendering/Scenery.h | 2 +- src/rendering/atmosphere/public.h | 2 +- src/rendering/camera.h | 2 +- src/rendering/geoarea.h | 2 +- src/rendering/layers.h | 2 +- src/rendering/noise.h | 2 +- src/rendering/noisenaive.h | 2 +- src/rendering/rendering.pro | 14 +- src/rendering/rendering_global.h | 4 + src/rendering/tools/color.h | 2 +- src/rendering/tools/curve.h | 2 +- src/rendering/tools/euclid.h | 2 +- src/rendering/tools/lighting.h | 2 +- src/rendering/tools/pack.c | 195 ------------------ src/rendering/tools/pack.h | 27 --- src/rendering/tools/texture.c | 1 + src/rendering/tools/texture.h | 2 +- src/rendering/tools/zone.h | 2 +- src/system/PackStream.cpp | 141 +++++++++++++ src/system/PackStream.h | 67 ++++++ src/system/system.pro | 6 +- src/testing/test_pack.c | 6 +- src/testing/testing.pro | 23 ++- 39 files changed, 355 insertions(+), 275 deletions(-) create mode 100644 src/basics/ColorRGB.cpp create mode 100644 src/basics/ColorRGB.h create mode 100644 src/definition/BaseDefinition.cpp create mode 100644 src/definition/BaseDefinition.h delete mode 100644 src/rendering/tools/pack.c delete mode 100644 src/rendering/tools/pack.h create mode 100644 src/system/PackStream.cpp create mode 100644 src/system/PackStream.h diff --git a/Makefile b/Makefile index c80ff4f..a1b5e20 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ BUILDMODE=release BUILDPATH=./build/${BUILDMODE} -LIBRARY_PATH=${BUILDPATH}/rendering:${BUILDPATH}/exploring:${BUILDPATH}/system +LIBRARY_PATH=${BUILDPATH}/rendering:${BUILDPATH}/exploring:${BUILDPATH}/system:${BUILDPATH}/basics:${BUILDPATH}/definition all:build diff --git a/src/basics/ColorRGB.cpp b/src/basics/ColorRGB.cpp new file mode 100644 index 0000000..2fa82ec --- /dev/null +++ b/src/basics/ColorRGB.cpp @@ -0,0 +1,5 @@ +#include "ColorRGB.h" + +ColorRGB::ColorRGB() +{ +} diff --git a/src/basics/ColorRGB.h b/src/basics/ColorRGB.h new file mode 100644 index 0000000..4c2998d --- /dev/null +++ b/src/basics/ColorRGB.h @@ -0,0 +1,10 @@ +#ifndef COLORRGB_H +#define COLORRGB_H + +class ColorRGB +{ +public: + ColorRGB(); +}; + +#endif // COLORRGB_H diff --git a/src/basics/basics.pro b/src/basics/basics.pro index 9574c78..9aa230e 100644 --- a/src/basics/basics.pro +++ b/src/basics/basics.pro @@ -6,15 +6,17 @@ QT -= gui -TARGET = basics +TARGET = paysages_basics TEMPLATE = lib DEFINES += BASICS_LIBRARY -SOURCES += +SOURCES += \ + ColorRGB.cpp HEADERS +=\ - basics_global.h + basics_global.h \ + ColorRGB.h unix:!symbian { maemo5 { diff --git a/src/definition/BaseDefinition.cpp b/src/definition/BaseDefinition.cpp new file mode 100644 index 0000000..ada1743 --- /dev/null +++ b/src/definition/BaseDefinition.cpp @@ -0,0 +1,6 @@ +#include "BaseDefinition.h" + +BaseDefinition::BaseDefinition(BaseDefinition* parent): + parent(parent) +{ +} diff --git a/src/definition/BaseDefinition.h b/src/definition/BaseDefinition.h new file mode 100644 index 0000000..a993fb2 --- /dev/null +++ b/src/definition/BaseDefinition.h @@ -0,0 +1,35 @@ +#ifndef BASEDEFINITION_H +#define BASEDEFINITION_H + +#include "definition_global.h" +#include + +class PackStream; + +namespace paysages { +namespace definition { + +/** + * @brief Base class for all definition containers + */ +class DEFINITIONSHARED_EXPORT BaseDefinition +{ +public: + BaseDefinition(BaseDefinition* parent); + virtual ~BaseDefinition(); + + void addChild(BaseDefinition* child); + void removeChild(BaseDefinition* child); + + virtual void save(PackStream* pack); + virtual void load(PackStream* pack); + +private: + BaseDefinition* parent; + QVector children; +}; + +} +} + +#endif // BASEDEFINITION_H diff --git a/src/definition/definition.pro b/src/definition/definition.pro index 624b6bd..a3d9a03 100644 --- a/src/definition/definition.pro +++ b/src/definition/definition.pro @@ -11,10 +11,12 @@ TEMPLATE = lib DEFINES += DEFINITION_LIBRARY -SOURCES += +SOURCES += \ + BaseDefinition.cpp HEADERS +=\ - definition_global.h + definition_global.h \ + BaseDefinition.h unix:!symbian { maemo5 { @@ -25,14 +27,14 @@ unix:!symbian { INSTALLS += target } -win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../rendering/release/ -lpaysages_rendering -else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../rendering/debug/ -lpaysages_rendering -else:unix: LIBS += -L$$OUT_PWD/../rendering/ -lpaysages_rendering -INCLUDEPATH += $$PWD/../rendering -DEPENDPATH += $$PWD/../rendering - win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../system/release/ -lpaysages_system else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../system/debug/ -lpaysages_system else:unix: LIBS += -L$$OUT_PWD/../system/ -lpaysages_system INCLUDEPATH += $$PWD/../system DEPENDPATH += $$PWD/../system + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../basics/release/ -lpaysages_basics +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../basics/debug/ -lpaysages_basics +else:unix: LIBS += -L$$OUT_PWD/../basics/ -lpaysages_basics +INCLUDEPATH += $$PWD/../basics +DEPENDPATH += $$PWD/../basics diff --git a/src/definition/definition_global.h b/src/definition/definition_global.h index a716918..81d2c02 100644 --- a/src/definition/definition_global.h +++ b/src/definition/definition_global.h @@ -23,9 +23,13 @@ #ifdef __cplusplus namespace paysages { + namespace system {} + namespace basics {} namespace definition {} } using namespace paysages::system; +using namespace paysages::basics; +using namespace paysages::definition; #endif /* Global imports */ diff --git a/src/editing/baseform.h b/src/editing/baseform.h index 9a46a8a..80dc3aa 100644 --- a/src/editing/baseform.h +++ b/src/editing/baseform.h @@ -9,7 +9,7 @@ #include "rendering/shared/types.h" #include "rendering/tools/curve.h" #include "rendering/tools/color.h" -#include "rendering/tools/pack.h" +#include "PackStream.h" #include "rendering/tools/lighting.h" #include "rendering/noise.h" #include "rendering/layers.h" diff --git a/src/editing/basepreview.h b/src/editing/basepreview.h index 1993a74..69ec388 100644 --- a/src/editing/basepreview.h +++ b/src/editing/basepreview.h @@ -13,7 +13,7 @@ #include "editing/common/DrawingWidget.h" #include "editing/common/previewrenderer.h" -#include "rendering/tools/pack.h" +#include "PackStream.h" #include "rendering/tools/color.h" class _ContextChoice diff --git a/src/editing/common/mainwindow.h b/src/editing/common/mainwindow.h index 4bdf527..c425a83 100644 --- a/src/editing/common/mainwindow.h +++ b/src/editing/common/mainwindow.h @@ -3,7 +3,7 @@ #include #include -#include "rendering/tools/pack.h" +#include "PackStream.h" class BaseForm; class FormRender; diff --git a/src/editing/formwater.cpp b/src/editing/formwater.cpp index 2a8238d..23c94c7 100644 --- a/src/editing/formwater.cpp +++ b/src/editing/formwater.cpp @@ -121,7 +121,7 @@ protected: cameraSetLocation(_renderer->render_camera, camera_location); } - static double _getWaterHeight(Renderer* renderer) + static double _getWaterHeight(Renderer*) { return 0.0; } diff --git a/src/editing/lighting/DialogMaterialEditor.cpp b/src/editing/lighting/DialogMaterialEditor.cpp index fc57691..f063957 100644 --- a/src/editing/lighting/DialogMaterialEditor.cpp +++ b/src/editing/lighting/DialogMaterialEditor.cpp @@ -69,6 +69,6 @@ void DialogMaterialEditor::commitLocalDataToScenery() { } -void DialogMaterialEditor::alterRenderer(Renderer* renderer) +void DialogMaterialEditor::alterRenderer(Renderer*) { } diff --git a/src/editing/terrain/paintingbrush.cpp b/src/editing/terrain/paintingbrush.cpp index 7ec28db..5c70582 100644 --- a/src/editing/terrain/paintingbrush.cpp +++ b/src/editing/terrain/paintingbrush.cpp @@ -76,9 +76,8 @@ double PaintingBrush::getInfluence(double relative_x, double relative_z) } } -void PaintingBrush::drawPreview(QWidget* widget) +void PaintingBrush::drawPreview(QWidget*) { - } QString PaintingBrush::getHelpText() diff --git a/src/editing/terrain/widgetheightmap.cpp b/src/editing/terrain/widgetheightmap.cpp index b8fcb58..bc135ff 100644 --- a/src/editing/terrain/widgetheightmap.cpp +++ b/src/editing/terrain/widgetheightmap.cpp @@ -187,16 +187,16 @@ void WidgetHeightMap::mouseReleaseEvent(QMouseEvent*) void WidgetHeightMap::mouseMoveEvent(QMouseEvent* event) { - if (event->buttons() & Qt::MiddleButton) + /*if (event->buttons() & Qt::MiddleButton) { // Rotate around the turntable int move_x = event->x() - _last_mouse_x; int move_y = event->y() - _last_mouse_y; // TODO - /*_angle_h -= (double)move_x * 0.008; - _angle_v += (double)move_y * 0.003;*/ - } + //_angle_h -= (double)move_x * 0.008; + //_angle_v += (double)move_y * 0.003; + }*/ _last_mouse_x = event->x(); _last_mouse_y = event->y(); diff --git a/src/paysages.pro b/src/paysages.pro index 96feac6..b72099f 100644 --- a/src/paysages.pro +++ b/src/paysages.pro @@ -3,18 +3,19 @@ CONFIG += ordered SUBDIRS = \ system \ + basics \ + definition \ rendering \ exploring \ editing \ - controlling \ - definition \ - basics + controlling unix:SUBDIRS += testing -rendering.depends = system +basics.depends = system +definition.depends = basics +rendering.depends = definition exploring.depends = rendering -definition.depends = rendering system editing.depends = exploring rendering controlling.depends = rendering unix:testing.depends = rendering diff --git a/src/rendering/Scenery.h b/src/rendering/Scenery.h index bb8a877..3f7e6b6 100644 --- a/src/rendering/Scenery.h +++ b/src/rendering/Scenery.h @@ -9,7 +9,7 @@ #include "rendering/terrain/public.h" #include "rendering/textures/public.h" #include "rendering/water/public.h" -#include "rendering/tools/pack.h" +#include "PackStream.h" #include "rendering/renderer.h" #ifdef __cplusplus diff --git a/src/rendering/atmosphere/public.h b/src/rendering/atmosphere/public.h index b4fb854..9a9e53f 100644 --- a/src/rendering/atmosphere/public.h +++ b/src/rendering/atmosphere/public.h @@ -5,7 +5,7 @@ #include "../tools/lighting.h" #include "../tools/euclid.h" #include "../tools/color.h" -#include "../tools/pack.h" +#include "PackStream.h" #include "../layers.h" #include "../shared/types.h" diff --git a/src/rendering/camera.h b/src/rendering/camera.h index 9548606..41fa00d 100644 --- a/src/rendering/camera.h +++ b/src/rendering/camera.h @@ -2,7 +2,7 @@ #define _PAYSAGES_CAMERA_H_ #include "rendering_global.h" -#include "tools/pack.h" +#include "PackStream.h" #include "tools/euclid.h" #include "tools/boundingbox.h" diff --git a/src/rendering/geoarea.h b/src/rendering/geoarea.h index 01067c2..73f5da8 100644 --- a/src/rendering/geoarea.h +++ b/src/rendering/geoarea.h @@ -4,7 +4,7 @@ /* Geographic area definition */ #include "rendering_global.h" -#include "tools/pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/layers.h b/src/rendering/layers.h index 27820ce..f2bb1db 100644 --- a/src/rendering/layers.h +++ b/src/rendering/layers.h @@ -4,7 +4,7 @@ /* Factorized layer management (with names) */ #include "rendering_global.h" -#include "tools/pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/noise.h b/src/rendering/noise.h index 9612a7c..25eeb00 100644 --- a/src/rendering/noise.h +++ b/src/rendering/noise.h @@ -2,7 +2,7 @@ #define _PAYSAGES_NOISE_H_ #include "rendering_global.h" -#include "tools/pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/noisenaive.h b/src/rendering/noisenaive.h index 4d55854..2239ece 100644 --- a/src/rendering/noisenaive.h +++ b/src/rendering/noisenaive.h @@ -2,7 +2,7 @@ #define _PAYSAGES_NOISENAIVE_H_ #include "rendering_global.h" -#include "tools/pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/rendering.pro b/src/rendering/rendering.pro index e6d3055..a78c0d4 100644 --- a/src/rendering/rendering.pro +++ b/src/rendering/rendering.pro @@ -48,7 +48,6 @@ SOURCES += main.c \ tools/zone.c \ tools/texture.c \ tools/parallel.c \ - tools/pack.c \ tools/memory.c \ tools/lighting.c \ tools/euclid.c \ @@ -96,7 +95,6 @@ HEADERS += \ tools/zone.h \ tools/texture.h \ tools/parallel.h \ - tools/pack.h \ tools/memory.h \ tools/lighting.h \ tools/euclid.h \ @@ -116,3 +114,15 @@ else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../system/debug/ -l else:unix: LIBS += -L$$OUT_PWD/../system/ -lpaysages_system INCLUDEPATH += $$PWD/../system DEPENDPATH += $$PWD/../system + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../basics/release/ -lpaysages_basics +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../basics/debug/ -lpaysages_basics +else:unix: LIBS += -L$$OUT_PWD/../basics/ -lpaysages_basics +INCLUDEPATH += $$PWD/../basics +DEPENDPATH += $$PWD/../basics + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../definition/release/ -lpaysages_definition +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../definition/debug/ -lpaysages_definition +else:unix: LIBS += -L$$OUT_PWD/../definition/ -lpaysages_definition +INCLUDEPATH += $$PWD/../definition +DEPENDPATH += $$PWD/../definition diff --git a/src/rendering/rendering_global.h b/src/rendering/rendering_global.h index a82fb9c..57c5639 100644 --- a/src/rendering/rendering_global.h +++ b/src/rendering/rendering_global.h @@ -24,9 +24,13 @@ namespace paysages { namespace system {} + namespace basics {} + namespace definition {} namespace rendering {} } using namespace paysages::system; +using namespace paysages::basics; +using namespace paysages::definition; using namespace paysages::rendering; #endif diff --git a/src/rendering/tools/color.h b/src/rendering/tools/color.h index 7b15a21..65dfacb 100644 --- a/src/rendering/tools/color.h +++ b/src/rendering/tools/color.h @@ -3,7 +3,7 @@ #include "../rendering_global.h" #include "curve.h" -#include "pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/tools/curve.h b/src/rendering/tools/curve.h index 8d51ba5..3027652 100644 --- a/src/rendering/tools/curve.h +++ b/src/rendering/tools/curve.h @@ -2,7 +2,7 @@ #define _PAYSAGES_TOOLS_CURVE_H_ #include "../rendering_global.h" -#include "pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/tools/euclid.h b/src/rendering/tools/euclid.h index 45c073e..064b100 100644 --- a/src/rendering/tools/euclid.h +++ b/src/rendering/tools/euclid.h @@ -2,7 +2,7 @@ #define _PAYSAGES_TOOLS_EUCLID_H_ #include "../rendering_global.h" -#include "pack.h" +#include "PackStream.h" /* * Cartesian coordinates (X, Y, Z) - right handed : diff --git a/src/rendering/tools/lighting.h b/src/rendering/tools/lighting.h index 3b113a7..7c6e0a9 100644 --- a/src/rendering/tools/lighting.h +++ b/src/rendering/tools/lighting.h @@ -4,7 +4,7 @@ #include "../rendering_global.h" #include "euclid.h" #include "color.h" -#include "pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/rendering/tools/pack.c b/src/rendering/tools/pack.c deleted file mode 100644 index f03723c..0000000 --- a/src/rendering/tools/pack.c +++ /dev/null @@ -1,195 +0,0 @@ -#include "pack.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "color.h" -#include "euclid.h" - -struct PackStream -{ - FILE* fd; - int write; -}; - -#define pack754_32(f) (pack754((f), 32, 8)) -#define pack754_64(f) (pack754((f), 64, 11)) -#define unpack754_32(i) (unpack754((i), 32, 8)) -#define unpack754_64(i) (unpack754((i), 64, 11)) - -static uint64_t pack754(double f, unsigned bits, unsigned expbits) -{ - double fnorm; - int shift; - long long sign, exp, significand; - unsigned significandbits = bits - expbits - 1; // -1 for sign bit - - if (f == 0.0) return 0; // get this special case out of the way - - // check sign and begin normalization - if (f < 0) { sign = 1; fnorm = -f; } - else { sign = 0; fnorm = f; } - - // get the normalized form of f and track the exponent - shift = 0; - while(fnorm >= 2.0) { fnorm /= 2.0; shift++; } - while(fnorm < 1.0) { fnorm *= 2.0; shift--; } - fnorm = fnorm - 1.0; - - // calculate the binary form (non-double) of the significand data - significand = fnorm * ((1LL<>significandbits)&((1LL< 0) { result *= 2.0; shift--; } - while(shift < 0) { result /= 2.0; shift++; } - - // sign it - result *= (i>>(bits-1))&1? -1.0: 1.0; - - return result; -} - -PackStream* packReadFile(const char* filepath) -{ - PackStream* result; - - result = malloc(sizeof(PackStream)); - result->fd = fopen(filepath, "rb"); - result->write = 0; - - return result; -} - -PackStream* packWriteFile(const char* filepath) -{ - PackStream* result; - - result = malloc(sizeof(PackStream)); - result->fd = fopen(filepath, "wb"); - result->write = 1; - - return result; -} - -void packCloseStream(PackStream* stream) -{ - if (stream->write) - { - fflush(stream->fd); - } - fclose(stream->fd); - free(stream); -} - -void packWriteDouble(PackStream* stream, double* value) -{ - int written; - uint64_t servalue; - - servalue = pack754_64(*value); - written = fwrite(&servalue, sizeof(uint64_t), 1, stream->fd); - assert(written == 1); -} - -void packReadDouble(PackStream* stream, double* value) -{ - int read; - uint64_t servalue; - - read = fread(&servalue, sizeof(uint64_t), 1, stream->fd); - assert(read == 1); - - *value = unpack754_64(servalue); -} - -void packWriteInt(PackStream* stream, int* value) -{ - int written; - - written = fprintf(stream->fd, "%d;", *value); - assert(written > 1); -} - -void packReadInt(PackStream* stream, int* value) -{ - int read; - - read = fscanf(stream->fd, "%d;", value); - assert(read == 1); -} - -void packWriteString(PackStream* stream, char* value, int max_length) -{ - int written; - int len = 0; - while (len < max_length - 1 && value[len] != '\0') - { - len++; - } - packWriteInt(stream, &len); - if (len > 0) - { - written = fwrite(value, 1, len, stream->fd); - assert(written == len); - } -} - -void packReadString(PackStream* stream, char* value, int max_length) -{ - int read; - int len, clen; - - packReadInt(stream, &len); - - if (len > max_length - 1) - { - clen = max_length - 1; - } - else - { - clen = len; - } - - if (clen > 0) - { - read = fread(value, 1, clen, stream->fd); - assert(read == clen); - value[clen] = '\0'; - - if (clen < len) - { - /* Read rest of the string, discarding it */ - char* buffer = malloc(len - clen); - fread(buffer, 1, len - clen, stream->fd); - free(buffer); - } - } -} diff --git a/src/rendering/tools/pack.h b/src/rendering/tools/pack.h deleted file mode 100644 index e83ceac..0000000 --- a/src/rendering/tools/pack.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef _PAYSAGES_TOOLS_PACK_H_ -#define _PAYSAGES_TOOLS_PACK_H_ - -#include "../rendering_global.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct PackStream PackStream; - -RENDERINGSHARED_EXPORT PackStream* packReadFile(const char* filepath); -RENDERINGSHARED_EXPORT PackStream* packWriteFile(const char* filepath); -RENDERINGSHARED_EXPORT void packCloseStream(PackStream* stream); - -RENDERINGSHARED_EXPORT void packWriteDouble(PackStream* stream, double* value); -RENDERINGSHARED_EXPORT void packReadDouble(PackStream* stream, double* value); -RENDERINGSHARED_EXPORT void packWriteInt(PackStream* stream, int* value); -RENDERINGSHARED_EXPORT void packReadInt(PackStream* stream, int* value); -RENDERINGSHARED_EXPORT void packWriteString(PackStream* stream, char* value, int max_length); -RENDERINGSHARED_EXPORT void packReadString(PackStream* stream, char* value, int max_length); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/rendering/tools/texture.c b/src/rendering/tools/texture.c index 3294a36..b80706d 100644 --- a/src/rendering/tools/texture.c +++ b/src/rendering/tools/texture.c @@ -1,5 +1,6 @@ #include "texture.h" +#include #include #include #include "System.h" diff --git a/src/rendering/tools/texture.h b/src/rendering/tools/texture.h index 6d3a74f..5e349b0 100644 --- a/src/rendering/tools/texture.h +++ b/src/rendering/tools/texture.h @@ -11,7 +11,7 @@ extern "C" { #include "../rendering_global.h" #include "color.h" -#include "pack.h" +#include "PackStream.h" typedef struct Texture2D Texture2D; typedef struct Texture3D Texture3D; diff --git a/src/rendering/tools/zone.h b/src/rendering/tools/zone.h index f9c040d..43bf798 100644 --- a/src/rendering/tools/zone.h +++ b/src/rendering/tools/zone.h @@ -8,7 +8,7 @@ #include "../rendering_global.h" #include "rendering/tools/euclid.h" #include "rendering/tools/curve.h" -#include "rendering/tools/pack.h" +#include "PackStream.h" #ifdef __cplusplus extern "C" { diff --git a/src/system/PackStream.cpp b/src/system/PackStream.cpp new file mode 100644 index 0000000..fe2d96f --- /dev/null +++ b/src/system/PackStream.cpp @@ -0,0 +1,141 @@ +#include "PackStream.h" + +#include +#include +#include + +PackStream::PackStream() +{ + file = NULL; + stream = NULL; +} + +PackStream::~PackStream() +{ + if (stream) + { + delete stream; + } + if (file) + { + delete file; + } +} + +void PackStream::bindToFile(const char* filepath, bool write) +{ + if (not file and not stream) + { + file = new QFile(filepath); + file->open(write ? QIODevice::WriteOnly : QIODevice::ReadOnly); + + stream = new QDataStream(file); + } +} + +void PackStream::write(int* value) +{ + if (stream and value) + { + *stream << *value; + } +} + +void PackStream::write(double* value) +{ + if (stream and value) + { + *stream << *value; + } +} + +void PackStream::write(char* value, int max_length) +{ + if (stream and value) + { + int length = qstrlen(value); + *stream << QString::fromUtf8(value, length > max_length ? max_length : length); + } +} + +void PackStream::read(int* value) +{ + if (stream and value and not stream->atEnd()) + { + int output; + *stream >> output; + *value = output; + } +} + +void PackStream::read(double* value) +{ + if (stream and value and not stream->atEnd()) + { + double output; + *stream >> output; + *value = output; + } +} + +void PackStream::read(char* value, int max_length) +{ + if (stream and value and not stream->atEnd()) + { + QString output; + *stream >> output; + QByteArray array = output.toUtf8(); + qstrncpy(value, array.constData(), max_length); + } +} + +// Transitional C-API + +PackStream* packReadFile(const char* filepath) +{ + PackStream* result = new PackStream(); + result->bindToFile(filepath, false); + return result; +} + +PackStream* packWriteFile(const char* filepath) +{ + PackStream* result = new PackStream(); + result->bindToFile(filepath, true); + return result; +} + +void packCloseStream(PackStream* stream) +{ + delete stream; +} + +void packWriteDouble(PackStream* stream, double* value) +{ + stream->write(value); +} + +void packReadDouble(PackStream* stream, double* value) +{ + stream->read(value); +} + +void packWriteInt(PackStream* stream, int* value) +{ + stream->write(value); +} + +void packReadInt(PackStream* stream, int* value) +{ + stream->read(value); +} + +void packWriteString(PackStream* stream, char* value, int max_length) +{ + stream->write(value, max_length); +} + +void packReadString(PackStream* stream, char* value, int max_length) +{ + stream->read(value, max_length); +} diff --git a/src/system/PackStream.h b/src/system/PackStream.h new file mode 100644 index 0000000..12b76ed --- /dev/null +++ b/src/system/PackStream.h @@ -0,0 +1,67 @@ +#ifndef PACKSTREAM_H +#define PACKSTREAM_H + +#include "system_global.h" + +#ifdef __cplusplus + +class QFile; +class QDataStream; + +namespace paysages +{ +namespace system +{ + +/*! + * \brief Data (de)serialization in files or streams. + */ +class SYSTEMSHARED_EXPORT PackStream +{ +public: + PackStream(); + ~PackStream(); + + void bindToFile(const char* filepath, bool write=false); + + void write(int* value); + void write(double* value); + void write(char* value, int max_length); + + void read(int* value); + void read(double* value); + void read(char* value, int max_length); + +private: + QFile* file; + QDataStream* stream; +}; + +} +} + +extern "C" { +#endif + +// Transitional C-API + +#ifndef __cplusplus +typedef struct PackStream PackStream; +#endif + +SYSTEMSHARED_EXPORT PackStream* packReadFile(const char* filepath); +SYSTEMSHARED_EXPORT PackStream* packWriteFile(const char* filepath); +SYSTEMSHARED_EXPORT void packCloseStream(PackStream* stream); + +SYSTEMSHARED_EXPORT void packWriteDouble(PackStream* stream, double* value); +SYSTEMSHARED_EXPORT void packReadDouble(PackStream* stream, double* value); +SYSTEMSHARED_EXPORT void packWriteInt(PackStream* stream, int* value); +SYSTEMSHARED_EXPORT void packReadInt(PackStream* stream, int* value); +SYSTEMSHARED_EXPORT void packWriteString(PackStream* stream, char* value, int max_length); +SYSTEMSHARED_EXPORT void packReadString(PackStream* stream, char* value, int max_length); + +#ifdef __cplusplus +} +#endif + +#endif // PACKSTREAM_H diff --git a/src/system/system.pro b/src/system/system.pro index 46bea97..3806695 100644 --- a/src/system/system.pro +++ b/src/system/system.pro @@ -16,14 +16,16 @@ SOURCES += \ PictureFile.cpp \ Thread.cpp \ Mutex.cpp \ - System.cpp + System.cpp \ + PackStream.cpp HEADERS += \ system_global.h \ PictureFile.h \ Thread.h \ Mutex.h \ - System.h + System.h \ + PackStream.h unix:!symbian { maemo5 { diff --git a/src/testing/test_pack.c b/src/testing/test_pack.c index 962fbec..7799bb4 100644 --- a/src/testing/test_pack.c +++ b/src/testing/test_pack.c @@ -1,6 +1,6 @@ #include "testing/common.h" -#include "rendering/tools/pack.h" +#include "PackStream.h" START_TEST(testPack) { @@ -21,7 +21,7 @@ START_TEST(testPack) data_d = (double)i; packWriteDouble(stream, &data_d); - data_s = "Testing string 0123 !"; + data_s = "Testing string 0123 (accentué) !"; packWriteString(stream, data_s, 100); packWriteString(stream, data_s, 4); packWriteString(stream, data_s, 5); @@ -39,7 +39,7 @@ START_TEST(testPack) ck_assert_double_eq(data_d, (double)i); packReadString(stream, buffer, 100); - ck_assert_str_eq(buffer, "Testing string 0123 !"); + ck_assert_str_eq(buffer, "Testing string 0123 (accentué) !"); packReadString(stream, buffer, 4); ck_assert_str_eq(buffer, "Tes"); packReadString(stream, buffer, 3); diff --git a/src/testing/testing.pro b/src/testing/testing.pro index d7820a2..05fc92e 100644 --- a/src/testing/testing.pro +++ b/src/testing/testing.pro @@ -13,11 +13,6 @@ unix { INCLUDEPATH += $$PWD/.. -win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../rendering/release/ -lpaysages_rendering -else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../rendering/debug/ -lpaysages_rendering -else:unix: LIBS += -L$$OUT_PWD/../rendering/ -lpaysages_rendering -DEPENDPATH += $$PWD/../rendering - SOURCES += main.c \ test_terrain_painting.c \ test_render.c \ @@ -37,3 +32,21 @@ else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../system/debug/ -l else:unix: LIBS += -L$$OUT_PWD/../system/ -lpaysages_system INCLUDEPATH += $$PWD/../system DEPENDPATH += $$PWD/../system + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../basics/release/ -lpaysages_basics +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../basics/debug/ -lpaysages_basics +else:unix: LIBS += -L$$OUT_PWD/../basics/ -lpaysages_basics +INCLUDEPATH += $$PWD/../basics +DEPENDPATH += $$PWD/../basics + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../definition/release/ -lpaysages_definition +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../definition/debug/ -lpaysages_definition +else:unix: LIBS += -L$$OUT_PWD/../definition/ -lpaysages_definition +INCLUDEPATH += $$PWD/../definition +DEPENDPATH += $$PWD/../definition + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../rendering/release/ -lpaysages_rendering +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../rendering/debug/ -lpaysages_rendering +else:unix: LIBS += -L$$OUT_PWD/../rendering/ -lpaysages_rendering +INCLUDEPATH += $$PWD/../rendering +DEPENDPATH += $$PWD/../rendering