Added texture, assets, tools and texture loading.

This commit is contained in:
2022-10-20 21:50:52 -07:00
parent 80d6cba854
commit 043873cc2d
38 changed files with 1626 additions and 15 deletions

View File

@ -0,0 +1,10 @@
# Copyright (c) 2022 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TextureAsset.cpp
)

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 "TextureAsset.hpp"
using namespace Dawn;
TextureAsset::TextureAsset(AssetManager &assetManager, std::string name) :
Asset(assetManager, name),
loader(name + ".texture")
{
this->texture = std::make_shared<Texture>();
}
void TextureAsset::updateSync() {
if(this->state == 0x00 || this->state == 0x01) return;
this->state = 0x03;
this->texture->setSize(this->width, this->height);
this->texture->buffer(this->colors);
this->state = 0x04;
this->loaded = true;
}
void TextureAsset::updateAsync() {
if(this->state != 0x00) return;
this->loader.loadRaw(&this->buffer);
this->state = 0x01;
// Parse header data.
char integer[32];
size_t j = 0, i = 0;
while(true) {
auto c = this->buffer[i++];
if(c == '|') {
integer[j] = '\0';
if(this->width == -1) {
this->width = atoi(integer);
if(this->width <= 0) throw "Invalid width";
j = 0;
continue;
} else {
this->height = atoi(integer);
if(this->height <= 0) throw "Invalid height";
break;
}
}
integer[j++] = c;
}
this->colors = (struct Color *)((void *)(this->buffer + i));
this->state = 0x02;
}
TextureAsset::~TextureAsset() {
if(this->buffer != nullptr) {
memoryFree(this->buffer);
}
}

View File

@ -0,0 +1,29 @@
// 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/Texture.hpp"
namespace Dawn {
class TextureAsset : public Asset {
protected:
AssetLoader loader;
uint8_t *buffer = nullptr;
int32_t width = -1, height = -1;
struct Color *colors;
public:
std::shared_ptr<Texture> texture;
TextureAsset(AssetManager &assetManager, std::string name);
void updateSync() override;
void updateAsync() override;
~TextureAsset();
};
}