2017-09-12 03:28:15 +02:00
|
|
|
/*
|
2019-08-03 19:43:48 +02:00
|
|
|
* This software is licensed under the terms of the MIT License.
|
2017-09-12 03:28:15 +02:00
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
2019-01-23 21:10:43 +01:00
|
|
|
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
|
2019-07-03 20:00:56 +02:00
|
|
|
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
|
2017-09-12 03:28:15 +02:00
|
|
|
*/
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
|
|
|
|
2017-04-02 16:06:18 +02:00
|
|
|
#include "stagetext.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
static StageText *textlist = NULL;
|
|
|
|
|
2019-11-22 04:37:11 +01:00
|
|
|
StageText* stagetext_add(const char *text, cmplx pos, Alignment align, Font *font, const Color *clr, int delay, int lifetime, int fadeintime, int fadeouttime) {
|
2023-04-03 03:49:39 +02:00
|
|
|
StageText *t = objpool_acquire(&stage_object_pools.stagetext);
|
2018-01-12 19:26:07 +01:00
|
|
|
list_append(&textlist, t);
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2019-04-07 00:55:13 +02:00
|
|
|
if(text != NULL) {
|
|
|
|
assert(strlen(text) < sizeof(t->text));
|
|
|
|
strcpy(t->text, text);
|
|
|
|
}
|
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
t->font = font;
|
|
|
|
t->pos = pos;
|
|
|
|
t->align = align;
|
2018-07-23 19:07:59 +02:00
|
|
|
t->color = *clr;
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
t->time.spawn = global.frames + delay;
|
|
|
|
t->time.fadein = fadeintime;
|
|
|
|
t->time.fadeout = fadeouttime;
|
2018-06-29 23:36:51 +02:00
|
|
|
t->time.life = lifetime + fadeouttime;
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
return t;
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 11:23:24 +02:00
|
|
|
static void stagetext_numeric_update(StageText *txt, int t, float a) {
|
2020-03-09 02:31:17 +01:00
|
|
|
format_huge_num(0, (uintptr_t)txt->custom.data1 * pow(a, 5), sizeof(txt->text), txt->text);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 04:37:11 +01:00
|
|
|
StageText* stagetext_add_numeric(int n, cmplx pos, Alignment align, Font *font, const Color *clr, int delay, int lifetime, int fadeintime, int fadeouttime) {
|
2020-03-09 02:31:17 +01:00
|
|
|
StageText *t = stagetext_add(NULL, pos, align, font, clr, delay, lifetime, fadeintime, fadeouttime);
|
2018-01-12 19:26:07 +01:00
|
|
|
t->custom.data1 = (void*)(intptr_t)n;
|
2019-04-11 11:23:24 +02:00
|
|
|
t->custom.update = stagetext_numeric_update;
|
2018-01-12 19:26:07 +01:00
|
|
|
return t;
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:45:01 +01:00
|
|
|
static void* stagetext_delete(List **dest, List *txt, void *arg) {
|
2023-04-03 03:49:39 +02:00
|
|
|
objpool_release(&stage_object_pools.stagetext, list_unlink(dest, txt));
|
2018-01-12 19:26:07 +01:00
|
|
|
return NULL;
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void stagetext_free(void) {
|
2018-01-12 19:26:07 +01:00
|
|
|
list_foreach(&textlist, stagetext_delete, NULL);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 11:23:24 +02:00
|
|
|
static inline float stagetext_alpha(StageText *txt) {
|
|
|
|
int t = global.frames - txt->time.spawn;
|
|
|
|
return clamp((txt->time.life - t) / (float)txt->time.fadeout, 0, clamp(t / (float)txt->time.fadein, 0, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stagetext_update_single(StageText *txt) {
|
|
|
|
if(global.frames < txt->time.spawn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(global.frames > txt->time.spawn + txt->time.life) {
|
|
|
|
stagetext_delete((List**)&textlist, (List*)txt, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(txt->custom.update) {
|
|
|
|
txt->custom.update(txt, global.frames - txt->time.spawn, stagetext_alpha(txt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 16:06:18 +02:00
|
|
|
static void stagetext_draw_single(StageText *txt) {
|
2018-01-12 19:26:07 +01:00
|
|
|
if(global.frames < txt->time.spawn) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
if(global.frames > txt->time.spawn + txt->time.life) {
|
2019-04-11 11:23:24 +02:00
|
|
|
log_warn("FIXME: deleting stagetext [%s] in draw function", txt->text);
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_delete((List**)&textlist, (List*)txt, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
int t = global.frames - txt->time.spawn;
|
2019-04-11 11:23:24 +02:00
|
|
|
float f = 1.0 - stagetext_alpha(txt);
|
2018-07-25 00:33:01 +02:00
|
|
|
float ofs_x, ofs_y;
|
|
|
|
|
|
|
|
if(txt->time.life - t < txt->time.fadeout) {
|
|
|
|
ofs_y = 10 * pow(f, 2);
|
|
|
|
ofs_x = 0;
|
|
|
|
} else {
|
|
|
|
ofs_x = ofs_y = 10 * pow(f, 2);
|
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-06-29 23:36:51 +02:00
|
|
|
TextParams params = { 0 };
|
|
|
|
params.font_ptr = txt->font;
|
|
|
|
params.align = txt->align;
|
2018-07-23 19:07:59 +02:00
|
|
|
params.blend = BLEND_PREMUL_ALPHA;
|
2020-06-09 03:33:22 +02:00
|
|
|
params.shader_ptr = res_shader("text_stagetext");
|
2018-07-23 19:07:59 +02:00
|
|
|
params.shader_params = &(ShaderCustomParams){{ 1 - f }},
|
2020-06-09 03:33:22 +02:00
|
|
|
params.aux_textures[0] = res_texture("titletransition");
|
2018-07-25 00:33:01 +02:00
|
|
|
params.pos.x = creal(txt->pos) + ofs_x;
|
|
|
|
params.pos.y = cimag(txt->pos) + ofs_y;
|
2018-07-23 19:07:59 +02:00
|
|
|
params.color = &txt->color;
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-07-24 20:00:44 +02:00
|
|
|
text_draw(txt->text, ¶ms);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 11:23:24 +02:00
|
|
|
void stagetext_update(void) {
|
|
|
|
for(StageText *t = textlist, *next = NULL; t; t = next) {
|
|
|
|
next = t->next;
|
|
|
|
stagetext_update_single(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 16:06:18 +02:00
|
|
|
void stagetext_draw(void) {
|
2018-01-12 19:26:07 +01:00
|
|
|
for(StageText *t = textlist, *next = NULL; t; t = next) {
|
|
|
|
next = t->next;
|
|
|
|
stagetext_draw_single(t);
|
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stagetext_table_push(StageTextTable *tbl, StageText *txt, bool update_pos) {
|
2018-01-12 19:26:07 +01:00
|
|
|
list_append(&tbl->elems, list_wrap_container(txt));
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
if(update_pos) {
|
2018-06-29 23:36:51 +02:00
|
|
|
tbl->pos += text_height(txt->font, txt->text, 0) * I;
|
2018-01-12 19:26:07 +01:00
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
tbl->delay += 5;
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:07:59 +02:00
|
|
|
void stagetext_begin_table(StageTextTable *tbl, const char *title, const Color *titleclr, const Color *clr, double width, int delay, int lifetime, int fadeintime, int fadeouttime) {
|
2018-01-12 19:26:07 +01:00
|
|
|
memset(tbl, 0, sizeof(StageTextTable));
|
|
|
|
tbl->pos = VIEWPORT_W/2 + VIEWPORT_H/2*I;
|
2018-07-23 19:07:59 +02:00
|
|
|
tbl->clr = *clr;
|
2018-01-12 19:26:07 +01:00
|
|
|
tbl->width = width;
|
|
|
|
tbl->lifetime = lifetime;
|
|
|
|
tbl->fadeintime = fadeintime;
|
|
|
|
tbl->fadeouttime = fadeouttime;
|
|
|
|
tbl->delay = delay;
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2020-06-09 03:33:22 +02:00
|
|
|
StageText *txt = stagetext_add(title, tbl->pos, ALIGN_CENTER, res_font("big"), titleclr, tbl->delay, lifetime, fadeintime, fadeouttime);
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_table_push(tbl, txt, true);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void stagetext_end_table(StageTextTable *tbl) {
|
2019-11-22 04:37:11 +01:00
|
|
|
cmplx ofs = -0.5 * I * (cimag(tbl->pos) - VIEWPORT_H/2);
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
for(ListContainer *c = tbl->elems; c; c = c->next) {
|
|
|
|
((StageText*)c->data)->pos += ofs;
|
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2018-01-12 19:26:07 +01:00
|
|
|
list_free_all(&tbl->elems);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stagetext_table_add_label(StageTextTable *tbl, const char *title) {
|
2020-06-09 03:33:22 +02:00
|
|
|
StageText *txt = stagetext_add(title, tbl->pos - tbl->width * 0.5, ALIGN_LEFT, res_font("standard"), &tbl->clr, tbl->delay, tbl->lifetime, tbl->fadeintime, tbl->fadeouttime);
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_table_push(tbl, txt, false);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void stagetext_table_add(StageTextTable *tbl, const char *title, const char *val) {
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_table_add_label(tbl, title);
|
2020-06-09 03:33:22 +02:00
|
|
|
StageText *txt = stagetext_add(val, tbl->pos + tbl->width * 0.5, ALIGN_RIGHT, res_font("standard"), &tbl->clr, tbl->delay, tbl->lifetime, tbl->fadeintime, tbl->fadeouttime);
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_table_push(tbl, txt, true);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void stagetext_table_add_numeric(StageTextTable *tbl, const char *title, int n) {
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_table_add_label(tbl, title);
|
2020-06-09 03:33:22 +02:00
|
|
|
StageText *txt = stagetext_add_numeric(n, tbl->pos + tbl->width * 0.5, ALIGN_RIGHT, res_font("standard"), &tbl->clr, tbl->delay, tbl->lifetime, tbl->fadeintime, tbl->fadeouttime);
|
2018-01-12 19:26:07 +01:00
|
|
|
stagetext_table_push(tbl, txt, true);
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 02:57:38 +02:00
|
|
|
void stagetext_table_add_numeric_nonzero(StageTextTable *tbl, const char *title, int n) {
|
2018-01-12 19:26:07 +01:00
|
|
|
if(n) {
|
|
|
|
stagetext_table_add_numeric(tbl, title, n);
|
|
|
|
}
|
2017-04-04 02:57:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-02 16:06:18 +02:00
|
|
|
void stagetext_table_add_separator(StageTextTable *tbl) {
|
2020-06-09 03:33:22 +02:00
|
|
|
tbl->pos += I * 0.5 * font_get_lineskip(res_font("standard"));
|
2017-04-02 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-14 06:16:10 +02:00
|
|
|
|
|
|
|
StageText *stagetext_list_head(void) {
|
|
|
|
return textlist;
|
|
|
|
}
|