137 lines
4.0 KiB
C++
137 lines
4.0 KiB
C++
// Copyright (c) 2022 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "TextureAsset.hpp"
|
|
#include "util/memory.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
TextureAsset::TextureAsset(const std::string name) :
|
|
Asset(name),
|
|
loader(name + ".texture"),
|
|
texture()
|
|
{
|
|
}
|
|
|
|
void TextureAsset::updateSync() {
|
|
if(this->state != TEXTURE_ASSET_LOAD_STATE_SYNC_HANDOFF) return;
|
|
|
|
this->state = TEXTURE_ASSET_LOAD_STATE_BUFFERING_TEXTURE;
|
|
this->texture.setSize(
|
|
this->width, this->height, this->format, TEXTURE_DATA_FORMAT_UNSIGNED_BYTE
|
|
);
|
|
this->texture.buffer(this->colors);
|
|
|
|
this->texture.wrapModeX = this->wrapModeX;
|
|
this->texture.wrapModeY = this->wrapModeY;
|
|
this->texture.filterModeMin = this->filterModeMin;
|
|
this->texture.filterModeMag = this->filterModeMag;
|
|
|
|
this->state = TEXTURE_ASSET_LOAD_STATE_COMPLETE;
|
|
this->loaded = true;
|
|
}
|
|
|
|
void TextureAsset::updateAsync() {
|
|
if(this->state != TEXTURE_ASSET_LOAD_STATE_INITIAL) return;
|
|
this->state = TEXTURE_ASSET_LOAD_STATE_OPENING;
|
|
this->loader.open();
|
|
this->buffer = memoryAllocate<uint8_t>(this->loader.getSize());
|
|
this->loader.read(this->buffer, this->loader.getSize());
|
|
this->loader.close();
|
|
this->state = TEXTURE_ASSET_LOAD_STATE_PARSING_HEADER;
|
|
|
|
// Parse header data.
|
|
char integer[256];
|
|
size_t j = 0, i = 0;
|
|
enum TextureAssetHeaderParseState parseState =
|
|
TEXTURE_ASSET_HEADER_PARSE_STATE_VERSION
|
|
;
|
|
while(true) {
|
|
if(parseState == TEXTURE_ASSET_HEADER_PARSE_STATE_END) break;
|
|
|
|
auto c = this->buffer[i++];
|
|
if(c != '|') {
|
|
integer[j++] = c;
|
|
continue;
|
|
}
|
|
|
|
integer[j] = '\0';
|
|
|
|
switch(parseState) {
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_VERSION: {
|
|
auto compared = strcmp(integer, "DT_2.00");
|
|
assertTrue(compared == 0, "Invalid version");
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_WIDTH;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_WIDTH: {
|
|
this->width = atoi(integer);
|
|
assertTrue(this->width > 0, "Invalid width");
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_HEIGHT;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_HEIGHT: {
|
|
this->height = atoi(integer);
|
|
assertTrue(this->height > 0, "Invalid height");
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_FORMAT;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_FORMAT: {
|
|
this->format = (enum TextureFormat)atoi(integer);
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_WRAP_MODE_X;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_WRAP_MODE_X: {
|
|
this->wrapModeX = (enum TextureWrapMode)atoi(integer);
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_WRAP_MODE_Y;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_WRAP_MODE_Y: {
|
|
this->wrapModeY = (enum TextureWrapMode)atoi(integer);
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_FILTER_MODE_MIN;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_FILTER_MODE_MIN: {
|
|
this->filterModeMin = (enum TextureFilterMode)atoi(integer);
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_FILTER_MODE_MAG;
|
|
break;
|
|
}
|
|
|
|
case TEXTURE_ASSET_HEADER_PARSE_STATE_FILTER_MODE_MAG: {
|
|
this->filterModeMag = (enum TextureFilterMode)atoi(integer);
|
|
j = 0;
|
|
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_END;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
assertUnreachable("Invalid parse state");
|
|
}
|
|
}
|
|
|
|
this->colors = (uint8_t*)((void *)(this->buffer + i));
|
|
this->state = TEXTURE_ASSET_LOAD_STATE_SYNC_HANDOFF;
|
|
}
|
|
|
|
TextureAsset::~TextureAsset() {
|
|
if(this->buffer != nullptr) {
|
|
memoryFree(this->buffer);
|
|
this->buffer = nullptr;
|
|
}
|
|
}
|