-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Engine core and assets loading refactor
- Loading branch information
Showing
10 changed files
with
325 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "AssetsLoader.h" | ||
#include "Assets.h" | ||
|
||
#include <iostream> | ||
|
||
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) { | ||
} | ||
|
||
void AssetsLoader::addLoader(int tag, aloader_func func) { | ||
loaders[tag] = func; | ||
} | ||
|
||
void AssetsLoader::add(int tag, const std::string filename, const std::string alias) { | ||
entries.push(aloader_entry{ tag, filename, alias }); | ||
} | ||
|
||
bool AssetsLoader::hasNext() const { | ||
return !entries.empty(); | ||
} | ||
|
||
bool AssetsLoader::loadNext() { | ||
const aloader_entry& entry = entries.front(); | ||
std::cout << " loading " << entry.filename << " as " << entry.alias << std::endl; | ||
std::cout.flush(); | ||
auto found = loaders.find(entry.tag); | ||
if (found == loaders.end()) { | ||
std::cerr << "unknown asset tag " << entry.tag << std::endl; | ||
return false; | ||
} | ||
aloader_func loader = found->second; | ||
bool status = loader(assets, entry.filename, entry.alias); | ||
entries.pop(); | ||
return status; | ||
} | ||
|
||
#include "graphics/Shader.h" | ||
#include "graphics/Texture.h" | ||
#include "graphics/Font.h" | ||
|
||
bool _load_shader(Assets* assets, const std::string& filename, const std::string& name) { | ||
Shader* shader = load_shader(filename + ".glslv", filename + ".glslf"); | ||
if (shader == nullptr) { | ||
std::cerr << "failed to load shader '" << name << "'" << std::endl; | ||
return false; | ||
} | ||
assets->store(shader, name); | ||
return true; | ||
} | ||
|
||
bool _load_texture(Assets* assets, const std::string& filename, const std::string& name) { | ||
Texture* texture = load_texture(filename); | ||
if (texture == nullptr) { | ||
std::cerr << "failed to load texture '" << name << "'" << std::endl; | ||
return false; | ||
} | ||
assets->store(texture, name); | ||
return true; | ||
} | ||
|
||
bool _load_font(Assets* assets, const std::string& filename, const std::string& name) { | ||
std::vector<Texture*> pages; | ||
for (size_t i = 0; i <= 4; i++) { | ||
Texture* texture = load_texture(filename + "_" + std::to_string(i) + ".png"); | ||
if (texture == nullptr) { | ||
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl; | ||
return false; | ||
} | ||
pages.push_back(texture); | ||
} | ||
Font* font = new Font(pages); | ||
assets->store(font, name); | ||
return true; | ||
} | ||
|
||
void AssetsLoader::createDefaults(AssetsLoader& loader) { | ||
loader.addLoader(ASSET_SHADER, _load_shader); | ||
loader.addLoader(ASSET_TEXTURE, _load_texture); | ||
loader.addLoader(ASSET_FONT, _load_font); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef SRC_ASSETS_LOADER_H | ||
#define SRC_ASSETS_LOADER_H | ||
|
||
#include <string> | ||
#include <functional> | ||
#include <map> | ||
#include <queue> | ||
|
||
#define ASSET_TEXTURE 1 | ||
#define ASSET_SHADER 2 | ||
#define ASSET_FONT 3 | ||
|
||
class Assets; | ||
|
||
typedef std::function<bool(Assets*, const std::string&, const std::string&)> aloader_func; | ||
|
||
struct aloader_entry { | ||
int tag; | ||
const std::string filename; | ||
const std::string alias; | ||
}; | ||
|
||
class AssetsLoader { | ||
Assets* assets; | ||
std::map<int, aloader_func> loaders; | ||
std::queue<aloader_entry> entries; | ||
public: | ||
AssetsLoader(Assets* assets); | ||
void addLoader(int tag, aloader_func func); | ||
void add(int tag, const std::string filename, const std::string alias); | ||
|
||
bool hasNext() const; | ||
bool loadNext(); | ||
|
||
static void createDefaults(AssetsLoader& loader); | ||
}; | ||
|
||
#endif // SRC_ASSETS_LOADER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.