Fixed whatever problem was with texture loading.

This commit is contained in:
2026-02-16 12:29:25 -06:00
parent d7a0bb4509
commit 9c9d2d548e
13 changed files with 86 additions and 34 deletions

View File

@@ -199,7 +199,7 @@ function sceneRender()
1, 1
)
textDraw(10, 10, "Minesweeper")
textDraw(10, 10, "A")
-- centerX = math.floor(screenGetWidth() / 2)
-- centerY = math.floor(screenGetHeight() / 2)

Binary file not shown.

BIN
assets/ui/minogram.dtf Normal file

Binary file not shown.

View File

@@ -20,10 +20,8 @@ typedef struct assetentire_s assetentire_t;
typedef struct {
char_t header[3];
uint8_t version;
uint32_t width;
uint32_t height;
uint8_t paletteIndex;
uint8_t palette[ASSET_TEXTURE_SIZE_MAX];
} assettexture_t;
#pragma pack(pop)

View File

@@ -8,6 +8,7 @@
#include "asset/asset.h"
#include "assert/assert.h"
#include "display/tileset/tileset.h"
#include "util/memory.h"
errorret_t assetTilesetLoad(assetentire_t entire) {
assertNotNull(entire.data, "Asset data cannot be null");
@@ -28,5 +29,51 @@ errorret_t assetTilesetLoad(assetentire_t entire) {
errorThrow("Unsupported tileset version");
}
errorThrow("unfinished");
// Fix endianness
tilesetData->tileWidth = le16toh(tilesetData->tileWidth);
tilesetData->tileHeight = le16toh(tilesetData->tileHeight);
tilesetData->columnCount = le16toh(tilesetData->columnCount);
tilesetData->rowCount = le16toh(tilesetData->rowCount);
tilesetData->right = le16toh(tilesetData->right);
tilesetData->bottom = le16toh(tilesetData->bottom);
if(tilesetData->tileWidth == 0) {
errorThrow("Tile width cannot be 0");
}
if(tilesetData->tileHeight == 0) {
errorThrow("Tile height cannot be 0");
}
if(tilesetData->columnCount == 0) {
errorThrow("Column count cannot be 0");
}
if(tilesetData->rowCount == 0) {
errorThrow("Row count cannot be 0");
}
uint32_t temp;
memoryCopy(&temp, &tilesetData->u0, sizeof(float_t));
temp = le32toh(temp);
memoryCopy(&tilesetData->u0, &temp, sizeof(float_t));
if(tilesetData->u0 < 0.0f || tilesetData->u0 > 1.0f) {
errorThrow("Invalid u0 value in tileset");
}
memoryCopy(&temp, &tilesetData->v0, sizeof(float_t));
temp = le32toh(temp);
memoryCopy(&tilesetData->v0, &temp, sizeof(float_t));
if(tilesetData->v0 < 0.0f || tilesetData->v0 > 1.0f) {
errorThrow("Invalid v0 value in tileset");
}
// Setup tileset
tileset->tileWidth = tilesetData->tileWidth;
tileset->tileHeight = tilesetData->tileHeight;
tileset->tileCount = tilesetData->columnCount * tilesetData->rowCount;
tileset->columns = tilesetData->columnCount;
tileset->rows = tilesetData->rowCount;
tileset->uv[0] = tilesetData->u0;
tileset->uv[1] = tilesetData->v0;
errorOk();
}

View File

@@ -12,6 +12,14 @@
typedef struct {
char_t header[3];
uint8_t version;
uint16_t tileWidth;
uint16_t tileHeight;
uint16_t columnCount;
uint16_t rowCount;
uint16_t right;
uint16_t bottom;
float_t u0;
float_t v0;
} assettileset_t;
#pragma pack(pop)

View File

