Skip to content

Commit

Permalink
Move text color handling to palette.
Browse files Browse the repository at this point in the history
Not quite correct, more fixes needed.
  • Loading branch information
katajakasa committed Jul 15, 2023
1 parent 92fc279 commit 41f57a4
Show file tree
Hide file tree
Showing 48 changed files with 184 additions and 259 deletions.
22 changes: 3 additions & 19 deletions src/console/console.c
Expand Up @@ -183,7 +183,6 @@ void console_output_render() {
int x = 0;
int y = 0;
unsigned int lines = 0;
const color textcolor = color_create(121, 121, 121, 255);
for(unsigned int i = con->output_pos; i != con->output_tail && lines < 15; i = BUFFER_INC(i)) {

char c = con->output[i];
Expand All @@ -193,7 +192,7 @@ void console_output_render() {
lines++;
} else {
// TODO add word wrapping?
font_render_char(&font_small, c, x, y + con->ypos - 100, textcolor);
font_render_char(&font_small, c, x, y + con->ypos - 100, TEXT_MEDIUM_GREEN);
x += font_small.w;
}
}
Expand All @@ -206,8 +205,6 @@ int console_init() {
con->isopen = 0;
con->ownsinput = 0;
con->ypos = 0;
con->ticks = 0;
con->dir = 0;
con->input[0] = '\0';
con->output[0] = '\0';
con->output_head = 0;
Expand Down Expand Up @@ -314,12 +311,10 @@ void console_render() {
con->histpos_changed = 0;
}
video_draw(&con->background, -1, con->ypos - 101);
int t = con->ticks / 2;
// input line
font_render(&font_small, con->input, 0, con->ypos - 7, color_create(121, 121, 121, 255));
font_render(&font_small, con->input, 0, con->ypos - 7, TEXT_MEDIUM_GREEN);
// cursor
font_render(&font_small, CURSOR_STR, strlen(con->input) * font_small.w, con->ypos - 7,
color_create(121 - t, 121 - t, 121 - t, 255));
font_render(&font_small, CURSOR_STR, strlen(con->input) * font_small.w, con->ypos - 7, TEXT_BLINKY_GREEN);
console_output_render();
}
}
Expand All @@ -336,17 +331,6 @@ void console_tick() {
con->ypos = 0;
}
}
if(!con->dir) {
con->ticks++;
} else {
con->ticks--;
}
if(con->ticks > 120) {
con->dir = 1;
}
if(con->ticks == 0) {
con->dir = 0;
}
}

