From a162002af24fe829cce85d6be2ebba11362084d8 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 18 Jun 2026 10:24:45 -0500 Subject: [PATCH] Show player pos, fix chunks off screen --- src/dusk/console/console.c | 2 +- src/dusk/rpg/overworld/worldpos.h | 4 +-- src/dusk/ui/CMakeLists.txt | 1 + src/dusk/ui/uielement.c | 2 ++ src/dusk/ui/uiplayerpos.c | 44 +++++++++++++++++++++++++++++++ src/dusk/ui/uiplayerpos.h | 18 +++++++++++++ 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 src/dusk/ui/uiplayerpos.c create mode 100644 src/dusk/ui/uiplayerpos.h diff --git a/src/dusk/console/console.c b/src/dusk/console/console.c index 5d50e1ed..d712a48e 100644 --- a/src/dusk/console/console.c +++ b/src/dusk/console/console.c @@ -70,7 +70,7 @@ errorret_t consoleDraw(void) { errorChain(textDraw( 0, FONT_DEFAULT.tileset->tileHeight * i, CONSOLE.line[i], - COLOR_WHITE, + COLOR_RED, &FONT_DEFAULT )); } diff --git a/src/dusk/rpg/overworld/worldpos.h b/src/dusk/rpg/overworld/worldpos.h index ca5711f2..87559cf1 100644 --- a/src/dusk/rpg/overworld/worldpos.h +++ b/src/dusk/rpg/overworld/worldpos.h @@ -17,8 +17,8 @@ #define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) -#define MAP_CHUNK_WIDTH 3 -#define MAP_CHUNK_HEIGHT 3 +#define MAP_CHUNK_WIDTH 5 +#define MAP_CHUNK_HEIGHT 5 #define MAP_CHUNK_DEPTH 3 #define MAP_CHUNK_COUNT (MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT * MAP_CHUNK_DEPTH) diff --git a/src/dusk/ui/CMakeLists.txt b/src/dusk/ui/CMakeLists.txt index 32030fc3..ce5eae43 100644 --- a/src/dusk/ui/CMakeLists.txt +++ b/src/dusk/ui/CMakeLists.txt @@ -13,4 +13,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} uifullbox.c uiloading.c uitextbox.c + uiplayerpos.c ) \ No newline at end of file diff --git a/src/dusk/ui/uielement.c b/src/dusk/ui/uielement.c index a5c156d6..7e8b8415 100644 --- a/src/dusk/ui/uielement.c +++ b/src/dusk/ui/uielement.c @@ -13,6 +13,7 @@ #include "ui/uitextbox.h" #include "ui/uifullbox.h" #include "ui/uiloading.h" +#include "ui/uiplayerpos.h" uielement_t UI_ELEMENTS[] = { // Fullbox under: above scene, below system UI. @@ -28,6 +29,7 @@ uielement_t UI_ELEMENTS[] = { // These render above the fullbox overlay. { .type = UI_ELEMENT_TYPE_NATIVE, .draw = consoleDraw }, { .type = UI_ELEMENT_TYPE_NATIVE, .draw = uiFPSDraw }, + { .type = UI_ELEMENT_TYPE_NATIVE, .draw = uiPlayerPosDraw }, { .type = UI_ELEMENT_TYPE_NATIVE, .draw = uiLoadingDraw }, diff --git a/src/dusk/ui/uiplayerpos.c b/src/dusk/ui/uiplayerpos.c new file mode 100644 index 00000000..dd6a17a5 --- /dev/null +++ b/src/dusk/ui/uiplayerpos.c @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "uiplayerpos.h" +#include "display/text/text.h" +#include "display/spritebatch/spritebatch.h" +#include "rpg/entity/entity.h" +#include "rpg/entity/entitytype.h" +#include "rpg/overworld/worldpos.h" + +errorret_t uiPlayerPosDraw() { + entity_t *player = NULL; + for(uint8_t i = 0; i < ENTITY_COUNT; i++) { + if(ENTITIES[i].type == ENTITY_TYPE_PLAYER) { + player = &ENTITIES[i]; + break; + } + } + if(!player) errorOk(); + + chunkpos_t chunkPos; + worldPosToChunkPos(&player->position, &chunkPos); + + char_t text[64]; + snprintf( + text, + sizeof(text), + "%d,%d,%d[%d,%d,%d]", + (int_t)player->position.x, + (int_t)player->position.y, + (int_t)player->position.z, + (int_t)chunkPos.x, + (int_t)chunkPos.y, + (int_t)chunkPos.z + ); + + float_t y = (float_t)FONT_DEFAULT.tileset->tileHeight; + errorChain(textDraw(0, y, text, COLOR_GREEN, &FONT_DEFAULT)); + return spriteBatchFlush(); +} diff --git a/src/dusk/ui/uiplayerpos.h b/src/dusk/ui/uiplayerpos.h new file mode 100644 index 00000000..88cde124 --- /dev/null +++ b/src/dusk/ui/uiplayerpos.h @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "error/error.h" + +/** + * Draws the player world and chunk position on screen. + * Searches for the first ENTITY_TYPE_PLAYER and renders: + * WORLDX,WORLDY,WORLDZ[chunkx,chunky,chunkz] + * + * @return Any error that occurs. + */ +errorret_t uiPlayerPosDraw();