From 691dbd5ac28ac87f720654b8f6a2aa7706339eb0 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sat, 2 Jan 2016 14:23:09 -0800 Subject: [PATCH] Coalesce duplicate fade-out codepaths --- src/video/glhudmodel.c | 21 ++++++++++++++++++ src/video/glhudmodel.h | 5 +++++ src/video/gltouchjoy.c | 31 +++++++-------------------- src/video/gltouchkbd.c | 32 ++++++---------------------- src/video/gltouchmenu.c | 47 ++++++++++++++--------------------------- 5 files changed, 57 insertions(+), 79 deletions(-) diff --git a/src/video/glhudmodel.c b/src/video/glhudmodel.c index 5f65d372..b634ff80 100644 --- a/src/video/glhudmodel.c +++ b/src/video/glhudmodel.c @@ -308,3 +308,24 @@ void glhud_quadModelToScreen(const GLModel *model, const int screenW, const int screenCoords[3] = yFlip1; } +float glhud_getTimedVisibility(struct timespec timingBegin, float minAlpha, float maxAlpha) { + + struct timespec now = { 0 }; + struct timespec deltat = { 0 }; + + clock_gettime(CLOCK_MONOTONIC, &now); + float alpha = minAlpha; + deltat = timespec_diff(timingBegin, now, NULL); + if (deltat.tv_sec == 0) { + alpha = maxAlpha; + if (deltat.tv_nsec >= NANOSECONDS_PER_SECOND/2) { + alpha -= ((float)deltat.tv_nsec-(NANOSECONDS_PER_SECOND/2)) / (float)(NANOSECONDS_PER_SECOND/2); + if (alpha < 0) { + alpha = 0; + } + } + } + + return alpha; +} + diff --git a/src/video/glhudmodel.h b/src/video/glhudmodel.h index 0e74faf6..33f92ac0 100644 --- a/src/video/glhudmodel.h +++ b/src/video/glhudmodel.h @@ -32,6 +32,8 @@ bool blackIsTransparent; \ bool opaquePixelHalo; \ \ + struct timespec timingBegin; \ + \ __VA_ARGS__ \ ) @@ -58,4 +60,7 @@ void glhud_screenToModel(const float x, const float y, const int screenW, const // orthographic translation of model coordinates to screen coordinates void glhud_quadModelToScreen(const GLModel *model, const int screenW, const int screenH, float screenCoords[4]); +// helper method to sync timing of fade out +float glhud_getTimedVisibility(struct timespec timingBegin, float minAlpha, float maxAlpha); + #endif diff --git a/src/video/gltouchjoy.c b/src/video/gltouchjoy.c index 608fe261..fc832751 100644 --- a/src/video/gltouchjoy.c +++ b/src/video/gltouchjoy.c @@ -74,27 +74,6 @@ static struct { // ---------------------------------------------------------------------------- -#warning FIXME TODO ... this can become a common helper function ... -static inline float _get_component_visibility(struct timespec timingBegin) { - struct timespec now = { 0 }; - struct timespec deltat = { 0 }; - - clock_gettime(CLOCK_MONOTONIC, &now); - float alpha = joyglobals.minAlpha; - deltat = timespec_diff(timingBegin, now, NULL); - if (deltat.tv_sec == 0) { - alpha = 1.0; - if (deltat.tv_nsec >= NANOSECONDS_PER_SECOND/2) { - alpha -= ((float)deltat.tv_nsec-(NANOSECONDS_PER_SECOND/2)) / (float)(NANOSECONDS_PER_SECOND/2); - if (alpha < joyglobals.minAlpha) { - alpha = joyglobals.minAlpha; - } - } - } - - return alpha; -} - static void _setup_axis_object(GLModel *parent) { if (UNLIKELY(!parent)) { LOG("gltouchjoy WARN : cannot setup axis object without parent"); @@ -308,7 +287,10 @@ static void gltouchjoy_render(void) { // draw axis - float alpha = _get_component_visibility(axes.timingBegin); + float alpha = glhud_getTimedVisibility(axes.timingBegin, joyglobals.minAlpha, 1.0); + if (alpha < joyglobals.minAlpha) { + alpha = joyglobals.minAlpha; + } if (alpha > 0.0) { glUniform1f(alphaValue, alpha); @@ -330,7 +312,10 @@ static void gltouchjoy_render(void) { // draw button(s) - alpha = _get_component_visibility(buttons.timingBegin); + alpha = glhud_getTimedVisibility(buttons.timingBegin, joyglobals.minAlpha, 1.0); + if (alpha < joyglobals.minAlpha) { + alpha = joyglobals.minAlpha; + } if (alpha > 0.0) { glUniform1f(alphaValue, alpha); diff --git a/src/video/gltouchkbd.c b/src/video/gltouchkbd.c index 4ce8205b..8eb43b69 100644 --- a/src/video/gltouchkbd.c +++ b/src/video/gltouchkbd.c @@ -237,30 +237,6 @@ static inline void _switch_keyboard(GLModel *parent, uint8_t *template) { } } -#warning FIXME TODO ... this can become a common helper function ... -static inline float _get_keyboard_visibility(void) { - struct timespec now = { 0 }; - struct timespec deltat = { 0 }; - - clock_gettime(CLOCK_MONOTONIC, &now); - float alpha = minAlpha; - deltat = timespec_diff(kbd.timingBegin, now, NULL); - if (deltat.tv_sec == 0) { - alpha = maxAlpha; - if (deltat.tv_nsec >= NANOSECONDS_PER_SECOND/2) { - alpha -= ((float)deltat.tv_nsec-(NANOSECONDS_PER_SECOND/2)) / (float)(NANOSECONDS_PER_SECOND/2); - if (alpha < minAlpha) { - alpha = minAlpha; - _rerender_selected(kbd.selectedCol, kbd.selectedRow); - kbd.selectedCol = -1; - kbd.selectedRow = -1; - } - } - } - - return alpha; -} - static inline bool _is_point_on_keyboard(float x, float y) { return (x >= touchport.kbdX && x <= touchport.kbdXMax && y >= touchport.kbdY && y <= touchport.kbdYMax); } @@ -560,7 +536,13 @@ static void gltouchkbd_render(void) { gltouchkbd_setup(); } - float alpha = _get_keyboard_visibility(); + float alpha = glhud_getTimedVisibility(kbd.timingBegin, minAlpha, maxAlpha); + if (alpha < minAlpha) { + alpha = minAlpha; + _rerender_selected(kbd.selectedCol, kbd.selectedRow); + kbd.selectedCol = -1; + kbd.selectedRow = -1; + } if (alpha > 0.0) { // draw touch keyboard diff --git a/src/video/gltouchmenu.c b/src/video/gltouchmenu.c index c842e407..26e385b8 100644 --- a/src/video/gltouchmenu.c +++ b/src/video/gltouchmenu.c @@ -35,8 +35,6 @@ HUD_CLASS(GLModelHUDMenu, static bool isAvailable = false; // Were there any OpenGL/memory errors on initialization? static bool isEnabled = true; // Does player want this enabled? -static float minAlpha = 1/4.f; // Minimum alpha value of components (at zero, will not render) -static float maxAlpha = 1.f; // NOTE : intent is to match touch keyboard width static uint8_t topMenuTemplate[MENU_TEMPLATE_ROWS][MENU_TEMPLATE_COLS+1] = { @@ -76,12 +74,14 @@ static struct { bool topLeftShowing; bool topRightShowing; + struct timespec timingBegin; + float minAlpha; // Minimum alpha value of components (at zero, will not render) + float maxAlpha; + // pending changes requiring reinitialization unsigned int nextGlyphMultiplier; } menu = { 0 }; -static struct timespec timingBegin = { 0 }; - // ---------------------------------------------------------------------------- static inline void _present_menu(GLModel *parent) { @@ -145,26 +145,6 @@ static inline void _hide_top_right(void) { _present_menu(menu.model); } -static float _get_menu_visibility(void) { - struct timespec now = { 0 }; - struct timespec deltat = { 0 }; - float alpha = minAlpha; - - clock_gettime(CLOCK_MONOTONIC, &now); - deltat = timespec_diff(timingBegin, now, NULL); - if (deltat.tv_sec == 0) { - alpha = maxAlpha; - if (deltat.tv_nsec >= NANOSECONDS_PER_SECOND/2) { - alpha -= ((float)deltat.tv_nsec-(NANOSECONDS_PER_SECOND/2)) / (float)(NANOSECONDS_PER_SECOND/2); - if (alpha < minAlpha) { - alpha = minAlpha; - } - } - } - - return alpha; -} - static inline bool _is_point_on_left_menu(float x, float y) { if (menu.topLeftShowing) { return (x >= touchport.topLeftX && x <= touchport.topLeftXMax && y >= touchport.topLeftY && y <= touchport.topLeftYMax); @@ -422,7 +402,7 @@ static void gltouchmenu_setup(void) { return; } - clock_gettime(CLOCK_MONOTONIC, &timingBegin); + clock_gettime(CLOCK_MONOTONIC, &menu.timingBegin); isAvailable = true; @@ -443,7 +423,10 @@ static void gltouchmenu_render(void) { gltouchmenu_setup(); } - float alpha = _get_menu_visibility(); + float alpha = glhud_getTimedVisibility(menu.timingBegin, menu.minAlpha, menu.maxAlpha); + if (alpha < menu.minAlpha) { + alpha = menu.minAlpha; + } if (alpha <= 0.0) { return; } @@ -533,7 +516,7 @@ static int64_t gltouchmenu_onTouchEvent(interface_touch_event_t action, int poin } if (handled) { - clock_gettime(CLOCK_MONOTONIC, &timingBegin); + clock_gettime(CLOCK_MONOTONIC, &menu.timingBegin); flags |= TOUCH_FLAGS_HANDLED; } @@ -552,18 +535,18 @@ static void gltouchmenu_setTouchMenuEnabled(bool enabled) { } static void _animation_showTouchMenu(void) { - clock_gettime(CLOCK_MONOTONIC, &timingBegin); + clock_gettime(CLOCK_MONOTONIC, &menu.timingBegin); } static void _animation_hideTouchMenu(void) { _hide_top_left(); _hide_top_right(); - timingBegin = (struct timespec){ 0 }; + menu.timingBegin = (struct timespec){ 0 }; } static void gltouchmenu_setTouchMenuVisibility(float inactiveAlpha, float activeAlpha) { - minAlpha = inactiveAlpha; - maxAlpha = activeAlpha; + menu.minAlpha = inactiveAlpha; + menu.maxAlpha = activeAlpha; } static void gltouchmenu_setGlyphScale(int glyphScale) { @@ -589,6 +572,8 @@ static void _init_gltouchmenu(void) { interface_setGlyphScale = &gltouchmenu_setGlyphScale; menu.glyphMultiplier = 1; + menu.minAlpha = 1/4.f; // Minimum alpha value of components (at zero, will not render) + menu.maxAlpha = 1.f; glnode_registerNode(RENDER_TOP, (GLNode){ .setup = &gltouchmenu_setup,