Euphoria
viewportdef.cc
Go to the documentation of this file.
1 #include "core/viewportdef.h"
2 
3 #include "base/cint.h"
4 
5 
6 namespace eu::core
7 {
8  ViewportDefinition
10  (
11  float width,
12  float height,
13  int window_width,
14  int window_height
15  )
16  {
17  ASSERTX(width > 0, width);
18  ASSERTX(height > 0, height);
19  ASSERTX(window_width >= 0, window_width);
20  ASSERTX(window_height >= 0, window_height);
21  const float w = static_cast<float>(window_width) / width;
22  const float h = static_cast<float>(window_height) / height;
23  const float s = eu::min(w, h);
24  ASSERTX(s > 0, s, w, h);
25  const float new_width = width * s;
26  const float new_height = height * s;
27  return ViewportDefinition
28  {
30  (
31  c_float_to_int(new_width),
32  c_float_to_int(new_height)
34  (
35  c_float_to_int((c_int_to_float(window_width) - new_width) / 2.0f),
36  c_float_to_int((c_int_to_float(window_height) - new_height) / 2.0f)
37  ),
38  width,
39  height
40  };
41  }
42 
43 
44  float
45  determine_extend_scale(float scale, float height, int window_height)
46  {
47  const auto scaled_height = height * scale;
48  const auto s = static_cast<float>(window_height) / scaled_height;
49  return s;
50  }
51 
52 
53  ViewportDefinition
55  (
56  float width,
57  float height,
58  int window_width,
59  int window_height
60  )
61  {
62  ASSERTX(width >= 0, width);
63  ASSERTX(height >= 0, height);
64  ASSERTX(window_width >= 0, window_width);
65  ASSERTX(window_height >= 0, window_height);
66  const auto w = static_cast<float>(window_width) / width;
67  const auto h = static_cast<float>(window_height) / height;
68  const auto r = Recti::from_width_height(window_width, window_height)
70  if(w < h)
71  {
72  const auto s = determine_extend_scale(w, height, window_height);
73  return ViewportDefinition {r, width, height * s};
74  }
75  else
76  {
77  const auto s = determine_extend_scale(h, width, window_width);
78  return ViewportDefinition {r, width * s, height};
79  }
80  }
81 
82 
84  ViewportDefinition::from_screen_pixel(int window_width, int window_height)
85  {
86  ASSERTX(window_width >= 0, window_width);
87  ASSERTX(window_height >= 0, window_height);
88 
89  return ViewportDefinition
90  {
91  Recti::from_width_height(window_width, window_height)
93  static_cast<float>(window_width),
94  static_cast<float>(window_height)
95  };
96  }
97 
98 
99  ViewportDefinition::ViewportDefinition(const Recti& screen, float w, float h)
100  : screen_rect(screen)
101  , virtual_width(w)
102  , virtual_height(h)
103  {
104  }
105 }
106 
#define ASSERTX(x,...)
Definition: assert.h:48
float determine_extend_scale(float scale, float height, int window_height)
Definition: viewportdef.cc:45
constexpr int c_float_to_int(float f)
Definition: cint.h:36
constexpr float c_int_to_float(int i)
Definition: cint.h:50
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
static Recti from_width_height(int width, int height)
Definition: rect.cc:573
Recti set_bottom_left_to_copy(int new_left, int new_bottom) const
Definition: rect.cc:867
ViewportDefinition(const Recti &screen, float w, float h)
Definition: viewportdef.cc:99
static ViewportDefinition from_fit_with_black_bars(float width, float height, int window_width, int window_height)
The viewport is scaled, with aspect in mind, and centered.
Definition: viewportdef.cc:10
static ViewportDefinition from_screen_pixel(int window_width, int window_height)
The viewport matches the screen pixel by pixel.
Definition: viewportdef.cc:84
static ViewportDefinition from_extend(float width, float height, int window_width, int window_height)
The viewports height or width is extended to match the screen.
Definition: viewportdef.cc:55