Process tileset.
This commit is contained in:
@@ -8,6 +8,7 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
display.c
|
||||
camera.c
|
||||
tileset.c
|
||||
)
|
||||
|
||||
# Subdirectories
|
||||
|
@@ -67,7 +67,7 @@ errorret_t displayInit(void) {
|
||||
frameBufferInitBackbuffer();
|
||||
spriteBatchInit();
|
||||
errorChain(uiInit());
|
||||
sceneManagerInit();
|
||||
errorChain(sceneManagerInit());
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
@@ -14,18 +14,24 @@
|
||||
#include "asset/assetmanager.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
#include "display/tileset/tileset_entities.h"
|
||||
|
||||
sceneoverworld_t SCENE_OVERWORLD;
|
||||
asset_t *testAsset;
|
||||
ref_t testAssetRef;
|
||||
|
||||
void sceneOverworldInit(void) {
|
||||
errorret_t sceneOverworldInit(void) {
|
||||
cameraInit(&SCENE_OVERWORLD.camera);
|
||||
glm_vec3_copy((vec3){ 0.0f, 1.0f, 0.0f }, SCENE_OVERWORLD.camera.lookat.up);
|
||||
|
||||
scene_t *scene = &SCENE_MANAGER_SCENES[SCENE_TYPE_OVERWORLD];
|
||||
scene->flags |= SCENE_FLAG_ACTIVE | SCENE_FLAG_VISIBLE;
|
||||
|
||||
assetManagerLoadAsset("entities.dpi", &testAsset, &testAssetRef);
|
||||
errorChain(assetManagerLoadAsset(
|
||||
TILESET_ENTITIES.image, &testAsset, &testAssetRef
|
||||
));
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void sceneOverworldUpdate(void) {
|
||||
@@ -79,11 +85,17 @@ void sceneOverworldRenderEntity(const entity_t *entity) {
|
||||
assertTrue(entity->type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
||||
assertTrue(entity->type != ENTITY_TYPE_NULL, "Cannot have NULL entity type");
|
||||
|
||||
vec4 uv;
|
||||
tilesetTileGetUV(&TILESET_ENTITIES, 0, uv);
|
||||
|
||||
// For now, just draw a placeholder quad.
|
||||
spriteBatchPush(
|
||||
&testAsset->paletteImage.texture,
|
||||
entity->x, entity->y, entity->x + 32.0f, entity->y + 32.0f,
|
||||
COLOR_WHITE, 0.0f, 0.0f, 0.125f, 0.125f
|
||||
entity->x, entity->y,
|
||||
entity->x + TILESET_ENTITIES.tileWidth,
|
||||
entity->y + TILESET_ENTITIES.tileHeight,
|
||||
COLOR_WHITE,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -8,6 +8,7 @@
|
||||
#pragma once
|
||||
#include "display/camera.h"
|
||||
#include "rpg/world/map.h"
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct {
|
||||
camera_t camera;
|
||||
@@ -18,7 +19,7 @@ extern sceneoverworld_t SCENE_OVERWORLD;
|
||||
/**
|
||||
* Initialize the overworld scene.
|
||||
*/
|
||||
void sceneOverworldInit(void);
|
||||
errorret_t sceneOverworldInit(void);
|
||||
|
||||
/**
|
||||
* Update the overworld scene.
|
||||
|
@@ -7,12 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
#include "error/error.h"
|
||||
|
||||
#define SCENE_FLAG_VISIBLE (1 << 0)
|
||||
#define SCENE_FLAG_ACTIVE (1 << 1)
|
||||
|
||||
typedef struct {
|
||||
void (*init)(void);
|
||||
errorret_t (*init)(void);
|
||||
void (*update)(void);
|
||||
void (*render)(void);
|
||||
void (*dispose)(void);
|
||||
|
@@ -22,9 +22,10 @@ scene_t SCENE_MANAGER_SCENES[SCENE_TYPE_COUNT] = {
|
||||
}
|
||||
};
|
||||
|
||||
void sceneManagerInit(void) {
|
||||
errorret_t sceneManagerInit(void) {
|
||||
scene_t *initial = &SCENE_MANAGER_SCENES[SCENE_TYPE_INITIAL];
|
||||
if(initial->init != NULL) initial->init();
|
||||
if(initial->init != NULL) errorChain(initial->init());
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void sceneManagerUpdate(void) {
|
||||
|
@@ -15,7 +15,7 @@ typedef struct {
|
||||
extern scenemanager_t SCENE_MANAGER;
|
||||
extern scene_t SCENE_MANAGER_SCENES[SCENE_TYPE_COUNT];
|
||||
|
||||
void sceneManagerInit(void);
|
||||
errorret_t sceneManagerInit(void);
|
||||
void sceneManagerUpdate(void);
|
||||
void sceneManagerRender(void);
|
||||
void sceneManagerDispose(void);
|
30
src/display/tileset.c
Normal file
30
src/display/tileset.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "tileset.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
|
||||
) {
|
||||
outUV[0] = column * tileset->uv[0];
|
||||
outUV[1] = row * tileset->uv[1];
|
||||
outUV[2] = outUV[0] + tileset->uv[0];
|
||||
outUV[3] = outUV[1] + tileset->uv[1];
|
||||
}
|
32
src/display/tileset.h
Normal file
32
src/display/tileset.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef struct tileset_s {
|
||||
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;
|
||||
} tileset_t;
|
||||
|
||||
void tilesetTileGetUV(
|
||||
const tileset_t *tileset,
|
||||
const uint16_t tileIndex,
|
||||
vec4 outUV
|
||||
);
|
||||
|
||||
void tilesetPositionGetUV(
|
||||
const tileset_t *tileset,
|
||||
const uint16_t column,
|
||||
const uint16_t row,
|
||||
vec4 outUV
|
||||
);
|
@@ -32,7 +32,7 @@ errorret_t uiInit(void) {
|
||||
UI.camera.viewType = CAMERA_VIEW_TYPE_MATRIX;
|
||||
glm_mat4_identity(UI.camera.view);
|
||||
|
||||
UI.scale = 2.0f;
|
||||
UI.scale = 1.0f;
|
||||
|
||||
uiFPSInit();
|
||||
|
||||
@@ -54,7 +54,7 @@ void uiRender(void) {
|
||||
cameraPushMatrix(&UI.camera);
|
||||
|
||||
uiConsoleRender();
|
||||
// uiFPSRender();
|
||||
uiFPSRender();
|
||||
|
||||
spriteBatchFlush();
|
||||
cameraPopMatrix();
|
||||
|
@@ -21,7 +21,7 @@ void uiConsoleRender(void) {
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
uiTextDraw(0, i * UI_TEXT_TILE_HEIGHT, line, COLOR_WHITE);
|
||||
uiTextDraw(0, i * TILESET_FONT_MINOGRAM.tileHeight, line, COLOR_WHITE);
|
||||
i--;
|
||||
} while(i > 0);
|
||||
}
|
@@ -17,7 +17,7 @@ errorret_t uiTextInit(void) {
|
||||
memoryZero(&UI_TEXT, sizeof(uitext_t));
|
||||
|
||||
errorChain(assetManagerLoadAsset(
|
||||
"font_minogram.dai", &UI_TEXT.asset, &UI_TEXT.assetRef
|
||||
TILESET_FONT_MINOGRAM.image, &UI_TEXT.asset, &UI_TEXT.assetRef
|
||||
));
|
||||
errorOk();
|
||||
}
|
||||
@@ -35,29 +35,25 @@ void uiTextDrawChar(
|
||||
const color_t color
|
||||
) {
|
||||
int32_t tileIndex = (int32_t)(c) - UI_TEXT_CHAR_START;
|
||||
if(tileIndex < 0 || tileIndex >= UI_TEXT_TILE_COUNT) {
|
||||
if(tileIndex < 0 || tileIndex >= TILESET_FONT_MINOGRAM.tileCount) {
|
||||
tileIndex = ((int32_t)'@') - UI_TEXT_CHAR_START;
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
tileIndex >= 0 && tileIndex <= UI_TEXT_TILE_COUNT,
|
||||
tileIndex >= 0 && tileIndex <= TILESET_FONT_MINOGRAM.tileCount,
|
||||
"Character is out of bounds for font tiles"
|
||||
);
|
||||
|
||||
const float_t w = (float)UI_TEXT.asset->alphaImage.texture.width;
|
||||
const float_t h = (float)UI_TEXT.asset->alphaImage.texture.height;
|
||||
const int32_t tileX = (tileIndex % UI_TEXT_COLUMN_COUNT);
|
||||
const int32_t tileY = (tileIndex / UI_TEXT_COLUMN_COUNT);
|
||||
vec4 uv;
|
||||
tilesetTileGetUV(&TILESET_FONT_MINOGRAM, tileIndex, uv);
|
||||
|
||||
spriteBatchPush(
|
||||
&UI_TEXT.asset->alphaImage.texture,
|
||||
x, y,
|
||||
x + UI_TEXT_TILE_WIDTH, y + UI_TEXT_TILE_HEIGHT,
|
||||
x + TILESET_FONT_MINOGRAM.tileWidth,
|
||||
y + TILESET_FONT_MINOGRAM.tileHeight,
|
||||
color,
|
||||
(tileX * UI_TEXT_TILE_WIDTH) / w,
|
||||
(tileY * UI_TEXT_TILE_HEIGHT) / h,
|
||||
((tileX + 1) * UI_TEXT_TILE_WIDTH) / w,
|
||||
((tileY + 1) * UI_TEXT_TILE_HEIGHT) / h
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -77,17 +73,17 @@ void uiTextDraw(
|
||||
while((c = text[i++]) != '\0') {
|
||||
if(c == '\n') {
|
||||
posX = x;
|
||||
posY += UI_TEXT_TILE_HEIGHT;
|
||||
posY += TILESET_FONT_MINOGRAM.tileHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == ' ') {
|
||||
posX += UI_TEXT_TILE_WIDTH;
|
||||
posX += TILESET_FONT_MINOGRAM.tileWidth;
|
||||
continue;
|
||||
}
|
||||
|
||||
uiTextDrawChar(posX, posY, c, color);
|
||||
posX += UI_TEXT_TILE_WIDTH;
|
||||
posX += TILESET_FONT_MINOGRAM.tileWidth;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +97,7 @@ void uiTextMeasure(
|
||||
assertNotNull(outHeight, "Output height pointer cannot be NULL");
|
||||
|
||||
int32_t width = 0;
|
||||
int32_t height = UI_TEXT_TILE_HEIGHT;
|
||||
int32_t height = TILESET_FONT_MINOGRAM.tileHeight;
|
||||
int32_t lineWidth = 0;
|
||||
|
||||
char_t c;
|
||||
@@ -112,11 +108,11 @@ void uiTextMeasure(
|
||||
width = lineWidth;
|
||||
}
|
||||
lineWidth = 0;
|
||||
height += UI_TEXT_TILE_HEIGHT;
|
||||
height += TILESET_FONT_MINOGRAM.tileHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
lineWidth += UI_TEXT_TILE_WIDTH;
|
||||
lineWidth += TILESET_FONT_MINOGRAM.tileWidth;
|
||||
}
|
||||
|
||||
if(lineWidth > width) {
|
||||
|
@@ -7,16 +7,10 @@
|
||||
|
||||
#pragma once
|
||||
#include "asset/assetmanager.h"
|
||||
#include "display/tileset/tileset_font_minogram.h"
|
||||
|
||||
#define UI_TEXT_CHAR_START '!'
|
||||
|
||||
#define UI_TEXT_COLUMN_COUNT 16
|
||||
#define UI_TEXT_ROW_COUNT 6
|
||||
#define UI_TEXT_TILE_COUNT (UI_TEXT_COLUMN_COUNT*UI_TEXT_ROW_COUNT)
|
||||
|
||||
#define UI_TEXT_TILE_WIDTH 6.0f
|
||||
#define UI_TEXT_TILE_HEIGHT 10.0f
|
||||
|
||||
typedef struct {
|
||||
ref_t assetRef;
|
||||
asset_t *asset;
|
||||
|
Reference in New Issue
Block a user