Made emscripten work a bit better

This commit is contained in:
2023-11-01 10:42:51 -05:00
parent 8ca8358aa5
commit 4315a7e847
33 changed files with 548 additions and 287 deletions

View File

@ -7,9 +7,7 @@
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
glm
stb
freetype
archive_static
)
# Includes

View File

@ -1,180 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "AssetLoader.hpp"
#include "util/mathutils.hpp"
using namespace Dawn;
ssize_t assetLoaderArchiveRead(
struct archive *archive,
void *d,
const void **buffer
) {
assertNotNull(archive, "assetArchiveRead: Archive is NULL!");
assertNotNull(d, "assetArchiveRead: Data is NULL!");
assertNotNull(buffer, "assetArchiveRead: Buffer is NULL!");
AssetLoader *loader = (AssetLoader*)d;
*buffer = loader->buffer;
size_t read = fread(
loader->buffer, 1, ASSET_LOADER_BUFFER_SIZE, loader->assetArchiveFile
);
if(ferror(loader->assetArchiveFile)) return ARCHIVE_FATAL;
return read;
}
int64_t assetLoaderArchiveSeek(
struct archive *archive,
void *d,
int64_t offset,
int32_t whence
) {
assertNotNull(archive, "assetArchiveSeek: Archive is NULL!");
assertNotNull(d, "assetArchiveSeek: Data is NULL!");
assertTrue(offset > 0, "assetArchiveSeek: Offset must be greater than 0!");
AssetLoader *loader = (AssetLoader*)d;
int32_t ret = fseek(loader->assetArchiveFile, offset, whence);
assertTrue(ret == 0, "assetArchiveSeek: Failed to seek!");
return ftell(loader->assetArchiveFile);
}
int32_t assetLoaderArchiveOpen(struct archive *a, void *d) {
assertNotNull(a, "assetArchiveOpen: Archive is NULL!");
assertNotNull(d, "assetArchiveOpen: Data is NULL!");
AssetLoader *loader = (AssetLoader*)d;
int32_t ret = fseek(loader->assetArchiveFile, 0, SEEK_SET);
assertTrue(ret == 0, "assetArchiveOpen: Failed to seek to start of file!");
return ARCHIVE_OK;
}
int32_t assetLoaderArchiveClose(struct archive *a, void *d) {
assertNotNull(a, "assetArchiveClose: Archive is NULL!");
assertNotNull(d, "assetArchiveClose: Data is NULL!");
return assetLoaderArchiveOpen(a, d);
}
AssetLoader::AssetLoader(std::string fileName) {
assertTrue(fileName.size() > 0, "AssetLoader::AssetLoader: fileName must be greater than 0");
this->fileName = fileName;
}
void AssetLoader::open() {
assertNull(this->assetArchiveFile, "AssetLoader::open: File is already open");
assertNull(this->assetArchive, "AssetLoader::open: Archive is already open");
assertNull(this->assetArchiveEntry, "AssetLoader::open: Entry is already open");
this->assetArchiveFile = fopen(DAWN_ASSET_LOCATION, "rb");
assertNotNull(this->assetArchiveFile, "AssetLoader::open: Failed to open file " + std::string(DAWN_ASSET_LOCATION));
// Open archive reader
assetArchive = archive_read_new();
assertNotNull(assetArchive, "AssetLoader::open: Failed to create archive reader");
// Set up the reader
archive_read_support_format_tar(assetArchive);
// Open reader
archive_read_set_open_callback(assetArchive, &assetLoaderArchiveOpen);
archive_read_set_read_callback(assetArchive, &assetLoaderArchiveRead);
archive_read_set_seek_callback(assetArchive, &assetLoaderArchiveSeek);
archive_read_set_close_callback(assetArchive, &assetLoaderArchiveClose);
archive_read_set_callback_data(assetArchive, this);
int32_t ret = archive_read_open1(assetArchive);
assertTrue(ret == ARCHIVE_OK, "AssetLoader::open: Failed to open archive!");
position = 0;
// Iterate over each file to find the one for this asset loader.
while(archive_read_next_header(assetArchive, &assetArchiveEntry) == ARCHIVE_OK) {
const char_t *headerFile = (char_t*)archive_entry_pathname(assetArchiveEntry);
if(std::string(headerFile) == this->fileName) return;
int32_t ret = archive_read_data_skip(assetArchive);
assertTrue(ret == ARCHIVE_OK, "AssetLoader::open: Failed to skip data!");
}
assertUnreachable("AssetLoader::open: Failed to find file!");
}
int32_t AssetLoader::close() {
assertNotNull(this->assetArchiveFile, "AssetLoader::close: File is NULL");
assertNotNull(this->assetArchive, "AssetLoader::close: Archive is NULL!");
assertNotNull(this->assetArchiveEntry, "AssetLoader::close: Entry is NULL!");
// Close the archive
int32_t ret = archive_read_free(this->assetArchive);
assertTrue(ret == ARCHIVE_OK, "AssetLoader::close: Failed to close archive!");
this->assetArchive = nullptr;
this->assetArchiveEntry = nullptr;
// Close the file
int32_t res = fclose(this->assetArchiveFile);
this->assetArchiveFile = nullptr;
return res;
}
size_t AssetLoader::read(uint8_t *buffer, size_t size) {
assertNotNull(buffer, "assetArchiveRead: Buffer is NULL!");
assertTrue(size > 0, "assetArchiveRead: Size must be greater than 0!");
assertNotNull(this->assetArchive, "assetRead: Archive is NULL!");
assertNotNull(this->assetArchiveEntry, "assetRead: Entry is NULL!");
ssize_t read = archive_read_data(this->assetArchive, buffer, size);
this->position += read;
if(read == ARCHIVE_FATAL) {
assertUnreachable(archive_error_string(this->assetArchive));
}
assertTrue(read != ARCHIVE_RETRY, "assetRead: Failed to read data (RETRY)!");
assertTrue(read != ARCHIVE_WARN, "assetRead: Failed to read data (WARN)!");
return read;
}
size_t AssetLoader::getSize() {
assertTrue(this->assetArchiveEntry != nullptr, "AssetLoader::getSize: Entry is NULL!");
assertTrue(archive_entry_size_is_set(assetArchiveEntry), "assetGetSize: Entry size is not set!");
return archive_entry_size(assetArchiveEntry);
}
size_t AssetLoader::skip(size_t n) {
assertTrue(n >= 0, "AssetLoader::skip: Byte count must be greater than 0.");
uint8_t dumpBuffer[ASSET_LOADER_BUFFER_SIZE];
size_t skipped = 0;
size_t n2, n3;
while(n != 0) {
n2 = mathMin<size_t>(n, ASSET_LOADER_BUFFER_SIZE);
n3 = this->read(dumpBuffer, n2);
assertTrue(n3 == n2, "AssetLoader::skip: Failed to skip bytes!");
n -= n3;
}
return skipped;
}
void AssetLoader::rewind() {
// TODO: See if I can optimize this
this->close();
this->open();
}
size_t AssetLoader::getPosition() {
assertNotNull(this->assetArchiveFile, "AssetLoader::getPosition: File is not open!");
return this->position;
}
size_t AssetLoader::setPosition(size_t position) {
assertTrue(position >= 0, "AssetLoader::setPosition: position must be greater than or equal to 0");
this->rewind();
return this->skip(position);
}
AssetLoader::~AssetLoader() {
if(this->assetArchiveFile != nullptr) this->close();
}

