Texture.hpp
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 #ifndef NOMLIB_SDL2_TEXTURE_HEADERS
30 #define NOMLIB_SDL2_TEXTURE_HEADERS
31 
32 #include <iostream>
33 #include <string>
34 #include <memory>
35 
36 #include "SDL.h"
37 
38 #include "nomlib/config.hpp"
39 #include "nomlib/SDL_helpers.hpp"
40 #include "nomlib/math/helpers.hpp"
41 #include "nomlib/math/Color.hpp"
42 #include "nomlib/math/Coords.hpp"
43 #include "nomlib/math/Point2-inl.hpp"
44 #include "nomlib/system/ObjectCache.hpp"
45 #include "nomlib/SDL_helpers.hpp"
46 #include "nomlib/graphics/Image.hpp"
47 #include "nomlib/graphics/Window.hpp"
48 
49 //#define NOM_DEBUG_SDL_TEXTURE
50 
51 namespace nom {
52 
55 enum ResizeAlgorithm
56 {
57  None = 0, // No resizing is applied
58  scale2x,
59  scale3x, // Reserved for future implementation
60  scale4x, // Reserved for future implementation
61  hq2x,
62  hq3x,
63  hq4x,
64  Stretch
65 };
66 
67 class Texture
68 {
69  public:
70  typedef std::shared_ptr<Texture> SharedPtr;
71 
73  Texture ( void );
74 
76  ~Texture ( void );
77 
79  Texture ( const Texture& other );
80 
93  //Texture ( int32 width, int32 height, uint8 bitsPerPixel, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask, uint32 flags = SDL_SWSURFACE );
94 
104  //Texture ( void* pixels, int32 width, int32 height, int32 depth, uint16 pitch, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask );
105 
106  //void initialize ( uint32 flags, int32 width, int32 height, uint8 bitsPerPixel, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask );
107 
111  bool initialize ( SDL_SURFACE::RawPtr video_buffer );
112 
115  bool initialize ( int32 width, int32 height, uint32 format, uint32 flags );
116 
119  Texture::SharedPtr clone ( void ) const;
120 
122  Texture& operator = ( const Texture& other );
123 
124  const Point2i& position ( void ) const;
125 
126  const Coords& bounds ( void ) const;
127 
129  SDL_TEXTURE::RawPtr texture ( void ) const;
130 
132  bool valid ( void ) const;
133 
137  int access ( void ) const;
138 
142 
143  void set_position ( const Point2i& pos );
144 
145  void set_bounds ( const Coords& clip );
146 
150  int32 width ( void ) const;
151 
155  int32 height ( void ) const;
156 
157  int pitch ( void ) const;
158 
159  void* pixels ( void ) const;
160 
162  uint8 bytes_per_pixel ( void ) const;
163 
164  uint32 pixel_format ( void ) const;
165 
171  uint8 bits_per_pixel ( void ) const;
172 
174  const SDL_BlendMode blend_mode ( void ) const;
175 
177  bool locked ( void ) const;
178 
183  const Color& colorkey ( void ) const;
184 
188  bool lock ( void );
189 
196  bool lock ( const Coords& lock_coords );
197 
200  void unlock ( void );
201 
217  bool load (
218  const std::string& filename, uint32 flags,
219  bool use_cache = false
220  );
221 
223  bool update ( const void* pixels, uint16 pitch, const Coords& update_area );
224 
229  void draw ( SDL_RENDERER::RawPtr target ) const;
230 
236  void draw ( const Window& target ) const;
237 
244  void draw ( SDL_RENDERER::RawPtr target, const double angle ) const;
245 
250  void draw ( const Window& target, const double angle ) const;
251 
252  bool set_alpha ( uint8 opacity );
253 
261  uint32 pixel ( int32 x, int32 y );
262 
268  bool resize ( enum ResizeAlgorithm scaling_algorithm );
269 
271  bool resize ( const Point2f& scale_factor );
272 
276  int32 getResizeScaleFactor ( enum ResizeAlgorithm scaling_algorithm );
277 
279  bool set_blend_mode ( const SDL_BlendMode blend );
280 
289  bool set_colorkey ( const Color& colorkey );
290 
291  private:
292  SDL_TEXTURE::SharedPtr texture_;
293 
296  void* pixels_;
297 
299  int pitch_;
300 
303 
306 
309 };
310 
311 
312 } // namespace nom
313 
314 #endif // include guard defined
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
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
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 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
void unlock(void)
Definition: Texture.cpp:340
~Texture(void)
Lazy destructor.
Definition: Texture.cpp:45
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
bool resize(enum ResizeAlgorithm scaling_algorithm)
Definition: Texture.cpp:559