nomlib
Loading...
Searching...
No Matches
Texture.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON 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
26SOFTWARE, 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 "export.hpp"
39#include "nomlib/config.hpp"
40#include "nomlib/math/Color4.hpp"
41#include "nomlib/math/Point2.hpp"
42#include "nomlib/math/Size2.hpp"
43#include "nomlib/math/Rect.hpp"
44
45// Dump the rescaled Texture as a PNG file
46//#define NOM_DEBUG_SDL2_RESIZE_PNG
47
48namespace nom {
49
50// Forward declarations
51class Image;
52class RenderWindow;
53
54class NOM_EXPORT Texture
55{
56 public:
57 typedef Texture self_type;
58
61 {
62 None = 0, // No resizing is applied
63 scale2x,
64 scale3x, // Reserved for future implementation
65 scale4x, // Reserved for future implementation
66 hq2x,
67 hq3x,
68 hq4x
69 };
70
71 enum Access
72 {
73 Invalid = 0,
74 Static = SDL_TEXTUREACCESS_STATIC, // Changes rarely; not lockable
75 Streaming = SDL_TEXTUREACCESS_STREAMING, // Frequent changes; lockable, RW
76 RenderTarget = SDL_TEXTUREACCESS_TARGET // Render (texture) to target
77 };
78
84 Texture( void );
85
92 ~Texture( void );
93
95 Texture ( const Texture& copy );
96
98 Texture& operator = ( const Texture& other );
99
111 Texture* clone() const;
112
121 bool initialize ( uint32 format, uint32 flags, int width, int height );
122
124 bool initialize(uint32 format, uint32 flags, const Size2i& dims);
125
127 bool create(SDL_Texture* source);
128
130 bool create ( const Image& source );
131
134 bool create( const Image& source, uint32 pixel_format, enum Texture::Access type );
135
136 const Point2i& position() const;
137
145 const Size2i& size() const;
146
147 const IntRect& bounds() const;
148
150 SDL_Texture* texture() const;
151
153 bool valid() const;
154
158 enum Texture::Access access ( void ) const;
159
160 void set_position(const Point2i& pos);
161
166 void set_size(const Size2i& size);
167
169 void set_bounds(const IntRect& bounds);
170
174 int32 width ( void ) const;
175
179 int32 height ( void ) const;
180
181 int pitch ( void ) const;
182
183 void* pixels ( void ) const;
184
185 uint8 bytes_per_pixel ( void ) const;
186
187 uint32 pixel_format ( void ) const;
188
193 uint8 bits_per_pixel ( void ) const;
194
196 SDL_BlendMode blend_mode ( void ) const;
197
202 const Color4i& colorkey ( void ) const;
203
208 uint8 alpha ( void ) const;
209
214 const Color4i color_modulation ( void ) const;
215
226 NOM_EXPORT static const Point2i maximum_size ( void );
227
229 bool locked() const;
230
235 bool lock();
236
244
245 bool lock(const IntRect& bounds);
246
250 void unlock ( void );
251
267 bool load ( const std::string& filename,
268 bool use_cache = false,
269 enum Texture::Access type = Access::Static
270 );
271
282 bool update_pixels(const void* source, uint16 pitch, const IntRect& bounds);
283
288 void draw(SDL_Renderer* target) const;
289
295 void draw(const RenderWindow& target) const;
296
303 void draw(SDL_Renderer* target, const real64 angle) const;
304
309 void draw(const RenderWindow& target, const real64 angle) const;
310
316 bool set_alpha ( uint8 opacity );
317
331 uint32 pixel ( int x, int y );
332
347 bool resize( enum ResizeAlgorithm scaling_algorithm );
348
350 int scale_factor( enum ResizeAlgorithm scaling_algorithm ) const;
351 int scale_factor() const;
352
354 bool set_blend_mode ( const SDL_BlendMode blend );
355
367 bool set_colorkey(const Color4i& colorkey);
368
379 bool set_color_modulation ( const Color4i& color );
380
381 bool copy_pixels ( const void* source, int pitch );
382
383 private:
384 void set_scale_factor(int factor);
385
386 std::shared_ptr<SDL_Texture> texture_;
387
389 void* pixels_;
390
393
398 Point2i position_; // This should probably be an IntRect. (Global bounds).
399
400 Size2i size_;
401
408 IntRect bounds_; // Local bounds
409
411 Color4i colorkey_;
412
413 int scale_factor_;
414};
415
416
417} // namespace nom
418
419#endif // include guard defined
Bitmap storage container.
Definition Image.hpp:49
int32 height(void) const
bool create(const Image &source)
Texture(void)
Default constructor.
bool set_colorkey(const Color4i &colorkey)
bool initialize(uint32 format, uint32 flags, const Size2i &dims)
enum Texture::Access access(void) const
Query texture access type.
void set_bounds(const IntRect &bounds)
Set bounding coordinates of the Texture.
bool initialize(uint32 format, uint32 flags, int width, int height)
Color4i colorkey_
Cached upon use of the set_colorkey method for use by external classes.
Definition Texture.hpp:411
void set_size(const Size2i &size)
Set the width & height dimensions of the texture.
bool load(const std::string &filename, bool use_cache=false, enum Texture::Access type=Access::Static)
void unlock(void)
Texture(const Texture &copy)
Copy constructor.
void draw(const RenderWindow &target, const real64 angle) const
bool lock()
Lock the entire bounds of the texture for write access to the pixel buffer.
bool set_blend_mode(const SDL_BlendMode blend)
Set a new blending mode for this texture.
int pitch_
Texture's pixel pitch; these are only available when a Texture is locked.
Definition Texture.hpp:392
Texture * clone() const
Get a shallow-copy of the underlying stored texture.
void draw(SDL_Renderer *target, const real64 angle) const
void draw(const RenderWindow &target) const
const Color4i color_modulation(void) const
Obtain the additional color value multiplied into render copy operations.
bool set_color_modulation(const Color4i &color)
Set an additional color value multiplied into render copy operations.
uint8 bits_per_pixel(void) const
Point2i position_
Definition Texture.hpp:398
void * pixels_
Texture's pixels; these are only available when a Texture is locked.
Definition Texture.hpp:389
static NOM_EXPORT const Point2i maximum_size(void)
Obtain the largest nom::Texture size allowable by your graphics hardware.
bool update_pixels(const void *source, uint16 pitch, const IntRect &bounds)
Upload pixels to texture.
bool create(SDL_Texture *source)
SDL_BlendMode blend_mode(void) const
Obtain the blending mode used for texture copies.
SDL_Texture * texture() const
Get the underlying texture stored.
IntRect bounds_
Definition Texture.hpp:408
bool lock(const IntRect &bounds)
Lock a portion of the texture for write access to the pixel buffer.
bool valid() const
Is this object initialized – not nullptr?
uint8 alpha(void) const
Obtain the set color key for this image.
int scale_factor(enum ResizeAlgorithm scaling_algorithm) const
Return the scaling factor of the chosen algorithm.
int32 width(void) const
bool locked() const
Query lock status of texture.
uint32 pixel(int x, int y)
Read a RGBA pixel from the video surface.
bool set_alpha(uint8 opacity)
Set an additional alpha value multiplied into render copy operations.
bool create(const Image &source, uint32 pixel_format, enum Texture::Access type)
const Color4i & colorkey(void) const
bool resize(enum ResizeAlgorithm scaling_algorithm)
Rescale the Texture with the chosen rescaling algorithm.
void draw(SDL_Renderer *target) const
ResizeAlgorithm
Available pixel rescaling algorithms.
Definition Texture.hpp:61
const Size2i & size() const
Get the width & height dimensions of the texture.
~Texture(void)
Destructor.