Skip to content

Commit

Permalink
Add cbehave and config tests (fixes #52)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jun 10, 2013
1 parent a25b5fc commit 528e697
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/cdogs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(CDOGS_SOURCES
automap.c
blit.c
config.c
config_apply.c
defs.c
draw.c
drawtools.c
Expand Down
24 changes: 6 additions & 18 deletions src/cdogs/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include <stdio.h>

#include <cdogs/files.h>
#include <cdogs/grafx.h>
#include <cdogs/keyboard.h>
#include <cdogs/music.h>
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/cdogs/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions src/cdogs/config_apply.c
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 0 additions & 11 deletions src/cdogs/gamedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/grafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#ifndef __GRAFX
#define __GRAFX

#include <SDL.h>
#include <SDL_video.h>

#include "sys_specifics.h"

Expand Down
2 changes: 1 addition & 1 deletion src/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
10 changes: 10 additions & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions src/tests/cbehave
Submodule cbehave added at 6035b7
56 changes: 56 additions & 0 deletions src/tests/config_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <cbehave/cbehave.h>

#include <config.h>


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);
}

0 comments on commit 528e697

Please sign in to comment.