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

@ -25,4 +25,6 @@
#define ASSERT_GREATER_THAN_EQUAL_TO(x, y) ASSERT_TRUE(x >= y)
#define ASSERT_FLAG_OFF(flags, flag) ASSERT_FALSE(flags & flag)
#define ASSERT_FLAG_ON(flags, flag) ASSERT_TRUE(flags & flag)
#define ASSERT_FLAG_ON(flags, flag) ASSERT_TRUE(flags & flag)
#define ASSERT_STRING_EQUALS(a, b) ASSERT_EQUAL(strcmp(a, b), 0)

View File

@ -29,7 +29,9 @@ void _standardShaderUpdateUniforms(standardshader_t *stds) {
);
}
bool _standardShaderOnAssetItemLoaded(void *u, event_t e, void *s[], int32_t c){
bool _standardShaderOnAssetItemLoaded(
void *u, event_t e, const void *s[], const int32_t c
) {
standardshader_t *ss = (standardshader_t *)u;
ASSERT_NOT_NULL(ss);

View File

@ -8,6 +8,7 @@
#include "../assert/assert.h"
#define TEXTURE_SLOTS_MAX 8
#define TEXTURE_CHANNELS 4
/** Texture slot that a texture can be bound to */
typedef GLuint textureslot_t;

View File

@ -9,7 +9,7 @@
#include "../libs.h"
#include "texture.h"
#define MANAGED_TEXTURE_SCALE_MAX 4
#define MANAGED_TEXTURE_SCALE_MAX 5
typedef struct {
int16_t width;

View File

@ -56,7 +56,7 @@ void eventManagerUnsubscribe(eventmanager_t *man, eventlistener_t *listener) {
}
bool eventManagerTrigger(
eventmanager_t *manager, event_t event, void *args[], int32_t argc
eventmanager_t *manager, event_t event, const void *args[], const int32_t argc
) {
uint8_t i;
eventlistener_t *listener;

View File

@ -13,7 +13,7 @@
typedef uint8_t event_t;
typedef bool eventcallback_t(
void *user, event_t event, void *args[], int32_t argc
void *user, event_t event, const void *args[], const int32_t argc
);
typedef struct {
@ -64,5 +64,5 @@ void eventManagerUnsubscribe(eventmanager_t *man, eventlistener_t *listener);
* event listeners also return true.
*/
bool eventManagerTrigger(
eventmanager_t *manager, event_t event, void *args[], int32_t argc
eventmanager_t *manager, event_t event, const void *args[], const int32_t argc
);

View File

@ -16,6 +16,9 @@ void epochInit(epoch_t *epoch) {
}
void epochUpdate(epoch_t *epoch, float platformDelta) {
ASSERT_NOT_NULL(epoch);
ASSERT_GREATER_THAN(platformDelta, 0);
platformDelta = mathClamp(platformDelta, 0, EPOCH_FIXED_STEP);
epoch->last = epoch->current;

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);

View File

@ -13,7 +13,7 @@ bool gameInit(game_t *game) {
// Init the engine and the rendering pipeline
engineInit(&game->engine);
cameraLookAt(&game->camera, 3,3,3, 0,0,0);
cameraLookAt(&game->camera, 1,1,1, 0,0,0);
cameraPerspective(&game->camera, 45.0f, 16.0f/9.0f, 0.001f, 1000.0f);
quadInit(&game->quad, 0.0f, 0.0f,0.0f,0.0f,0.0f, 1.0f,1.0f,1.0f,1.0f);
@ -21,7 +21,7 @@ bool gameInit(game_t *game) {
game->owner = assetManagerHolderCreate(&game->engine.assetManager);
game->item = assetManagerLoadScaledTexture(
&game->engine.assetManager, game->owner, "textures", "test_texture"
&game->engine.assetManager, game->owner, "poker/world", "pub"
);
return true;
@ -34,9 +34,8 @@ bool gameUpdate(game_t *game, float delta) {
engineUpdateStart(&game->engine, delta);
if(game->engine.time.current > 4.0f && !doneResize) {
printf("Resizing");
// saveManagerSetUint8(&game->engine.save, SAVE_KEY_TEXTURE_SCALE, 0x03);
// assetManagerScaledTextureRescaleAll(&game->engine.assetManager);
printf("Resizing\n");
saveManagerSetU8(&game->engine.save, SAVE_KEY_TEXTURE_SCALE, 0x04);
doneResize = true;
}

View File

@ -8,6 +8,7 @@
#include "vnscene.h"
void vnSceneInit(vnscene_t *scene, font_t *font, texture_t *text) {
// Init the conversation
vnConversationInit(&scene->conversation, font, text);
scene->conversation.textbox.linesMax = 3;

View File

@ -9,8 +9,8 @@
#include "../utils/file.h"
#include "../utils/image.h"
#define RESIZE_VARIANT_COUNT 4
int RESIZE_SCALES[RESIZE_VARIANT_COUNT] = { 1, 2, 3, 4 };
#define RESIZE_VARIANT_COUNT 5
int RESIZE_SCALES[RESIZE_VARIANT_COUNT] = { 1, 2, 3, 4, 10 };
int main(int argc, char *argv[]) {
FILE *file;