Euphoria
viewporthandler.cc
Go to the documentation of this file.
2 
3 #include "core/viewportdef.h"
4 #include "base/cint.h"
5 #include "core/viewport.h"
6 
7 #include "render/viewport.h"
8 #include "render/init.h"
9 #include "render/shader.h"
10 
11 
12 namespace
13 {
14  namespace core = eu::core;
15 
17  create_definition(const eu::render::ViewportHandler& handler)
18  {
19  switch(handler.type)
20  {
23  (
24  handler.virtual_width,
25  handler.virtual_height,
26  handler.window_width,
27  handler.window_height
28  );
31  (
32  handler.window_width,
33  handler.window_height
34  );
37  (
38  handler.virtual_width,
39  handler.virtual_height,
40  handler.window_width,
41  handler.window_height
42  );
43  default:
44  DIE("Unhandled viewport case");
46  (
47  handler.window_width,
48  handler.window_height
49  );
50  }
51  }
52 
53 
54  void apply_viewport
55  (
57  const core::ViewportDefinition& vp,
58  bool shaders_too
59  )
60  {
61  if(shaders_too)
62  {
63  const auto projection = handler->init->get_ortho_projection
64  (
65  vp.virtual_width,
67  );
68  for(auto* shader: handler->shaders)
69  {
70  shader->set_uniform
71  (
72  shader->get_uniform("projection"),
73  projection
74  );
75  }
76  }
77 
78  if(handler->virtual_screen != nullptr)
79  {
81  (
82  vp.virtual_width,
84  );
85  }
86 
87  auto viewport = eu::core::Viewport{vp.screen_rect};
88  eu::render::activate(viewport);
89  }
90 }
91 
92 
93 namespace eu::render
94 {
96  : init(i)
97  , virtual_screen(s)
98  {
99  }
100 
101 
102  void
104  {
105  shaders.emplace_back(shader);
106  }
107 
108 
109  void
110  ViewportHandler::set_size(int width, int height)
111  {
112  window_width = width;
113  window_height = height;
114 
115  const auto vp = create_definition(*this);
116 
117  apply_viewport(this, vp, true);
118  }
119 
120 
121  void
123  {
124  if(type != ViewportType::fit_with_black_bars) { return; }
125 
126  apply_viewport
127  (
128  this,
130  false
131  );
132 
134 
135  apply_viewport
136  (
137  this,
138  create_definition(*this),
139  false
140  );
141  }
142 
143 
144 
147  {
148  const auto viewport = core::Viewport
149  {
152  (
153  0,
154  0
155  )
156  };
157  return viewport;
158  }
159 }
160 
#define DIE(message)
Definition: assert.h:67
void activate(const core::Viewport &vp)
Sets the gl viewport.
Definition: viewport.cc:11
Definition: rect.h:27
static Rectf from_width_height(float width, float height)
Definition: rect.cc:98
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
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
void clear_screen(const Rgb &color) const
Definition: init.cc:77
mat4f get_ortho_projection(float width, float height) const
Definition: init.cc:61
void set_size(int width, int height)
ViewportHandler(render::Init *i, Rectf *s)
std::vector< ShaderProgram * > shaders
core::Viewport get_full_viewport() const
void add(ShaderProgram *shader)