Textbox example.

This commit is contained in:
2025-11-08 17:26:25 -06:00
parent 0a83175b66
commit db589b7d91
13 changed files with 184 additions and 18 deletions

View File

@@ -8,6 +8,7 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
rpg.c
rpgcamera.c
rpgtextbox.c
)
# Subdirs

View File

@@ -12,8 +12,10 @@
void cutsceneItemStart(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
switch(item->type) {
case CUTSCENE_ITEM_TYPE_TEXT: {
strncpy(data->text.buffer, item->text, CUTSCENE_TEXT_BUFFER);
data->text.length = strlen(data->text.buffer);
rpgTextboxShow(
item->text.position,
item->text.text
);
break;
}
@@ -36,6 +38,11 @@ void cutsceneItemStart(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
void cutsceneItemUpdate(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
switch(item->type) {
case CUTSCENE_ITEM_TYPE_TEXT:
if(rpgTextboxIsVisible()) return;
cutsceneSystemNext();
break;
case CUTSCENE_ITEM_TYPE_WAIT:
data->wait -= TIME.fixedDelta;
if(data->wait <= 0) cutsceneSystemNext();

View File

@@ -27,12 +27,11 @@ typedef struct cutsceneitem_s {
cutscenetext_t text;
cutscenecallback_t callback;
cutscenewait_t wait;
cutscenecutscene_t cutscene;
const cutscenecutscene_t cutscene;
};
} cutsceneitem_t;
typedef union {
cutscenetextdata_t text;
cutscenewaitdata_t wait;
} cutsceneitemdata_t;

View File

@@ -6,14 +6,9 @@
*/
#pragma once
#include "dusk.h"
#include "rpg/rpgtextbox.h"
#define CUTSCENE_TEXT_BUFFER 256
typedef const char_t *cutscenetext_t;
typedef struct cutscenetextdata_s {
char_t buffer[CUTSCENE_TEXT_BUFFER];
uint8_t length;
uint8_t scroll;
} cutscenetextdata_t;
typedef struct {
char_t text[RPG_TEXTBOX_MAX_CHARS];
rpgtextboxpos_t position;
} cutscenetext_t;

View File

@@ -9,9 +9,9 @@
#include "rpg/cutscene/cutscenesystem.h"
static const cutsceneitem_t TEST_CUTSCENE_ONE_ITEMS[] = {
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = "This is a test cutscene." },
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = { .text = "This is a test cutscene.", .position = RPG_TEXTBOX_POS_BOTTOM } },
{ .type = CUTSCENE_ITEM_TYPE_WAIT, .wait = 2.0f },
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = "It has multiple lines of text.\nAnd waits in between." },
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = { .text = "It has multiple lines of text.\nAnd waits in between.", .position = RPG_TEXTBOX_POS_TOP } },
};
static const cutscene_t TEST_CUTSCENE_ONE = {
@@ -21,7 +21,7 @@ static const cutscene_t TEST_CUTSCENE_ONE = {
static const cutsceneitem_t TEST_CUTSCENE_TWO_ITEMS[] = {
{ .type = CUTSCENE_ITEM_TYPE_WAIT, .wait = 1.0f },
// { .type = CUTSCENE_ITEM_TYPE_CUTSCENE, .cutscene = &TEST_CUTSCENE_ONE },
{ .type = CUTSCENE_ITEM_TYPE_CUTSCENE, .cutscene = &TEST_CUTSCENE_ONE },
};
static const cutscene_t TEST_CUTSCENE = {

View File

@@ -9,6 +9,7 @@
#include "assert/assert.h"
#include "rpg/cutscene/scene/testcutscene.h"
#include "rpg/rpgtextbox.h"
void npcInit(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
@@ -23,6 +24,8 @@ bool_t npcInteract(entity_t *player, entity_t *npc) {
assertNotNull(npc, "NPC entity pointer cannot be NULL");
cutsceneSystemStartCutscene(&TEST_CUTSCENE);
// rpgTextboxShow(RPG_TEXTBOX_POS_BOTTOM, "Hello World!");
return false;
};

View File

@@ -11,6 +11,7 @@
#include "rpg/cutscene/cutscenesystem.h"
#include "time/time.h"
#include "rpgcamera.h"
#include "rpgtextbox.h"
#include "util/memory.h"
errorret_t rpgInit(void) {
@@ -22,8 +23,8 @@ errorret_t rpgInit(void) {
// Init the world.
worldInit();
// Initialize the camera
rpgCameraInit();
rpgTextboxInit();
// TEST: Create some entities.
entity_t *ent;

39
src/rpg/rpgtextbox.c Normal file
View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpgtextbox.h"
#include "util/memory.h"
#include "util/string.h"
#include "assert/assert.h"
rpgtextbox_t RPG_TEXTBOX;
void rpgTextboxInit() {
memoryZero(&RPG_TEXTBOX, sizeof(rpgtextbox_t));
}
void rpgTextboxShow(
const rpgtextboxpos_t position,
const char_t *text
) {
RPG_TEXTBOX.position = position;
RPG_TEXTBOX.visible = true;
stringCopy(
RPG_TEXTBOX.text,
text,
RPG_TEXTBOX_MAX_CHARS
);
}
void rpgTextboxHide() {
RPG_TEXTBOX.visible = false;
}
bool_t rpgTextboxIsVisible() {
return RPG_TEXTBOX.visible;
}

52
src/rpg/rpgtextbox.h Normal file
View File

@@ -0,0 +1,52 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#define RPG_TEXTBOX_MAX_CHARS 256
typedef enum {
RPG_TEXTBOX_POS_TOP,
RPG_TEXTBOX_POS_BOTTOM,
} rpgtextboxpos_t;
typedef struct {
rpgtextboxpos_t position;
bool_t visible;
char_t text[RPG_TEXTBOX_MAX_CHARS];
} rpgtextbox_t;
extern rpgtextbox_t RPG_TEXTBOX;
/**
* Initializes the RPG textbox.
*/
void rpgTextboxInit();
/**
* Shows the RPG textbox at a specified position.
*
* @param position The position to show the textbox at.
* @param text The text to display in the textbox (copied).
*/
void rpgTextboxShow(
const rpgtextboxpos_t position,
const char_t *text
);
/**
* Hides the RPG textbox.
*/
void rpgTextboxHide();
/**
* Checks if the RPG textbox is currently visible.
*
* @return true if the textbox is visible, false otherwise.
*/
bool_t rpgTextboxIsVisible();