Added texture, assets, tools and texture loading.
This commit is contained in:
10
src/dawn/asset/assets/CMakeLists.txt
Normal file
10
src/dawn/asset/assets/CMakeLists.txt
Normal 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
|
||||
)
|
62
src/dawn/asset/assets/TextureAsset.cpp
Normal file
62
src/dawn/asset/assets/TextureAsset.cpp
Normal 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);
|
||||
}
|
||||
}
|
29
src/dawn/asset/assets/TextureAsset.hpp
Normal file
29
src/dawn/asset/assets/TextureAsset.hpp
Normal 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();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user