diff --git a/.gitmodules b/.gitmodules index edeffe7cc..414132795 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "src/tinydir"] path = src/tinydir url = https://github.com/cxong/tinydir +[submodule "src/tests/cbehave"] + path = src/tests/cbehave + url = https://github.com/cxong/cbehave.git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 130b6385d..f75416bb4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -99,6 +99,7 @@ CONFIGURE_FILE(cdogs/sys_config.h.cmake ${SOURCE_DIRECTORY}/src/cdogs/sys_config CONFIGURE_FILE(${SOURCE_DIRECTORY}/README.md.cmake ${SOURCE_DIRECTORY}/README.md) add_subdirectory(cdogs) +add_subdirectory(tests) set(CDOGS_SDL_SOURCES campaigns.c diff --git a/src/cdogs.c b/src/cdogs.c index 87f0f71d2..e317b40bd 100644 --- a/src/cdogs.c +++ b/src/cdogs.c @@ -964,7 +964,7 @@ int main(int argc, char *argv[]) SetupConfigDir(); ConfigLoadDefault(&gConfig); - ConfigLoad(&gConfig, CONFIG_FILE); + ConfigLoad(&gConfig, GetConfigFilePath(CONFIG_FILE)); LoadCredits(&creditsDisplayer, &tablePurple, &tableDarker); for (i = 1; i < argc; i++) { @@ -1101,7 +1101,7 @@ int main(int argc, char *argv[]) GraphicsTerminate(&gGraphicsDevice); - ConfigSave(&gConfig, CONFIG_FILE); + ConfigSave(&gConfig, GetConfigFilePath(CONFIG_FILE)); SaveTemplates(); FreeSongs(&gMenuSongs); FreeSongs(&gGameSongs); diff --git a/src/cdogs/CMakeLists.txt b/src/cdogs/CMakeLists.txt index b3011c896..24944a310 100644 --- a/src/cdogs/CMakeLists.txt +++ b/src/cdogs/CMakeLists.txt @@ -4,6 +4,7 @@ set(CDOGS_SOURCES automap.c blit.c config.c + config_apply.c defs.c draw.c drawtools.c diff --git a/src/cdogs/config.c b/src/cdogs/config.c index 0d1505328..31328ea40 100644 --- a/src/cdogs/config.c +++ b/src/cdogs/config.c @@ -30,7 +30,6 @@ #include -#include #include #include #include @@ -42,15 +41,15 @@ Config gConfig; void ConfigLoad(Config *config, const char *filename) { - FILE *f = fopen(GetConfigFilePath(filename), "r"); + FILE *f = fopen(filename, "r"); int dummy; int fscanfres; int i; - memset(config, 0, sizeof(Config)); + ConfigLoadDefault(config); if (f == NULL) { - printf("Error loading config '%s'\n", GetConfigFilePath(filename)); + printf("Error loading config '%s'\n", filename); return; } @@ -126,14 +125,12 @@ void ConfigLoad(Config *config, const char *filename) void ConfigSave(Config *config, const char *filename) { - FILE *f = fopen(GetConfigFilePath(filename), "w"); + FILE *f = fopen(filename, "w"); int i; - debug(D_NORMAL, "begin\n"); - if (f == NULL) { - printf("Error saving config '%s'\n", GetConfigFilePath(filename)); + printf("Error saving config '%s'\n", filename); return; } @@ -176,21 +173,12 @@ void ConfigSave(Config *config, const char *filename) config->Graphics.ScaleFactor); fclose(f); - - debug(D_NORMAL, "saved config\n"); -} - -void ConfigApply(Config *config) -{ - BlitSetBrightness(config->Graphics.Brightness); - SoundReconfigure(&gSoundDevice, &config->Sound); - gCampaign.seed = config->Game.RandomSeed; - GraphicsInitialize(&gGraphicsDevice, &config->Graphics, 0); } void ConfigLoadDefault(Config *config) { int i; + memset(config, 0, sizeof(Config)); config->Game.Difficulty = DIFFICULTY_NORMAL; config->Game.EnemyDensity = 100; config->Game.FriendlyFire = 0; diff --git a/src/cdogs/config.h b/src/cdogs/config.h index 2e36b40c6..1776c6808 100644 --- a/src/cdogs/config.h +++ b/src/cdogs/config.h @@ -29,12 +29,23 @@ #ifndef __CONFIG #define __CONFIG -#include "gamedata.h" +#include "grafx.h" #include "input.h" #include "sounds.h" #define CONFIG_FILE "options.cnf" +typedef enum +{ + DIFFICULTY_VERYEASY = 1, + DIFFICULTY_EASY, + DIFFICULTY_NORMAL, + DIFFICULTY_HARD, + DIFFICULTY_VERYHARD +} difficulty_e; + +const char *DifficultyStr(difficulty_e d); + typedef struct { input_device_e Device; diff --git a/src/cdogs/config_apply.c b/src/cdogs/config_apply.c new file mode 100644 index 000000000..8f0bd7e63 --- /dev/null +++ b/src/cdogs/config_apply.c @@ -0,0 +1,38 @@ +/* + C-Dogs SDL + A port of the legendary (and fun) action/arcade cdogs. + + Copyright (c) 2013, Cong Xu + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ +#include "config.h" + + +void ConfigApply(Config *config) +{ + BlitSetBrightness(config->Graphics.Brightness); + SoundReconfigure(&gSoundDevice, &config->Sound); + gCampaign.seed = config->Game.RandomSeed; + GraphicsInitialize(&gGraphicsDevice, &config->Graphics, 0); +} diff --git a/src/cdogs/gamedata.h b/src/cdogs/gamedata.h index 9bc5e0f60..e4b69aac0 100644 --- a/src/cdogs/gamedata.h +++ b/src/cdogs/gamedata.h @@ -81,17 +81,6 @@ struct PlayerData extern struct PlayerData gPlayer1Data; extern struct PlayerData gPlayer2Data; -typedef enum -{ - DIFFICULTY_VERYEASY = 1, - DIFFICULTY_EASY, - DIFFICULTY_NORMAL, - DIFFICULTY_HARD, - DIFFICULTY_VERYHARD -} difficulty_e; - -const char *DifficultyStr(difficulty_e d); - struct GameOptions { int twoPlayers; int badGuys; diff --git a/src/cdogs/grafx.h b/src/cdogs/grafx.h index 9e728e072..f8a4b027e 100644 --- a/src/cdogs/grafx.h +++ b/src/cdogs/grafx.h @@ -49,7 +49,7 @@ #ifndef __GRAFX #define __GRAFX -#include +#include #include "sys_specifics.h" diff --git a/src/cdogsed.c b/src/cdogsed.c index 527144b94..e4b4bdf0b 100644 --- a/src/cdogsed.c +++ b/src/cdogsed.c @@ -1551,7 +1551,7 @@ int main(int argc, char *argv[]) CDogsTextInit(GetDataFilePath("graphics/font.px"), -2); ConfigLoadDefault(&gConfig); - ConfigLoad(&gConfig, CONFIG_FILE); + ConfigLoad(&gConfig, GetConfigFilePath(CONFIG_FILE)); GraphicsInitialize(&gGraphicsDevice, &gConfig.Graphics, 0); if (!gGraphicsDevice.IsInitialized) { diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt new file mode 100644 index 000000000..a17471ca4 --- /dev/null +++ b/src/tests/CMakeLists.txt @@ -0,0 +1,10 @@ +if(MSVC) + add_definitions(-wd"4127" -wd"4102") +endif() + +add_subdirectory(cbehave) + +include_directories(. ../cdogs) + +add_executable(config_test config_test.c ../cdogs/config.h ../cdogs/config.c) +target_link_libraries(config_test cbehave) \ No newline at end of file diff --git a/src/tests/cbehave b/src/tests/cbehave new file mode 160000 index 000000000..6035b7166 --- /dev/null +++ b/src/tests/cbehave @@ -0,0 +1 @@ +Subproject commit 6035b71668a958db9ac76b9996066f18d3824a24 diff --git a/src/tests/config_test.c b/src/tests/config_test.c new file mode 100644 index 000000000..b5fbcabf4 --- /dev/null +++ b/src/tests/config_test.c @@ -0,0 +1,56 @@ +#include + +#include + + +FEATURE(1, "Load default config") + SCENARIO("Load a default config") + { + Config config1, config2; + GIVEN("two configs") + GIVEN_END + + WHEN("I load them both with defaults") + ConfigLoadDefault(&config1); + ConfigLoadDefault(&config2); + WHEN_END + + THEN("they should equal each other") + SHOULD_MEM_EQUAL(&config1, &config2, sizeof(Config)); + THEN_END + } + SCENARIO_END +FEATURE_END + +FEATURE(2, "Save and load") + SCENARIO("Save and load a config file") + { + Config config1, config2; + GIVEN("a config file with some values, and I save the config to file") + ConfigLoadDefault(&config1); + config1.Game.FriendlyFire = 1; + config1.Graphics.Brightness = 5; + ConfigSave(&config1, "tmp"); + GIVEN_END + + WHEN("I load a second config from that file") + ConfigLoad(&config2, "tmp"); + WHEN_END + + THEN("the two configs should be equal") + SHOULD_MEM_EQUAL(&config1, &config2, sizeof(Config)); + THEN_END + } + SCENARIO_END +FEATURE_END + +int main(void) +{ + cbehave_feature features[] = + { + {feature_idx(1)}, + {feature_idx(2)} + }; + + return cbehave_runner("Config features are:", features); +}