Skip to content

Commit

Permalink
Batch text rendering per palette offset
Browse files Browse the repository at this point in the history
  • Loading branch information
nopjne committed Nov 2, 2024
1 parent 5e76088 commit 185e77c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void console_output_render(void) {
lines++;
} else {
// TODO add word wrapping?
text_render_char(&tconf, TEXT_DEFAULT, x, y + con->ypos - 100, c);
text_render_char(&tconf, TEXT_DEFAULT, x, y + con->ypos - 100, c, false);
x += font_small.w;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ void engine_run(engine_init_flags *init_flags) {
// Render scene
uint64_t frame_dt = SDL_GetTicks64() - frame_start;
frame_start = SDL_GetTicks64();
//debugf("s:%llu dt:%llu\n", frame_start, frame_dt);
if(!visual_debugger) {
dynamic_wait += frame_dt;
static_wait += frame_dt;
Expand Down
52 changes: 41 additions & 11 deletions src/game/gui/text_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void text_defaults(text_settings *settings) {
settings->strip_trailing_whitespace = false;
}

int text_render_char(const text_settings *settings, text_mode state, int x, int y, char ch) {
int text_render_char(const text_settings *settings, text_mode state, int x, int y, char ch, bool shadow) {
// Make sure code is valid
int code = ch - 32;
surface **sur = NULL;
Expand All @@ -46,14 +46,18 @@ int text_render_char(const text_settings *settings, text_mode state, int x, int
}

// Handle shadows if necessary
if(settings->shadow & TEXT_SHADOW_RIGHT)
video_draw_offset(*sur, x + 1, y, settings->cshadow, 255);
if(settings->shadow & TEXT_SHADOW_LEFT)
video_draw_offset(*sur, x - 1, y, settings->cshadow, 255);
if(settings->shadow & TEXT_SHADOW_BOTTOM)
video_draw_offset(*sur, x, y + 1, settings->cshadow, 255);
if(settings->shadow & TEXT_SHADOW_TOP)
video_draw_offset(*sur, x, y - 1, settings->cshadow, 255);
if (shadow != false) {
if(settings->shadow & TEXT_SHADOW_RIGHT)
video_draw_offset(*sur, x + 1, y, settings->cshadow, 255);
if(settings->shadow & TEXT_SHADOW_LEFT)
video_draw_offset(*sur, x - 1, y, settings->cshadow, 255);
if(settings->shadow & TEXT_SHADOW_BOTTOM)
video_draw_offset(*sur, x, y + 1, settings->cshadow, 255);
if(settings->shadow & TEXT_SHADOW_TOP)
video_draw_offset(*sur, x, y - 1, settings->cshadow, 255);

return (*sur)->w;
}

int color;
switch(state) {
Expand Down Expand Up @@ -305,14 +309,39 @@ void text_render(const text_settings *settings, text_mode mode, int x, int y, in
}

int w = 0;
// Render characters
int mxstart = mx;
int mystart = my;
int kstart = k;

// Render shadow characters.
for(; k < line_len; k++) {
// Skip line endings.
if(text[ptr + k] == '\n')
continue;

// Render character
w = text_render_char(settings, mode, mx + start_x, my + start_y, text[ptr + k], true);

// Render to the right direction
if(settings->direction == TEXT_HORIZONTAL) {
mx += w + settings->cspacing;
} else {
my += charh;
}
}

// Render regular color characters.
w = 0;
mx = mxstart;
my = mystart;
k = kstart;
for(; k < line_len; k++) {
// Skip line endings.
if(text[ptr + k] == '\n')
continue;

// Render character
w = text_render_char(settings, mode, mx + start_x, my + start_y, text[ptr + k]);
w = text_render_char(settings, mode, mx + start_x, my + start_y, text[ptr + k], false);

// Render to the right direction
if(settings->direction == TEXT_HORIZONTAL) {
Expand All @@ -322,6 +351,7 @@ void text_render(const text_settings *settings, text_mode mode, int x, int y, in
}
}


ptr += advance;
line++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/gui/text_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typedef struct {
void text_defaults(text_settings *settings);
int text_find_max_strlen(const text_settings *settings, int max_chars, const char *ptr);
int text_find_line_count(const text_settings *settings, int cols, int rows, int len, const char *text);
int text_render_char(const text_settings *settings, text_mode mode, int x, int y, char ch);
int text_render_char(const text_settings *settings, text_mode mode, int x, int y, char ch, bool shadow);
void text_render(const text_settings *settings, text_mode mode, int x, int y, int w, int h, const char *text);
int text_char_width(const text_settings *settings);
int text_width(const text_settings *settings, const char *text);
Expand Down
6 changes: 3 additions & 3 deletions src/resources/bk.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void bk_create(bk *b, void *src) {
// Copy info structs
hashmap_create(&b->infos);
bk_info tmp_bk_info;
for (int i = 0; i < 50; i++) {
if (sdbk->anims[i] != NULL) {
for(int i = 0; i < 50; i++) {
if(sdbk->anims[i] != NULL) {
bk_info_create(&tmp_bk_info, &b->sprites, (void*)sdbk->anims[i], i);
hashmap_iput(&b->infos, i, &tmp_bk_info, sizeof(bk_info));
}
Expand Down Expand Up @@ -64,7 +64,7 @@ void bk_free(bk *b) {
iterator it;
hashmap_iter_begin(&b->infos, &it);
hashmap_pair* pair = NULL;
while ((pair = iter_next(&it)) != NULL) {
while((pair = iter_next(&it)) != NULL) {
bk_info_free((bk_info*)pair->value);
}
hashmap_free(&b->infos);
Expand Down
2 changes: 0 additions & 2 deletions src/video/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ enum
SUB_METHOD_MIRROR
};

typedef unsigned int SDL_RendererFlip;

void surface_create(surface *sur, int w, int h, int transparent);
void surface_create_from(surface *dst, const surface *src);
void surface_create_from_vga(surface *sur, const sd_vga_image *src, int transparent);
Expand Down
8 changes: 3 additions & 5 deletions tools/fonttool/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include <stdint.h>
#include <string.h>

#define EMPTY_INDEX 0

SDL_Surface *render_text(sd_font *font, const char *text, int area_w) {
// Vars
unsigned int rmask, gmask, bmask, amask;
Expand Down Expand Up @@ -58,7 +56,7 @@ SDL_Surface *render_text(sd_font *font, const char *text, int area_w) {
dst.h = font->h;
sd_rgba_image_create(&img, font->h, font->h);
for(int i = 0; i < slen; i++) {
sd_font_decode(font, &img, text[i] - 32, 64, 128, 64);
sd_font_decode_rgb(font, &img, text[i] - 32, 64, 128, 64);
memcpy(tmp->pixels, img.data, 4 * font->h * font->h);
dst.y = i / char_w * font->h;
dst.x = i % char_w * font->h;
Expand All @@ -77,7 +75,7 @@ void export_to(sd_font *font, const char *filename, int split) {
sd_rgba_image ch_img;
sd_rgba_image_create(&ch_img, font->h, font->h);
for(int i = 32; i < 256; i++) {
sd_font_decode(font, &ch_img, (char)(i - 32), 0, 0, 0);
sd_font_decode_rgb(font, &ch_img, (char)(i - 32), 0, 0, 0);
sprintf(path, "%s/uni%04x.png", filename, i);
int ret = sd_rgba_image_to_png(&ch_img, path);
if(ret != SD_SUCCESS) {
Expand All @@ -93,7 +91,7 @@ void export_to(sd_font *font, const char *filename, int split) {
for(int i = 32; i < 256; i++) {
int x = i % 16;
int y = i / 16;
sd_font_decode(font, &ch_img, (char)(i - 32), 0, 0, 0);
sd_font_decode_rgb(font, &ch_img, (char)(i - 32), 0, 0, 0);
sd_rgba_image_blit(&dst_img, &ch_img, x * font->h, y * font->h);
}
sd_rgba_image_free(&ch_img);
Expand Down

0 comments on commit 185e77c

Please sign in to comment.