prog
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
tileset.c
|
||||
texture.c
|
||||
palette.c
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "palette.h"
|
||||
|
||||
palette_t PALETTES[PALETTE_COUNT];
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/color.h"
|
||||
|
||||
#define PALETTE_COLOR_COUNT 0xFF
|
||||
#define PALETTE_COUNT 6
|
||||
|
||||
typedef struct {
|
||||
color_t colors[PALETTE_COLOR_COUNT];
|
||||
uint8_t count;
|
||||
} palette_t;
|
||||
|
||||
extern palette_t PALETTES[PALETTE_COUNT];
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "texture.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/math.h"
|
||||
#include "display/display.h"
|
||||
|
||||
texture_t TEXTURE_WHITE;
|
||||
color_t TEXTURE_WHITE_PIXELS[4*4] = {
|
||||
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||
};
|
||||
|
||||
texture_t TEXTURE_TEST;
|
||||
color_t TEXTURE_TEST_PIXELS[4*4] = {
|
||||
COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA,
|
||||
COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK,
|
||||
COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA,
|
||||
COLOR_MAGENTA, COLOR_BLACK, COLOR_MAGENTA, COLOR_BLACK,
|
||||
};
|
||||
|
||||
errorret_t textureInit(
|
||||
texture_t *texture,
|
||||
const int32_t width,
|
||||
const int32_t height,
|
||||
const textureformat_t format,
|
||||
const texturedata_t data
|
||||
) {
|
||||
assertNotNull(texture, "Texture cannot be NULL");
|
||||
assertTrue(width > 0 && height > 0, "width/height must be greater than 0");
|
||||
assertTrue(width == mathNextPowTwo(width), "Width must be a power of 2.");
|
||||
assertTrue(height == mathNextPowTwo(height), "Height must be a power of 2.");
|
||||
|
||||
if(texture->format == TEXTURE_FORMAT_RGBA) {
|
||||
assertNotNull(data.rgbaColors, "RGBA color data cannot be NULL");
|
||||
} else if(texture->format == TEXTURE_FORMAT_PALETTE) {
|
||||
assertNotNull(data.paletted.indices, "Palette indices cannot be NULL");
|
||||
assertNotNull(data.paletted.palette, "Palette colors cannot be NULL");
|
||||
assertTrue(
|
||||
data.paletted.palette->count ==
|
||||
mathNextPowTwo(data.paletted.palette->count),
|
||||
"Palette color count must be a power of 2"
|
||||
);
|
||||
}
|
||||
|
||||
memoryZero(texture, sizeof(texture_t));
|
||||
texture->width = width;
|
||||
texture->height = height;
|
||||
texture->format = format;
|
||||
|
||||
errorChain(textureInitPlatform(texture, width, height, format, data));
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t textureDispose(texture_t *texture) {
|
||||
assertNotNull(texture, "Texture cannot be NULL");
|
||||
|
||||
errorChain(textureDisposePlatform(texture));
|
||||
errorOk();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
#include "display/texture/palette.h"
|
||||
#include "display/texture/textureplatform.h"
|
||||
|
||||
#ifndef textureInitPlatform
|
||||
#error "textureInitPlatform should not be defined."
|
||||
#endif
|
||||
#ifndef textureDisposePlatform
|
||||
#error "textureDisposePlatform should not be defined."
|
||||
#endif
|
||||
|
||||
typedef textureformatplatform_t textureformat_t;
|
||||
typedef textureplatform_t texture_t;
|
||||
|
||||
typedef union texturedata_u {
|
||||
struct {
|
||||
uint8_t *indices;
|
||||
palette_t *palette;
|
||||
} paletted;
|
||||
color_t *rgbaColors;
|
||||
} texturedata_t;
|
||||
|
||||
extern texture_t TEXTURE_WHITE;
|
||||
extern color_t TEXTURE_WHITE_PIXELS[4*4];
|
||||
extern texture_t TEXTURE_TEST;
|
||||
extern color_t TEXTURE_TEST_PIXELS[4*4];
|
||||
|
||||
/**
|
||||
* Initializes a texture.
|
||||
*
|
||||
* @param texture The texture to initialize.
|
||||
* @param width The width of the texture.
|
||||
* @param height The height of the texture.
|
||||
* @param format The format of the texture (e.g., GL_RGBA, GL_ALPHA).
|
||||
* @param data The data for the texture, the format changes per format.
|
||||
*/
|
||||
errorret_t textureInit(
|
||||
texture_t *texture,
|
||||
const int32_t width,
|
||||
const int32_t height,
|
||||
const textureformat_t format,
|
||||
const texturedata_t data
|
||||
);
|
||||
|
||||
/**
|
||||
* Disposes a texture.
|
||||
*
|
||||
* @param texture The texture to dispose.
|
||||
*/
|
||||
errorret_t textureDispose(texture_t *texture);
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "tileset.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/string.h"
|
||||
|
||||
void tilesetTileGetUV(
|
||||
const tileset_t *tileset,
|
||||
const uint16_t tileIndex,
|
||||
vec4 outUV
|
||||
) {
|
||||
const uint16_t column = tileIndex % tileset->columns;
|
||||
const uint16_t row = tileIndex / tileset->columns;
|
||||
tilesetPositionGetUV(tileset, column, row, outUV);
|
||||
}
|
||||
|
||||
void tilesetPositionGetUV(
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
vec4 outUV
|
||||
) {
|
||||
assertNotNull(tileset, "Tileset cannot be NULL");
|
||||
assertTrue(column < tileset->columns, "Column index out of bounds");
|
||||
assertTrue(row < tileset->rows, "Row index out of bounds");
|
||||
|
||||
outUV[0] = ((float_t)column) * tileset->uv[0];
|
||||
outUV[1] = ((float_t)row) * tileset->uv[1];
|
||||
outUV[2] = outUV[0] + tileset->uv[0];
|
||||
outUV[3] = outUV[1] + tileset->uv[1];
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef struct tileset_s {
|
||||
uint16_t tileWidth;
|
||||
uint16_t tileHeight;
|
||||
uint16_t tileCount;
|
||||
uint16_t columns;
|
||||
uint16_t rows;
|
||||
vec2 uv;
|
||||
} tileset_t;
|
||||
|
||||
/**
|
||||
* Gets the UV coordinates for a tile index in the tileset.
|
||||
*
|
||||
* @param tileset The tileset to get the UV coordinates from.
|
||||
* @param tileIndex The index of the tile to get the UV coordinates for.
|
||||
* @param outUV The output UV coordinates (vec4).
|
||||
*/
|
||||
void tilesetTileGetUV(
|
||||
const tileset_t *tileset,
|
||||
const uint16_t tileIndex,
|
||||
vec4 outUV
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets the UV coordinates for a tile position in the tileset.
|
||||
*
|
||||
* @param tileset The tileset to get the UV coordinates from.
|
||||
* @param column The column of the tile to get the UV coordinates for.
|
||||
* @param row The row of the tile to get the UV coordinates for.
|
||||
* @param outUV The output UV coordinates (vec4).
|
||||
*/
|
||||
void tilesetPositionGetUV(
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
vec4 outUV
|
||||
);
|
||||
Reference in New Issue
Block a user