Skip to content

Commit 760c54f

Browse files
committed
Configurable data dir at compile time (fixes #20)
1 parent e58657b commit 760c54f

File tree

8 files changed

+36
-16
lines changed

8 files changed

+36
-16
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
project(FallingTime)
22
cmake_minimum_required(VERSION 2.8.10)
3+
if("${CMAKE_VERSION}" VERSION_GREATER 3.1)
4+
cmake_policy(SET CMP0054 NEW)
5+
endif()
36

47
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
58

@@ -78,6 +81,11 @@ ELSE()
7881
include_directories(${SDL2TTF_INCLUDE_DIR})
7982
ENDIF()
8083

84+
set(DATA_DIR "./data/" CACHE STRING "Data directory")
85+
message("Data dir is ${DATA_DIR}")
86+
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/sys_config.h.cmake ${CMAKE_SOURCE_DIR}/sys_config.h)
87+
SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/sys_config.h PROPERTIES GENERATED TRUE)
88+
8189
include_directories(.)
8290
include_directories(chipmunk/include)
8391

bg.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "draw.h"
2727
#include "init.h"
2828
#include "main.h"
29+
#include "sys_config.h"
2930
#include "utils.h"
3031

3132
#define ICICLE_WIDTH 58
@@ -155,7 +156,7 @@ static void DrawParticleScroll(
155156
bool BackgroundsLoad(Backgrounds* bg)
156157
{
157158
#define LOAD_TEX(_t, _filename)\
158-
_t = LoadTex("data/graphics/" _filename);\
159+
_t = LoadTex(DATA_DIR "graphics/" _filename);\
159160
if (_t.T == NULL)\
160161
{\
161162
return false;\

box.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
3232
#include "gap.h"
3333
#include "main.h"
3434
#include "space.h"
35+
#include "sys_config.h"
3536
#include "utils.h"
3637

3738
#define BLOCK_SPRITE_HEIGHT 15
@@ -95,7 +96,7 @@ bool BoxTexesLoad(void)
9596
for (int i = 0; i < 6; i++)
9697
{
9798
char buf[256];
98-
sprintf(buf, "data/graphics/floor%d.png", i);
99+
sprintf(buf, DATA_DIR "graphics/floor%d.png", i);
99100
BoxTexes[i] = LoadTex(buf);
100101
if (BoxTexes[i].T == NULL)
101102
{

init.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "player.h"
3636
#include "space.h"
3737
#include "sound.h"
38+
#include "sys_config.h"
3839
#include "title.h"
3940

4041
SDL_Surface *icon = NULL;
@@ -101,7 +102,7 @@ void Initialize(bool* Continue, bool* Error)
101102
else
102103
printf("TTF_Init succeeded\n");
103104

104-
icon = IMG_Load("data/graphics/icon.png");
105+
icon = IMG_Load(DATA_DIR "graphics/icon.png");
105106
if (icon == NULL)
106107
{
107108
*Continue = false; *Error = true;
@@ -112,7 +113,7 @@ void Initialize(bool* Continue, bool* Error)
112113
SDL_SetWindowIcon(Window, icon);
113114

114115
#define LOAD_IMG(_t, _path)\
115-
_t = LoadTex("data/graphics/" _path);\
116+
_t = LoadTex(DATA_DIR "graphics/" _path);\
116117
if (_t.T == NULL)\
117118
{\
118119
*Continue = false; *Error = true;\
@@ -125,7 +126,7 @@ void Initialize(bool* Continue, bool* Error)
125126
LOAD_IMG(PickupTex, "eggplant.png");
126127

127128
#define LOAD_ANIM(_anim, _path, _w, _h, _fps)\
128-
if (!AnimationLoad(&(_anim), "data/graphics/" _path, (_w), (_h), (_fps)))\
129+
if (!AnimationLoad(&(_anim), DATA_DIR "graphics/" _path, (_w), (_h), (_fps)))\
129130
{\
130131
*Continue = false; *Error = true;\
131132
return;\
@@ -151,7 +152,7 @@ void Initialize(bool* Continue, bool* Error)
151152
}
152153

153154
#define LOAD_SOUND(_sound, _path)\
154-
_sound = Mix_LoadWAV("data/sounds/" _path);\
155+
_sound = Mix_LoadWAV(DATA_DIR "sounds/" _path);\
155156
if (_sound == NULL)\
156157
{\
157158
*Continue = false; *Error = true;\
@@ -166,7 +167,7 @@ void Initialize(bool* Continue, bool* Error)
166167
LOAD_SOUND(SoundScore, "score.ogg");
167168
SoundLoad();
168169

169-
music = Mix_LoadMUS("data/sounds/music.ogg");
170+
music = Mix_LoadMUS(DATA_DIR "sounds/music.ogg");
170171
if (music == NULL)
171172
{
172173
*Continue = false; *Error = true;
@@ -176,7 +177,7 @@ void Initialize(bool* Continue, bool* Error)
176177
}
177178

178179
#define LOAD_FONT(_f, _file, _size)\
179-
_f = TTF_OpenFont("data/" _file, _size);\
180+
_f = TTF_OpenFont(DATA_DIR _file, _size);\
180181
if (_f == NULL)\
181182
{\
182183
*Continue = false; *Error = true;\

sound.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <math.h>
44

55
#include "player.h"
6+
#include "sys_config.h"
67
#include "utils.h"
78

89
#define BOUNCE_SPEED_MAX_VOLUME 150.0f
@@ -22,7 +23,7 @@ Mix_Chunk *SoundPlayerCalls[MAX_PLAYERS];
2223
bool SoundLoad(void)
2324
{
2425
#define LOAD_SOUND(_sound, _path)\
25-
_sound = Mix_LoadWAV("data/sounds/" _path);\
26+
_sound = Mix_LoadWAV(DATA_DIR "sounds/" _path);\
2627
if (_sound == NULL)\
2728
{\
2829
printf("Mix_LoadWAV failed: %s\n", SDL_GetError());\

sys_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define DATA_DIR "./datad/"

sys_config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define DATA_DIR "@DATA_DIR@"

title.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <inttypes.h>
2424
#include <stdlib.h>
2525
#include <stdio.h>
26+
#include <string.h>
2627

2728
#include "SDL_image.h"
2829

@@ -40,6 +41,7 @@
4041
#include "text.h"
4142
#include "game.h"
4243
#include "bg.h"
44+
#include "sys_config.h"
4345
#include "sys_specifics.h"
4446
#include "utils.h"
4547

@@ -351,12 +353,12 @@ void ToTitleScreen(const bool start)
351353

352354
bool TitleImagesLoad(void)
353355
{
354-
if (!AnimationLoad(&TitleAnim, "data/graphics/anim.png", 169, 40, 12))
356+
if (!AnimationLoad(&TitleAnim, DATA_DIR "graphics/anim.png", 169, 40, 12))
355357
{
356358
return false;
357359
}
358360
if (!AnimationLoad(
359-
&GameOverAnim, "data/graphics/gameover.png", 131, 40, 3))
361+
&GameOverAnim, DATA_DIR "graphics/gameover.png", 131, 40, 3))
360362
{
361363
return false;
362364
}
@@ -372,15 +374,15 @@ bool TitleImagesLoad(void)
372374
{
373375
char buf[256];
374376
#ifdef __GCW0__
375-
sprintf(buf, "data/graphics/gcw%d.png", i);
377+
sprintf(buf, DATA_DIR "graphics/gcw%d.png", i);
376378
#else
377379
if (i < numJoysticks)
378380
{
379-
strcpy(buf, "data/graphics/360.png");
381+
strcpy(buf, DATA_DIR "graphics/360.png");
380382
}
381383
else
382384
{
383-
sprintf(buf, "data/graphics/keyboard%d.png", i - numJoysticks);
385+
sprintf(buf, DATA_DIR "graphics/keyboard%d.png", i - numJoysticks);
384386
}
385387
#endif
386388
ControlTexes[i] = LoadTex(buf);
@@ -390,9 +392,9 @@ bool TitleImagesLoad(void)
390392
}
391393
}
392394
#ifdef __GCW0__
393-
ControlTex0Analog = LoadTex("data/graphics/gcw0analog.png");
395+
ControlTex0Analog = LoadTex(DATA_DIR "graphics/gcw0analog.png");
394396
if (ControlTex0Analog.T == NULL) return false;
395-
ControlTex0G = LoadTex("data/graphics/gcw0g.png");
397+
ControlTex0G = LoadTex(DATA_DIR "graphics/gcw0g.png");
396398
if (ControlTex0G.T == NULL) return false;
397399
#endif
398400
return true;

0 commit comments

Comments
 (0)