stagetext: separate custom updates from drawing

This commit is contained in:
Andrei Alexeyev 2019-04-11 12:23:24 +03:00
parent fd09bf02c8
commit 28bf0fc0f4
No known key found for this signature in database
GPG key ID: 363707CD4C7FE8A4
5 changed files with 45 additions and 15 deletions

View file

@ -1353,7 +1353,7 @@ void player_add_bombs(Player *plr, int bombs) {
player_add_bomb_fragments(plr, PLR_MAX_BOMB_FRAGMENTS);
}
static void scoretext_predraw(StageText *txt, int t, float a) {
static void scoretext_update(StageText *txt, int t, float a) {
float r = bits_to_float((uintptr_t)txt->custom.data1);
txt->pos -= I * cexp(I*r) * a;
}
@ -1382,7 +1382,7 @@ void player_add_points(Player *plr, uint points, complex location) {
StageText *t = stagetext_add(NULL, location, ALIGN_CENTER, get_font("small"), c, 0, 25 + 20 * imp, 10, 20);
format_huge_num(0, points, sizeof(t->text), t->text);
t->custom.data1 = (void*)(uintptr_t)float_to_bits(rnd);
t->custom.predraw = scoretext_predraw;
t->custom.update = scoretext_update;
}
void player_add_piv(Player *plr, uint piv, complex location) {
@ -1406,7 +1406,7 @@ void player_add_piv(Player *plr, uint piv, complex location) {
format_huge_num(0, piv, sizeof(t->text), t->text);
strcat(t->text, "v");
t->custom.data1 = (void*)(uintptr_t)float_to_bits(rnd);
t->custom.predraw = scoretext_predraw;
t->custom.update = scoretext_update;
}
void player_add_voltage(Player *plr, uint voltage) {

View file

@ -477,6 +477,8 @@ static void stage_logic(void) {
global.gameover != GAMEOVER_TRANSITIONING) {
stage_finish(GAMEOVER_DEFEAT);
}
stagetext_update();
}
void stage_clear_hazards_predicate(bool (*predicate)(EntityInterface *ent, void *arg), void *arg, ClearHazardsFlags flags) {

View file

@ -180,6 +180,11 @@ static bool stage_draw_event(SDL_Event *e, void *arg) {
break;
}
case TE_FRAME: {
fapproach_p(&stagedraw.clear_screen.alpha, stagedraw.clear_screen.target_alpha, 0.01);
break;
}
}
return false;
@ -923,8 +928,6 @@ static void stage_draw_overlay(void) {
draw_boss_overlay(global.boss);
}
fapproach_p(&stagedraw.clear_screen.alpha, stagedraw.clear_screen.target_alpha, 0.01);
if(stagedraw.clear_screen.alpha > 0) {
fade_out(stagedraw.clear_screen.alpha * 0.5);
}

View file

@ -38,7 +38,7 @@ StageText* stagetext_add(const char *text, complex pos, Alignment align, Font *f
return t;
}
static void stagetext_numeric_predraw(StageText *txt, int t, float a) {
static void stagetext_numeric_update(StageText *txt, int t, float a) {
// snprintf(txt->text, sizeof(NUM_PLACEHOLDER), "%i", (int)((intptr_t)txt->custom.data1 * pow(a, 5)));
format_huge_num(0, (uintptr_t)txt->custom.data1 * pow(a, 5), sizeof(NUM_PLACEHOLDER), txt->text);
}
@ -46,7 +46,7 @@ static void stagetext_numeric_predraw(StageText *txt, int t, float a) {
StageText* stagetext_add_numeric(int n, complex pos, Alignment align, Font *font, const Color *clr, int delay, int lifetime, int fadeintime, int fadeouttime) {
StageText *t = stagetext_add(NUM_PLACEHOLDER, pos, align, font, clr, delay, lifetime, fadeintime, fadeouttime);
t->custom.data1 = (void*)(intptr_t)n;
t->custom.predraw = stagetext_numeric_predraw;
t->custom.update = stagetext_numeric_update;
return t;
}
@ -59,7 +59,12 @@ void stagetext_free(void) {
list_foreach(&textlist, stagetext_delete, NULL);
}
static void stagetext_draw_single(StageText *txt) {
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;
}
@ -69,8 +74,24 @@ static void stagetext_draw_single(StageText *txt) {
return;
}
if(txt->custom.update) {
txt->custom.update(txt, global.frames - txt->time.spawn, stagetext_alpha(txt));
}
}
static void stagetext_draw_single(StageText *txt) {
if(global.frames < txt->time.spawn) {
return;
}
if(global.frames > txt->time.spawn + txt->time.life) {
log_warn("FIXME: deleting stagetext [%s] in draw function", txt->text);
stagetext_delete((List**)&textlist, (List*)txt, NULL);
return;
}
int t = global.frames - txt->time.spawn;
float f = 1.0 - clamp((txt->time.life - t) / (float)txt->time.fadeout, 0, clamp(t / (float)txt->time.fadein, 0, 1));
float f = 1.0 - stagetext_alpha(txt);
float ofs_x, ofs_y;
if(txt->time.life - t < txt->time.fadeout) {
@ -80,10 +101,6 @@ static void stagetext_draw_single(StageText *txt) {
ofs_x = ofs_y = 10 * pow(f, 2);
}
if(txt->custom.predraw) {
txt->custom.predraw(txt, t, 1.0 - f);
}
TextParams params = { 0 };
params.font_ptr = txt->font;
params.align = txt->align;
@ -98,6 +115,13 @@ static void stagetext_draw_single(StageText *txt) {
text_draw(txt->text, &params);
}
void stagetext_update(void) {
for(StageText *t = textlist, *next = NULL; t; t = next) {
next = t->next;
stagetext_update_single(t);
}
}
void stagetext_draw(void) {
for(StageText *t = textlist, *next = NULL; t; t = next) {
next = t->next;

View file

@ -18,7 +18,7 @@
typedef struct StageText StageText;
typedef LIST_ANCHOR(StageText) StageTextList;
typedef void (*StageTextPreDrawFunc)(StageText* txt, int t, float alpha);
typedef void (*StageTextUpdateFunc)(StageText* txt, int t, float alpha);
typedef struct StageTextTable StageTextTable;
@ -32,7 +32,7 @@ struct StageText {
complex pos;
struct {
StageTextPreDrawFunc predraw;
StageTextUpdateFunc update;
void *data1;
void *data2;
} custom;
@ -51,6 +51,7 @@ struct StageText {
};
void stagetext_free(void);
void stagetext_update(void);
void stagetext_draw(void);
StageText* stagetext_add(const char *text, complex pos, Alignment align, Font *font, const Color *clr, int delay, int lifetime, int fadeintime, int fadeouttime);
StageText* stagetext_add_numeric(int n, complex pos, Alignment align, Font *font, const Color *clr, int delay, int lifetime, int fadeintime, int fadeouttime);