Working on assets

This commit is contained in:
2022-12-01 22:37:42 -08:00
parent 535e2b2dc5
commit ba9881e39d
18 changed files with 355 additions and 42 deletions

View File

@ -50,7 +50,6 @@ static inline void assertTrue(bool_t x) {}
* Asserts a function as being deprecated.
*/
void assertDeprecated();
#else
#define assertTrue assert

View File

@ -6,6 +6,7 @@
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
#include "event/Event.hpp"
namespace Dawn {
class AssetManager;
@ -16,6 +17,7 @@ namespace Dawn {
std::string name;
uint8_t state = 0x00;
bool loaded = false;
Event<> eventLoaded;
/**
* Create an abstract Asset object.

View File

@ -15,26 +15,72 @@ void AssetManager::init() {
}
void AssetManager::update() {
auto it = this->assetsNotLoaded.begin();
while(it != this->assetsNotLoaded.end()) {
auto asset = it->second;
void AssetManager::queueLoad(std::vector<Asset*> assets) {
vectorAppend(&this->assetsToLoad, &assets);
}
void AssetManager::queueUnload(std::vector<Asset*> assets) {
vectorAppend(&this->assetsToUnload, &assets);
}
void AssetManager::queueSwap(
std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets
) {
std::vector<Asset*> unload;
std::vector<Asset*> load;
// Determine assets to unload.
auto itUnload = oldAssets.begin();
while(itUnload != oldAssets.end()) {
auto inNewAssets = std::find(newAssets.begin(), newAssets.end(), *itUnload);
if(inNewAssets == oldAssets.end()) unload.push_back(*itUnload);
++itUnload;
}
// Determine assets to load
auto itLoad = newAssets.begin();
while(itLoad != newAssets.end()) {
auto inOldAssets = std::find(oldAssets.begin(), oldAssets.end(), *itLoad);
if(inOldAssets == oldAssets.end()) load.push_back(*itLoad);
++itLoad;
}
this->queueUnload(unload);
this->queueLoad(load);
}
void AssetManager::syncTick() {
auto it = this->assetsToLoad.begin();
// auto it = this->assetsNotLoaded.begin();
while(it != this->assetsToLoad.end()) {
auto asset = *it;
if(asset->loaded) {
it = this->assetsNotLoaded.erase(it);
it = this->assetsToLoad.erase(it);
continue;
}
asset->updateSync();
asset->updateAsync();
asset->updateAsync();//TODO: Make Async
if(asset->loaded) {
it = this->assetsNotLoaded.erase(it);
this->assets[asset->name] = asset;
it = this->assetsToLoad.erase(it);
continue;
}
++it;
}
// auto it2 = this->assetsToUnload.begin();
// while(it2 != this->assetsToUnload.end()) {
// ++it2;
// }
}
void AssetManager::syncLoad() {
while(this->assetsToLoad.size() > 0) {
this->syncTick();
}
}
AssetManager::~AssetManager() {
@ -43,10 +89,4 @@ AssetManager::~AssetManager() {
delete it->second;
++it;
}
auto it2 = this->assetsNotLoaded.begin();
while(it2 != this->assetsNotLoaded.end()) {
delete it2->second;
++it2;
}
}

View File

@ -5,13 +5,15 @@
#pragma once
#include "Asset.hpp"
#include "util/array.hpp"
namespace Dawn {
class AssetManager {
private:
/** List of pointers to assets, mapped by their asset key. */
std::map<std::string, Asset*> assets;
std::map<std::string, Asset*> assetsNotLoaded;
std::vector<Asset*> assetsToLoad;
std::vector<Asset*> assetsToUnload;
public:
/**
@ -20,9 +22,41 @@ namespace Dawn {
void init();
/**
* Ticks the asset manager for a single frame, synchronously.
* Queue a loading of a list of assets. Does not actually begin loading.
*
* @param assets Assets to load.
*/
void update();
void queueLoad(std::vector<Asset*> assets);
/**
* Takes a list of lists to queue to unload. Does not immediately unload.
*
* @param assets Assets to unload.
*/
void queueUnload(std::vector<Asset*> assets);
/**
* Queues load and unload based on the difference between two sets of
* assets. This is mostly used to perform a scene change.
*
* @param newAssets New list of assets to maintain.
* @param oldAssets Old list of assets to no longer maintain.
*/
void queueSwap(
std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets
);
/**
* Ticks the asset manager, synchronously.
*/
void syncTick();
/**
* Loads everything that isn't loaded, blocks the current thread until
* that has finished.
*/
void syncLoad();
/**
* Creates and queue an asset to load.
@ -31,7 +65,7 @@ namespace Dawn {
* @return The asset element to be loaded.
*/
template<class T>
T * load(std::string name) {
T * get(std::string name) {
assertTrue(name.size() > 0);
auto existing = this->assets.find(name);
@ -40,7 +74,6 @@ namespace Dawn {
}
auto asset = new T(this, name);
this->assets[name] = asset;
this->assetsNotLoaded[name] = asset;
return asset;
}

View File

@ -24,6 +24,7 @@ void TextureAsset::updateSync() {
this->texture.buffer(this->colors);
this->state = 0x05;
this->loaded = true;
this->eventLoaded.invoke();
}
void TextureAsset::updateAsync() {

View File

@ -29,6 +29,7 @@ void TrueTypeAsset::updateSync() {
this->state = 0x05;
this->loaded = true;
this->eventLoaded.invoke();
}
void TrueTypeAsset::updateAsync() {

View File

@ -6,6 +6,7 @@
#pragma once
#include "SceneItem.hpp"
#include "event/Event.hpp"
#include "asset/Asset.hpp"
namespace Dawn {
class DawnGame;
@ -41,6 +42,21 @@ namespace Dawn {
*/
SceneItem * createSceneItem();
/**
* Returns the required assets for this scene. Assets required are meant
* to be referenced back to the loader so that special loading actions can
* be performed against them.
*
* @return List of assets required by this scene.
*/
virtual std::vector<Asset*> getRequiredAssets() = 0;
/**
* Method to begin the actual staging of the scene, typically called after
* the scene has finished loading.
*/
virtual void stage() = 0;
/**
* Finds an existing component on the scene (Root Level Only) that has a
* component matching the given component type. Returns nullptr if no item

View File

@ -11,4 +11,5 @@ target_sources(${DAWN_TARGET_NAME}
UILabel.cpp
UISprite.cpp
UIEmpty.cpp
UIGrid.cpp
)

35
src/dawn/ui/UIGrid.cpp Normal file
View File

@ -0,0 +1,35 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIGrid.hpp"
using namespace Dawn;
UIGrid::UIGrid(UICanvas *canvas) : UIComponent(canvas) {
}
void UIGrid::setSize(int32_t rows, int32_t columns) {
this->gridArea.clear();
this->rows = rows;
this->columns = columns;
int32_t i, l;
l = rows * columns;
for(i = 0; i < l; i++) {
struct GridArea area;
area.uiComponent = nullptr;
this->gridArea.push_back(area);
}
}
void UIGrid::setItem(int32_t x, int32_t y, UIComponent *comp) {
auto item = &this->gridArea[(y * this->rows) + x];
//Too lazy to support re setting. Need to mod setSize too
assertNull(item->uiComponent);
item->uiComponent = comp;
}

41
src/dawn/ui/UIGrid.hpp Normal file
View File

@ -0,0 +1,41 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
namespace Dawn {
enum GridAlign {
ALIGN_GROW,
ALIGN_CONTENT,
ALIGN_FIXED
};
struct GridArea {
enum GridAlignment xAlign;
enum GridAlignment yAlign;
float_t width, height;
float_t x, y;
UIComponent *uiComponent;
};
class UIGrid : public UIComponent {
protected:
void alignChildren();
public:
glm::vec2 cellPadding;
glm::vec2 cellMargin;
int32_t rows = 1;
int32_t columns = 1;
std::vector<struct GridArea> gridArea;
UIGrid(UICanvas *canvas);
void setSize(int32_t rows, int32_t columns);
void setItem(int32_t row, int32_t col, UIComponent *comp);
};
}

22
src/dawn/util/array.hpp Normal file
View File

@ -0,0 +1,22 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
namespace Dawn {
template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> *append) {
assertNotNull(list);
assertNotNull(append);
auto it = append->begin();
while(it != append->end()) {
list->push_back(*it);
++it;
}
}
}

View File

@ -65,6 +65,14 @@ int32_t DawnHost::init(DawnGame *game) {
auto result = game->init();
if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result;
// Hard-Load the first scene assets.
if(game->scene != nullptr) {
auto assets = game->scene->getRequiredAssets();
game->assetManager.queueLoad(assets);
game->assetManager.syncLoad();
game->scene->stage();
}
// Set up event listeners
glfwSetWindowSizeCallback(this->data->window, &glfwOnResize);
glfwSetKeyCallback(this->data->window, &glfwOnKey);

View File

@ -22,13 +22,12 @@ int32_t DawnGame::init() {
this->assetManager.init();
this->renderManager.init();
this->scene = TestScene::create(this);
this->scene = new TestScene(this);
return DAWN_GAME_INIT_RESULT_SUCCESS;
}
int32_t DawnGame::update(float_t delta) {
this->assetManager.update();
this->inputManager.update();
this->timeManager.update(delta);

View File

@ -6,6 +6,7 @@
#pragma once
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
#include "util/array.hpp"
#include "scene/components/Components.hpp"
#include "ui/PokerGameTextbox.hpp"
#include "visualnovel/VisualNovelManager.hpp"
@ -14,34 +15,48 @@
#include "visualnovel/events/PokerBetLoopEvent.hpp"
#include "visualnovel/events/PokerInitialEvent.hpp"
#include "visualnovel/events/SimpleLoopEvent.hpp"
#include "ui/PokerPlayerDisplay.hpp"
namespace Dawn {
class TestScene {
class TestScene : public Scene {
public:
static Scene * create(DawnGame *game) {
Scene *scene;
TestScene(DawnGame *game) : Scene(game) {
scene = new Scene(game);
}
std::vector<Asset*> getRequiredAssets() override {
auto assMan = &this->game->assetManager;
std::vector<Asset*> assets;
vectorAppend(&assets, &PokerGameTextbox::getAssets(assMan));
return assets;
}
void stage() override {
// Camera
auto camera = Camera::create(scene);
auto camera = Camera::create(this);
camera->transform->lookAt(glm::vec3(50, 50, 50), glm::vec3(0, 0, 0));
// UI
auto canvas = UICanvas::createCanvas(scene);
auto canvas = UICanvas::createCanvas(this);
auto textbox = PokerGameTextbox::create(canvas);
// VN Manager
auto vnManagerItem = scene->createSceneItem();
auto vnManagerItem = this->createSceneItem();
auto vnManager = vnManagerItem->addComponent<VisualNovelManager>();
// Poker Test
auto pokerGameItem = scene->createSceneItem();
auto pokerGameItem = this->createSceneItem();
auto pokerGame = pokerGameItem->addComponent<PokerGame>();
for(int32_t i = 0; i < 4; i++) {
auto pokerPlayerInstance = scene->createSceneItem();
auto pokerPlayerInstance = this->createSceneItem();
auto pokerPlayer = pokerPlayerInstance->addComponent<PokerPlayer>();
auto uiPlayer = canvas->addElement<PokerPlayerDisplay>();
uiPlayer->setTransform(UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH, glm::vec4(0, 0, 0, 0), 0);
uiPlayer->setPlayer(pokerPlayer);
}
auto betting = vnManager
@ -50,8 +65,6 @@ namespace Dawn {
->then(new VisualNovelTextboxEvent(vnManager, "Game Started"))
->then(new PokerInitialEvent(vnManager))
;
return scene;
}
};
}

View File

@ -4,7 +4,7 @@
# https://opensource.org/licenses/MIT
# Sources
# target_sources(${DAWN_TARGET_NAME}
# PRIVATE
# PokerGameTextbox.cpp
# )
target_sources(${DAWN_TARGET_NAME}
PRIVATE
PokerPlayerDisplay.cpp
)

View File

@ -11,12 +11,20 @@
namespace Dawn {
class PokerGameTextbox {
public:
static VisualNovelTextbox * create(UICanvas *canvas) {
auto textbox = canvas->addElement<VisualNovelTextbox>();
auto assetFont = canvas->getGame()->assetManager.load<TrueTypeAsset>("truetype_ark");
auto assetTexture = canvas->getGame()->assetManager.load<TextureAsset>("texture_test");
static std::vector<Asset*> getAssets(AssetManager *man) {
return std::vector<Asset*>{
man->get<TrueTypeAsset>("truetype_ark"),
man->get<TextureAsset>("texture_test")
};
}
static VisualNovelTextbox * create(UICanvas *canvas) {
auto assMan = &canvas->getGame()->assetManager;
auto assetFont = assMan->get<TrueTypeAsset>("truetype_ark");
auto assetTexture = assMan->get<TextureAsset>("texture_test");
auto textbox = canvas->addElement<VisualNovelTextbox>();
textbox->setBorder(&assetTexture->texture, glm::vec2(16, 16));
textbox->setFont(&assetFont->font);
textbox->setFontSize(40);

View File

@ -0,0 +1,62 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PokerPlayerDisplay.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
void PokerPlayerDisplay::updatePositions() {
UIEmpty::updatePositions();
this->labelChips.setTransform(
UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START,
glm::vec4(0, this->labelName.getHeight(), 0, 0),
0
);
}
PokerPlayerDisplay::PokerPlayerDisplay(UICanvas *canvas) :
UIEmpty(canvas),
labelName(canvas),
labelChips(canvas)
{
this->font = canvas->getGame()->assetManager.get<TrueTypeAsset>("truetype_ark");
this->addChild(&this->labelName);
this->labelName.setFont(&this->font->font);
this->labelName.setFontSize(40);
this->labelName.setTransform(
UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START,
glm::vec4(0, 0, 0, 0),
0.0f
);
this->addChild(&this->labelChips);
this->labelChips.setFont(&this->font->font);
this->labelChips.setFontSize(40);
this->font->eventLoaded.addListener(this, &PokerPlayerDisplay::onFontLoaded);
}
void PokerPlayerDisplay::setPlayer(PokerPlayer *player) {
assertNull(this->player);
this->player = player;
this->labelName.setText("Player");
this->labelChips.setText("Chips");
this->updatePositions();
}
void PokerPlayerDisplay::onFontLoaded() {
this->labelName.setFont(&this->font->font);
this->labelChips.setFont(&this->font->font);
this->updatePositions();
}
PokerPlayerDisplay::~PokerPlayerDisplay() {
this->font->eventLoaded.removeListener(this, &PokerPlayerDisplay::onFontLoaded);
}

View File

@ -0,0 +1,32 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "ui/UILabel.hpp"
#include "ui/UIEmpty.hpp"
#include "poker/PokerPlayer.hpp"
#include "asset/assets/TrueTypeAsset.hpp"
namespace Dawn {
class PokerPlayerDisplay : public UIEmpty {
private:
void onFontLoaded();
protected:
PokerPlayer *player = nullptr;
UILabel labelName;
UILabel labelChips;
TrueTypeAsset *font;
void updatePositions() override;
public:
PokerPlayerDisplay(UICanvas *canvas);
void setPlayer(PokerPlayer *player);
~PokerPlayerDisplay();
};
}