Moved TERM files around

This commit is contained in:
2024-10-05 08:35:36 -05:00
parent cfe4cf6cad
commit 704002e671
14 changed files with 65 additions and 97 deletions

View File

@ -0,0 +1,14 @@
# Copyright (c) 2024 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
color.c
frame.c
symbol.c
)

13
src/dawn/display/color.c Normal file
View File

@ -0,0 +1,13 @@
/**
* Copyright (c) 2023 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "color.h"
#include "assert/assert.h"
void color4fCopy(const color4f_t src, color4f_t dest) {
memcpy(dest, src, sizeof(color4f_t));
}

View File

@ -8,6 +8,7 @@
#pragma once
#include "dawn.h"
// Simple colors, used by the frame system.
#define COLOR_BLACK 0x00
#define COLOR_WHITE 0x01
#define COLOR_RED 0x02
@ -15,4 +16,62 @@
#define COLOR_BLUE 0x04
#define COLOR_YELLOW 0x05
#define COLOR_MAGENTA 0x06
#define COLOR_CYAN 0x07
#define COLOR_CYAN 0x07
typedef float_t color3f_t[3];
typedef float_t color4f_t[4];
typedef color4f_t color_t;
#define COLOR3F(r,g,b) ((color3f_t){ r, g, b })
#define COLOR3F_RED COLOR3F(1, 0, 0)
#define COLOR3F_GREEN COLOR3F(0, 1, 0)
#define COLOR3F_BLUE COLOR3F(0, 0, 1)
#define COLOR3F_BLACK COLOR3F(0, 0, 0)
#define COLOR3F_WHITE COLOR3F(1, 1, 1)
#define COLOR3F_MAGENTA COLOR3F(1, 0, 1)
#define COLOR3F_YELLOW COLOR3F(1, 1, 0)
#define COLOR3F_CYAN COLOR3F(0, 1, 1)
#define COLOR3F_GRAY COLOR3F(0.5f, 0.5f, 0.5f)
#define COLOR3F_DARKGRAY COLOR3F(0.25f, 0.25f, 0.25f)
#define COLOR3F_LIGHTGRAY COLOR3F(0.75f, 0.75f, 0.75f)
#define COLOR3F_ORANGE COLOR3F(1, 0.5f, 0)
#define COLOR3F_PURPLE COLOR3F(0.5f, 0, 1)
#define COLOR3F_PINK COLOR3F(1, 0, 0.5f)
#define COLOR3F_BROWN COLOR3F(0.5f, 0.25f, 0)
#define COLOR3F_GOLD COLOR3F(1, 0.75f, 0)
#define COLOR3F_SILVER COLOR3F(0.75f, 0.75f, 0.75f)
#define COLOR3F_BRONZE COLOR3F(0.75f, 0.5f, 0.25f)
#define COLOR3F_CORNFLOWERBLUE COLOR3F(0.4f, 0.6f, 0.9f)
#define COLOR4F(r,g,b,a) ((color4f_t){ r, g, b, a })
#define COLOR4F_RED COLOR4F(1, 0, 0, 1)
#define COLOR4F_GREEN COLOR4F(0, 1, 0, 1)
#define COLOR4F_BLUE COLOR4F(0, 0, 1, 1)
#define COLOR4F_BLACK COLOR4F(0, 0, 0, 1)
#define COLOR4F_WHITE COLOR4F(1, 1, 1, 1)
#define COLOR4F_MAGENTA COLOR4F(1, 0, 1, 1)
#define COLOR4F_YELLOW COLOR4F(1, 1, 0, 1)
#define COLOR4F_CYAN COLOR4F(0, 1, 1, 1)
#define COLOR4F_GRAY COLOR4F(0.5f, 0.5f, 0.5f, 1)
#define COLOR4F_DARKGRAY COLOR4F(0.25f, 0.25f, 0.25f, 1)
#define COLOR4F_LIGHTGRAY COLOR4F(0.75f, 0.75f, 0.75f, 1)
#define COLOR4F_ORANGE COLOR4F(1, 0.5f, 0, 1)
#define COLOR4F_PURPLE COLOR4F(0.5f, 0, 1, 1)
#define COLOR4F_PINK COLOR4F(1, 0, 0.5f, 1)
#define COLOR4F_BROWN COLOR4F(0.5f, 0.25f, 0, 1)
#define COLOR4F_GOLD COLOR4F(1, 0.75f, 0, 1)
#define COLOR4F_SILVER COLOR4F(0.75f, 0.75f, 0.75f, 1)
#define COLOR4F_BRONZE COLOR4F(0.75f, 0.5f, 0.25f, 1)
#define COLOR4F_CORNFLOWERBLUE COLOR4F(0.4f, 0.6f, 0.9f, 1)
#define COLOR4F_TRANSPARENT_BLACK COLOR4F(0, 0, 0, 0)
#define COLOR4F_TRANSPARENT_WHITE COLOR4F(1, 1, 1, 0)
#define COLOR4F_TRANSPARENT COLOR4F_TRANSPARENT_BLACK
/**
* Copies a color.
*
* @param src Source color.
* @param dest Destination color.
*/
void color4fCopy(const color4f_t src, color4f_t dest);