void console_add_cmd(const char *name, command_func func, const char *doc) {
Expand Down
2 changes: 0 additions & 2 deletions src/console/console_type.h
Expand Up @@ -19,8 +19,6 @@ typedef struct {
int isopen;
int ownsinput;
int ypos;
unsigned int ticks;
unsigned int dir;
hashmap cmds; // string -> command
} console;

Expand Down
7 changes: 0 additions & 7 deletions src/formats/palette.c
Expand Up @@ -42,13 +42,6 @@ unsigned char palette_resolve_color(uint8_t r, uint8_t g, uint8_t b, const palet
return 0;
}

color palette_lookup_color(uint8_t i, const palette *pal) {
uint8_t red = pal->data[i][0] & 0xff;
uint8_t green = pal->data[i][1] & 0xff;
uint8_t blue = pal->data[i][2] & 0xff;
return color_create(red, green, blue, 255);
}

int palette_to_gimp_palette(const palette *pal, const char *filename) {
sd_writer *w;
const unsigned char *d;
Expand Down
2 changes: 0 additions & 2 deletions src/formats/palette.h
Expand Up @@ -65,8 +65,6 @@ void palette_free(palette *pal);
*/
unsigned char palette_resolve_color(uint8_t r, uint8_t g, uint8_t b, const palette *pal);

color palette_lookup_color(uint8_t i, const palette *pal);

/*! \brief Exports palette to GIMP palette file.
*
* Exports a palette to GIMP palette format (GPL). Palette remappings are NOT
Expand Down
12 changes: 6 additions & 6 deletions src/game/gui/dialog.c
Expand Up @@ -12,12 +12,12 @@ void dialog_create(dialog *dlg, dialog_style style, const char *text, int x, int
text_settings tconf;
text_defaults(&tconf);
tconf.font = FONT_BIG;
tconf.cforeground = color_create(0, 121, 0, 255);
tconf.cforeground = TEXT_MEDIUM_GREEN;

text_settings tconf_desc;
text_defaults(&tconf_desc);
tconf_desc.font = FONT_SMALL;
tconf_desc.cforeground = color_create(0, 121, 0, 255);
tconf_desc.cforeground = TEXT_MEDIUM_GREEN;

dlg->x = x;
dlg->y = y;
Expand All @@ -36,15 +36,15 @@ void dialog_create(dialog *dlg, dialog_style style, const char *text, int x, int
if(style == DIALOG_STYLE_YES_NO) {
dlg->yes = textbutton_create(&tconf, "YES", COM_ENABLED, NULL, NULL);
dlg->no = textbutton_create(&tconf, "NO", COM_ENABLED, NULL, NULL);
textbutton_set_border(dlg->yes, COLOR_BLUE);
textbutton_set_border(dlg->no, COLOR_BLUE);
textbutton_set_border(dlg->yes, TEXT_MEDIUM_GREEN);
textbutton_set_border(dlg->no, TEXT_MEDIUM_GREEN);
component_layout(dlg->yes, x + 54, x + h + 6, 8, 8);
component_layout(dlg->no, x + 114, x + h + 6, 8, 8);
component_select(dlg->yes, 1);
dlg->result = DIALOG_RESULT_YES_OK;
} else if(style == DIALOG_STYLE_OK) {
dlg->ok = textbutton_create(&tconf, "OK", COM_ENABLED, NULL, NULL);
textbutton_set_border(dlg->ok, COLOR_BLUE);
textbutton_set_border(dlg->ok, TEXT_MEDIUM_GREEN);
component_layout(dlg->ok, x + 84, x + h + 6, 8, 8);
component_select(dlg->ok, 1);
dlg->result = DIALOG_RESULT_YES_OK;
Expand Down Expand Up @@ -86,7 +86,7 @@ void dialog_render(dialog *dlg) {
if(dlg->ok) {
component_render(dlg->ok);
}
font_render_wrapped_shadowed(&font_small, dlg->text, dlg->x + 15, dlg->y + 3, MAX_WIDTH, COLOR_GREEN,
font_render_wrapped_shadowed(&font_small, dlg->text, dlg->x + 15, dlg->y + 3, MAX_WIDTH, TEXT_MEDIUM_GREEN,
TEXT_SHADOW_RIGHT | TEXT_SHADOW_BOTTOM);
}

Expand Down
65 changes: 29 additions & 36 deletions src/game/gui/text_render.c
Expand Up @@ -2,13 +2,13 @@
#include <math.h>

#include "game/gui/text_render.h"
#include "utils/log.h"
#include "utils/vector.h"
#include "video/video.h"

void text_defaults(text_settings *settings) {
memset(settings, 0, sizeof(text_settings));
settings->cforeground = color_create(0xFF, 0xFF, 0xFF, 0xFF);
settings->opacity = 0xFF;
settings->cforeground = 0xFF;
}

void text_render_char(const text_settings *settings, int x, int y, char ch) {
Expand All @@ -31,26 +31,20 @@ void text_render_char(const text_settings *settings, int x, int y, char ch) {
}

// Handle shadows if necessary
float of = settings->opacity / 255.0f;
if(settings->shadow & TEXT_SHADOW_RIGHT)
video_render_sprite_flip_scale_opacity_tint(*sur, x + 1, y, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, of * 80,
settings->cforeground);
video_draw_offset(*sur, x + 1, y, 0xC0, 255);
if(settings->shadow & TEXT_SHADOW_LEFT)
video_render_sprite_flip_scale_opacity_tint(*sur, x - 1, y, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, of * 80,
settings->cforeground);
video_draw_offset(*sur, x - 1, y, 0xC0, 255);
if(settings->shadow & TEXT_SHADOW_BOTTOM)
video_render_sprite_flip_scale_opacity_tint(*sur, x, y + 1, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, of * 80,
settings->cforeground);
video_draw_offset(*sur, x, y + 1, 0xC0, 255);
if(settings->shadow & TEXT_SHADOW_TOP)
video_render_sprite_flip_scale_opacity_tint(*sur, x, y - 1, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, of * 80,
settings->cforeground);
video_draw_offset(*sur, x, y - 1, 0xC0, 255);

// Handle the font face itself
video_render_sprite_flip_scale_opacity_tint(*sur, x, y, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f,
settings->opacity, settings->cforeground);
// Draw the actual font
video_draw_offset(*sur, x, y, settings->cforeground, 255);
}

int text_find_max_strlen(int maxchars, const char *ptr) {
int text_find_max_strlen(int max_chars, const char *ptr) {
int i;
int len = strlen(ptr);

Expand All @@ -65,8 +59,8 @@ int text_find_max_strlen(int maxchars, const char *ptr) {

// Walk through the rest of the string
int last_space = i;
int max = maxchars + i;
int lstart = i;
int max = max_chars + i;
int line_start = i;
for(; i < len; i++) {
// If we detect newline, this line ends here
if(ptr[i] == '\n')
Expand All @@ -76,7 +70,7 @@ int text_find_max_strlen(int maxchars, const char *ptr) {
if(i >= max) {
if(ptr[i] == ' ') { // If we are at valid end character (space), end here
return i;
} else if(last_space != lstart) {
} else if(last_space != line_start) {
return last_space;
}
} else if(ptr[i] == ' ') {
Expand Down Expand Up @@ -244,11 +238,11 @@ void text_render(const text_settings *settings, int x, int y, int w, int h, cons

/// ---------------- OLD RENDERER FUNCTIONS ---------------------

void font_render_char(const font *font, char ch, int x, int y, color c) {
void font_render_char(const font *font, char ch, int x, int y, uint8_t c) {
font_render_char_shadowed(font, ch, x, y, c, 0);
}

void font_render_char_shadowed(const font *font, char ch, int x, int y, color c, int shadow_flags) {
void font_render_char_shadowed(const font *font, char ch, int x, int y, uint8_t c, int shadow_flags) {
// Make sure code is valid
int code = ch - 32;
surface **sur = NULL;
Expand All @@ -261,45 +255,45 @@ void font_render_char_shadowed(const font *font, char ch, int x, int y, color c,

// Handle shadows if necessary
if(shadow_flags & TEXT_SHADOW_RIGHT)
video_render_sprite_flip_scale_opacity_tint(*sur, x + 1, y, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, 80, c);
video_draw_offset(*sur, x + 1, y, 0xC0, 255);
if(shadow_flags & TEXT_SHADOW_LEFT)
video_render_sprite_flip_scale_opacity_tint(*sur, x - 1, y, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, 80, c);
video_draw_offset(*sur, x - 1, y, 0xC0, 255);
if(shadow_flags & TEXT_SHADOW_BOTTOM)
video_render_sprite_flip_scale_opacity_tint(*sur, x, y + 1, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, 80, c);
video_draw_offset(*sur, x, y + 1, 0xC0, 255);
if(shadow_flags & TEXT_SHADOW_TOP)
video_render_sprite_flip_scale_opacity_tint(*sur, x, y - 1, BLEND_ALPHA, 0, 255, FLIP_NONE, 1.0f, 1.0f, 80, c);
video_draw_offset(*sur, x, y - 1, 0xC0, 255);

// Handle the font face itself
video_render_sprite_tint(*sur, x, y, c, 0);
video_draw_offset(*sur, x, y, c, 255);
}

void font_render_len(const font *font, const char *text, int len, int x, int y, color c) {
void font_render_len(const font *font, const char *text, int len, int x, int y, uint8_t c) {
font_render_len_shadowed(font, text, len, x, y, c, 0);
}

void font_render_len_shadowed(const font *font, const char *text, int len, int x, int y, color c, int shadow_flags) {
void font_render_len_shadowed(const font *font, const char *text, int len, int x, int y, uint8_t c, int shadow_flags) {
int pos_x = x;
for(int i = 0; i < len; i++) {
font_render_char_shadowed(font, text[i], pos_x, y, c, shadow_flags);
pos_x += font->w;
}
}

void font_render(const font *font, const char *text, int x, int y, color c) {
void font_render(const font *font, const char *text, int x, int y, uint8_t c) {
int len = strlen(text);
font_render_len(font, text, len, x, y, c);
}

void font_render_shadowed(const font *font, const char *text, int x, int y, color c, int shadow_flags) {
void font_render_shadowed(const font *font, const char *text, int x, int y, uint8_t c, int shadow_flags) {
int len = strlen(text);
font_render_len_shadowed(font, text, len, x, y, c, shadow_flags);
}

void font_render_wrapped(const font *font, const char *text, int x, int y, int w, color c) {
void font_render_wrapped(const font *font, const char *text, int x, int y, int w, uint8_t c) {
font_render_wrapped_shadowed(font, text, x, y, w, c, 0);
}

void font_render_wrapped_internal(const font *font, const char *text, int x, int y, int max_w, color c,
void font_render_wrapped_internal(const font *font, const char *text, int x, int y, int max_w, uint8_t c,
int shadow_flags, int only_size, int *out_w, int *out_h) {
int len = strlen(text);
int has_newline = 0;
Expand Down Expand Up @@ -395,18 +389,17 @@ void font_render_wrapped_internal(const font *font, const char *text, int x, int
}
}

void font_render_wrapped_shadowed(const font *font, const char *text, int x, int y, int w, color c, int shadow_flags) {
void font_render_wrapped_shadowed(const font *font, const char *text, int x, int y, int w, uint8_t c,
int shadow_flags) {
int tmp;
font_render_wrapped_internal(font, text, x, y, w, c, shadow_flags, 0, &tmp, &tmp);
}

void font_get_wrapped_size(const font *font, const char *text, int max_w, int *out_w, int *out_h) {
static color c = {0};
font_render_wrapped_internal(font, text, 0, 0, max_w, c, 0, 1, out_w, out_h);
font_render_wrapped_internal(font, text, 0, 0, max_w, 0, 0, 1, out_w, out_h);
}

void font_get_wrapped_size_shadowed(const font *font, const char *text, int max_w, int shadow_flag, int *out_w,
int *out_h) {
static color c = {0};
font_render_wrapped_internal(font, text, 0, 0, max_w, c, shadow_flag, 1, out_w, out_h);
font_render_wrapped_internal(font, text, 0, 0, max_w, 0, shadow_flag, 1, out_w, out_h);
}
35 changes: 20 additions & 15 deletions src/game/gui/text_render.h
@@ -1,11 +1,19 @@
#ifndef TEXT_RENDER_H
#define TEXT_RENDER_H

#include "resources/fonts.h"
#include "video/color.h"
#include <stdbool.h>
#include <stdint.h>

#include "resources/fonts.h"
#include "utils/str.h"
#include "video/color.h"

#define TEXT_DARK_GREEN 0xFE
#define TEXT_MEDIUM_GREEN 0xFE
#define TEXT_BLINKY_GREEN 0xFF
#define TEXT_BLACK 0
#define TEXT_TRN_BLUE 0xAB

typedef enum
{
TEXT_TOP = 0,
Expand Down Expand Up @@ -40,13 +48,11 @@ enum
TEXT_SHADOW_BOTTOM = 0x2,
TEXT_SHADOW_LEFT = 0x4,
TEXT_SHADOW_RIGHT = 0x8,
TEXT_SHADOW_HORIZONTAL = 0xC,
TEXT_SHADOW_VERTICAL = 0x3,
TEXT_SHADOW_ALL = 0xF
};

typedef struct {
color cforeground;
uint8_t cforeground;
text_valign valign;
text_halign halign;
text_padding padding;
Expand All @@ -55,13 +61,12 @@ typedef struct {
uint8_t shadow;
uint8_t cspacing;
uint8_t lspacing;
uint8_t opacity;
bool wrap;
} text_settings;

// New text rendering functions
void text_defaults(text_settings *settings);
int text_find_max_strlen(int maxchars, const char *ptr);
int text_find_max_strlen(int max_chars, const char *ptr);
int text_find_line_count(text_direction dir, int cols, int rows, int len, const char *text);
void text_render_char(const text_settings *settings, int x, int y, char ch);
void text_render(const text_settings *settings, int x, int y, int w, int h, const char *text);
Expand All @@ -71,13 +76,13 @@ int text_char_width(const text_settings *settings);
void font_get_wrapped_size(const font *font, const char *text, int max_w, int *out_w, int *out_h);
void font_get_wrapped_size_shadowed(const font *font, const char *text, int max_w, int shadow_flag, int *out_w,
int *out_h);
void font_render_char(const font *font, char ch, int x, int y, color c);
void font_render_char_shadowed(const font *font, char ch, int x, int y, color c, int shadow_flags);
void font_render_len(const font *font, const char *text, int len, int x, int y, color c);
void font_render_len_shadowed(const font *font, const char *text, int len, int x, int y, color c, int shadow_flags);
void font_render(const font *font, const char *text, int x, int y, color c);
void font_render_shadowed(const font *font, const char *text, int x, int y, color c, int shadow_flags);
void font_render_wrapped(const font *font, const char *text, int x, int y, int w, color c);
void font_render_wrapped_shadowed(const font *font, const char *text, int x, int y, int w, color c, int shadow_flags);
void font_render_char(const font *font, char ch, int x, int y, uint8_t c);
void font_render_char_shadowed(const font *font, char ch, int x, int y, uint8_t c, int shadow_flags);
void font_render_len(const font *font, const char *text, int len, int x, int y, uint8_t c);
void font_render_len_shadowed(const font *font, const char *text, int len, int x, int y, uint8_t c, int shadow_flags);
void font_render(const font *font, const char *text, int x, int y, uint8_t c);
void font_render_shadowed(const font *font, const char *text, int x, int y, uint8_t c, int shadow_flags);
void font_render_wrapped(const font *font, const char *text, int x, int y, int w, uint8_t c);
void font_render_wrapped_shadowed(const font *font, const char *text, int x, int y, int w, uint8_t c, int shadow_flags);

#endif // TEXT_RENDER_H

0 comments on commit 41f57a4

Please sign in to comment.