Textbox rendering back.

This commit is contained in:
2024-10-06 17:16:24 -05:00
parent 5444b0b8c7
commit 5256b32055
14 changed files with 140 additions and 82 deletions

View File

@ -8,6 +8,7 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
drawstateoverworld.c
drawmap.c
drawtext.c
drawshape.c

View File

@ -0,0 +1,39 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "drawstateoverworld.h"
#include "ui/textbox.h"
#include "rpg/world/map.h"
#include "game/game.h"
#include "display/draw/drawmap.h"
#include "display/draw/drawui.h"
void drawStateOverworld() {
map_t *map = GAME.currentMap;
if(map == NULL) return;
// Try get player
entity_t *player = mapEntityGetByType(map, ENTITY_TYPE_PLAYER);
uint16_t cameraPositionX, cameraPositionY;
if(player == NULL) {
cameraPositionX = 0;
cameraPositionY = 0;
} else {
cameraPositionX = player->x;
cameraPositionY = player->y;
}
drawMap(
GAME.currentMap,
cameraPositionX, cameraPositionY,
0, 0,
FRAME_WIDTH, FRAME_HEIGHT
);
// Draw UI
drawUITextbox();
}

View File

@ -0,0 +1,14 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/frame.h"
/**
* Draws the overworld state.
*/
void drawStateOverworld();

View File

@ -7,20 +7,22 @@
#include "drawtext.h"
#include "assert/assert.h"
#include "util/math.h"
void drawText(
const char_t *text,
const size_t len,
const uint16_t x,
const uint16_t y,
const uint8_t color
) {
size_t len = strlen(text);
if(len == 0) return;
size_t rLen = mathMin(len, strlen(text));
assertTrue(x + len <= FRAME_WIDTH, "Text is too long.");
assertTrue(x + rLen <= FRAME_WIDTH, "Text is too long.");
assertTrue(y < FRAME_HEIGHT, "Text is too low.");
uint16_t i = y * FRAME_WIDTH + x;
memcpy(&FRAME_BUFFER[i], text, len);
memset(&FRAME_COLOR[i], color, len);
memcpy(&FRAME_BUFFER[i], text, rLen);
memset(&FRAME_COLOR[i], color, rLen);
}

View File

@ -12,12 +12,14 @@
* Draws some text to the frame buffer.
*
* @param text The text to draw.
* @param len The length of the text to draw.
* @param x The x position to draw the text.
* @param y The y position to draw the text.
* @param color The color to draw the text.
*/
void drawText(
const char_t *text,
const size_t len,
const uint16_t x,
const uint16_t y,
const uint8_t color

View File

@ -8,6 +8,9 @@
#include "drawui.h"
#include "drawshape.h"
#include "assert/assert.h"
#include "ui/textbox.h"
#include "game/time.h"
#include "display/draw/drawtext.h"
void drawUIBox(
const uint16_t x,
@ -54,3 +57,39 @@ void drawUIBox(
if(!fill) return;
drawBox(x + 1, y + 1, width - 2, height - 2, ' ', COLOR_BLACK);
}
void drawUITextbox() {
if(!textboxIsOpen()) return;
// Border
drawUIBox(
0, FRAME_HEIGHT - DRAW_UI_TEXTBOX_HEIGHT,
DRAW_UI_TEXTBOX_WIDTH, DRAW_UI_TEXTBOX_HEIGHT,
COLOR_MAGENTA, true
);
// Title
drawText(
TEXTBOX.title,
-1,
2, FRAME_HEIGHT - DRAW_UI_TEXTBOX_HEIGHT,
COLOR_WHITE
);
// Text
drawText(
TEXTBOX.text,
TEXTBOX.textIndex,
1, FRAME_HEIGHT - DRAW_UI_TEXTBOX_HEIGHT + 1,
COLOR_WHITE
);
// Blinking cursor
memset(&FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS], ' ', 3);
memset(&FRAME_COLOR[DRAW_UI_TEXTBOX_CURSOR_POS], COLOR_WHITE, 3);
if(TEXTBOX.textIndex < TEXTBOX.textLength) return;
int32_t blink = (int32_t)(TIME.time * TEXTBOX_BLINKS_PER_SECOND) % 2;
FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 1] = blink ? '>' : ' ';
FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 2] = blink ? ' ' : '>';
}

View File

@ -8,6 +8,10 @@
#pragma once
#include "display/frame.h"
#define DRAW_UI_TEXTBOX_WIDTH FRAME_WIDTH
#define DRAW_UI_TEXTBOX_HEIGHT 8
#define DRAW_UI_TEXTBOX_CURSOR_POS ((FRAME_HEIGHT * FRAME_WIDTH) - 4)
/**
* Draws a UI box to the frame buffer.
*
@ -25,4 +29,9 @@ void drawUIBox(
const uint16_t height,
const uint8_t color,
const bool_t fill
);
);
/**
* Draws the UI textbox to the frame buffer.
*/
void drawUITextbox();

View File

@ -6,15 +6,11 @@
*/
#include "frame.h"
#include "display/symbol.h"
#include "rpg/world/map.h"
#include "game/game.h"
#include "ui/textbox.h"
#include "display/draw/drawshape.h"
#include "display/draw/drawui.h"
#include "display/draw/drawtext.h"
#include "display/draw/drawmap.h"
#include "display/draw/drawstateoverworld.h"
char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH];
uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH];
@ -26,27 +22,22 @@ void frameInit() {
void frameUpdate() {
switch(GAME.state) {
case GAME_STATE_PAUSED:
const char_t *str = "PAUSED";
size_t len = strlen(str);
drawText(
str,
-1,
(FRAME_WIDTH - len) / 2,
(FRAME_HEIGHT - 1) / 2,
COLOR_WHITE
);
break;
case GAME_STATE_OVERWORLD:
drawStateOverworld();
break;
default:
break;
}
map_t *map = GAME.currentMap;
if(map == NULL) return;
// Try get player
entity_t *player = mapEntityGetByType(map, ENTITY_TYPE_PLAYER);
uint16_t cameraPositionX, cameraPositionY;
if(player == NULL) {
cameraPositionX = 0;
cameraPositionY = 0;
} else {
cameraPositionX = player->x;
cameraPositionY = player->y;
}
drawMap(
GAME.currentMap,
cameraPositionX, cameraPositionY,
0, 0,
FRAME_WIDTH, FRAME_HEIGHT
);
}

View File

@ -14,8 +14,6 @@
#define FRAME_WIDTH FRAME_PIXEL_WIDTH/FRAME_CHAR_SIZE
#define FRAME_HEIGHT FRAME_PIXEL_HEIGHT/FRAME_CHAR_SIZE
#define FRAME_BOTTOM_UI_HEIGHT 8
extern char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH];
extern uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH];