Renderer.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/Renderer.hpp"
30 
31 namespace nom {
32 
33 Renderer::Renderer ( void ): renderer_
34  { SDL_RENDERER::UniquePtr ( nullptr, priv::FreeRenderTarget ) }
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38 }
39 
41 {
42 NOM_LOG_TRACE ( NOM );
43 
44  // Thanks for all the fish!
45 }
46 
47 bool Renderer::initialize ( SDL_WINDOW::RawPtr window, int32 rendering_driver, uint32 context_flags )
48 {
49 NOM_LOG_TRACE ( NOM );
50 
51  this->renderer_.reset ( SDL_CreateRenderer ( window, rendering_driver, context_flags ) );
52 
53  if ( this->renderer_valid() == false ) return false;
54 
55  return true;
56 }
57 
58 SDL_RENDERER::RawPtr Renderer::renderer ( void ) const
59 {
60  return this->renderer_.get();
61 }
62 
63 bool Renderer::renderer_valid ( void ) const
64 {
65  if ( this->renderer() != nullptr )
66  {
67  return true;
68  }
69  else
70  {
71  return false;
72  }
73 }
74 
75 const Point2i Renderer::scale_factor ( void ) const
76 {
78 
79  SDL_RenderGetScale ( this->renderer(), &scale_factor.x, &scale_factor.y );
80 
81  return scale_factor;
82 }
83 
85 {
86  Point2i size;
87 
88  SDL_RenderGetLogicalSize ( this->renderer(), &size.x, &size.y );
89 
90  return size;
91 }
92 
93 const Coords Renderer::viewport ( void ) const
94 {
95  SDL_Rect v;
96  Coords view_pos;
97 
98  SDL_RenderGetViewport ( this->renderer(), &v );
99 
100  view_pos.x = v.x;
101  view_pos.y = v.y;
102  view_pos.width = v.w;
103  view_pos.height = v.h;
104 
105  return view_pos;
106 }
107 
108 const SDL_BlendMode Renderer::blend_mode ( void ) const
109 {
110  SDL_BlendMode blend;
111 
112  if ( SDL_GetRenderDrawBlendMode ( this->renderer(), &blend ) != 0 )
113  {
114 NOM_LOG_ERR ( NOM, SDL_GetError() );
115  return blend;
116  }
117 
118  return blend;
119 }
120 
121 const Point2i Renderer::size ( void ) const
122 {
123  Point2i size; // width & height in pixels
124 
125  if ( SDL_GetRendererOutputSize ( this->renderer(), &size.x, &size.y ) != 0 )
126  {
127 NOM_LOG_ERR ( NOM, SDL_GetError() );
128  return Point2i( -1, -1 );
129  //return Point2i::null;
130  }
131 
132  return size;
133 }
134 
135 const Coords Renderer::bounds ( void ) const
136 {
137  SDL_Rect clip_bounds;
138 
139  SDL_RenderGetClipRect( this->renderer(), &clip_bounds );
140 
141  return Coords( clip_bounds.x, clip_bounds.y, clip_bounds.w, clip_bounds.h );
142 }
143 
144 void Renderer::update ( void ) const
145 {
146  SDL_RenderPresent ( this->renderer() );
147 }
148 
149 bool Renderer::update ( SDL_TEXTURE::RawPtr input_texture )
150 {
151  if ( SDL_SetRenderTarget ( this->renderer(), input_texture ) != 0 )
152  {
153 NOM_LOG_ERR ( NOM, SDL_GetError() );
154  return false;
155  }
156  return true;
157 }
158 
159 bool Renderer::clear ( void ) const
160 {
161  if ( SDL_RenderClear ( this->renderer() ) != 0 )
162  {
163 NOM_LOG_ERR ( NOM, SDL_GetError() );
164  return false;
165  }
166 
167  return true;
168 }
169 
170 bool Renderer::fill ( const Color& color )
171 {
172  if ( this->set_color ( color ) == false )
173  {
174  return false;
175  }
176 
177  if ( this->clear() == false )
178  {
179  return false;
180  }
181 
182  return true;
183 }
184 
185 bool Renderer::set_logical_resolution ( int width, int height )
186 {
187  if ( SDL_RenderSetLogicalSize ( this->renderer(), width, height ) != 0 )
188  {
189 NOM_LOG_ERR ( NOM, SDL_GetError() );
190  return false;
191  }
192  return true;
193 }
194 
195 bool Renderer::set_viewport ( const Coords& rect )
196 {
197  if ( rect != Coords::null )
198  {
199  SDL_Rect area = SDL_RECT(rect);
200 
201  if ( SDL_RenderSetViewport ( this->renderer(), &area ) != 0 )
202  {
203 NOM_LOG_ERR ( NOM, SDL_GetError() );
204  return false;
205  }
206  }
207  else
208  {
209  if ( SDL_RenderSetViewport ( this->renderer(), nullptr ) != 0 )
210  {
211 NOM_LOG_ERR ( NOM, SDL_GetError() );
212  return false;
213  }
214  }
215 
216  return true;
217 }
218 
219 bool Renderer::set_color ( const Color& color )
220 {
221  if ( SDL_SetRenderDrawColor ( this->renderer(), color.red, color.green, color.blue, color.alpha ) != 0 )
222  {
223 NOM_LOG_ERR ( NOM, SDL_GetError() );
224  return false;
225  }
226  return true;
227 }
228 
229 bool Renderer::set_blend_mode ( const SDL_BlendMode mode )
230 {
231  if ( SDL_SetRenderDrawBlendMode ( this->renderer(), mode ) != 0 )
232  {
233 NOM_LOG_ERR ( NOM, SDL_GetError() );
234  return false;
235  }
236  return true;
237 }
238 
239 bool Renderer::set_bounds ( const Coords& clip_bounds )
240 {
241  if ( clip_bounds == Coords::null ) // Disable clipping bounds rectangle
242  {
243  if ( SDL_RenderSetClipRect( this->renderer(), nullptr ) != 0 )
244  {
245 NOM_LOG_ERR ( NOM, SDL_GetError() );
246  return false;
247  }
248 
249  return true;
250  }
251 
252  SDL_Rect bounds = SDL_RECT ( clip_bounds );
253  if ( SDL_RenderSetClipRect( this->renderer(), &bounds ) != 0 )
254  {
255 NOM_LOG_ERR ( NOM, SDL_GetError() );
256  return false;
257  }
258 
259  return true;
260 }
261 
262 void* Renderer::pixels ( void ) const
263 {
264  void* pixels = static_cast<uint32*> ( malloc (this->size().x * this->size().y * 4) );
265 
266  if ( SDL_RenderReadPixels( this->renderer(),
267  nullptr, // Dump pixels of the entire target
268  0, // Use the pixel format of the render
269  // target.
270  pixels, // Pointer to be filled in with pixels
271  // from rendering target.
272  this->size().x * 4 ) != 0 )
273  {
274 NOM_LOG_ERR( NOM, SDL_GetError() );
275  return nullptr;
276  }
277 
278  return pixels;
279 }
280 
281 } // namespace nom
const Point2i size(void) const
Definition: Renderer.cpp:121
const Coords bounds(void) const
Definition: Renderer.cpp:135
SDL_RENDERER::UniquePtr renderer_
Definition: Renderer.hpp:165
Renderer(void)
Default constructor; initializes instance to sane defaults.
Definition: Renderer.cpp:33
static const Coords null
Convenience object that will always contain a value of -1.
Definition: Coords.hpp:94
const Point2i scale_factor(void) const
Obtain the current scale factor.
Definition: Renderer.cpp:75
const SDL_BlendMode blend_mode(void) const
Obtain the blending mode used for drawing.
Definition: Renderer.cpp:108
bool set_blend_mode(const SDL_BlendMode mode)
Definition: Renderer.cpp:229
virtual ~Renderer(void)
Lazy destructor with nothing to do.
Definition: Renderer.cpp:40
bool renderer_valid(void) const
Is this object initialized? Valid when NOT nullptr.
Definition: Renderer.cpp:63
bool fill(const Color &color=Color::Blue)
Definition: Renderer.cpp:170
bool set_bounds(const Coords &clip_bounds)
Definition: Renderer.cpp:239
bool set_viewport(const Coords &rect)
Definition: Renderer.cpp:195
const Point2i logical_resolution(void) const
Definition: Renderer.cpp:84
void update(void) const
Update the renderer surface on the attached window.
Definition: Renderer.cpp:144
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
Definition: Renderer.cpp:58
bool clear(void) const
Definition: Renderer.cpp:159
bool initialize(SDL_WINDOW::RawPtr window, int32 rendering_driver=-1, uint32 context_flags=SDL_RENDERER_ACCELERATED)
Definition: Renderer.cpp:47
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
bool set_logical_resolution(int width, int height)
Definition: Renderer.cpp:185
Utility class container for RGBA colors.
Definition: Color.hpp:43
void * pixels(void) const
Definition: Renderer.cpp:262
const Coords viewport(void) const
Definition: Renderer.cpp:93