94
src/dawn/display/frame.c Normal file
View File

@ -0,0 +1,94 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "frame.h"
#include "display/symbol.h"
#include "rpg/world/map.h"
#include "game.h"
#include "ui/textbox.h"
char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH];
uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH];
void frameInit() {
memset(FRAME_BUFFER, ' ', FRAME_HEIGHT * FRAME_WIDTH);
// Draw UI top and bottom borders
for(uint16_t x = 0; x < FRAME_WIDTH; x++) {
char_t c = x == 0 || x == FRAME_WIDTH-1 ? '+' : '-';
int32_t i = (FRAME_HEIGHT - 1) * FRAME_WIDTH + x;
FRAME_BUFFER[i] = c;
FRAME_COLOR[i] = COLOR_WHITE;
i = (FRAME_HEIGHT - FRAME_BOTTOM_UI_HEIGHT) * FRAME_WIDTH + x;
FRAME_BUFFER[i] = c;
FRAME_COLOR[i] = COLOR_WHITE;
}
for(uint16_t y = FRAME_HEIGHT - FRAME_BOTTOM_UI_HEIGHT + 1; y < FRAME_HEIGHT - 1; y++) {
int32_t i = y * FRAME_WIDTH;
FRAME_BUFFER[i] = '|';
FRAME_COLOR[i] = COLOR_WHITE;
i = y * FRAME_WIDTH + FRAME_WIDTH - 1;
FRAME_BUFFER[i] = '|';
FRAME_COLOR[i] = COLOR_WHITE;
}
}
void frameUpdate() {
// Draw buffer
entity_t *ent;
tile_t tile;
uint32_t i;
map_t *map = GAME.currentMap;
if(map == NULL) return;
// Draw the map area.
i = 0;
for(uint16_t y = 0; y < FRAME_HEIGHT - FRAME_BOTTOM_UI_HEIGHT; y++) {
for(uint16_t x = 0; x < FRAME_WIDTH; x++) {
if(x >= map->width || y >= map->height) {
FRAME_COLOR[i] = COLOR_BLACK;
FRAME_BUFFER[i++] = ' ';
continue;
}
// Entity?
ent = mapEntityGetByPosition(map, x, y);
if(ent != NULL) {
FRAME_COLOR[i] = symbolGetColorByEntity(ent);
FRAME_BUFFER[i++] = symbolGetCharByEntity(ent);
continue;
}
// Tile?
tile = mapTileGetByPosition(map, x, y, 0);
FRAME_COLOR[i] = symbolGetColorByTile(tile);
FRAME_BUFFER[i++] = symbolGetCharByTile(tile);
}
}
// UI Area.
if(textboxIsOpen()) {
const char_t *text = TEXTBOX.text;
for(uint16_t x = 1; x < FRAME_WIDTH - 1; x++) {
if(*text == '\0') break;
FRAME_BUFFER[(FRAME_HEIGHT - 2) * FRAME_WIDTH + x] = *text++;
FRAME_COLOR[(FRAME_HEIGHT - 2) * FRAME_WIDTH + x] = COLOR_WHITE;
}
FRAME_BUFFER[(FRAME_HEIGHT * FRAME_WIDTH) - 4] = ' ';
FRAME_BUFFER[(FRAME_HEIGHT * FRAME_WIDTH) - 3] = '>';
FRAME_BUFFER[(FRAME_HEIGHT * FRAME_WIDTH) - 2] = ' ';
} else {
FRAME_BUFFER[(FRAME_HEIGHT * FRAME_WIDTH) - 4] = '-';
FRAME_BUFFER[(FRAME_HEIGHT * FRAME_WIDTH) - 3] = '-';
FRAME_BUFFER[(FRAME_HEIGHT * FRAME_WIDTH) - 2] = '-';
}
}

