Tested that texture scaling changes work

This commit is contained in:
2022-01-03 20:35:23 -08:00
parent c1583392c4
commit 7b61b590d7
15 changed files with 105 additions and 23 deletions

View File

@ -31,11 +31,15 @@ assetmanagerloaderdefinition_t ASSET_MANAGER_LOADERS[] = {
};
bool _assetManagerOnSaveManagerValueChange(
void *usr, event_t evt, void *args[], int32_t argc
void *usr, event_t evt, const void *args[], const int32_t argc
) {
ASSERT_NOT_NULL(args);
ASSERT_EQUAL(argc, 3);
printf("Hello World\n");
// Was the scale changed?
if(strcmp(args[1], SAVE_KEY_TEXTURE_SCALE) == 0) {
assetManagerScaledTextureRescaleAll((assetmanager_t *)usr);
}
return true;
}

View File

@ -18,7 +18,7 @@
/** Callback Listener for save manager value changes */
bool _assetManagerOnSaveManagerValueChange(
void *usr, event_t evt, void *args[], int32_t argc
void *usr, event_t evt, const void *args[], const int32_t argc
);
/**

View File

@ -16,8 +16,13 @@ assetmanageritem_t * assetManagerLoadScaledTexture(
uint8_t scale;
char buffer[ASSET_MANAGER_ITEM_NAME_MAX];
ASSERT_NOT_NULL(manager);
ASSERT_NOT_NULL(path);
ASSERT_NOT_NULL(file);
// Get the scale
scale = saveManagerGetU8(manager->save, SAVE_KEY_TEXTURE_SCALE);
ASSERT_LESS_THAN(scale, MANAGED_TEXTURE_SCALE_MAX);
// Generate a key
sprintf(buffer, "%s/%s", path, file);
@ -51,10 +56,15 @@ bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item) {
texturescalescale_t *sts;
size_t length;
ASSERT_NOT_NULL(item);
ASSERT_EQUAL(item->type, ASSET_MANAGER_TYPE_SCALED_TEXTURE);
// TODO: This can be improved if we allow both asset dependencies and
// dependency sibling adding
st = &item->data.scaledTexture.textureScale;
ASSERT_NOT_NULL(st->path);
ASSERT_NOT_NULL(st->file);
ASSERT_EQUAL(st->scaleCount, 0);
// Begin loading texture XML
sprintf(buffer, "%s/%s.xml", st->path, st->file);
@ -64,22 +74,45 @@ bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item) {
xmlLoad(&xml, xmlData);
free(xmlData);
ASSERT_STRING_EQUALS(xml.node, "texture");
ASSERT_EQUAL(xml.childrenCount, MANAGED_TEXTURE_SCALE_MAX);
// Parse root texture info
i = xmlGetAttributeByName(&xml, "channels");
ASSERT_GREATER_THAN_EQUAL_TO(i, 0);
st->channels = (uint8_t)atoi(xml.attributeDatas[i]);
ASSERT_EQUAL(st->channels, TEXTURE_CHANNELS);
i = xmlGetAttributeByName(&xml, "width");
ASSERT_GREATER_THAN_EQUAL_TO(i, 0);
st->baseWidth = (int16_t)atoi(xml.attributeDatas[i]);
ASSERT_GREATER_THAN(st->baseWidth, 0);
i = xmlGetAttributeByName(&xml, "height");
ASSERT_GREATER_THAN_EQUAL_TO(i, 0);
st->baseHeight = (int16_t)atoi(xml.attributeDatas[i]);
ASSERT_GREATER_THAN(st->baseHeight, 0);
for(j = 0; j < xml.childrenCount; j++) {
child = xml.children + j;
ASSERT_STRING_EQUALS(child->node, "texture-scale");
ASSERT_EQUAL(child->childrenCount, 0);
i = xmlGetAttributeByName(child, "scale");
ASSERT_GREATER_THAN_EQUAL_TO(i, 0);
st->scales[st->scaleCount].scale = (uint8_t)atoi(child->attributeDatas[i]);
ASSERT_GREATER_THAN_EQUAL_TO(st->scales[st->scaleCount].scale, 1);
i = xmlGetAttributeByName(child, "width");
ASSERT_GREATER_THAN_EQUAL_TO(i, 0);
st->scales[st->scaleCount].width = (int16_t)atoi(child->attributeDatas[i]);
ASSERT_GREATER_THAN(st->scales[st->scaleCount].width, 0);
i = xmlGetAttributeByName(child, "height");
ASSERT_GREATER_THAN_EQUAL_TO(i, 0);
st->scales[st->scaleCount].height = (int16_t)atoi(child->attributeDatas[i]);
ASSERT_GREATER_THAN(st->scales[st->scaleCount].height, 0);
st->scaleCount++;
}
@ -87,6 +120,7 @@ bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item) {
xmlDispose(&xml);
// Get the scale
ASSERT_LESS_THAN(item->data.scaledTexture.scale, st->scaleCount);
sts = st->scales + item->data.scaledTexture.scale;
// Get filename
@ -94,12 +128,14 @@ bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item) {
// Create some space
length = assetRawLoad(buffer, NULL);
if(length == 0) return false;
if(length <= 0) return false;
item->data.scaledTexture.data = malloc(sizeof(pixel_t) * length);
if(item->data.scaledTexture.data == NULL) return false;
// Load
length = assetRawLoad(buffer, (uint8_t *)item->data.scaledTexture.data);
if(length == 0) {
if(length <= 0) {
free(item->data.scaledTexture.data);
return false;
}
@ -110,16 +146,26 @@ bool _assetManagerLoaderScaledTextureAsync(assetmanageritem_t *item) {
bool _assetManagerLoaderScaledTextureSync(assetmanageritem_t *item) {
texturescale_t *st;
texturescalescale_t *sts;
st = &item->data.scaledTexture.textureScale;
sts = st->scales + item->data.scaledTexture.scale;
ASSERT_NOT_NULL(item);
ASSERT_EQUAL(item->type, ASSET_MANAGER_TYPE_SCALED_TEXTURE);
ASSERT_NOT_NULL(item->data.scaledTexture.data);
st = &item->data.scaledTexture.textureScale;
ASSERT_EQUAL(st->scaleCount, MANAGED_TEXTURE_SCALE_MAX);
ASSERT_LESS_THAN(item->data.scaledTexture.scale, st->scaleCount);
sts = st->scales + item->data.scaledTexture.scale;
printf("Loading texture synchronously. %i x %i\n", sts->width, sts->height);
// Create the actual texture.
textureInit(
&item->data.scaledTexture.texture,
sts->width, sts->height,
item->data.scaledTexture.data
);
free(item->data.scaledTexture.data);
return true;
}
@ -131,6 +177,11 @@ bool _assetManagerLoaderScaledTextureDispose(assetmanageritem_t *item) {
bool _assetManagerLoaderScaledTextureResize(
assetmanager_t *manager, assetmanageritem_t *item, uint8_t scale
) {
ASSERT_NOT_NULL(manager);
ASSERT_NOT_NULL(item);
ASSERT_LESS_THAN(scale, MANAGED_TEXTURE_SCALE_MAX);
ASSERT_EQUAL(item->type, ASSET_MANAGER_TYPE_SCALED_TEXTURE);
// Check if we need to update
if(item->data.scaledTexture.scale == scale) return true;
@ -151,6 +202,7 @@ bool _assetManagerLoaderScaledTextureResize(
// Update scale.
item->data.scaledTexture.scale = scale;
item->data.scaledTexture.textureScale.scaleCount = 0;
// Immediately requeue a texture re-load
if(!_assetManagerLoaderScaledTextureAsync(item)) return false;
@ -166,8 +218,11 @@ bool _assetManagerLoaderScaledTextureResize(
void assetManagerScaledTextureRescaleAll(assetmanager_t *manager) {
uint8_t i, scale;
ASSERT_NOT_NULL(manager);
// Get the new scale
scale = saveManagerGetU8(manager->save, SAVE_KEY_TEXTURE_SCALE);
ASSERT_LESS_THAN(scale, MANAGED_TEXTURE_SCALE_MAX);
// Rescale each texture. This is, unfortunately, blocking for now.
for(i = 0; i < manager->itemCount; i++) {

View File

@ -7,6 +7,7 @@
#pragma once
#include "item.h"
#include "../../assert/assert.h"
#include "../xml.h"
#include "../asset.h"
#include "../../save/save.h"

View File

@ -61,6 +61,20 @@ void xmlLoad(xml_t *xml, char *data);
*/
void xmlDispose(xml_t *xml);
/**
* Returns the index that the given attribute has based on its name.
*
* @param xml XML to get the attribute from.
* @param name Attribute name to look for.
* @return The index that the attribute is within the XML, or -1 if not found.
*/
int16_t xmlGetAttributeByName(xml_t *xml, char *name);
/**
* Tool to confirm if the given character is considered a whitespace or not.
* XML whitespace is considered; \t, \n, \r, or the space ASCII character.
*
* @param c Character to check.
* @return True if the character is whitespace, otherwise false.
*/
bool xmlIsWhitespace(char c);