Converted first thing to shared pointers.

This commit is contained in:
2023-11-02 20:35:11 -05:00
parent 5547c7c236
commit 0beb1d9cb7
17 changed files with 37 additions and 67 deletions

View File

@ -16,11 +16,7 @@ void AssetManager::update() {
} }
void AssetManager::queueLoad(const std::vector<std::shared_ptr<Asset>> assets) { void AssetManager::queueLoad(const std::vector<std::shared_ptr<Asset>> assets) {
std::merge( assetsToLoad.insert(this->assetsToLoad.end(), assets.begin(), assets.end());
this->assetsToLoad.begin(), this->assetsToLoad.end(),
assets.begin(), assets.end(),
this->assetsToLoad.begin()
);
} }
void AssetManager::queueLoad(const std::shared_ptr<Asset> &asset) { void AssetManager::queueLoad(const std::shared_ptr<Asset> &asset) {
@ -33,11 +29,7 @@ void AssetManager::queueUnload(
std::cout << std::cout <<
"Asset list was queued to unload, but is not yet implemented" << "Asset list was queued to unload, but is not yet implemented" <<
std::endl; std::endl;
std::merge( assetsToUnload.insert(assetsToUnload.end(), assets.begin(), assets.end());
this->assetsToUnload.begin(), this->assetsToUnload.end(),
assets.begin(), assets.end(),
this->assetsToUnload.begin()
);
} }
void AssetManager::queueUnload(const std::shared_ptr<Asset> &asset) { void AssetManager::queueUnload(const std::shared_ptr<Asset> &asset) {
@ -51,17 +43,17 @@ void AssetManager::queueSwap(
const std::vector<std::shared_ptr<Asset>> &newAssets, const std::vector<std::shared_ptr<Asset>> &newAssets,
const std::vector<std::shared_ptr<Asset>> &oldAssets const std::vector<std::shared_ptr<Asset>> &oldAssets
) { ) {
std::vector<Asset*> unload; std::vector<std::shared_ptr<Asset>> unload;
std::vector<Asset*> load; std::vector<std::shared_ptr<Asset>> load;
// Determine assets to unload. // Determine assets to unload.
std::for_each(oldAssets.begin(), oldAssets.end(), [&](Asset *asset){ std::for_each(oldAssets.begin(), oldAssets.end(), [&](const auto &asset){
auto it = std::find(newAssets.begin(), newAssets.end(), asset); auto it = std::find(newAssets.begin(), newAssets.end(), asset);
if(it == newAssets.end()) unload.push_back(asset); if(it == newAssets.end()) unload.push_back(asset);
}); });
// Determine assets to load // Determine assets to load
std::for_each(newAssets.begin(), newAssets.end(), [&](Asset *asset){ std::for_each(newAssets.begin(), newAssets.end(), [&](const auto &asset){
auto it = std::find(oldAssets.begin(), oldAssets.end(), asset); auto it = std::find(oldAssets.begin(), oldAssets.end(), asset);
if(it == oldAssets.end()) load.push_back(asset); if(it == oldAssets.end()) load.push_back(asset);
}); });

View File

@ -90,9 +90,9 @@ namespace Dawn {
auto existing = this->assets.find(name); auto existing = this->assets.find(name);
if(existing != this->assets.end()) { if(existing != this->assets.end()) {
return (T*)existing->second; return dynamic_pointer_cast<T>(existing->second);
} }
auto asset = new T(name); auto asset = std::make_shared<T>(name);
this->assets[name] = asset; this->assets[name] = asset;
return asset; return asset;
} }

View File

@ -115,7 +115,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
auto renderables = scene->findComponents<IRenderable>(); auto renderables = scene->findComponents<IRenderable>();
auto itRenderables = renderables.begin(); auto itRenderables = renderables.begin();
while(itRenderables != renderables.end()) { while(itRenderables != renderables.end()) {
vectorAppend(&shaderPassItems, (*itRenderables)->getRenderPasses(context)); vectorAppend(shaderPassItems, (*itRenderables)->getRenderPasses(context));
++itRenderables; ++itRenderables;
} }

View File

@ -20,8 +20,8 @@ namespace Dawn {
DawnGame *game; DawnGame *game;
LanguageAsset *asset; LanguageAsset *asset;
struct Locale locale; struct Locale locale;
LanguageAsset *currentlyLoadedAsset = nullptr; std::shared_ptr<LanguageAsset> currentlyLoadedAsset;
LanguageAsset *loadingAsset = nullptr; std::shared_ptr<LanguageAsset> loadingAsset;
std::function<void()> eventTeardownLanguageLoaded; std::function<void()> eventTeardownLanguageLoaded;
/** Listens for when the pending language loads. */ /** Listens for when the pending language loads. */

View File

@ -18,7 +18,9 @@ namespace Dawn {
* @param man Asset Manasger for getting required assets from. * @param man Asset Manasger for getting required assets from.
* @return List of required assets this prefab. * @return List of required assets this prefab.
*/ */
static std::vector<Asset*> getRequiredAssets(AssetManager *man) { static std::vector<std::shared_ptr<Asset>>
getRequiredAssets(AssetManager *man)
{
return P::prefabAssets(man); return P::prefabAssets(man);
} }

View File

@ -19,8 +19,8 @@ namespace Dawn {
public: public:
SimpleTexturedMaterial *material; SimpleTexturedMaterial *material;
static std::vector<Asset*> prefabAssets(AssetManager *man) { static std::vector<std::shared_ptr<Asset>> prefabAssets(AssetManager *man) {
return std::vector<Asset*>(); return std::vector<std::shared_ptr<Asset>>();
} }
SimpleSpinningCubePrefab(Scene *s, sceneitemid_t i) : SimpleSpinningCubePrefab(Scene *s, sceneitemid_t i) :

View File

@ -78,7 +78,7 @@ namespace Dawn {
* *
* @return List of assets required by this scene. * @return List of assets required by this scene.
*/ */
virtual std::vector<Asset*> getRequiredAssets() = 0; virtual std::vector<std::shared_ptr<Asset>> getRequiredAssets() = 0;
/** /**
* Method to begin the actual staging of the scene, typically called after * Method to begin the actual staging of the scene, typically called after

View File

@ -149,7 +149,7 @@ namespace Dawn {
while(transformsToCheck.size() > 0) { while(transformsToCheck.size() > 0) {
Transform *tras = *transformsToCheck.begin(); Transform *tras = *transformsToCheck.begin();
vectorAppend(&transformsToCheck, tras->children); vectorAppend(transformsToCheck, tras->children);
auto component = tras->item->getComponent<T>(); auto component = tras->item->getComponent<T>();
if(component != nullptr) itemsFound.push_back(component); if(component != nullptr) itemsFound.push_back(component);
transformsToCheck.erase(transformsToCheck.begin()); transformsToCheck.erase(transformsToCheck.begin());

View File

@ -17,7 +17,7 @@ namespace Dawn {
flag_t style = 0; flag_t style = 0;
flag_t decorations = 0; flag_t decorations = 0;
uint32_t size = 16; uint32_t size = 16;
TrueTypeAsset *font = nullptr; std::shared_ptr<TrueTypeAsset> font;
}; };
struct UILabelText { struct UILabelText {

View File

@ -12,7 +12,7 @@ namespace Dawn {
// @optional // @optional
StateProperty<std::string> text; StateProperty<std::string> text;
// @optional // @optional
StateProperty<TrueTypeAsset*> font; StateProperty<std::shared_ptr<TrueTypeAsset>> font;
// @optional // @optional
StateProperty<uint32_t> size; StateProperty<uint32_t> size;
// @optional // @optional

View File

@ -20,10 +20,10 @@ namespace Dawn {
cube = SimpleSpinningCubePrefab::prefabCreate(this); cube = SimpleSpinningCubePrefab::prefabCreate(this);
} }
std::vector<Asset*> getRequiredAssets() override { std::vector<std::shared_ptr<Asset>> getRequiredAssets() override {
auto assMan = &this->game->assetManager; auto assMan = &this->game->assetManager;
std::vector<Asset*> assets; std::vector<std::shared_ptr<Asset>> assets;
vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan)); vectorAppend(assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan));
return assets; return assets;
} }

View File

@ -15,31 +15,7 @@ namespace Dawn {
* @param append Pointer to list that will be appended. * @param append Pointer to list that will be appended.
*/ */
template<typename T> template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> *append) { void vectorAppend(std::vector<T> &list, const std::vector<T> &append) {
assertNotNull(list, "vectorAppend: list cannot be null"); list.insert(list.end(), append.begin(), append.end());
assertNotNull(append, "vectorAppend: append cannot be null");
auto it = append->begin();
while(it != append->end()) {
list->push_back(*it);
++it;
}
}
/**
* Append a list on to another list.
*
* @param list Pointer to list that is being appended to.
* @param append List that will be appended.
*/
template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> append) {
assertNotNull(list, "vectorAppend: list cannot be null");
auto it = append.begin();
while(it != append.end()) {
list->push_back(*it);
++it;
}
} }
} }

View File

@ -24,10 +24,10 @@ namespace Dawn {
canvas = UICanvas::create(this); canvas = UICanvas::create(this);
} }
std::vector<Asset*> getRequiredAssets() override { std::vector<std::shared_ptr<Asset>> getRequiredAssets() override {
auto assMan = &this->game->assetManager; auto assMan = &this->game->assetManager;
std::vector<Asset*> assets; std::vector<std::shared_ptr<Asset>> assets;
vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan)); vectorAppend(assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan));
return assets; return assets;
} }

