Skip to content

Commit

Permalink
thcrap: TL notes: Add a fade-in effect. [V]
Browse files Browse the repository at this point in the history
Fade-outs would look a bit too weird, I think? Especially when quitting
back to the menu from in-game.

Funded by zorg.
  • Loading branch information
nmlgc committed Mar 17, 2019
1 parent 71e32f6 commit 7e7ef9b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 23 additions & 1 deletion thcrap/src/tlnote.cpp
Expand Up @@ -13,6 +13,8 @@
#include "textdisp.h"
#include "tlnote.hpp"

#pragma comment(lib, "winmm.lib")

#define COM_ERR_WRAP(func, ...) { \
HRESULT d3d_ret = func(##__VA_ARGS__); \
if(FAILED(d3d_ret)) { \
Expand Down Expand Up @@ -139,6 +141,13 @@ bool tlnote_env_from_runconfig(tlnote_env_t &env)
fail(#var, parsed.err.c_str()); \
}, on_success)

#define PARSE_FLOAT_POSITIVE(var) \
PARSE(var, json_number_value, (parsed <= 0.0f), { \
fail(#var, "Must be a positive, nonzero real number."); \
}, { \
env.var = (float)parsed; \
});

auto cfg = json_object_get(runconfig_get(), "tlnotes");
if(!cfg) {
return true;
Expand Down Expand Up @@ -173,6 +182,9 @@ bool tlnote_env_from_runconfig(tlnote_env_t &env)
env.outline_radius = (uint8_t)parsed;
});

PARSE_FLOAT_POSITIVE(fade_ms);

#undef PARSE_FLOAT_POSITIVE
#undef PARSE_TUPLE
#undef PARSE

Expand Down Expand Up @@ -519,9 +531,16 @@ bool tlnote_frame(d3d_version_t ver, IDirect3DDevice *d3dd)

// What do we render?
// ------------------
static decltype(id_active) id_last = RENDERED_NONE;
static DWORD time_birth = 0;
defer({ id_last = id_active; });
if(id_active == RENDERED_NONE) {
return true;
}
auto now = timeGetTime();
if(id_last != id_active) {
time_birth = now;
}
// OK, *something.*
// ------------------

Expand Down Expand Up @@ -645,6 +664,7 @@ bool tlnote_frame(d3d_version_t ver, IDirect3DDevice *d3dd)

d3dd_SetTexture(ver, d3dd, 0, tlr->render(ver, d3dd));

auto age = now - time_birth;
float texcoord_y = 0.0f;
float texcoord_h = 1.0f;
if(tlr->tex_h > region_unscaled.h) {
Expand All @@ -654,6 +674,8 @@ bool tlnote_frame(d3d_version_t ver, IDirect3DDevice *d3dd)
region_unscaled.h = (float)tlr->tex_h;
}

float alpha = min((age / env.fade_ms), 1.0f);

vector2_t res = { (float)viewport.Width, (float)viewport.Height };
#ifdef _DEBUG
auto bounds_shadow_col = D3DCOLOR_ARGB(0xFF, 0, 0, 0);
Expand All @@ -666,7 +688,7 @@ bool tlnote_frame(d3d_version_t ver, IDirect3DDevice *d3dd)
render_colored_quad(bounds, D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
#endif
auto tlr_quad = env.scale_to(res, region_unscaled);
auto tlr_col = D3DCOLOR_ARGB(0xFF, 0xFF, 0xFF, 0xFF);
auto tlr_col = D3DCOLOR_ARGB((uint8_t)(alpha * 255.0f), 0xFF, 0xFF, 0xFF);
render_textured_quad(tlr_quad, tlr_col, texcoord_y, texcoord_h);
d3dd_ApplyStateBlock(ver, d3dd, sb_game);
d3dd_DeleteStateBlock(ver, d3dd, sb_game);
Expand Down
2 changes: 2 additions & 0 deletions thcrap/src/tlnote.hpp
Expand Up @@ -67,6 +67,8 @@ struct tlnote_env_t : public tlnote_env_render_t {
float region_left;
float region_h;

float fade_ms = 500.0f;

xywh_t region() {
return { region_left, region_top, region_w(), region_h };
}
Expand Down

0 comments on commit 7e7ef9b

Please sign in to comment.