display-helpers.hpp 4.6 KB

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