Texture.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  scale2x Algorithm
29 1. Copyright (c) Andrea Mazzoleni
30 
31 ******************************************************************************/
32 #include "nomlib/graphics/Texture.hpp"
33 #include "nomlib/graphics/scale2x/scale2x.hpp"
34 #include "nomlib/graphics/hqx/hqx.hpp"
35 
36 namespace nom {
37 
38 Texture::Texture ( void ) : texture_ { nullptr, priv::FreeTexture },
39  pixels_ ( nullptr ), pitch_ ( 0 ), position_ ( 0, 0 ),
40  bounds_ ( 0, 0, -1, -1 ), colorkey_ { Color::null }
41 {
42 NOM_LOG_TRACE ( NOM );
43 }
44 
46 {
47 NOM_LOG_TRACE ( NOM );
48 }
49 
50 Texture::Texture ( const Texture& other ) :
51  texture_ { other.texture(), priv::FreeTexture },
52  pixels_ { other.pixels() }, pitch_ { other.pitch() },
53  position_ { other.position() }, bounds_ { other.bounds() },
54  colorkey_ { other.colorkey() }
55 {
56 NOM_LOG_TRACE ( NOM );
57 }
58 
59 /*
60 Texture::Texture ( int32 width, int32 height, uint8 bitsPerPixel, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask, uint32 flags )
61 {
62 NOM_LOG_TRACE ( NOM );
63 
64  this->initialize ( flags, width, height, bitsPerPixel, Rmask, Gmask, Bmask, Amask );
65 }
66 
67 Texture::Texture ( void* pixels, int32 width, int32 height, int32 depth, uint16 pitch, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask )
68 {
69 NOM_LOG_TRACE ( NOM );
70 
71  //this->texture_.reset ( SDL_CreateRGBSurfaceFrom ( pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask ), nom::priv::FreeSurface );
72 
73  this->bounds_.setSize ( width, height );
74 }
75 */
76 
77 /*
78 void Texture::initialize ( uint32 flags, int32 width, int32 height, uint8 bitsPerPixel, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask )
79 {
80 NOM_LOG_TRACE ( NOM );
81 
82  //this->texture_.reset ( SDL_CreateRGBSurface ( flags, width, height, bitsPerPixel, Rmask, Gmask, Bmask, Amask ), nom::priv::FreeSurface );
83  this->bounds_.setSize ( width, height );
84 
85  // If the video surface is marked for color keying transparency, we must do
86  // so here.
87  //if ( flags & SDL_TRUE )//SDL_SRCCOLORKEY )
88  //{
89  //if ( this->setTransparent ( this->getTextureColorKey(), SDL_RLEACCEL | SDL_TRUE ) == false )//SDL_SRCCOLORKEY ) == false )
90  //{
91 //NOM_LOG_ERR ( NOM, "Could not create the video surface with color key transparency." );
92  //}
93  //}
94 //}
95 */
96 
97 bool Texture::initialize ( SDL_SURFACE::RawPtr video_buffer )
98 {
99  // We leak memory, or even worse, crash, depending on state at the time, if we
100  // do not ensure that we free existing buffers! Certain rendering methods --
101  // *cough* nom::TrueTypeFont -- need this done because they create new
102  // surfaces (instead of updating what they have) upon every update cycle.
103  if ( this->valid() ) this->texture_.reset();
104 
105  this->texture_.reset ( SDL_CreateTextureFromSurface ( Window::context(), video_buffer ), priv::FreeTexture );
106 
107  if ( this->valid() == false )
108  {
109 NOM_LOG_ERR ( NOM, SDL_GetError() );
110  return false;
111  }
112 
113  // TODO: cache width & height
114 
115  // Cache the size of our new Texture object with the existing surface info
116  this->set_bounds ( Coords ( 0, 0, video_buffer->w, video_buffer->h ) );
117 
118  priv::FreeSurface ( video_buffer );
119 
120  return true;
121 }
122 
123 bool Texture::initialize ( int32 width, int32 height, uint32 format, uint32 flags )
124 {
125  this->texture_.reset ( SDL_CreateTexture ( Window::context(), format, flags, width, height ), priv::FreeTexture );
126 
127  if ( this->valid() == false )
128  {
129 NOM_LOG_ERR ( NOM, SDL_GetError() );
130  return false;
131  }
132 
133  // TODO: cache width & height
134 
135  // Cache the size of our new Texture object with the existing surface info
136  this->set_bounds( Coords ( 0, 0, width, height ) );
137 
138  return true;
139 }
140 
141 Texture::SharedPtr Texture::clone ( void ) const
142 {
143  return Texture::SharedPtr ( new Texture ( *this ) );
144 }
145 
147 {
148  this->texture_ = other.texture_;
149  this->pixels_ = other.pixels_;
150  this->pitch_ = other.pitch_;
151  this->position_ = other.position_;
152  this->bounds_ = other.bounds_;
153 
154  return *this;
155 }
156 
157 SDL_TEXTURE::RawPtr Texture::texture ( void ) const
158 {
159  return this->texture_.get();
160 }
161 
162 bool Texture::valid ( void ) const
163 {
164  if ( this->texture() != nullptr ) return true;
165 
166  return false;
167 }
168 
169 int Texture::access ( void ) const
170 {
171  int type;
172 
173  if ( SDL_QueryTexture ( this->texture(), nullptr, &type, nullptr, nullptr ) != 0 )
174  {
175 NOM_LOG_ERR ( NOM, SDL_GetError() );
176  return type;
177  }
178 
179  return type;
180 }
181 
182 /* RELOCATE
183 void Texture::setTexture ( const Texture& surface )
184 {
185  //this->texture_.reset ( SDL_ConvertSurface ( surface.texture_.get(), surface.getTexturePixelsFormat(), surface.getTextureFlags() ), nom::priv::FreeSurface );
186 
187  //this->offsets.setSize ( surface.width(), surface.height() );
188 }
189 RELOCATE */
190 
191 const Point2i& Texture::position ( void ) const
192 {
193  return this->position_;
194 }
195 
196 const Coords& Texture::bounds ( void ) const
197 {
198  return this->bounds_;
199 }
200 
201 void Texture::set_position ( const Point2i& pos )
202 {
203  this->position_.x = pos.x;
204  this->position_.y = pos.y;
205 }
206 
207 void Texture::set_bounds ( const Coords& clip )
208 {
209  this->bounds_ = clip;
210 }
211 
212 int32 Texture::width ( void ) const
213 {
214  int32 tex_width;
215 
216  if ( SDL_QueryTexture ( this->texture(), nullptr, nullptr, &tex_width, nullptr ) != 0 )
217  {
218 NOM_LOG_ERR ( NOM, SDL_GetError() );
219  return -1;
220  }
221 
222  return tex_width;
223 }
224 
225 int32 Texture::height ( void ) const
226 {
227  int32 tex_height;
228 
229  if ( SDL_QueryTexture ( this->texture(), nullptr, nullptr, nullptr, &tex_height ) != 0 )
230  {
231 NOM_LOG_ERR ( NOM, SDL_GetError() );
232  return -1;
233  }
234 
235  return tex_height;
236 }
237 
238 int Texture::pitch ( void ) const
239 {
240  return pitch_;
241 }
242 
243 void* Texture::pixels ( void ) const
244 {
245  return this->pixels_;
246 }
247 
248 uint8 Texture::bits_per_pixel ( void ) const
249 {
250  switch ( this->bytes_per_pixel() )
251  {
252  default: return 0; break; // Unknown color depth
253 
254  case 1: return 8; break; // 8-bit bpp
255  case 2: return 16; break; // 16-bit bpp
256  case 3: return 24; break; // 24-bit bpp
257  case 4: return 32; break; // 32-bit bpp
258  }
259 }
260 
261 uint8 Texture::bytes_per_pixel ( void ) const
262 {
263  return SDL_BYTESPERPIXEL ( this->pixel_format() );
264 }
265 
266 uint32 Texture::pixel_format ( void ) const
267 {
268  uint32 format = 0;
269 
270  if ( SDL_QueryTexture ( this->texture(), &format, nullptr, nullptr, nullptr ) != 0 )
271  {
272 NOM_LOG_ERR ( NOM, SDL_GetError() );
273  return 0;
274  }
275 
276  return format;
277 }
278 
279 const SDL_BlendMode Texture::blend_mode ( void ) const
280 {
281  SDL_BlendMode blend;
282 
283  if ( SDL_GetTextureBlendMode ( this->texture(), &blend ) != 0 )
284  {
285 NOM_LOG_ERR ( NOM, SDL_GetError() );
286  return blend;
287  }
288 
289  return blend;
290 }
291 
292 bool Texture::locked ( void ) const
293 {
294  if ( this->pixels() != nullptr ) return true;
295 
296  return false;
297 }
298 
299 const Color& Texture::colorkey ( void ) const
300 {
301  return this->colorkey_;
302 }
303 
304 bool Texture::lock ( void )
305 {
306  if ( this->locked() )
307  {
308 NOM_LOG_ERR ( NOM, "Texture is already locked." );
309  return false;
310  }
311 
312  if ( SDL_LockTexture ( this->texture(), nullptr, &this->pixels_, &this->pitch_ ) != 0 )
313  {
314 NOM_LOG_ERR ( NOM, SDL_GetError() );
315  return false;
316  }
317 
318  return true;
319 }
320 
321 bool Texture::lock ( const Coords& lock_coords )
322 {
323  SDL_Rect area = SDL_RECT(lock_coords);
324 
325  if ( this->locked() )
326  {
327 NOM_LOG_ERR ( NOM, "Texture is already locked." );
328  return false;
329  }
330 
331  if ( SDL_LockTexture ( this->texture(), &area, &this->pixels_, &this->pitch_ ) != 0 )
332  {
333 NOM_LOG_ERR ( NOM, SDL_GetError() );
334  return false;
335  }
336 
337  return true;
338 }
339 
340 void Texture::unlock ( void )
341 {
342  if ( this->pixels() == nullptr )
343  {
344 NOM_LOG_INFO ( NOM, "Texture is not locked." );
345  return;
346  }
347 
348  SDL_UnlockTexture ( this->texture() );
349  this->pixels_ = nullptr;
350  this->pitch_ = 0;
351 }
352 
354  const std::string& filename, uint32 flags,
355  bool use_cache
356  )
357 {
358  Image image;
359 
360 /* TODO
361  // By default -- for peace of mind above all else -- we have caching turned
362  // off
363  if ( use_cache )
364  {
365  priv::ObjectCache cache;
366 
367  this->texture_ = cache.getObject ( filename );
368 
369  if ( this->texture_ == nullptr )
370  {
371  this->texture_ = cache.addObject ( filename, image.load ( filename ) );
372  }
373  }
374  else // Do not use the object cache
375  {
376 
377  this->texture_ = image.load ( filename );
378  }
379 TODO */
380 
381  if ( image.load ( filename ) == false ) return false;
382 
383  // We produce a segmentation fault here if we do not have SDL's video
384  // subsystem initialized before making the following calls -- transparency
385  // and display format conversion.
386 NOM_ASSERT ( SDL_WasInit ( SDL_INIT_VIDEO) );
387 
388  if ( this->initialize ( image.width(), image.height(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING ) == false )
389  {
390 NOM_LOG_ERR ( NOM, "Error: Failed to initialize texture." );
391  return false;
392  }
393 
394  // Set our default blending mode for texture copies
395  this->set_blend_mode( SDL_BLENDMODE_BLEND );
396 
397  this->lock ( image.bounds() ); // Safe for writing
398 
399  // Copy pixels from image into our freshly initialized texture
400  memcpy ( this->pixels(), image.pixels(), image.pitch() * image.height() );
401 
402  // Once we unlock the texture, it will be uploaded to the GPU for us!
403  this->unlock();
404 
405  // Update our Texture clipping bounds with the new source
406  this->set_bounds ( image.bounds() );
407 
408  return true;
409 }
410 
411 bool Texture::update ( const void* pixels, uint16 pitch, const Coords& update_area )
412 {
413  if ( SDL_UpdateTexture ( this->texture(), nullptr, pixels, pitch ) != 0 )
414  {
415 NOM_LOG_ERR ( NOM, SDL_GetError() );
416  return false;
417  }
418 
419  return true;
420 }
421 
422 void Texture::draw ( SDL_RENDERER::RawPtr target ) const
423 {
424  Point2i pos = this->position();
425  SDL_Rect render_coords;
426 
427  render_coords.x = pos.x;
428  render_coords.y = pos.y;
429 
430  // Use preset clipping bounds for the width and height of this texture
431  render_coords.w = this->bounds().w;
432  render_coords.h = this->bounds().h;
433 
434  // Render with set clipping bounds; we are rendering only a portion of a
435  // larger Texture; think: sprite sheets.
436  if ( this->bounds().w != -1 && this->bounds().h != -1 )
437  {
438  SDL_Rect render_bounds;
439  render_bounds.x = this->bounds().x;
440  render_bounds.y = this->bounds().y;
441  render_bounds.w = this->bounds().w;
442  render_bounds.h = this->bounds().h;
443  if ( SDL_RenderCopy ( target, this->texture(), &render_bounds, &render_coords ) != 0 )
444  {
445 NOM_LOG_ERR ( NOM, SDL_GetError() );
446  return;
447  }
448  }
449  else // Render the entire Texture we have in memory
450  {
451  if ( SDL_RenderCopy ( target, this->texture(), nullptr, &render_coords ) != 0 )
452  {
453 NOM_LOG_ERR ( NOM, SDL_GetError() );
454  return;
455  }
456  }
457 }
458 
459 void Texture::draw ( const Window& target ) const
460 {
461  this->draw ( target.renderer() );
462 }
463 
464 void Texture::draw ( SDL_RENDERER::RawPtr target, const double angle ) const
465 {
466  Point2i pos = this->position();
467  SDL_Rect render_coords;
468 
469  render_coords.x = pos.x;
470  render_coords.y = pos.y;
471 
472  // Use preset clipping bounds for the width and height of this texture
473  render_coords.w = this->bounds().w;
474  render_coords.h = this->bounds().h;
475 
476  // Render with set clipping bounds; we are rendering only a portion of a
477  // larger Texture; think: sprite sheets.
478  if ( this->bounds().w != -1 && this->bounds().h != -1 )
479  {
480  SDL_Rect render_bounds;
481  render_bounds.x = this->bounds().x;
482  render_bounds.y = this->bounds().y;
483  render_bounds.w = this->bounds().w;
484  render_bounds.h = this->bounds().h;
485  if ( SDL_RenderCopyEx ( target, this->texture(), &render_bounds, &render_coords, angle, nullptr, SDL_FLIP_NONE ) != 0 )
486  {
487 NOM_LOG_ERR ( NOM, SDL_GetError() );
488  return;
489  }
490  }
491  else // Render the entire Texture we have in memory
492  {
493  if ( SDL_RenderCopyEx ( target, this->texture(), nullptr, &render_coords, angle, nullptr, SDL_FLIP_NONE ) != 0 )
494  {
495 NOM_LOG_ERR ( NOM, SDL_GetError() );
496  return;
497  }
498  }
499 }
500 
501 void Texture::draw ( const Window& target, const double degrees ) const
502 {
503  this->draw ( target.renderer(), degrees );
504 }
505 
506 bool Texture::set_alpha ( uint8 opacity )
507 {
508 NOM_ASSERT ( ! ( opacity > SDL_ALPHA_OPAQUE ) || ( opacity < SDL_ALPHA_TRANSPARENT ) );
509 
510  if ( SDL_SetTextureAlphaMod ( this->texture(), opacity ) != 0 )
511  {
512 NOM_LOG_ERR ( NOM, SDL_GetError() );
513  return false;
514  }
515 
516  return true;
517 }
518 
519 uint32 Texture::pixel ( int32 x, int32 y )
520 {
521  switch ( this->bits_per_pixel() )
522  {
523  default: return -1; break; // Unknown
524 
525  case 8:
526  {
527  uint8* pixels = static_cast<uint8*> ( this->pixels() );
528 
529  return pixels[ ( y * this->pitch() ) + x ];
530  }
531  break;
532 
533  case 16:
534  {
535  uint16* pixels = static_cast<uint16*> ( this->pixels() );
536 
537  return pixels[ ( y * this->pitch() / 2 ) + x ];
538  }
539  break;
540 
541  case 24:
542  {
543  uint8* pixels = static_cast<uint8*> ( this->pixels() );
544 
545  return pixels[ ( y * this->pitch() ) + x ];
546  }
547  break;
548 
549  case 32:
550  {
551  uint32* pixels = static_cast<uint32*> ( this->pixels() );
552 
553  return pixels[ ( y * this->pitch()/4 ) + x ];
554  }
555  break;
556  } // end switch
557 }
558 
559 bool Texture::resize ( enum ResizeAlgorithm scaling_algorithm )
560 {
561 /*
562  // Ensure that our existing video surface is OK first
563  if ( this->valid() == false )
564  {
565 NOM_LOG_ERR ( NOM, "The existing video surface is not valid." );
566  return false;
567  }
568 
569  // Current video surface flags state -- the destination buffer will be set
570  // with these.
571  uint32 flags = this->getTextureFlags();
572 
573  // This is the target video surface object that is created from the existing
574  // video surface, with the existing width & height recomputed to whatever the
575  // chosen algorithm expects. The target buffer (upon success) becomes the new
576  // video surface of this instance.
577  Texture destination_buffer;
578 
579  // Pick out the suitable scaling factor for determining the new video surface
580  // width and height.
581  int32 scale_factor = getResizeScaleFactor ( scaling_algorithm );
582 
583  // We must not set an alpha mask value if our existing video surface is color
584  // keyed (or bad things ensue -- like many hours spent reading up on this
585  // surprisingly confusing subject).
586  uint32 alpha_mask = 0; // no alpha mask is default (works with color keying)
587 
588  // If the video surface does *NOT* have color keying set
589  if ( ! ( flags & SDL_TRUE ) )//SDL_SRCCOLORKEY ) )
590  {
591  alpha_mask = this->getTextureAlphaMask();
592  }
593 
594  destination_buffer = Texture (
595  this->width() * scale_factor,
596  this->height() * scale_factor,
597  this->getTextureBitsPerPixel(),
598  this->getTextureRedMask(),
599  this->getTextureGreenMask(),
600  this->getTextureBlueMask(),
601  alpha_mask,
602  flags
603  );
604 
605  // Ensure that our new video surface is sane before feeding
606  if ( destination_buffer.valid() == false )
607  {
608 NOM_LOG_ERR ( NOM, "The destination video surface is not valid." );
609  return false;
610  }
611 
612  // Lock pixels buffer for writing to
613  if ( this->lock() == false )
614  {
615 NOM_LOG_ERR ( NOM, "Could not lock video surface memory." );
616  return false;
617  }
618 
619  switch ( scaling_algorithm )
620  {
621  default: this->unlock(); return false; break; // No resizing is applied
622 
623  case ResizeAlgorithm::scale2x:
624  {
625  if ( priv::scale2x (
626  this->getTexturePixels(),
627  destination_buffer.getTexturePixels(),
628  this->width(),
629  this->height(),
630  this->getTextureColorDepth(),
631  this->getTexturePitch(),
632  destination_buffer.getTexturePitch()
633  ) == false )
634  {
635 NOM_LOG_ERR ( NOM, "Failed to resize video surface with scale2x." );
636  this->unlock(); // Relinquish our write lock
637  return false;
638  }
639  }
640  break;
641 
642  case ResizeAlgorithm::scale3x:
643  {
644  if ( priv::scale3x (
645  this->getTexturePixels(),
646  destination_buffer.getTexturePixels(),
647  this->width(),
648  this->height(),
649  this->getTextureColorDepth(),
650  this->getTexturePitch(),
651  destination_buffer.getTexturePitch()
652  ) == false )
653  {
654 NOM_LOG_ERR ( NOM, "Failed to resize video surface with scale3x." );
655  this->unlock(); // Relinquish our write lock
656  return false;
657  }
658  }
659  break;
660 
661  case ResizeAlgorithm::scale4x:
662  {
663  if ( priv::scale2x (
664  this->getTexturePixels(),
665  destination_buffer.getTexturePixels(),
666  this->width(),
667  this->height(),
668  this->getTextureColorDepth(),
669  this->getTexturePitch(),
670  destination_buffer.getTexturePitch()
671  ) == false )
672  {
673 NOM_LOG_ERR ( NOM, "Failed to resize video surface with scale4x." );
674  this->unlock(); // Relinquish our write lock
675  return false;
676  }
677  }
678  break;
679 
680  case ResizeAlgorithm::hq2x:
681  {
682  priv::hqxInit();
683  // Note that we must pass the *source* width and height here
684  priv::hq2x_32 (
685  static_cast<uint32*> ( this->getTexturePixels() ),
686  static_cast<uint32*> ( destination_buffer.getTexturePixels() ),
687  this->width(), this->height()
688  );
689  }
690  break;
691 
692  case ResizeAlgorithm::hq3x:
693  {
694  priv::hqxInit();
695  // Note that we must pass the *source* width and height here
696  priv::hq3x_32 (
697  static_cast<uint32*> ( this->getTexturePixels() ),
698  static_cast<uint32*> ( destination_buffer.getTexturePixels() ),
699  this->width(), this->height()
700  );
701  }
702  break;
703 
704  case ResizeAlgorithm::hq4x:
705  {
706  priv::hqxInit();
707  // Note that we must pass the *source* width and height here
708  priv::hq4x_32 (
709  static_cast<uint32*> ( this->getTexturePixels() ),
710  static_cast<uint32*> ( destination_buffer.getTexturePixels() ),
711  this->width(), this->height()
712  );
713  }
714  break;
715  } // end switch scaling_algorithm
716 
717  this->unlock(); // Relinquish our write lock
718 
719  // Do one more sanity check on our new video surface before do the transfer
720  if ( destination_buffer.valid() == false )
721  {
722 NOM_LOG_ERR ( NOM, "The rescaled video surface is not valid." );
723  return false;
724  }
725 
726  // Reset the video surface object's video memory to the rescaled pixel data.
727  this->setTexture ( destination_buffer );
728 */
729  return true;
730 }
731 
732 bool Texture::resize ( const Point2f& scale_factor )
733 {
734 /*
735  // set ResizeAlgorithm::Stretch
736 
737  // Ensure that our existing video surface is OK first
738  if ( this->valid() == false )
739  {
740 NOM_LOG_ERR ( NOM, "The existing video surface is not valid." );
741  return false;
742  }
743 
744  // Current video surface flags state -- the destination buffer will be set
745  // with these.
746  uint32 flags = this->getTextureFlags();
747 
748  // This is the target video surface object that is created from the existing
749  // video surface, with the existing width & height recomputed to whatever the
750  // chosen algorithm expects. The target buffer (upon success) becomes the new
751  // video surface of this instance.
752  Texture destination_buffer;
753 
754  // Pick out the suitable scaling factor for determining the new video surface
755  // width and height.
756  //int32 scale_factor = getResizeScaleFactor ( scaling_algorithm );
757 
758  // We must not set an alpha mask value if our existing video surface is color
759  // keyed (or bad things ensue -- like many hours spent reading up on this
760  // surprisingly confusing subject).
761  uint32 alpha_mask = 0; // no alpha mask is default (works with color keying)
762 
763  // If the video surface does *NOT* have color keying set
764  if ( ! ( flags & SDL_TRUE ) )//SDL_SRCCOLORKEY ) )
765  {
766  //alpha_mask = this->getTextureAlphaMask();
767  }
768 
769  destination_buffer = Texture (
770  this->width()*scale_factor.x,
771  this->height()*scale_factor.y,
772  this->getTextureBitsPerPixel(),
773  this->getTextureRedMask(),
774  this->getTextureGreenMask(),
775  this->getTextureBlueMask(),
776  alpha_mask,
777  flags
778  );
779 
780  // Ensure that our new video surface is sane before feeding
781  if ( destination_buffer.valid() == false )
782  {
783 NOM_LOG_ERR ( NOM, "The destination video surface is not valid." );
784  return false;
785  }
786 
787  // Lock pixels buffer for writing to
788  if ( this->lock() == false )
789  {
790 NOM_LOG_ERR ( NOM, "Could not lock video surface memory." );
791  return false;
792  }
793 
794  Vector2f r;
795  Color color;
796  Pixel pixels;
797 
798  double stretch_x = (double) this->width() * scale_factor.x / (double) this->width();
799  double stretch_y = (double) this->height() * scale_factor.y / (double) this->height();
800 
801  for ( int y = 0; y < this->height(); y++ )
802  {
803  for ( int x = 0; x < this->width(); x++ )
804  {
805  for ( int sY = 0; sY < stretch_y; sY++ )
806  {
807  for ( int sX = 0; sX < stretch_x; sX++ )
808  {
809  uint32 pixel = this->getPixel ( x, y );
810  RGBA::asRGB ( pixel, this->getTexturePixelsFormat(), color );
811  //NOM_DUMP_VAR(color);
812  if ( color.red == 0 && color.green == 0 && color.blue == 0 ) continue;
813 
814  pixels = Pixel ( ( stretch_x * x ) + sX, ( stretch_y * y ) + sY, color );
815  pixels.Draw ( destination_buffer.get() );
816  }
817  }
818  }
819  }
820 
821  this->unlock(); // Relinquish our write lock
822 
823  // Do one more sanity check on our new video surface before do the transfer
824  if ( destination_buffer.valid() == false )
825  {
826 NOM_LOG_ERR ( NOM, "The destination video surface is not valid." );
827  return false;
828  }
829 
830  // Reset the video surface object's video memory to the rescaled pixel data.
831  this->setTexture ( destination_buffer );
832 */
833  return true;
834 }
835 
836 int32 Texture::getResizeScaleFactor ( enum ResizeAlgorithm scaling_algorithm )
837 {
838  switch ( scaling_algorithm )
839  {
840  default: return 1; break;
841 
842  case ResizeAlgorithm::hq2x:
843  case ResizeAlgorithm::scale2x:
844  return 2;
845  break;
846 
847  case ResizeAlgorithm::scale3x:
848  case ResizeAlgorithm::hq3x:
849  return 3;
850  break;
851 
852  case ResizeAlgorithm::scale4x:
853  case ResizeAlgorithm::hq4x:
854  return 4;
855  break;
856  }
857 }
858 
859 bool Texture::set_blend_mode ( const SDL_BlendMode blend )
860 {
861  if ( SDL_SetTextureBlendMode ( this->texture(), blend ) != 0 )
862  {
863 NOM_LOG_ERR ( NOM, SDL_GetError() );
864  return false;
865  }
866 
867  return true;
868 }
869 
870 bool Texture::set_colorkey ( const Color& colorkey )
871 {
872  this->lock ( this->bounds() ); // Safe for writing
873 
874  uint32* pixels = static_cast<uint32*> ( this->pixels() );
875 
876  uint32 key = RGB ( colorkey, this->pixel_format() );
877  uint32 transparent = RGBA ( colorkey, this->pixel_format() );
878 
879  for ( auto idx = 0; idx < ( this->pitch() / 4 ) * this->height(); ++idx )
880  {
881  if ( pixels[idx] == key ) pixels[idx] = transparent;
882  }
883 
884  // Once we unlock the texture, it will be uploaded to the GPU
885  this->unlock();
886 
887  this->colorkey_ = colorkey; // Cache the state of our color key used
888 
889  return true;
890 }
891 
892 } // namespace nom
int32 width(void) const
Definition: Texture.cpp:212
Texture::SharedPtr clone(void) const
Definition: Texture.cpp:141
uint8 bits_per_pixel(void) const
Definition: Texture.cpp:248
static SDL_RENDERER::RawPtr context(void)
Definition: Window.cpp:491
void set_position(const Point2i &pos)
Definition: Texture.cpp:201
int pitch_
Texture&#39;s pixel pitch; these are only available when a Texture is locked.
Definition: Texture.hpp:299
uint32 pixel(int32 x, int32 y)
Definition: Texture.cpp:519
bool locked(void) const
Query memory lock status.
Definition: Texture.cpp:292
const Color & colorkey(void) const
Definition: Texture.cpp:299
bool set_colorkey(const Color &colorkey)
Definition: Texture.cpp:870
static const Color null
Convenience object that will always contain a value of -1.
Definition: Color.hpp:74
Color colorkey_
Cached upon use of the set_colorkey method for use by external classes.
Definition: Texture.hpp:308
uint8 bytes_per_pixel(void) const
Definition: Texture.cpp:261
void draw(SDL_RENDERER::RawPtr target) const
Definition: Texture.cpp:422
bool load(const std::string &filename, uint32 flags, bool use_cache=false)
Definition: Texture.cpp:353
void * pixels_
Definition: Texture.hpp:296
bool update(const void *pixels, uint16 pitch, const Coords &update_area)
Upload texture copy with new pixel data.
Definition: Texture.cpp:411
const Coords bounds(void) const
Definition: Image.cpp:162
bool load(const std::string &filename)
Definition: Image.cpp:185
const SDL_BlendMode blend_mode(void) const
Obtain the blending mode used for texture copies.
Definition: Texture.cpp:279
bool set_blend_mode(const SDL_BlendMode blend)
Set a new blending mode for this texture.
Definition: Texture.cpp:859
int32 & h
Reference alias for height instance variable.
Definition: Coords.hpp:106
void unlock(void)
Definition: Texture.cpp:340
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
Definition: Renderer.cpp:58
~Texture(void)
Lazy destructor.
Definition: Texture.cpp:45
Bitmap storage container.
Definition: Image.hpp:49
int access(void) const
Definition: Texture.cpp:169
Coords bounds_
Holds surface offsets (clipping area: X, Y, width &amp; height)
Definition: Texture.hpp:305
int32 height(void) const
Definition: Texture.cpp:225
int32 getResizeScaleFactor(enum ResizeAlgorithm scaling_algorithm)
Definition: Texture.cpp:836
bool valid(void) const
Is this object initialized – not nullptr?
Definition: Texture.cpp:162
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
bool initialize(SDL_SURFACE::RawPtr video_buffer)
Definition: Texture.cpp:97
Texture(void)
Default constructor.
Definition: Texture.cpp:38
SDL_TEXTURE::RawPtr texture(void) const
Get the video memory surface of the Texture object.
Definition: Texture.cpp:157
Point2i position_
Holds surface position (X, Y)
Definition: Texture.hpp:302
bool lock(void)
Definition: Texture.cpp:304
Texture & operator=(const Texture &other)
Copy assignment operator.
Definition: Texture.cpp:146
Utility class container for RGBA colors.
Definition: Color.hpp:43
int32 & w
Reference alias for width instance variable.
Definition: Coords.hpp:103
bool resize(enum ResizeAlgorithm scaling_algorithm)
Definition: Texture.cpp:559