display-helpers.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <graphics/vec4.h>
  16. #include <graphics/matrix4.h>
  17. static inline void GetScaleAndCenterPos(int baseCX, int baseCY, int windowCX, int windowCY, int &x, int &y,
  18. float &scale)
  19. {
  20. double windowAspect, baseAspect;
  21. int newCX, newCY;
  22. windowAspect = double(windowCX) / double(windowCY);
  23. baseAspect = double(baseCX) / double(baseCY);
  24. if (windowAspect > baseAspect) {
  25. scale = float(windowCY) / float(baseCY);
  26. newCX = int(double(windowCY) * baseAspect);
  27. newCY = windowCY;
  28. } else {
  29. scale = float(windowCX) / float(baseCX);
  30. newCX = windowCX;
  31. newCY = int(float(windowCX) / baseAspect);
  32. }
  33. x = windowCX / 2 - newCX / 2;
  34. y = windowCY / 2 - newCY / 2;
  35. }
  36. static inline void GetCenterPosFromFixedScale(int baseCX, int baseCY, int windowCX, int windowCY, int &x, int &y,
  37. float scale)
  38. {
  39. x = (float(windowCX) - float(baseCX) * scale) / 2.0f;
  40. y = (float(windowCY) - float(baseCY) * scale) / 2.0f;
  41. }
  42. static inline QSize GetPixelSize(QWidget *widget)
  43. {
  44. return widget->size() * widget->devicePixelRatioF();
  45. }
  46. #define OUTLINE_COLOR 0xFFD0D0D0
  47. #define LINE_LENGTH 0.1f
  48. // Rec. ITU-R BT.1848-1 / EBU R 95
  49. #define ACTION_SAFE_PERCENT 0.035f // 3.5%
  50. #define GRAPHICS_SAFE_PERCENT 0.05f // 5.0%
  51. #define FOURBYTHREE_SAFE_PERCENT 0.1625f // 16.25%
  52. static inline void InitSafeAreas(gs_vertbuffer_t **actionSafeMargin, gs_vertbuffer_t **graphicsSafeMargin,
  53. gs_vertbuffer_t **fourByThreeSafeMargin, gs_vertbuffer_t **leftLine,
  54. gs_vertbuffer_t **topLine, gs_vertbuffer_t **rightLine)
  55. {
  56. obs_enter_graphics();
  57. // All essential action should be placed inside this area
  58. gs_render_start(true);
  59. gs_vertex2f(ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
  60. gs_vertex2f(ACTION_SAFE_PERCENT, 1 - ACTION_SAFE_PERCENT);
  61. gs_vertex2f(1 - ACTION_SAFE_PERCENT, 1 - ACTION_SAFE_PERCENT);
  62. gs_vertex2f(1 - ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
  63. gs_vertex2f(ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
  64. *actionSafeMargin = gs_render_save();
  65. // All graphics should be placed inside this area
  66. gs_render_start(true);
  67. gs_vertex2f(GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  68. gs_vertex2f(GRAPHICS_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  69. gs_vertex2f(1 - GRAPHICS_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  70. gs_vertex2f(1 - GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  71. gs_vertex2f(GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  72. *graphicsSafeMargin = gs_render_save();
  73. // 4:3 safe area for widescreen
  74. gs_render_start(true);
  75. gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  76. gs_vertex2f(1 - FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  77. gs_vertex2f(1 - FOURBYTHREE_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  78. gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  79. gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  80. *fourByThreeSafeMargin = gs_render_save();
  81. gs_render_start(true);
  82. gs_vertex2f(0.0f, 0.5f);
  83. gs_vertex2f(LINE_LENGTH, 0.5f);
  84. *leftLine = gs_render_save();
  85. gs_render_start(true);
  86. gs_vertex2f(0.5f, 0.0f);
  87. gs_vertex2f(0.5f, LINE_LENGTH);
  88. *topLine = gs_render_save();
  89. gs_render_start(true);
  90. gs_vertex2f(1.0f, 0.5f);
  91. gs_vertex2f(1 - LINE_LENGTH, 0.5f);
  92. *rightLine = gs_render_save();
  93. obs_leave_graphics();
  94. }
  95. static inline void RenderSafeAreas(gs_vertbuffer_t *vb, int cx, int cy)
  96. {
  97. if (!vb)
  98. return;
  99. matrix4 transform;
  100. matrix4_identity(&transform);
  101. transform.x.x = cx;
  102. transform.y.y = cy;
  103. gs_load_vertexbuffer(vb);
  104. gs_matrix_push();
  105. gs_matrix_mul(&transform);
  106. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  107. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  108. gs_effect_set_color(color, OUTLINE_COLOR);
  109. while (gs_effect_loop(solid, "Solid"))
  110. gs_draw(GS_LINESTRIP, 0, 0);
  111. gs_matrix_pop();
  112. }