Added tileset loading.

This commit is contained in:
2022-12-05 13:57:18 -08:00
parent bcdb0742f3
commit 8059158bae
10 changed files with 314 additions and 4 deletions

View File

@ -7,6 +7,10 @@
#include "Asset.hpp"
#include "util/array.hpp"
#include "assets/TextureAsset.hpp"
#include "assets/TilesetAsset.hpp"
#include "assets/TrueTypeAsset.hpp"
namespace Dawn {
class AssetManager {
private:

View File

@ -7,5 +7,6 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TextureAsset.cpp
TilesetAsset.cpp
TrueTypeAsset.cpp
)

View File

@ -0,0 +1,87 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TilesetAsset.hpp"
using namespace Dawn;
TilesetAsset::TilesetAsset(AssetManager *assMan, std::string name) :
Asset(assMan, name),
loader(name + ".tileset"),
tileset()
{
}
void TilesetAsset::updateSync() {
}
void TilesetAsset::updateAsync() {
uint8_t *buffer;
assertTrue(this->state == 0x00);
this->state = 0x01;
this->loader.loadRaw(&buffer);
this->state = 0x02;
char *strCurrent = (char *)buffer;
char *strNext, *strSubCurrent;
// Read cols and rows
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.columns = atoi(strCurrent);
this->state = 0x03;
assertTrue(this->tileset.columns > 0);
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.rows = atoi(strCurrent);
this->state = 0x04;
assertTrue(this->tileset.rows > 0);
// Begin reading tiles.
int32_t done = 0;
int32_t count = this->tileset.columns * this->tileset.rows;
while(done < count) {
struct Tile tile;
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
// Parse ux0
strSubCurrent = strchr(strCurrent, ',');
*strSubCurrent = '\0';
tile.uv0.x = atof(strCurrent);
// Parse ux1
strCurrent = strSubCurrent+1;
strSubCurrent = strchr(strCurrent, ',');
*strSubCurrent = '\0';
tile.uv1.x = atof(strCurrent);
// Parse uy0
strCurrent = strSubCurrent+1;
strSubCurrent = strchr(strCurrent, ',');
*strSubCurrent = '\0';
tile.uv0.y = atof(strCurrent);
// Parse uy1
strCurrent = strSubCurrent+1;
*strNext = '\0';
tile.uv1.y = atof(strCurrent);
// Next.
this->tileset.tiles.push_back(tile);
done++;
}
this->state = 0x05;
this->loaded = true;
this->eventLoaded.invoke();
memoryFree(buffer);
}

View File

@ -0,0 +1,25 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "display/Tileset.hpp"
namespace Dawn {
class TilesetAsset : public Asset {
protected:
AssetLoader loader;
uint8_t state = 0x00;
public:
struct TilesetGrid tileset;
TilesetAsset(AssetManager *assMan, std::string name);
void updateSync() override;
void updateAsync() override;
};
}

View File

@ -22,6 +22,10 @@ namespace Dawn {
public:
int32_t rows;
int32_t columns;
TilesetGrid() {
}
TilesetGrid(
int32_t columns,