display-helpers.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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,
  18. int windowCY, int &x, int &y,
  19. float &scale)
  20. {
  21. double windowAspect, baseAspect;
  22. int newCX, newCY;
  23. windowAspect = double(windowCX) / double(windowCY);
  24. baseAspect = double(baseCX) / double(baseCY);
  25. if (windowAspect > baseAspect) {
  26. scale = float(windowCY) / float(baseCY);
  27. newCX = int(double(windowCY) * baseAspect);
  28. newCY = windowCY;
  29. } else {
  30. scale = float(windowCX) / float(baseCX);
  31. newCX = windowCX;
  32. newCY = int(float(windowCX) / baseAspect);
  33. }
  34. x = windowCX / 2 - newCX / 2;
  35. y = windowCY / 2 - newCY / 2;
  36. }
  37. static inline void GetCenterPosFromFixedScale(int baseCX, int baseCY,
  38. int windowCX, int windowCY,
  39. int &x, int &y, float scale)
  40. {
  41. x = (float(windowCX) - float(baseCX) * scale) / 2.0f;
  42. y = (float(windowCY) - float(baseCY) * scale) / 2.0f;
  43. }
  44. static inline QSize GetPixelSize(QWidget *widget)
  45. {
  46. return widget->size() * widget->devicePixelRatioF();
  47. }
  48. #define OUTLINE_COLOR 0xFFD0D0D0
  49. #define LINE_LENGTH 0.1f
  50. // Rec. ITU-R BT.1848-1 / EBU R 95
  51. #define ACTION_SAFE_PERCENT 0.035f // 3.5%
  52. #define GRAPHICS_SAFE_PERCENT 0.05f // 5.0%
  53. #define FOURBYTHREE_SAFE_PERCENT 0.1625f // 16.25%
  54. static inline void InitSafeAreas(gs_vertbuffer_t **actionSafeMargin,
  55. gs_vertbuffer_t **graphicsSafeMargin,
  56. gs_vertbuffer_t **fourByThreeSafeMargin,
  57. gs_vertbuffer_t **leftLine,
  58. gs_vertbuffer_t **topLine,
  59. gs_vertbuffer_t **rightLine)
  60. {
  61. obs_enter_graphics();
  62. // All essential action should be placed inside this area
  63. gs_render_start(true);
  64. gs_vertex2f(ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
  65. gs_vertex2f(ACTION_SAFE_PERCENT, 1 - ACTION_SAFE_PERCENT);
  66. gs_vertex2f(1 - ACTION_SAFE_PERCENT, 1 - ACTION_SAFE_PERCENT);
  67. gs_vertex2f(1 - ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
  68. gs_vertex2f(ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
  69. *actionSafeMargin = gs_render_save();
  70. // All graphics should be placed inside this area
  71. gs_render_start(true);
  72. gs_vertex2f(GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  73. gs_vertex2f(GRAPHICS_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  74. gs_vertex2f(1 - GRAPHICS_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  75. gs_vertex2f(1 - GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  76. gs_vertex2f(GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  77. *graphicsSafeMargin = gs_render_save();
  78. // 4:3 safe area for widescreen
  79. gs_render_start(true);
  80. gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  81. gs_vertex2f(1 - FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  82. gs_vertex2f(1 - FOURBYTHREE_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  83. gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
  84. gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
  85. *fourByThreeSafeMargin = gs_render_save();
  86. gs_render_start(true);
  87. gs_vertex2f(0.0f, 0.5f);
  88. gs_vertex2f(LINE_LENGTH, 0.5f);
  89. *leftLine = gs_render_save();
  90. gs_render_start(true);
  91. gs_vertex2f(0.5f, 0.0f);
  92. gs_vertex2f(0.5f, LINE_LENGTH);
  93. *topLine = gs_render_save();
  94. gs_render_start(true);
  95. gs_vertex2f(1.0f, 0.5f);
  96. gs_vertex2f(1 - LINE_LENGTH, 0.5f);
  97. *rightLine = gs_render_save();
  98. obs_leave_graphics();
  99. }
  100. static inline void RenderSafeAreas(gs_vertbuffer_t *vb, int cx, int cy)
  101. {
  102. if (!vb)
  103. return;
  104. matrix4 transform;
  105. matrix4_identity(&transform);
  106. transform.x.x = cx;
  107. transform.y.y = cy;
  108. gs_load_vertexbuffer(vb);
  109. gs_matrix_push();
  110. gs_matrix_mul(&transform);
  111. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  112. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  113. gs_effect_set_color(color, OUTLINE_COLOR);
  114. while (gs_effect_loop(solid, "Solid"))
  115. gs_draw(GS_LINESTRIP, 0, 0);
  116. gs_matrix_pop();
  117. }