View File

@ -26,9 +26,9 @@ namespace Dawn {
canvas->camera = camera; canvas->camera = camera;
} }
std::vector<Asset*> getRequiredAssets() override { std::vector<std::shared_ptr<Asset>> getRequiredAssets() override {
auto assMan = &this->game->assetManager; auto assMan = &this->game->assetManager;
std::vector<Asset*> assets; std::vector<std::shared_ptr<Asset>> assets;
vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan)); vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan));
return assets; return assets;
} }

View File

@ -46,9 +46,9 @@ void ScenePrologue8CustomEventForAfterCharacterFollowup::onStart() {
ScenePrologue8::ScenePrologue8(DawnGame *game) : SceneMonologue(game) { ScenePrologue8::ScenePrologue8(DawnGame *game) : SceneMonologue(game) {
} }
std::vector<Asset*> ScenePrologue8::getRequiredAssets() { std::vector<std::shared_ptr<Asset>> ScenePrologue8::getRequiredAssets() {
auto man = &this->game->assetManager; auto man = &this->game->assetManager;
std::vector<Asset*> assets = SceneMonologue::getRequiredAssets(); auto assets = SceneMonologue::getRequiredAssets();
return assets; return assets;
} }

View File

@ -36,7 +36,7 @@ namespace Dawn {
ScenePrologue8(DawnGame *game); ScenePrologue8(DawnGame *game);
std::vector<Asset*> getRequiredAssets() override; std::vector<std::shared_ptr<Asset>> getRequiredAssets() override;
void stage() override; void stage() override;
}; };

View File

@ -47,11 +47,11 @@ namespace Dawn {
gameCamera->player = player->player; gameCamera->player = player->player;
} }
std::vector<Asset*> getRequiredAssets() override { std::vector<std::shared_ptr<Asset>> getRequiredAssets() override {
auto assMan = &this->game->assetManager; auto assMan = &this->game->assetManager;
std::vector<Asset*> assets; std::vector<std::shared_ptr<Asset>> assets;
vectorAppend(&assets, Urchin::getRequiredAssets(assMan)); vectorAppend(assets, Urchin::getRequiredAssets(assMan));
vectorAppend(&assets, Player::getRequiredAssets(assMan)); vectorAppend(assets, Player::getRequiredAssets(assMan));
return assets; return assets;
} }