View File

@ -7,7 +7,7 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Asset.cpp
AssetLoader.cpp
IAssetLoader.cpp
AssetManager.cpp
)

View File

@ -0,0 +1,22 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "IAssetLoader.hpp"
using namespace Dawn;
IAssetLoader::IAssetLoader(std::string fileName) {
assertTrue(fileName.size() > 0, "IAssetLoader::IAssetLoader: fileName must be greater than 0");
this->fileName = fileName;
}
size_t IAssetLoader::setPosition(size_t position) {
assertTrue(position >= 0, "IAssetLoader::setPosition: position must be greater than or equal to 0");
this->rewind();
return this->skip(position);
}
IAssetLoader::~IAssetLoader() {
}

View File

@ -4,10 +4,6 @@
// https://opensource.org/licenses/MIT
#pragma once
extern "C" {
#include <archive.h>
#include <archive_entry.h>
}
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
#include "util/memory.hpp"
@ -18,87 +14,30 @@ extern "C" {
#error Asset Archive Location has not been defined.
#endif
/**
* Method invoked by the libarchive internals to read bytes from the archive
* file pointer.
*
* @param archive Archive requesting the read.
* @param data Data pointer passed to the archive.
* @param buffer Pointer to where the buffer pointer should be stored.
* @return Count of bytes read.
*/
ssize_t assetLoaderArchiveRead(
struct archive *archive,
void *data,
const void **buffer
);
/**
* Method invoked by the libarchive internals to seek the archive file pointer.
*
* @param archive Archive requesting the seek.
* @param data Data pointer passed to the archive.
* @param offset Offset to seek to.
* @param whence Whence to seek from.
* @return The new offset.
*/
int64_t assetLoaderArchiveSeek(
struct archive *archive,
void *data,
int64_t offset,
int32_t whence
);
/**
* Method invoked by the libarchive internals to open the archive file pointer.
*
* @param archive Archive requesting the open.
* @param data Data pointer passed to the archive.
* @return 0 if success, otherwise for failure.
*/
int32_t assetLoaderArchiveOpen(struct archive *a, void *data);
/**
* Method invoked by the libarchive internals to close the archive file pointer.
*
* @param archive Archive requesting the close.
* @param data Data pointer passed to the archive.
* @return 0 if success, otherwise for failure.
*/
int32_t assetLoaderArchiveClose(struct archive *a, void *data);
namespace Dawn {
class AssetLoader {
private:
class IAssetLoader {
protected:
std::string fileName;
struct archive *assetArchive = nullptr;
struct archive_entry *assetArchiveEntry = nullptr;
size_t position;
public:
uint8_t buffer[ASSET_LOADER_BUFFER_SIZE];
FILE *assetArchiveFile = nullptr;
/**
* Create a new asset loader. Asset Loaders can be used to load data from
* a file in a myriad of ways.
*
* @param fileName File name of the asset that is to be loaded.
*/
AssetLoader(std::string fileName);
IAssetLoader(std::string fileName);
/**
* Platform-centric method to open a file buffer to an asset.
*
* @return 0 if success, otherwise for failure.
*/
void open();
virtual void open() = 0;
/**
* Closes the previously ppened asset.
* @return 0 if successful, otherwise false.
*/
int32_t close();
virtual int32_t close() = 0;
/**
* Read bytes from buffer.
@ -106,13 +45,13 @@ namespace Dawn {
* @param size Length of the data buffer (How many bytes to read).
* @return The count of bytes read.
*/
size_t read(uint8_t *buffer, size_t size);
virtual size_t read(uint8_t *buffer, size_t size) = 0;
/**
* Get the size of the asset.
* @return The size of the asset in bytes.
*/
size_t getSize();
virtual size_t getSize() = 0;
/**
* Skips the read head forward to a given position.
@ -120,12 +59,12 @@ namespace Dawn {
* @param n Count of bytes to progress the read head by.
* @return Count of bytes progressed.
*/
size_t skip(size_t n);
virtual size_t skip(size_t n) = 0;
/**
* Rewind the read head to the beginning of the file.
*/
void rewind();
virtual void rewind() = 0;
/**
* Sets the absolute position of the read head within the buffer of the
@ -140,7 +79,7 @@ namespace Dawn {
*
* @return The current read head position.
*/
size_t getPosition();
virtual size_t getPosition() = 0;
/**
* Run a callback for each byte within the asset. The callback will
@ -189,6 +128,6 @@ namespace Dawn {
/**
* Cleanup the asset loader.
*/
virtual ~AssetLoader();
virtual ~IAssetLoader();
};
}

View File

@ -5,7 +5,7 @@
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "asset/AssetLoader.hpp"
namespace Dawn {
class AudioSource;

View File

@ -5,7 +5,7 @@
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "asset/AssetLoader.hpp"
namespace Dawn {
struct AssetLanguageValue {

View File

@ -5,7 +5,7 @@
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "asset/AssetLoader.hpp"
#include "display/Texture.hpp"
namespace Dawn {

View File

@ -5,7 +5,7 @@
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "asset/AssetLoader.hpp"
#include "display/Tileset.hpp"
namespace Dawn {

View File

@ -5,7 +5,7 @@
#pragma once
#include "../Asset.hpp"
#include "../AssetLoader.hpp"
#include "asset/AssetLoader.hpp"
#include "util/flag.hpp"
#include "display/font/truetype/TrueTypeFaceTexture.hpp"
#include "util/UsageLock.hpp"

View File

@ -9,8 +9,6 @@
#include "dawnsharedlibs.hpp"
#include "util/memory.hpp"
#include <stb_truetype.h>
#include <ft2build.h>
#include FT_FREETYPE_H

View File

@ -27,7 +27,7 @@ TrueTypeFaceTexture::TrueTypeFaceTexture(
// First pass, determine the textures' dimensions.
for(c = TRUE_TYPE_CHAR_BEGIN; c < TRUE_TYPE_CHAR_END; c++) {
// Load the character
auto ret = FT_Load_Char(face, c, FT_LOAD_BITMAP_METRICS_ONLY);
auto ret = FT_Load_Char(face, c, ~FT_LOAD_RENDER);
if(ret) {
assertUnreachable("TrueTypeFaceTexture::TrueTypeFaceTexture: Failed to load character (0)");
}

View File

@ -20,6 +20,7 @@ namespace Dawn {
public:
virtual void removeListener() = 0;
virtual void teardown() = 0;
virtual ~IStateOwnerEventLegacy() {}
};
class IStateOwner {