Skip to content

Commit 8882de2

Browse files
committed
yes
1 parent 9292664 commit 8882de2

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

assets/main.script

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test script

source/assets.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ void AssetManagerFree(AssetManager *this) {
2828
DgTableFree(&this->types, true);
2929
DgTableFree(&this->loaders, true);
3030
DgTableFree(&this->assets, true);
31+
DgMemoryFree(this->dir);
32+
}
33+
34+
DgError AssetManagerSetSource(AssetManager *this, int source_type, const char *path) {
35+
/**
36+
* Set where to load assets from
37+
*/
38+
39+
if (source_type == ASSET_SOURCE_FOLDER) {
40+
this->dir = DgStringDuplicate(path);
41+
42+
if (!this->dir) {
43+
return DG_ERROR_FAILED;
44+
}
45+
else {
46+
DgLog(DG_LOG_INFO, "Asset directory is now: %s", this->dir);
47+
}
48+
49+
return DG_ERROR_SUCCESS;
50+
}
51+
52+
return DG_ERROR_FAILED;
3153
}
3254

3355
DgError AssetManagerAddType(AssetManager *this, AssetType *type) {
@@ -83,14 +105,19 @@ static Asset AssetManager_LoadNewAsset(AssetManager *this, AssetTypeName type, c
83105
size_t size;
84106
void *data;
85107

108+
char *path = DgStringConcatinateL(DgStringConcatinate(this->dir, "/"), name);
109+
86110
// Load data
87-
DgError status = DgStorageLoad(NULL, name, &size, &data);
111+
DgError status = DgStorageLoad(NULL, path, &size, &data);
88112

89113
if (status) {
90-
DgLog(DG_LOG_ERROR, "Failed to load asset: %s", name);
114+
DgLog(DG_LOG_ERROR, "Failed to load asset: %s [%s]", path, DgErrorString(status));
115+
DgMemoryFree(path);
91116
return NULL;
92117
}
93118

119+
DgMemoryFree(path);
120+
94121
// Determine best loader
95122
AssetLoader *loader = NULL;
96123
DgValue *key, *value;

source/assets.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,16 @@ typedef struct AssetManager {
7171
DgTable types;
7272
DgTable loaders;
7373
DgTable assets;
74+
const char *dir;
7475
} AssetManager;
7576

77+
enum {
78+
ASSET_SOURCE_FOLDER = 1,
79+
};
80+
7681
DgError AssetManagerInit(AssetManager *this);
7782
void AssetManagerFree(AssetManager *this);
83+
DgError AssetManagerSetSource(AssetManager *this, int source_type, const char *path);
7884
DgError AssetManagerAddType(AssetManager *this, AssetType *type);
7985
DgError AssetManagerAddLoader(AssetManager *this, const char *ext, AssetLoader *loader);
8086
Asset AssetManagerLoad(AssetManager *this, AssetTypeName type, const char *name);

source/engine.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99
Engine *gEngine;
1010

1111
DgError EngineInit(Engine *this, DgArgs *args) {
12-
DgInitTime();
12+
DgError err;
1313

14-
DgStorageAddPool(NULL, DgFilesystemCreatePool(NULL, ""));
15-
DgStorageAddPool(NULL, DgFilesystemCreatePool("assets", DgArgGetValue(args, "assets")));
14+
DgInitTime();
1615

17-
DgError err;
16+
if (( err = DgStorageAddPool(NULL, DgFilesystemCreatePool(NULL, "")) )) {
17+
DgLog(DG_LOG_ERROR, "Failed to setup fs pool.");
18+
return err;
19+
}
1820

1921
if ((err = AssetManagerInit(&this->assman))) {
2022
DgLog(DG_LOG_ERROR, "Failed to init asset manager!");
2123
return err;
2224
}
2325

26+
if ((err = AssetManagerSetSource(&this->assman, ASSET_SOURCE_FOLDER, "assets"))) {
27+
DgLog(DG_LOG_ERROR, "Failed to set assets source!");
28+
return err;
29+
}
30+
2431
RegisterTextAssetTypeAndLoader(&this->assman);
2532

2633
if ((err = DgTableInit(&this->properties))) {
@@ -43,17 +50,17 @@ DgError EngineInit(Engine *this, DgArgs *args) {
4350
return DG_ERROR_SUCCESS;
4451
}
4552

46-
const char *gMainScript = "main.script";
53+
const char *gMainScriptPath = "main.script";
4754

4855
DgError EngineLoadMainScene(Engine *this) {
49-
Text mainScriptText = LoadText(&this->assman, gMainScript);
56+
Text mainScriptText = LoadText(&this->assman, gMainScriptPath);
5057

5158
if (!mainScriptText) {
5259
DgLog(DG_LOG_ERROR, "Could not load main script");
5360
return DG_ERROR_FAILED;
5461
}
5562

56-
DgLog(DG_LOG_INFO, "Loaded main text asset: %s (size = %d)", gMainScript, mainScriptText->size);
63+
DgLog(DG_LOG_INFO, "Loaded main text asset: %s (size = %d)", gMainScriptPath, mainScriptText->size);
5764
}
5865

5966
DgError EngineRun(Engine *this) {

0 commit comments

Comments
 (0)