30
src/dawn/display/frame.h Normal file
View File

@ -0,0 +1,30 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/color.h"
#define FRAME_CHAR_SIZE 8
#define FRAME_PIXEL_WIDTH 320
#define FRAME_PIXEL_HEIGHT 240
#define FRAME_WIDTH FRAME_PIXEL_WIDTH/FRAME_CHAR_SIZE
#define FRAME_HEIGHT FRAME_PIXEL_HEIGHT/FRAME_CHAR_SIZE
#define FRAME_BOTTOM_UI_HEIGHT 6
extern char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH];
extern uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH];
/**
* Initializes the frame buffer.
*/
void frameInit();
/**
* Updates the terminal frame.
*/
void frameUpdate();

70
src/dawn/display/symbol.c Normal file
View File

@ -0,0 +1,70 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assert/assert.h"
#include "rpg/entity/entitydirection.h"
#include "symbol.h"
#include "time.h"
char_t symbolGetCharByEntity(const entity_t *ent) {
assertNotNull(ent, "Entity cannot be NULL.");
switch(ent->direction) {
case ENTITY_DIRECTION_EAST: return '>';
case ENTITY_DIRECTION_WEST: return '<';
case ENTITY_DIRECTION_NORTH: return '^';
case ENTITY_DIRECTION_SOUTH: return 'v';
default:
assertUnreachable("Invalid entity direction.");
}
}
char_t symbolGetCharByTile(const tile_t tile) {
switch(tile.id) {
case TILE_ID_GRASS:
return '#';
case TILE_ID_WATER:
// Take remainder
int32_t j = (int32_t)(TIME.time * 2) % 4;
switch(j) {
case 0: return '/';
case 1: return '|';
case 2: return '\\';
case 3: return '|';
}
default:
return ' ';
}
}
uint8_t symbolGetColorByEntity(const entity_t *ent) {
assertNotNull(ent, "Entity cannot be NULL.");
switch(ent->type) {
case ENTITY_TYPE_PLAYER:
return COLOR_RED;
case ENTITY_TYPE_NPC:
return COLOR_YELLOW;
default:
assertUnreachable("Invalid entity type.");
}
}
uint8_t symbolGetColorByTile(const tile_t tile) {
switch(tile.id) {
case TILE_ID_GRASS:
return COLOR_GREEN;
case TILE_ID_WATER:
return COLOR_BLUE;
default:
return COLOR_BLACK;
}
}

43
src/dawn/display/symbol.h Normal file
View File

@ -0,0 +1,43 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/entity/entity.h"
#include "rpg/world/tile.h"
#include "display/color.h"
/**
* Returns the symbol for the given entity.
*
* @param ent Entity to get the symbol for.
* @return The symbol for the entity.
*/
char_t symbolGetCharByEntity(const entity_t *ent);
/**
* Returns the symbol for the given tile.
*
* @param tile Tile to get the symbol for.
* @return The symbol for the tile.
*/
char_t symbolGetCharByTile(const tile_t tile);
/**
* Returns the color for the given entity.
*
* @param ent Entity to get the color for.
* @return The color for the entity.
*/
uint8_t symbolGetColorByEntity(const entity_t *ent);
/**
* Returns the color for the given tile.
*
* @param tile Tile to get the color for.
* @return The color for the tile.
*/
uint8_t symbolGetColorByTile(const tile_t tile);