From 3eedaf1366d60ad2c990cf814ec5bb7e632f1d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Thu, 5 Jul 2012 17:05:03 +0000 Subject: [PATCH] paysages : Added layer naming (WIP). git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@380 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- gui_qt/baseform.cpp | 19 +++++++++++++------ gui_qt/baseform.h | 4 +++- gui_qt/formclouds.cpp | 20 ++++++++++++++++---- gui_qt/formclouds.h | 1 + gui_qt/formtextures.cpp | 20 ++++++++++++++++---- gui_qt/formtextures.h | 1 + lib_paysages/auto.c | 2 ++ lib_paysages/clouds.c | 9 +++++++++ lib_paysages/clouds.h | 3 +++ lib_paysages/pack.c | 22 ++++++++++++++++++++++ lib_paysages/pack.h | 2 ++ lib_paysages/textures.c | 10 ++++++++++ lib_paysages/textures.h | 3 +++ 13 files changed, 101 insertions(+), 15 deletions(-) diff --git a/gui_qt/baseform.cpp b/gui_qt/baseform.cpp index a1c3f48..59e51d4 100644 --- a/gui_qt/baseform.cpp +++ b/gui_qt/baseform.cpp @@ -13,6 +13,7 @@ #include #include #include +#include BaseForm::BaseForm(QWidget* parent, bool auto_apply, bool with_layers) : QWidget(parent) { @@ -187,6 +188,7 @@ void BaseForm::revertConfig() if (_with_layers) { + rebuildLayerList(); if (_layer_list->currentIndex() < 0 && _layer_list->count() > 0) { _layer_list->setCurrentIndex(0); @@ -214,10 +216,13 @@ void BaseForm::rebuildLayerList() { int selected = _layer_list->currentIndex(); _layer_list->clear(); + + _layer_names = getLayers(); + _layer_count = _layer_names.count(); for (int i = 0; i < _layer_count; i++) { - _layer_list->addItem(QString(tr("Layer %1")).arg(i + 1)); + _layer_list->addItem(QString(tr("Layer %1 - %2")).arg(i + 1).arg(_layer_names[i])); } if (selected >= 0) { @@ -416,22 +421,24 @@ int BaseForm::currentLayer() } } -void BaseForm::setLayerCount(int layer_count) +QStringList BaseForm::getLayers() { - this->_layer_count = layer_count; - rebuildLayerList(); + return QStringList(); } void BaseForm::layerAddedEvent() { + rebuildLayerList(); } -void BaseForm::layerDeletedEvent(int layer) +void BaseForm::layerDeletedEvent(int) { + rebuildLayerList(); } -void BaseForm::layerMovedEvent(int layer, int new_position) +void BaseForm::layerMovedEvent(int, int) { + rebuildLayerList(); } void BaseForm::layerSelectedEvent(int layer) diff --git a/gui_qt/baseform.h b/gui_qt/baseform.h index 708653e..82863a5 100644 --- a/gui_qt/baseform.h +++ b/gui_qt/baseform.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "basepreview.h" #include "baseinput.h" #include "../lib_paysages/shared/types.h" @@ -58,7 +59,7 @@ protected: void disablePreviewsUpdate(); int currentLayer(); - void setLayerCount(int layer_count); + virtual QStringList getLayers(); virtual void layerAddedEvent(); virtual void layerDeletedEvent(int layer); @@ -71,6 +72,7 @@ private: bool _with_layers; QComboBox* _layer_list; int _layer_count; + QStringList _layer_names; QPushButton* _layer_new; QPushButton* _layer_del; QPushButton* _layer_up; diff --git a/gui_qt/formclouds.cpp b/gui_qt/formclouds.cpp index 7389363..35c180f 100644 --- a/gui_qt/formclouds.cpp +++ b/gui_qt/formclouds.cpp @@ -175,7 +175,6 @@ FormClouds::FormClouds(QWidget *parent): void FormClouds::revertConfig() { sceneryGetClouds(&_definition); - setLayerCount(cloudsGetLayerCount(&_definition)); BaseForm::revertConfig(); } @@ -192,11 +191,26 @@ void FormClouds::configChangeEvent() BaseForm::configChangeEvent(); } +QStringList FormClouds::getLayers() +{ + QStringList result; + CloudsLayerDefinition* layer; + int i, n; + + n = cloudsGetLayerCount(&_definition); + for (i = 0; i < n; i++) + { + layer = cloudsGetLayer(&_definition, i); + result << QString(layer->name); + } + + return result; +} + void FormClouds::layerAddedEvent() { if (cloudsAddLayer(&_definition) >= 0) { - setLayerCount(cloudsGetLayerCount(&_definition)); BaseForm::layerAddedEvent(); } } @@ -204,7 +218,6 @@ void FormClouds::layerAddedEvent() void FormClouds::layerMovedEvent(int layer, int new_position) { cloudsMoveLayer(&_definition, layer, new_position); - setLayerCount(cloudsGetLayerCount(&_definition)); BaseForm::layerMovedEvent(layer, new_position); } @@ -212,7 +225,6 @@ void FormClouds::layerMovedEvent(int layer, int new_position) void FormClouds::layerDeletedEvent(int layer) { cloudsDeleteLayer(&_definition, layer); - setLayerCount(cloudsGetLayerCount(&_definition)); BaseForm::layerDeletedEvent(layer); } diff --git a/gui_qt/formclouds.h b/gui_qt/formclouds.h index f2d2984..2193cc1 100644 --- a/gui_qt/formclouds.h +++ b/gui_qt/formclouds.h @@ -20,6 +20,7 @@ protected slots: virtual void configChangeEvent(); protected: + virtual QStringList getLayers(); virtual void layerAddedEvent(); virtual void layerDeletedEvent(int layer); virtual void layerMovedEvent(int layer, int new_position); diff --git a/gui_qt/formtextures.cpp b/gui_qt/formtextures.cpp index 0fd499e..2b6247c 100644 --- a/gui_qt/formtextures.cpp +++ b/gui_qt/formtextures.cpp @@ -159,7 +159,6 @@ FormTextures::~FormTextures() void FormTextures::revertConfig() { sceneryGetTextures(&_definition); - setLayerCount(texturesGetLayerCount(&_definition)); BaseForm::revertConfig(); } @@ -181,11 +180,26 @@ void FormTextures::configChangeEvent() BaseForm::configChangeEvent(); } +QStringList FormTextures::getLayers() +{ + QStringList result; + TextureLayerDefinition* layer; + int i, n; + + n = texturesGetLayerCount(&_definition); + for (i = 0; i < n; i++) + { + layer = texturesGetLayer(&_definition, i); + result << QString(layer->name); + } + + return result; +} + void FormTextures::layerAddedEvent() { if (texturesAddLayer(&_definition) >= 0) { - setLayerCount(texturesGetLayerCount(&_definition)); BaseForm::layerAddedEvent(); } } @@ -193,7 +207,6 @@ void FormTextures::layerAddedEvent() void FormTextures::layerDeletedEvent(int layer) { texturesDeleteLayer(&_definition, layer); - setLayerCount(texturesGetLayerCount(&_definition)); BaseForm::layerDeletedEvent(layer); } @@ -201,7 +214,6 @@ void FormTextures::layerDeletedEvent(int layer) void FormTextures::layerMovedEvent(int layer, int new_position) { texturesMoveLayer(&_definition, layer, new_position); - setLayerCount(texturesGetLayerCount(&_definition)); BaseForm::layerMovedEvent(layer, new_position); } diff --git a/gui_qt/formtextures.h b/gui_qt/formtextures.h index 20f484e..9b2b7f3 100644 --- a/gui_qt/formtextures.h +++ b/gui_qt/formtextures.h @@ -14,6 +14,7 @@ public: ~FormTextures(); protected: + virtual QStringList getLayers(); virtual void layerAddedEvent(); virtual void layerDeletedEvent(int layer); virtual void layerMovedEvent(int layer, int new_position); diff --git a/lib_paysages/auto.c b/lib_paysages/auto.c index e5fcfab..c32617d 100644 --- a/lib_paysages/auto.c +++ b/lib_paysages/auto.c @@ -147,6 +147,7 @@ void autoGenRealisticLandscape(int seed) /* Textures */ textures = texturesCreateDefinition(); texture = texturesGetLayer(&textures, texturesAddLayer(&textures)); + texturesLayerSetName(texture, "Ground"); noiseGenerateBaseNoise(texture->bump_noise, 102400); noiseClearLevels(texture->bump_noise); noiseAddLevelsSimple(texture->bump_noise, 8, 1.0, 1.0); @@ -161,6 +162,7 @@ void autoGenRealisticLandscape(int seed) texture->slope_range = 0.001; texture->thickness_transparency = 0.0; texture = texturesGetLayer(&textures, texturesAddLayer(&textures)); + texturesLayerSetName(texture, "Grass"); zoneAddHeightRangeQuick(texture->zone, 1.0, -6.0, -5.0, 3.0, 15.0); zoneAddSlopeRangeQuick(texture->zone, 1.0, 0.0, 0.0, 0.05, 0.4); noiseGenerateBaseNoise(texture->bump_noise, 102400); diff --git a/lib_paysages/clouds.c b/lib_paysages/clouds.c index 5f1f8a0..8e20681 100644 --- a/lib_paysages/clouds.c +++ b/lib_paysages/clouds.c @@ -41,6 +41,7 @@ void cloudsSave(PackStream* stream, CloudsDefinition* definition) { layer = definition->layers + i; + packWriteString(stream, layer->name, CLOUDS_MAX_NAME_LENGTH); packWriteDouble(stream, &layer->lower_altitude); packWriteDouble(stream, &layer->thickness); curveSave(stream, layer->coverage_by_altitude); @@ -73,6 +74,7 @@ void cloudsLoad(PackStream* stream, CloudsDefinition* definition) { layer = definition->layers + cloudsAddLayer(definition); + packReadString(stream, layer->name, CLOUDS_MAX_NAME_LENGTH); packReadDouble(stream, &layer->lower_altitude); packReadDouble(stream, &layer->thickness); curveLoad(stream, layer->coverage_by_altitude); @@ -148,6 +150,7 @@ CloudsLayerDefinition cloudsLayerCreateDefinition() { CloudsLayerDefinition result; + cloudsLayerSetName(&result, "Unnamed"); result.lower_altitude = 4.0; result.thickness = 6.0; result.coverage_by_altitude = curveCreate(); @@ -210,6 +213,7 @@ void cloudsLayerCopyDefinition(CloudsLayerDefinition* source, CloudsLayerDefinit void cloudsLayerValidateDefinition(CloudsLayerDefinition* definition) { + definition->name[CLOUDS_MAX_NAME_LENGTH] = '\0'; if (definition->shape_scaling < 0.0001) { definition->shape_scaling = 0.00001; @@ -224,6 +228,11 @@ void cloudsLayerValidateDefinition(CloudsLayerDefinition* definition) } } +void cloudsLayerSetName(CloudsLayerDefinition* definition, const char* name) +{ + strncpy(definition->name, name, CLOUDS_MAX_NAME_LENGTH); +} + int cloudsGetLayerCount(CloudsDefinition* definition) { return definition->nblayers; diff --git a/lib_paysages/clouds.h b/lib_paysages/clouds.h index 74239d3..724574b 100644 --- a/lib_paysages/clouds.h +++ b/lib_paysages/clouds.h @@ -11,6 +11,7 @@ extern "C" { #endif #define CLOUDS_MAX_LAYERS 6 +#define CLOUDS_MAX_NAME_LENGTH 50 typedef struct CloudsLayerDefinition CloudsLayerDefinition; @@ -18,6 +19,7 @@ typedef double (*CloudCoverageFunc)(CloudsLayerDefinition* definition, Vector3 p struct CloudsLayerDefinition { + char name[CLOUDS_MAX_NAME_LENGTH + 1]; double lower_altitude; double thickness; double base_coverage; @@ -55,6 +57,7 @@ CloudsLayerDefinition cloudsLayerCreateDefinition(); void cloudsLayerDeleteDefinition(CloudsLayerDefinition* definition); void cloudsLayerCopyDefinition(CloudsLayerDefinition* source, CloudsLayerDefinition* destination); void cloudsLayerValidateDefinition(CloudsLayerDefinition* definition); +void cloudsLayerSetName(CloudsLayerDefinition* definition, const char* name); int cloudsGetLayerCount(CloudsDefinition* definition); CloudsLayerDefinition* cloudsGetLayer(CloudsDefinition* definition, int layer); diff --git a/lib_paysages/pack.c b/lib_paysages/pack.c index 0250866..3329f0f 100644 --- a/lib_paysages/pack.c +++ b/lib_paysages/pack.c @@ -3,8 +3,10 @@ #include #include #include +#include #include #include +#include #include "color.h" #include "euclid.h" @@ -138,3 +140,23 @@ void packReadInt(PackStream* stream, int* value) read = fscanf(stream->fd, "%d;", value); assert(read == 1); } + +void packWriteString(PackStream* stream, char* value, int max_length) +{ + int len = strnlen(value, max_length - 1); + packWriteInt(stream, &len); + fwrite(value, len + 1, 1, stream->fd); +} + +void packReadString(PackStream* stream, char* value, int max_length) +{ + int read; + int len; + packReadInt(stream, &len); + if (len > max_length - 1) + { + len = max_length - 1; + } + read = fread(value, len + 1, 1, stream->fd); + assert(read == len + 1); +} diff --git a/lib_paysages/pack.h b/lib_paysages/pack.h index ddc2de7..4fd1483 100644 --- a/lib_paysages/pack.h +++ b/lib_paysages/pack.h @@ -15,6 +15,8 @@ void packWriteDouble(PackStream* stream, double* value); void packReadDouble(PackStream* stream, double* value); void packWriteInt(PackStream* stream, int* value); void packReadInt(PackStream* stream, int* value); +void packWriteString(PackStream* stream, char* value, int max_length); +void packReadString(PackStream* stream, char* value, int max_length); #ifdef __cplusplus } diff --git a/lib_paysages/textures.c b/lib_paysages/textures.c index edf4330..88ab553 100644 --- a/lib_paysages/textures.c +++ b/lib_paysages/textures.c @@ -39,6 +39,7 @@ void texturesSave(PackStream* stream, TexturesDefinition* definition) packWriteInt(stream, &definition->nblayers); for (i = 0; i < definition->nblayers; i++) { + packWriteString(stream, definition->layers[i].name, TEXTURES_MAX_NAME_LENGTH); zoneSave(stream, definition->layers[i].zone); noiseSaveGenerator(stream, definition->layers[i].bump_noise); packWriteDouble(stream, &definition->layers[i].bump_height); @@ -65,6 +66,7 @@ void texturesLoad(PackStream* stream, TexturesDefinition* definition) { layer = definition->layers + texturesAddLayer(definition); + packReadString(stream, layer->name, TEXTURES_MAX_NAME_LENGTH); zoneLoad(stream, layer->zone); noiseLoadGenerator(stream, layer->bump_noise); packReadDouble(stream, &layer->bump_height); @@ -124,6 +126,7 @@ TextureLayerDefinition texturesLayerCreateDefinition() { TextureLayerDefinition result; + texturesLayerSetName(&result, "Unnamed"); result.zone = zoneCreate(); result.bump_noise = noiseCreateGenerator(); noiseGenerateBaseNoise(result.bump_noise, 102400); @@ -148,6 +151,7 @@ void texturesLayerDeleteDefinition(TextureLayerDefinition* definition) void texturesLayerCopyDefinition(TextureLayerDefinition* source, TextureLayerDefinition* destination) { + strncpy(destination->name, source->name, TEXTURES_MAX_NAME_LENGTH); destination->material = source->material; destination->bump_height = source->bump_height; destination->bump_scaling = source->bump_scaling; @@ -160,6 +164,7 @@ void texturesLayerCopyDefinition(TextureLayerDefinition* source, TextureLayerDef void texturesLayerValidateDefinition(TextureLayerDefinition* definition) { + definition->name[TEXTURES_MAX_NAME_LENGTH] = '\0'; if (definition->bump_scaling < 0.000001) { definition->bump_scaling = 0.000001; @@ -170,6 +175,11 @@ void texturesLayerValidateDefinition(TextureLayerDefinition* definition) } } +void texturesLayerSetName(TextureLayerDefinition* definition, const char* name) +{ + strncpy(definition->name, name, TEXTURES_MAX_NAME_LENGTH); +} + int texturesGetLayerCount(TexturesDefinition* definition) { return definition->nblayers; diff --git a/lib_paysages/textures.h b/lib_paysages/textures.h index aed2c71..8841070 100644 --- a/lib_paysages/textures.h +++ b/lib_paysages/textures.h @@ -12,9 +12,11 @@ extern "C" { #endif #define TEXTURES_MAX_LAYERS 50 +#define TEXTURES_MAX_NAME_LENGTH 50 typedef struct { + char name[TEXTURES_MAX_NAME_LENGTH + 1]; Zone* zone; NoiseGenerator* bump_noise; double bump_scaling; @@ -45,6 +47,7 @@ TextureLayerDefinition texturesLayerCreateDefinition(); void texturesLayerDeleteDefinition(TextureLayerDefinition* definition); void texturesLayerCopyDefinition(TextureLayerDefinition* source, TextureLayerDefinition* destination); void texturesLayerValidateDefinition(TextureLayerDefinition* definition); +void texturesLayerSetName(TextureLayerDefinition* definition, const char* name); int texturesGetLayerCount(TexturesDefinition* definition); TextureLayerDefinition* texturesGetLayer(TexturesDefinition* definition, int layer);