@@ -88,6 +88,7 @@ errorret_t displayInit(void) {
#else
GLint mask = 0;
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
DISPLAY.usingShaderedPalettes = true;
if(mask & GL_CONTEXT_CORE_PROFILE_BIT) {
GLint numExtens = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &numExtens);
@@ -100,7 +101,7 @@ errorret_t displayInit(void) {
}
} else {
const char* ext = (const char*)glGetString(GL_EXTENSIONS);
DISPLAY.usingShaderedPalettes = (
DISPLAY.usingShaderedPalettes = !(
ext && strstr(ext, "GL_EXT_paletted_texture")
);
}

View File

@@ -28,7 +28,7 @@ void paletteInit(
memoryZero(palette, sizeof(palette_t));
palette->colorCount = colorCount;
memoryCopy(colors, palette->colors, colorCount * sizeof(color_t));
memoryCopy(palette->colors, colors, colorCount * sizeof(color_t));
}
void paletteBind(palette_t *palette, const uint8_t slot) {

View File

@@ -36,7 +36,6 @@ void textDrawChar(
if(tileIndex < 0 || tileIndex >= tileset->tileCount) {
tileIndex = ((int32_t)'@') - TEXT_CHAR_START;
}
assertTrue(
tileIndex >= 0 && tileIndex <= tileset->tileCount,
"Character is out of bounds for font tiles"

View File

@@ -52,12 +52,13 @@ void textureInit(
uint8_t formatted[width * height];
for(int32_t i = 0; i < width * height; i++) {
uint8_t index = data.paletteData[i];
formatted[i] = index;
formatted[i] = index * 128;
}
glTexImage2D(
GL_TEXTURE_2D, 0, GL_R, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (void*)formatted
GL_TEXTURE_2D, 0, GL_R8, width, height, 0,
GL_RED, GL_UNSIGNED_BYTE, (void*)formatted
);
} else {
glTexImage2D(
GL_TEXTURE_2D,
@@ -66,12 +67,16 @@ void textureInit(
0, GL_COLOR_INDEX8_EXT,
GL_UNSIGNED_BYTE, (void*)data.paletteData
);
// glColorTableEXT(
// GL_TEXTURE_2D, GL_RGBA, data.palette.palette->colorCount, GL_RGBA,
// GL_UNSIGNED_BYTE, (const void*)data.palette.palette->colors
// );
}
GLenum err = glGetError();
if(err != GL_NO_ERROR) {
assertUnreachable("GL error uploading palette texture");
}
break;
default:

View File

@@ -9,14 +9,12 @@
#include "dusk.h"
typedef struct tileset_s {
const char_t *name;
const uint16_t tileWidth;
const uint16_t tileHeight;
const uint16_t tileCount;
const uint16_t columns;
const uint16_t rows;
const vec2 uv;
const char_t *image;
uint16_t tileWidth;
uint16_t tileHeight;
uint16_t tileCount;
uint16_t columns;
uint16_t rows;
vec2 uv;
} tileset_t;
/**

View File

@@ -41,13 +41,7 @@ int moduleTilesetIndex(lua_State *l) {
tileset_t *ts = (tileset_t *)luaL_checkudata(l, 1, "tileset_mt");
assertNotNull(ts, "Tileset pointer cannot be NULL.");
if(stringCompare(key, "name") == 0) {
lua_pushstring(l, ts->name);
return 1;
} else if(stringCompare(key, "texture") == 0) {
lua_pushstring(l, ts->image);
return 1;
} else if(stringCompare(key, "tileWidth") == 0) {
if(stringCompare(key, "tileWidth") == 0) {
lua_pushnumber(l, ts->tileWidth);
return 1;
} else if(stringCompare(key, "tileHeight") == 0) {
@@ -72,7 +66,9 @@ int moduleTilesetToString(lua_State *l) {
tileset_t *ts = (tileset_t *)luaL_checkudata(l, 1, "tileset_mt");
assertNotNull(ts, "Tileset pointer cannot be NULL.");
lua_pushfstring(l, "Tileset: %s", ts->name);
lua_pushfstring(l, "Tileset: %dx%d tile, %d columns, %d rows",
ts->tileWidth, ts->tileHeight, ts->columns, ts->rows
);
return 1;
}

View File

@@ -151,7 +151,7 @@
const getValues = () => {
if(!pixels) return null;
let tileWidth, tileHeight, columnCount, rowCount, right, bottom;
let tileWidth, tileHeight, columnCount, rowCount;
if(elDefineBySize.checked) {
console.log('Defining by size');
tileWidth = parseInt(elTileWidth.value) || 0;
@@ -166,8 +166,8 @@
tileHeight = Math.floor(imageHeight / rowCount);
}
right = parseInt(elRight.value) || 0;
bottom = parseInt(elBottom.value) || 0;
const right = parseInt(elRight.value) || 0;
const bottom = parseInt(elBottom.value) || 0;
const scale = parseInt(elScale.value) || 1;
const scaledWidth = imageWidth * scale;
@@ -177,13 +177,13 @@
const scaledRight = right * scale;
const scaledBottom = bottom * scale;
const u0 = tileWidth / imageWidth - (right / imageWidth);
const v0 = tileHeight / imageHeight - (bottom / imageHeight);
const u0 = (tileWidth / imageWidth);
const v0 = (tileHeight / imageHeight);
const hoveredTileX = isNaN(hoveredX) || hoveredX < 0 ? 0 : hoveredX;
const hoveredTileY = isNaN(hoveredY) || hoveredY < 0 ? 0 : hoveredY;
const hoveredU0 = hoveredTileX * tileWidth / imageWidth;
const hoveredV0 = hoveredTileY * tileHeight / imageHeight;
const hoveredU0 = hoveredTileX * u0;
const hoveredV0 = hoveredTileY * v0;
const hoveredU1 = hoveredU0 + u0;
const hoveredV1 = hoveredV0 + v0;
const hoveredTileIndex = hoveredTileY * columnCount + hoveredTileX;